Skip to content

Commit

Permalink
Task 20 : Revise all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 committed Sep 28, 2024
1 parent 21199a6 commit b4ed9a4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void logCourierLocation_shouldReturnSuccessMessage() throws Exception {

@Test
public void getPastTravels_shouldReturnListOfTravels() throws Exception {

// Given
String courierId = "123e4567-e89b-12d3-a456-426614174000";
List<Courier> travels = List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public TravelQueryRequestTest() {

@Test
void shouldThrowIllegalArgumentExceptionWhenStartTimeIsAfterEndTime() {

// Given
LocalDateTime start = LocalDateTime.now().plusDays(1); // Future date
LocalDateTime end = LocalDateTime.now(); // Current date
Expand All @@ -41,10 +42,12 @@ void shouldThrowIllegalArgumentExceptionWhenStartTimeIsAfterEndTime() {
assertFalse(violations.isEmpty(), "Violations should be present");
assertTrue(violations.stream().anyMatch(v -> v.getMessage().equals("Start time must be before end time")),
"Violation for invalid time range should be present");

}

@Test
void shouldPassWhenValidTimeRangeProvided() {

// Given
LocalDateTime start = LocalDateTime.now().minusDays(1); // Past date
LocalDateTime end = LocalDateTime.now().plusDays(1); // Future date
Expand All @@ -61,10 +64,12 @@ void shouldPassWhenValidTimeRangeProvided() {

// Then
assertTrue(violations.isEmpty(), "No violations should be present for a valid time range");

}

@Test
void shouldFailOnInvalidUUIDFormat() {

// Given
LocalDateTime start = LocalDateTime.now().minusDays(1);
LocalDateTime end = LocalDateTime.now().plusDays(1);
Expand All @@ -83,10 +88,12 @@ void shouldFailOnInvalidUUIDFormat() {
assertFalse(violations.isEmpty(), "Violations should be present for invalid UUID format");
assertTrue(violations.stream().anyMatch(v -> v.getMessage().equals("Invalid UUID format")),
"Violation for invalid UUID should be present");

}

@Test
void shouldFailWhenStoreNameIsBlank() {

// Given
LocalDateTime start = LocalDateTime.now().minusDays(1);
LocalDateTime end = LocalDateTime.now().plusDays(1);
Expand All @@ -105,10 +112,12 @@ void shouldFailWhenStoreNameIsBlank() {
assertFalse(violations.isEmpty(), "Violations should be present for blank store name");
assertTrue(violations.stream().anyMatch(v -> v.getMessage().equals("Store name cannot be blank")),
"Violation for blank store name should be present");

}

@Test
void shouldFailWhenStartIsNull() {

// Given
LocalDateTime end = LocalDateTime.now().plusDays(1);

Expand All @@ -130,6 +139,7 @@ void shouldFailWhenStartIsNull() {

@Test
void shouldFailWhenEndIsNull() {

// Given
LocalDateTime start = LocalDateTime.now().minusDays(1);

Expand All @@ -147,6 +157,7 @@ void shouldFailWhenEndIsNull() {
assertFalse(violations.isEmpty(), "Violations should be present for null end time");
assertTrue(violations.stream().anyMatch(v -> v.getMessage().equals("End time cannot be null")),
"Violation for null end time should be present");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ void logCourierLocation_shouldThrowTimestampBeforeStoreCreateException_ifTimesta
// Then
assertThrows(TimestampBeforeStoreCreateException.class, () -> courierService.logCourierLocation(logRequest));

// Verify
verify(storeRepository).findAll();

}

@Test
Expand All @@ -94,6 +97,9 @@ void logCourierLocation_shouldThrowStoreFarAwayException_ifCourierIsFarAwayFromA
// Then
assertThrows(StoreFarAwayException.class, () -> courierService.logCourierLocation(logRequest));

// Verify
verify(storeRepository).findAll();

}

@Test
Expand All @@ -112,6 +118,8 @@ void getPastTravelsByCourierId_shouldReturnTravelsForGivenCourierId() {

assertFalse(result.isEmpty());
assertEquals(couriers.get(0).getCourierId(), result.get(0).getCourierId());

// Verify
verify(courierRepository).findByCourierId(courierId);

}
Expand All @@ -132,6 +140,8 @@ void getTravelsByCourierIdStoreNameAndTimeRange_shouldReturnTravelsWithinTimeRan

assertFalse(result.isEmpty());
assertEquals(couriers.get(0).getCourierId(), result.get(0).getCourierId());

// Verify
verify(courierRepository).findByCourierIdAndStoreNameAndTimestampBetween(request.getCourierId(), request.getStoreName(), request.getStart(), request.getEnd());

}
Expand Down Expand Up @@ -163,6 +173,8 @@ void getTotalTravelDistance_shouldReturnTotalDistanceTraveledByCourier() {
double totalDistance = courierService.getTotalTravelDistance(courierId);

assertEquals(distanceInKilometers, totalDistance, 0.001); // Allow a small tolerance for floating point comparisons

// Verify
verify(courierRepository).findByCourierIdOrderByTimestampAsc(courierId);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void setUp() {

@Test
public void testCalculateDistance_sameLocation() {

// Given
Location startLoc = new Location(39.9255, 32.8662); // Ankara
Location endLoc = new Location(39.9255, 32.8662); // Ankara
Expand All @@ -26,6 +27,7 @@ public void testCalculateDistance_sameLocation() {

// Then
assertEquals(0.0, distance, 0.001); // Distance should be 0 meters

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void utilityClass_ShouldNotBeInstantiated() {

@Test
public void testIsWithinRadiusReturnsTrueWhenWithinRadiusInKilometers() {

double courierLat = 37.7749;
double courierLng = -122.4194;
double storeLat = 37.7750;
Expand All @@ -33,10 +34,12 @@ public void testIsWithinRadiusReturnsTrueWhenWithinRadiusInKilometers() {
boolean result = DistanceUtils.isWithinRadius(courierLat, courierLng, storeLat, storeLng, radiusInMeters);

assertTrue(result, "Courier should be within the radius of the store.");

}

@Test
public void testIsWithinRadiusReturnsFalseWhenOutsideRadiusInKilometers() {

double courierLat = 37.7749;
double courierLng = -122.4194;
double storeLat = 37.7800;
Expand All @@ -48,10 +51,12 @@ public void testIsWithinRadiusReturnsFalseWhenOutsideRadiusInKilometers() {
boolean result = DistanceUtils.isWithinRadius(courierLat, courierLng, storeLat, storeLng, radiusInMeters);

assertFalse(result, "Courier should not be within the radius of the store.");

}

@Test
public void testCalculateDistanceReturnsCorrectDistanceInKilometers() {

double lat1 = 37.7749;
double lng1 = -122.4194;
double lat2 = 37.7750;
Expand All @@ -65,34 +70,40 @@ public void testCalculateDistanceReturnsCorrectDistanceInKilometers() {

assertTrue(Math.abs(distance - expectedDistanceInKm) <= tolerance,
"Calculated distance should be within the expected range.");

}

@Test
public void testIsMoreThanOneMinuteAgoReturnsTrue() {

LocalDateTime lastTimestamp = LocalDateTime.now().minusMinutes(2);
LocalDateTime currentTimestamp = LocalDateTime.now();

// Directly call the static method
boolean result = DistanceUtils.isMoreThanOneMinuteAgo(lastTimestamp, currentTimestamp);

assertTrue(result, "Timestamp should be more than one minute ago.");

}

@Test
public void testIsMoreThanOneMinuteAgoReturnsFalse() {

LocalDateTime lastTimestamp = LocalDateTime.now().minusSeconds(30);
LocalDateTime currentTimestamp = LocalDateTime.now();

// Directly call the static method
boolean result = DistanceUtils.isMoreThanOneMinuteAgo(lastTimestamp, currentTimestamp);

assertFalse(result, "Timestamp should not be more than one minute ago.");

}

/**
* Haversine formula to calculate the distance between two points on the Earth's surface.
*/
private double haversine(double lat1, double lng1, double lat2, double lng2) {

final double R = 6371.0; // Radius of the Earth in kilometers
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
Expand All @@ -101,5 +112,7 @@ private double haversine(double lat1, double lng1, double lat2, double lng2) {
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;

}

}

0 comments on commit b4ed9a4

Please sign in to comment.