|
@@ -1,21 +1,182 @@
|
|
|
package eu.fibane.parkingtoll;
|
|
package eu.fibane.parkingtoll;
|
|
|
|
|
|
|
|
import eu.fibane.parkingtoll.api.ParkingLotApiController;
|
|
import eu.fibane.parkingtoll.api.ParkingLotApiController;
|
|
|
|
|
+import eu.fibane.parkingtoll.core.InMemoryPersistenceManager;
|
|
|
|
|
+import eu.fibane.parkingtoll.exceptions.ParkingNotFoundException;
|
|
|
|
|
+import eu.fibane.parkingtoll.model.CarSlot;
|
|
|
|
|
+import eu.fibane.parkingtoll.model.ParkingLot;
|
|
|
|
|
+import eu.fibane.parkingtoll.model.ParkingLotTest;
|
|
|
|
|
+import eu.fibane.parkingtoll.model.PricingPolicy;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.mockito.InjectMocks;
|
|
|
|
|
+import org.mockito.Mock;
|
|
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
|
|
|
|
-import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.net.URI;
|
|
|
|
|
+import java.time.Duration;
|
|
|
|
|
+import java.time.Instant;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
|
|
|
@SpringBootTest
|
|
@SpringBootTest
|
|
|
class ParkingTollApplicationTests {
|
|
class ParkingTollApplicationTests {
|
|
|
|
|
|
|
|
- @Autowired
|
|
|
|
|
|
|
+ @Mock @Autowired
|
|
|
|
|
+ private InMemoryPersistenceManager persistenceManager;
|
|
|
|
|
+ @InjectMocks
|
|
|
private ParkingLotApiController parkingLotApiController;
|
|
private ParkingLotApiController parkingLotApiController;
|
|
|
|
|
|
|
|
|
|
+ private List<ParkingLot> parkingLots;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void init(){
|
|
|
|
|
+ MockitoAnnotations.initMocks(this);
|
|
|
|
|
+
|
|
|
|
|
+ //create data for our tests
|
|
|
|
|
+ parkingLots = new ArrayList<>();
|
|
|
|
|
+ parkingLots.add(ParkingLotTest.generateParking("Test parking 1",1L));
|
|
|
|
|
+ parkingLots.add(ParkingLotTest.generateParking("Test parking 2",2L));
|
|
|
|
|
+ parkingLots.add(ParkingLotTest.generateParking("Test parking 3",3L));
|
|
|
|
|
+ parkingLots.add(ParkingLotTest.generateParking("Test parking 3 second part",4L));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void searchEmptyParkingLotTest() {
|
|
|
|
|
+ when(persistenceManager.getAllParkingLots()).thenReturn(Collections.emptyList());
|
|
|
|
|
+
|
|
|
|
|
+ ResponseEntity<Collection<ParkingLot>> result = parkingLotApiController.searchParkingLot("");
|
|
|
|
|
+ assertNotNull(result.getBody());
|
|
|
|
|
+ assertEquals(0, result.getBody().size());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void searchParkingLotTestWithWrongName() {
|
|
|
|
|
+ when(persistenceManager.getAllParkingLots()).thenReturn(parkingLots);
|
|
|
|
|
+
|
|
|
|
|
+ ResponseEntity<Collection<ParkingLot>> result = parkingLotApiController.searchParkingLot("");
|
|
|
|
|
+ assertNotNull(result.getBody());
|
|
|
|
|
+ assertEquals(parkingLots.size(), result.getBody().size());
|
|
|
|
|
+
|
|
|
|
|
+ result = parkingLotApiController.searchParkingLot("String that is not in the names of the parking lots");
|
|
|
|
|
+ assertNotNull(result.getBody());
|
|
|
|
|
+ assertEquals(0, result.getBody().size());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void searchParkingLotTestWithCorrectName() {
|
|
|
|
|
+ when(persistenceManager.getAllParkingLots()).thenReturn(parkingLots);
|
|
|
|
|
+
|
|
|
|
|
+ ResponseEntity<Collection<ParkingLot>> result = parkingLotApiController.searchParkingLot("");
|
|
|
|
|
+ assertNotNull(result.getBody());
|
|
|
|
|
+ assertEquals(parkingLots.size(), result.getBody().size());
|
|
|
|
|
+
|
|
|
|
|
+ //entire name
|
|
|
|
|
+ result = parkingLotApiController.searchParkingLot("Test parking 1");
|
|
|
|
|
+ assertNotNull(result.getBody());
|
|
|
|
|
+ assertEquals(1, result.getBody().size());
|
|
|
|
|
+
|
|
|
|
|
+ result = parkingLotApiController.searchParkingLot("Test parking 3");
|
|
|
|
|
+ assertNotNull(result.getBody());
|
|
|
|
|
+ assertEquals(2, result.getBody().size());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void parkingLotDeleteByIdTest(){
|
|
|
|
|
+ when(persistenceManager.deleteParkingLotById(eq(-5L))).thenThrow(ParkingNotFoundException.class);
|
|
|
|
|
+ when(persistenceManager.deleteParkingLotById(eq(1L))).thenReturn(parkingLots.get(0));
|
|
|
|
|
+
|
|
|
|
|
+ //non existing id
|
|
|
|
|
+ assertThrows(ParkingNotFoundException.class, () -> parkingLotApiController.parkingLotDeleteById(-5L));
|
|
|
|
|
+
|
|
|
|
|
+ ResponseEntity<ParkingLot> result = parkingLotApiController.parkingLotDeleteById(1L);
|
|
|
|
|
+ assertEquals(parkingLots.get(0), result.getBody());
|
|
|
|
|
+ assertEquals(result.getStatusCode(), HttpStatus.OK);
|
|
|
|
|
+
|
|
|
|
|
+ verify(persistenceManager, times(2)).deleteParkingLotById(any());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Test
|
|
@Test
|
|
|
- void contextLoads() {
|
|
|
|
|
- assertNotNull(parkingLotApiController);
|
|
|
|
|
|
|
+ void addParkingLotTest(){
|
|
|
|
|
+ ParkingLot storedParkingLot = ParkingLotTest.generateParking("created parking", 55L);
|
|
|
|
|
+
|
|
|
|
|
+ when(persistenceManager.addParkingLot(any())).thenReturn(storedParkingLot);
|
|
|
|
|
+
|
|
|
|
|
+ ResponseEntity<ParkingLot> response = parkingLotApiController.addParkingLot(parkingLots.get(0));
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(HttpStatus.CREATED, response.getStatusCode());
|
|
|
|
|
+ assertEquals(URI.create("/parking_lot/55") , response.getHeaders().getLocation());
|
|
|
|
|
+ assertNotNull(response.getBody());
|
|
|
|
|
+ assertEquals(55L, response.getBody().getId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void parkingLotGetByIdTest(){
|
|
|
|
|
+ ParkingLot storedParkingLot = ParkingLotTest.generateParking("created parking", 5L);
|
|
|
|
|
+ when(persistenceManager.getParkingLotById(5L)).thenReturn(storedParkingLot);
|
|
|
|
|
+ ResponseEntity<ParkingLot> result = parkingLotApiController.parkingLotGetById(5L);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(HttpStatus.OK, result.getStatusCode());
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void leaveParkingLotTest(){
|
|
|
|
|
+ ParkingLot storedParkingLot = ParkingLotTest.generateParking("created parking", 5L);
|
|
|
|
|
+ PricingPolicy policy = new PricingPolicy();
|
|
|
|
|
+ policy.setFlatFee(BigDecimal.valueOf(1));
|
|
|
|
|
+ storedParkingLot.setPricingPolicy(policy);
|
|
|
|
|
+
|
|
|
|
|
+ when(persistenceManager.getParkingLotById(storedParkingLot.getId())).thenReturn(storedParkingLot);
|
|
|
|
|
+
|
|
|
|
|
+ CarSlot carSlot = new CarSlot();
|
|
|
|
|
+ carSlot.setArrivalTime(Instant.now().minus(Duration.ofHours(1)));
|
|
|
|
|
+ carSlot.setType(storedParkingLot.getSlotTypes().get(0));
|
|
|
|
|
+ storedParkingLot.parkCar(carSlot);
|
|
|
|
|
+
|
|
|
|
|
+ ResponseEntity<CarSlot> result = parkingLotApiController.leaveParkingLot(storedParkingLot.getId(), carSlot);
|
|
|
|
|
+ assertEquals(HttpStatus.OK, result.getStatusCode());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void parkParkingLotTest(){
|
|
|
|
|
+ ParkingLot storedParkingLot = ParkingLotTest.generateParking("created parking", 5L);
|
|
|
|
|
+ CarSlot carSlot = new CarSlot();
|
|
|
|
|
+ carSlot.setType(storedParkingLot.getSlotTypes().get(0));
|
|
|
|
|
+ when(persistenceManager.getParkingLotById(storedParkingLot.getId())).thenReturn(storedParkingLot);
|
|
|
|
|
+
|
|
|
|
|
+ ResponseEntity<CarSlot> result = parkingLotApiController.parkParkingLot(storedParkingLot.getId(), carSlot);
|
|
|
|
|
+ assertEquals(HttpStatus.OK, result.getStatusCode());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void updateParkingLotTest(){
|
|
|
|
|
+ //no existing parking lots
|
|
|
|
|
+ when(persistenceManager.getParkingLotById(any())).thenReturn(null);
|
|
|
|
|
+ doNothing().when(persistenceManager).updateParkingLot(anyLong(), any());
|
|
|
|
|
+
|
|
|
|
|
+ ParkingLot newParkingLot = ParkingLotTest.generateParking("created parking", 5L);
|
|
|
|
|
+ ResponseEntity<ParkingLot> result = parkingLotApiController.updateParkingLot(newParkingLot.getId(), newParkingLot);
|
|
|
|
|
+ assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode());
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ when(persistenceManager.parkingLotExists(5L)).thenReturn(true);
|
|
|
|
|
+ newParkingLot = ParkingLotTest.generateParking("created parking", 5L);
|
|
|
|
|
+ result = parkingLotApiController.updateParkingLot(newParkingLot.getId(), newParkingLot);
|
|
|
|
|
+ assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode());
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|