Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed unnecessary try-catch blocks and refactor controller method #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@
import com.hpoyraz.locationbyip.model.Location;
import com.hpoyraz.locationbyip.service.impl.LocationService;
import com.hpoyraz.locationbyip.util.constants.ApiConstants;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import lombok.SneakyThrows;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Controller
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiConstants.Location.BASE_URL)
public class LocationController {
private final LocationService locationService;

@SneakyThrows
@GetMapping("/{ipAddress}")
public ResponseEntity<?> getLocationByIP(@PathVariable String ipAddress) {
try {
Location location = locationService.getLocationByIP(ipAddress);
return ResponseEntity.ok(location);
} catch (IOException | GeoIp2Exception e) {
String errorMessage = "Error: Location information not found.";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
}
public Location getLocationByIP(@PathVariable String ipAddress) {
return locationService.getLocationByIP(ipAddress);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hpoyraz.locationbyip.exception;

import com.maxmind.geoip2.exception.GeoIp2Exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand All @@ -9,28 +10,33 @@
import java.io.IOException;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(GeoIp2Exception.class)
public ResponseEntity<String> handleGeoIp2Exception(GeoIp2Exception ex) {
String errorMessage = "Error: Location information not found.";
log.error(ex.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
}

@ExceptionHandler(IOException.class)
public ResponseEntity<String> handleIOException(IOException ex) {
String errorMessage = "Error: Input/Output error has occurred.";
log.error(ex.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
}

@ExceptionHandler(DistrictnameNotFoundException.class)
public ResponseEntity<String> handeleDistrictnameNotFoundException (DistrictnameNotFoundException ex) {
String errorMessage = "Error: District name not found.";
log.error(ex.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception ex) {
String errorMessage = "Something went wrong.";
log.error(ex.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Location getLocationByIP(String ipAddress) throws IOException, GeoIp2Exce
Location location = new Location();
location.setIp(ipAddress);
location.setNetwork(String.valueOf(response.getTraits().getNetwork()));
location.setCountryName(response.getCountry().getName().equals("Turkey") ? "Türkiye" : response.getCountry().getName());
location.setCountryName(country.getName().equals("Turkey") ? "Türkiye" : country.getName());

String cityName = response.getCity().getName();
if (!subdivisionList.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,32 @@ public void setUp() {
}

@Test
public void shouldReturnLocationForValidIP() {
public void shouldReturnLocationForValidIP() throws IOException, GeoIp2Exception {
String validIpAddress = "123.45.67.89";
try {
Location location = locationService.getLocationByIP(validIpAddress);

assertEquals(validIpAddress, location.getIp());
assertNotNull(location.getNetwork());
assertNotNull(location.getCountryName());
assertNotNull(location.getContinentName());
assertNotNull(location.getContinentCode());
assertNotNull(location.getTimeZone());
assertNotNull(location.getCity());
} catch (IOException | GeoIp2Exception e) {
System.out.println("Something went wrong: " + e.getMessage());
}
Location location = locationService.getLocationByIP(validIpAddress);

assertEquals(validIpAddress, location.getIp());
assertNotNull(location.getNetwork());
assertNotNull(location.getCountryName());
assertNotNull(location.getContinentName());
assertNotNull(location.getContinentCode());
assertNotNull(location.getTimeZone());
assertNotNull(location.getCity());
}

@Test
public void shouldReturnNullForInvalidIP() {
public void shouldReturnNullForInvalidIP() throws IOException, GeoIp2Exception {
String invalidIpAddress = "192.168.1.1";
try {
Location location = locationService.getLocationByIP(invalidIpAddress);
Location location = locationService.getLocationByIP(invalidIpAddress);

assertNull(location);
} catch (IOException | GeoIp2Exception e) {
System.out.println("Something went wrong: " + e.getMessage());
}
assertNull(location);
}

@Test
public void shouldReturnNullForNullIP() {
try {
Location location = locationService.getLocationByIP(null);

assertNull(location);
} catch (IOException | GeoIp2Exception e) {
System.out.println("Something went wrong: " + e.getMessage());
}
public void shouldReturnNullForNullIP() throws IOException, GeoIp2Exception {
Location location = locationService.getLocationByIP(null);

assertNull(location);
}
}