|
| 1 | +package csembstu.alamgir.server.service; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.*; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.DisplayName; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.mockito.InjectMocks; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 16 | + |
| 17 | +import csembstu.alamgir.server.dto.request.DateRequestValidation; |
| 18 | +import csembstu.alamgir.server.dto.response.FullLogisticsValidationResponse; |
| 19 | +import csembstu.alamgir.server.dto.response.StorageCapacityViolation; |
| 20 | +import csembstu.alamgir.server.entity.*; |
| 21 | +import csembstu.alamgir.server.repository.*; |
| 22 | + |
| 23 | +@ExtendWith(MockitoExtension.class) |
| 24 | +class ValidationApiService2Test { |
| 25 | + |
| 26 | + @InjectMocks private ValidationApiService2 service; |
| 27 | + |
| 28 | + @Mock private DemandRepository demandRepository; |
| 29 | + @Mock private RouteRepository routeRepository; |
| 30 | + @Mock private StorageUnitRepository storageUnitRepository; |
| 31 | + |
| 32 | + private Product product; |
| 33 | + private Location destination; |
| 34 | + private Location warehouse; |
| 35 | + private Demand demand; |
| 36 | + private Route route; |
| 37 | + private StorageUnit unit; |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void init() { |
| 43 | + product = new Product().setName("Ice Cream").setMinTemperature(-10) .setMaxTemperature(-5); |
| 44 | + destination = new Location() .setId("L2") .setName("Shop"); |
| 45 | + warehouse = new Location() .setId("W1") .setName("Warehouse A"); |
| 46 | + demand = new Demand() .setMinQuantity(10) .setMaxQuantity(20); |
| 47 | + route = new Route() .setCapacity(100) .setMinShipment(5); |
| 48 | + unit = new StorageUnit() .setMinTemperature(5); // temperature incompatible .setMaxTemperature(25) .setCapacity(200); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + @Test |
| 54 | + @DisplayName("Should detect temperature incompatible demand") |
| 55 | + void temperatureIncompatibleDemand() { |
| 56 | + |
| 57 | + // GIVEN |
| 58 | + demand.setProduct(product).setLocation(destination); |
| 59 | + route.setFromLocation(warehouse).setToLocation(destination); |
| 60 | + |
| 61 | + when(demandRepository.findByDate(any())).thenReturn(List.of(demand)); |
| 62 | + when(routeRepository.findByToLocation_Id("L2")).thenReturn(List.of(route)); |
| 63 | + when(storageUnitRepository.findByLocation_Id(route.getFromLocation().getId())).thenReturn(List.of(unit)); |
| 64 | + |
| 65 | + // WHEN |
| 66 | + FullLogisticsValidationResponse response = service.fullLogisticsValidation(new DateRequestValidation()); |
| 67 | + |
| 68 | + // THEN |
| 69 | + String expectedError = "Product Ice Cream has no safe warehouse storage to deliver to Shop"; |
| 70 | + assertEquals(expectedError, response.getTemperature_incompatible_demands().get(0)); |
| 71 | + assertEquals(1, response.getTemperature_incompatible_demands().size()); |
| 72 | + assertTrue(response.getCapacity_violations().isEmpty()); |
| 73 | + assertTrue(response.getStorage_capacity_violations().isEmpty()); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("Should detect capacity violations") |
| 80 | + void capacityViolationDetected() { |
| 81 | + |
| 82 | + // GIVEN |
| 83 | + product.setMinTemperature(2).setMaxTemperature(8); |
| 84 | + demand.setProduct(product) .setLocation(destination) .setMinQuantity(50) .setMaxQuantity(200); |
| 85 | + route.setFromLocation(warehouse) .setToLocation(destination).setCapacity(100); // max violated.setMinShipment(60); // min violated |
| 86 | + unit.setMinTemperature(0).setMaxTemperature(10).setCapacity(500); |
| 87 | + |
| 88 | + when(demandRepository.findByDate(any())).thenReturn(List.of(demand)); |
| 89 | + when(routeRepository.findByToLocation_Id("L2")).thenReturn(List.of(route)); |
| 90 | + when(storageUnitRepository.findByLocation_Id(route.getFromLocation().getId())).thenReturn(List.of(unit)); |
| 91 | + |
| 92 | + // WHEN |
| 93 | + FullLogisticsValidationResponse response = service.fullLogisticsValidation(new DateRequestValidation()); |
| 94 | + |
| 95 | + // THEN |
| 96 | + assertEquals(2, response.getCapacity_violations().size(), "Should have detected 2 capacity violations"); |
| 97 | + |
| 98 | + boolean hasMinError = response.getCapacity_violations().stream().anyMatch(v -> v.getViolation().equals("MIN_CAPACITY_VIOLATION")); |
| 99 | + boolean hasMaxError = response.getCapacity_violations().stream().anyMatch(v -> v.getViolation().equals("MAX_CAPACITY_VIOLATION")); |
| 100 | + assertTrue(hasMinError); |
| 101 | + assertTrue(hasMaxError); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + @Test |
| 108 | + @DisplayName("Should detect storage capacity overflow") |
| 109 | + void storageCapacityViolationDetected() { |
| 110 | + |
| 111 | + //GIVEN |
| 112 | + product.setMinTemperature(2).setMaxTemperature(8); |
| 113 | + demand.setProduct(product).setLocation(destination).setMinQuantity(10).setMaxQuantity(300); // exceeds storage |
| 114 | + route.setFromLocation(warehouse) .setToLocation(destination) .setCapacity(500).setMinShipment(5); |
| 115 | + unit.setMinTemperature(0) .setMaxTemperature(10) .setCapacity(200);// total storage < demand max |
| 116 | + |
| 117 | + when(demandRepository.findByDate(any())).thenReturn(List.of(demand)); |
| 118 | + when(routeRepository.findByToLocation_Id("L2")).thenReturn(List.of(route)); |
| 119 | + when(storageUnitRepository.findByLocation_Id("W1")).thenReturn(List.of(unit)); |
| 120 | + |
| 121 | + //WHEN |
| 122 | + FullLogisticsValidationResponse response = service.fullLogisticsValidation(new DateRequestValidation()); |
| 123 | + |
| 124 | + // THEN |
| 125 | + StorageCapacityViolation violation = response.getStorage_capacity_violations().get(0); |
| 126 | + assertEquals("Warehouse A", violation.getWarehouse()); |
| 127 | + assertEquals(200, violation.getStorage_capacity()); |
| 128 | + assertEquals(300, violation.getTotal_required_capacity()); |
| 129 | + assertEquals(1, response.getStorage_capacity_violations().size()); |
| 130 | + } |
| 131 | +} |
0 commit comments