test: Add test to verify exception thrown

This commit is contained in:
js0ny 2025-10-18 21:09:23 +01:00
parent 49a1c2ef07
commit 5369465c14
2 changed files with 23 additions and 1 deletions

View file

@ -40,6 +40,7 @@ public class GpsCalculationService {
public double calculateDistance(LngLatDto position1, LngLatDto position2) { public double calculateDistance(LngLatDto position1, LngLatDto position2) {
double lngDistance = position2.lng() - position1.lng(); double lngDistance = position2.lng() - position1.lng();
double latDistance = position2.lat() - position1.lat(); double latDistance = position2.lat() - position1.lat();
// Euclidean: \sqrt{a^2 + b^2}
return Math.sqrt(lngDistance * lngDistance + latDistance * latDistance); return Math.sqrt(lngDistance * lngDistance + latDistance * latDistance);
} }
@ -70,7 +71,7 @@ public class GpsCalculationService {
* @see io.github.js0ny.ilp_coursework.controller.ApiController#getNextPosition(MovementRequestDto) * @see io.github.js0ny.ilp_coursework.controller.ApiController#getNextPosition(MovementRequestDto)
*/ */
public LngLatDto nextPosition(LngLatDto start, double angle) { public LngLatDto nextPosition(LngLatDto start, double angle) {
double rad = Math.toRadians(angle); double rad = Math.toRadians(angle); // Convert to radian for Java triangle function calculation
double newLng = Math.cos(rad) * STEP + start.lng(); double newLng = Math.cos(rad) * STEP + start.lng();
double newLat = Math.sin(rad) * STEP + start.lat(); double newLat = Math.sin(rad) * STEP + start.lat();
return new LngLatDto(newLng, newLat); return new LngLatDto(newLng, newLat);

View file

@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.AssertionsForClassTypes.within; import static org.assertj.core.api.AssertionsForClassTypes.within;
@ -350,5 +351,25 @@ public class GpsCalculationServiceTest {
boolean actual = service.checkIsInRegion(position, RECTANGLE_REGION); boolean actual = service.checkIsInRegion(position, RECTANGLE_REGION);
assertThat(actual).isEqualTo(expected); assertThat(actual).isEqualTo(expected);
} }
@Test
@DisplayName("Edge Case: Region not forming polygon")
void isInRegion_shouldThrowExceptions_whenRegionNotFormingPolygon() {
var position = new LngLatDto(2.0, 2.0);
var region = new RegionDto("line", List.of(new LngLatDto(0.0, 0.0), new LngLatDto(0.0001, 0.0), new LngLatDto(0.0, 0.0)));
assertThatThrownBy(() -> {
service.checkIsInRegion(position, region);
}).isInstanceOf(IllegalArgumentException.class).hasMessage("Region is not closed.");
}
@Test
@DisplayName("Edge Case: Region is not closed")
void isInRegion_shouldThrowExceptions_whenRegionNotClose() {
var position = new LngLatDto(2.0, 2.0);
var region = new RegionDto("rectangle", List.of(new LngLatDto(0.0, 0.0), new LngLatDto(2.0, 0.0), new LngLatDto(2.0, 2.0), new LngLatDto(0.0, 2.0), new LngLatDto(0.0, -1.0)));
assertThatThrownBy(() -> {
service.checkIsInRegion(position, region);
}).isInstanceOf(IllegalArgumentException.class).hasMessage("Region is not closed.");
}
} }
} }