Skip to content

Commit

Permalink
Task 28 : Revise CourierServiceTest with adding mapper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 committed Sep 29, 2024
1 parent 1afc40c commit f24658b
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.casestudy.migroscouriertracking.courier.model.mapper;

import com.casestudy.migroscouriertracking.courier.model.Courier;
import com.casestudy.migroscouriertracking.courier.model.entity.CourierEntity;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

class CourierEntityToCourierMapperTest {

private final CourierEntityToCourierMapper mapper = CourierEntityToCourierMapper.initialize();

@Test
void testMapCourierEntityCollectionNull() {

List<Courier> result = mapper.map((Collection<CourierEntity>) null);
assertNull(result);

}

@Test
void testMapCourierEntityListNull() {

List<Courier> result = mapper.map((List<CourierEntity>) null);
assertNull(result);
}

@Test
void testMapCourierEntityListEmpty() {

List<Courier> result = mapper.map(Collections.emptyList());
assertNotNull(result);
assertTrue(result.isEmpty());

}


@Test
void testMapCourierEntityListWithNullElements() {

List<CourierEntity> courierEntities = Arrays.asList(new CourierEntity(), null);
List<Courier> result = mapper.map(courierEntities);
assertNotNull(result);
assertEquals(2, result.size());
assertNull(result.get(1));

}


@Test
void testMapSingleCourierEntity() {

CourierEntity courierEntity = CourierEntity.builder()
.id(UUID.randomUUID().toString())
.courierId(UUID.randomUUID().toString())
.lat(47.0)
.lng(8.0)
.storeName("Migros")
.timestamp(LocalDateTime.now())
.build();

Courier result = mapper.map(courierEntity);

assertNotNull(result);
assertEquals(courierEntity.getId(), result.getId());
assertEquals(courierEntity.getCourierId(), result.getCourierId());
assertEquals(courierEntity.getLat(), result.getLat());
assertEquals(courierEntity.getLng(), result.getLng());
assertEquals(courierEntity.getStoreName(), result.getStoreName());
assertEquals(courierEntity.getTimestamp(), result.getTimestamp());

}

@Test
void testMapWithEdgeCaseValues() {

CourierEntity courierEntity = CourierEntity.builder()
.lat(Double.MAX_VALUE)
.lng(Double.MIN_VALUE)
.timestamp(LocalDateTime.of(2024, 9, 29, 16, 30))
.build();

Courier result = mapper.map(courierEntity);

assertNotNull(result);
assertEquals(Double.MAX_VALUE, result.getLat());
assertEquals(Double.MIN_VALUE, result.getLng());
assertEquals(LocalDateTime.of(2024, 9, 29, 16, 30), result.getTimestamp());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.casestudy.migroscouriertracking.courier.model.mapper;

import com.casestudy.migroscouriertracking.courier.model.Courier;
import com.casestudy.migroscouriertracking.courier.model.dto.response.CourierResponse;
import com.casestudy.migroscouriertracking.courier.model.entity.CourierEntity;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

class CourierToCourierResponseMapperTest {

private final CourierToCourierResponseMapper mapper = CourierToCourierResponseMapper.initialize();

@Test
void testMapCourierCollectionNull() {

List<CourierResponse> result = mapper.map((Collection<Courier>) null);
assertNull(result);

}

@Test
void testMapCourierListNull() {

List<CourierResponse> result = mapper.map((List<Courier>) null);
assertNull(result);

}

@Test
void testMapCourierListEmpty() {

List<CourierResponse> result = mapper.map(Collections.emptyList());
assertNotNull(result);
assertTrue(result.isEmpty());

}

@Test
void testMapCourierListWithNullElements() {

List<Courier> couriers = Arrays.asList(new Courier(), null);
List<CourierResponse> result = mapper.map(couriers);
assertNotNull(result);
assertEquals(2, result.size());
assertNull(result.get(1));

}


@Test
void testMapSingleCourier() {

Courier courier = Courier.builder()
.id(UUID.randomUUID().toString())
.courierId("courier123")
.lat(47.0)
.lng(8.0)
.storeName("Migros")
.timestamp(LocalDateTime.now())
.build();

CourierResponse result = mapper.map(courier);

assertNotNull(result);
assertEquals(courier.getId(), result.getId());
assertEquals(courier.getLat(), result.getLat());
assertEquals(courier.getLng(), result.getLng());
assertEquals(courier.getStoreName(), result.getStoreName());
assertEquals(courier.getTimestamp(), result.getTimestamp());

}

@Test
void testMapWithEdgeCaseValues() {

Courier courier = Courier.builder()
.lat(Double.MAX_VALUE)
.lng(Double.MIN_VALUE)
.timestamp(LocalDateTime.of(2024, 9, 29, 16, 30))
.build();

CourierResponse result = mapper.map(courier);

assertNotNull(result);
assertEquals(Double.MAX_VALUE, result.getLat());
assertEquals(Double.MIN_VALUE, result.getLng());
assertEquals(LocalDateTime.of(2024, 9, 29, 16, 30), result.getTimestamp());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CourierServiceTest extends AbstractBaseServiceTest {
void logCourierLocation_shouldSaveCourierLocation_ifWithinRadiusAndTimestampValid() {

// Given
String courierId = "courier1";
String courierId = UUID.randomUUID().toString();
double lat = 37.7749;
double lng = -122.4194;
LocalDateTime timestamp = LocalDateTime.now();
Expand Down Expand Up @@ -93,7 +93,7 @@ void logCourierLocation_shouldSaveCourierLocation_ifWithinRadiusAndTimestampVali
void logCourierLocation_shouldThrowTimestampBeforeStoreCreateException_ifTimestampIsBeforeStoreCreation() {

// Given
String courierId = "courier1";
String courierId = UUID.randomUUID().toString();
double lat = 37.7749;
double lng = -122.4194;
LocalDateTime timestamp = LocalDateTime.now();
Expand Down Expand Up @@ -129,8 +129,10 @@ void logCourierLocation_shouldThrowTimestampBeforeStoreCreateException_ifTimesta
void logCourierLocation_shouldThrowStoreFarAwayException_ifCourierIsFarAwayFromAllStores() {

// Given
String courierId = UUID.randomUUID().toString();

LogCourierLocationRequest logRequest = LogCourierLocationRequest.builder()
.courierId("courier1")
.courierId(courierId)
.lat(37.7749)
.lng(-122.4194)
.timestamp(LocalDateTime.now())
Expand Down Expand Up @@ -158,7 +160,7 @@ void logCourierLocation_shouldThrowStoreFarAwayException_ifCourierIsFarAwayFromA
void getPastTravelsByCourierId_shouldReturnTravelsForGivenCourierId() {

// Given
String courierId = "courier1";
String courierId = UUID.randomUUID().toString();

List<CourierEntity> courierEntities = List.of(CourierEntity.builder()
.id(UUID.randomUUID().toString())
Expand Down Expand Up @@ -189,16 +191,18 @@ void getPastTravelsByCourierId_shouldReturnTravelsForGivenCourierId() {
void getTravelsByCourierIdStoreNameAndTimeRange_shouldReturnTravelsWithinTimeRange() {

// Given
String courierId = UUID.randomUUID().toString();

TravelQueryRequest request = TravelQueryRequest.builder()
.courierId("courier1")
.courierId(courierId)
.storeName("store1")
.start(LocalDateTime.now().minusHours(1))
.end(LocalDateTime.now())
.build();

List<CourierEntity> courierEntities = List.of(CourierEntity.builder()
.id(UUID.randomUUID().toString())
.courierId("courier1")
.courierId(courierId)
.lat(37.7749)
.lng(-122.4194)
.storeName("store1")
Expand All @@ -225,7 +229,7 @@ void getTravelsByCourierIdStoreNameAndTimeRange_shouldReturnTravelsWithinTimeRan
void getTotalTravelDistance_shouldReturnTotalDistanceTraveledByCourier() {

// Given
String courierId = "courier1";
String courierId = UUID.randomUUID().toString();
LocalDateTime timestamp1 = LocalDateTime.now().minusMinutes(2);
LocalDateTime timestamp2 = LocalDateTime.now();
double lat1 = 37.7749;
Expand Down

0 comments on commit f24658b

Please sign in to comment.