Skip to content

Commit 4a2fc13

Browse files
complete : validation api testing
1 parent f93b699 commit 4a2fc13

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package csembstu.alamgir.server.controller;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.when;
5+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
6+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
7+
8+
import java.util.List;
9+
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Test;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
17+
import org.springframework.http.MediaType;
18+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
19+
import org.springframework.test.web.servlet.MockMvc;
20+
21+
import csembstu.alamgir.server.dto.request.DateRequestValidation;
22+
import csembstu.alamgir.server.dto.response.*;
23+
import csembstu.alamgir.server.service.ValidationApiService2;
24+
25+
@WebMvcTest(ValidationApiController2.class)
26+
class ValidationApiController2Test {
27+
28+
@Autowired private MockMvc mockMvc;
29+
@Autowired private ObjectMapper objectMapper;
30+
31+
@MockitoBean
32+
private ValidationApiService2 validationApiService2;
33+
34+
private DateRequestValidation request;
35+
private FullLogisticsValidationResponse response;
36+
37+
38+
39+
@BeforeEach
40+
void init() {
41+
42+
request = new DateRequestValidation().setDate("2026-02-08");
43+
response = new FullLogisticsValidationResponse()
44+
.setTemperature_incompatible_demands(List.of("Product Ice Cream has no safe warehouse"))
45+
.setCapacity_violations(
46+
List.of(new CapacityViolation(
47+
"MAX_CAPACITY_VIOLATION",
48+
"Warehouse A",
49+
"Shop",
50+
10,
51+
100)))
52+
.setStorage_capacity_violations(
53+
List.of(new StorageCapacityViolation(
54+
"Warehouse A",
55+
300,
56+
200)));
57+
}
58+
59+
60+
61+
@Test
62+
@DisplayName("POST /full-logistics-validation should return validation result")
63+
void fullLogisticsValidation_success() throws Exception {
64+
65+
// GIVEN
66+
when(validationApiService2.fullLogisticsValidation(any(DateRequestValidation.class))).thenReturn(response);
67+
68+
// WHEN & THEN
69+
mockMvc.perform(post("/full-logistics-validation")
70+
.contentType(MediaType.APPLICATION_JSON)
71+
.content(objectMapper.writeValueAsString(request)))
72+
.andExpect(status().isOk())
73+
.andExpect(jsonPath("$.temperature_incompatible_demands").isArray())
74+
.andExpect(jsonPath("$.temperature_incompatible_demands[0]").exists())
75+
.andExpect(jsonPath("$.capacity_violations").isArray())
76+
.andExpect(jsonPath("$.storage_capacity_violations").isArray());
77+
}
78+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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

Comments
 (0)