ParkingLotApiControllerTest.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package eu.fcheret.parkingtoll.controller;
  2. import eu.fcheret.parkingtoll.api.ParkingLotApiController;
  3. import eu.fcheret.parkingtoll.model.CarSlot;
  4. import eu.fcheret.parkingtoll.model.ParkingLot;
  5. import eu.fcheret.parkingtoll.model.ParkingLotTest;
  6. import eu.fcheret.parkingtoll.model.PricingPolicy;
  7. import eu.fcheret.parkingtoll.service.ParkingLotService;
  8. import org.junit.jupiter.api.BeforeEach;
  9. import org.junit.jupiter.api.Test;
  10. import org.mockito.InjectMocks;
  11. import org.mockito.Mock;
  12. import org.mockito.MockitoAnnotations;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.boot.test.context.SpringBootTest;
  15. import org.springframework.http.HttpStatus;
  16. import org.springframework.http.ResponseEntity;
  17. import java.math.BigDecimal;
  18. import java.net.URI;
  19. import java.time.Duration;
  20. import java.time.Instant;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import static org.junit.jupiter.api.Assertions.assertEquals;
  26. import static org.junit.jupiter.api.Assertions.assertNotNull;
  27. import static org.mockito.ArgumentMatchers.any;
  28. import static org.mockito.ArgumentMatchers.anyLong;
  29. import static org.mockito.Mockito.*;
  30. @SpringBootTest
  31. public class ParkingLotApiControllerTest {
  32. @Mock @Autowired
  33. private ParkingLotService parkingLotService;
  34. @InjectMocks
  35. private ParkingLotApiController parkingLotApiController;
  36. private List<ParkingLot> parkingLots;
  37. @BeforeEach
  38. void init(){
  39. MockitoAnnotations.initMocks(this);
  40. //create data for our tests
  41. parkingLots = new ArrayList<>();
  42. parkingLots.add(ParkingLotTest.generateParking("Test parking 1",1L));
  43. parkingLots.add(ParkingLotTest.generateParking("Test parking 2",2L));
  44. parkingLots.add(ParkingLotTest.generateParking("Test parking 3",3L));
  45. parkingLots.add(ParkingLotTest.generateParking("Test parking 3 second part",4L));
  46. }
  47. @Test
  48. void searchEmptyParkingLotTest() {
  49. when(parkingLotService.searchParkingLot(any())).thenReturn(Collections.emptyList());
  50. ResponseEntity<Collection<ParkingLot>> result = parkingLotApiController.searchParkingLot("");
  51. assertNotNull(result.getBody());
  52. assertEquals(0, result.getBody().size());
  53. }
  54. @Test
  55. void searchParkingLotTestWithWrongName() {
  56. when(parkingLotService.searchParkingLot("")).thenReturn(parkingLots);
  57. when(parkingLotService.searchParkingLot("String that is not in the names of the parking lots"))
  58. .thenReturn(Collections.emptyList());
  59. ResponseEntity<Collection<ParkingLot>> result = parkingLotApiController.searchParkingLot("");
  60. assertNotNull(result.getBody());
  61. assertEquals(parkingLots.size(), result.getBody().size());
  62. result = parkingLotApiController.searchParkingLot("String that is not in the names of the parking lots");
  63. assertNotNull(result.getBody());
  64. assertEquals(0, result.getBody().size());
  65. }
  66. @Test
  67. void searchParkingLotTestWithCorrectName() {
  68. when(parkingLotService.searchParkingLot("")).thenReturn(parkingLots);
  69. when(parkingLotService.searchParkingLot("Test parking 1"))
  70. .thenReturn(Collections.singletonList(parkingLots.get(0)));
  71. when(parkingLotService.searchParkingLot("Test parking 3"))
  72. .thenReturn(parkingLots.subList(2,4));
  73. ResponseEntity<Collection<ParkingLot>> result = parkingLotApiController.searchParkingLot("");
  74. assertNotNull(result.getBody());
  75. assertEquals(parkingLots.size(), result.getBody().size());
  76. //entire name
  77. result = parkingLotApiController.searchParkingLot("Test parking 1");
  78. assertNotNull(result.getBody());
  79. assertEquals(1, result.getBody().size());
  80. result = parkingLotApiController.searchParkingLot("Test parking 3");
  81. assertNotNull(result.getBody());
  82. assertEquals(2, result.getBody().size());
  83. }
  84. @Test
  85. void parkingLotDeleteByIdTest(){
  86. //non existing id
  87. ResponseEntity<ParkingLot> result = parkingLotApiController.deleteParkingLotById(-5L);
  88. assertEquals(HttpStatus.OK, result.getStatusCode());
  89. when(parkingLotService.deleteParkingLotById(1L)).thenReturn(parkingLots.get(0));
  90. result = parkingLotApiController.deleteParkingLotById(1L);
  91. assertEquals(parkingLots.get(0), result.getBody());
  92. assertEquals(result.getStatusCode(), HttpStatus.OK);
  93. verify(parkingLotService, times(2)).deleteParkingLotById(any());
  94. }
  95. @Test
  96. void addParkingLotTest(){
  97. ParkingLot storedParkingLot = ParkingLotTest.generateParking("created parking", 55L);
  98. when(parkingLotService.createParkingLot(any())).thenReturn(storedParkingLot);
  99. ResponseEntity<ParkingLot> response = parkingLotApiController.createParkingLot(parkingLots.get(0));
  100. assertEquals(HttpStatus.CREATED, response.getStatusCode());
  101. assertEquals(URI.create("/parking_lot/55") , response.getHeaders().getLocation());
  102. assertNotNull(response.getBody());
  103. assertEquals(55L, response.getBody().getId());
  104. }
  105. @Test
  106. void parkingLotGetByIdTest(){
  107. ParkingLot storedParkingLot = ParkingLotTest.generateParking("created parking", 5L);
  108. when(parkingLotService.getParkingById(5L)).thenReturn(storedParkingLot);
  109. ResponseEntity<ParkingLot> result = parkingLotApiController.getParkingById(5L);
  110. assertEquals(HttpStatus.OK, result.getStatusCode());
  111. }
  112. @Test
  113. void leaveParkingLotTest(){
  114. ParkingLot storedParkingLot = ParkingLotTest.generateParking("created parking", 5L);
  115. PricingPolicy policy = new PricingPolicy();
  116. policy.setFlatFee(BigDecimal.valueOf(1));
  117. storedParkingLot.setFareProcessor(policy);
  118. when(parkingLotService.getParkingById(storedParkingLot.getId())).thenReturn(storedParkingLot);
  119. CarSlot carSlot = new CarSlot();
  120. carSlot.setArrivalTime(Instant.now().minus(Duration.ofHours(1)));
  121. carSlot.setType(storedParkingLot.getSlotTypes().get(0));
  122. //storedParkingLot.parkCar(carSlot);
  123. ResponseEntity<CarSlot> result = parkingLotApiController.leaveParkingLot(storedParkingLot.getId(), carSlot);
  124. assertEquals(HttpStatus.OK, result.getStatusCode());
  125. }
  126. @Test
  127. void parkParkingLotTest(){
  128. ParkingLot storedParkingLot = ParkingLotTest.generateParking("created parking", 5L);
  129. CarSlot carSlot = new CarSlot();
  130. carSlot.setType(storedParkingLot.getSlotTypes().get(0));
  131. when(parkingLotService.getParkingById(storedParkingLot.getId())).thenReturn(storedParkingLot);
  132. ResponseEntity<CarSlot> result = parkingLotApiController.parkAtParkingLot(storedParkingLot.getId(), carSlot);
  133. assertEquals(HttpStatus.OK, result.getStatusCode());
  134. }
  135. @Test
  136. void updateParkingLotTest(){
  137. //no existing parking lots
  138. ParkingLot storedParkingLot = ParkingLotTest.generateParking("created parking", 5L);
  139. when(parkingLotService.updateParkingLot(anyLong(),any())).thenReturn(null);
  140. ResponseEntity<ParkingLot> result = parkingLotApiController.updateParkingLot(5L, storedParkingLot);
  141. assertEquals(HttpStatus.CREATED, result.getStatusCode());
  142. assertEquals(URI.create("/parking_lot/" + storedParkingLot.getId()), result.getHeaders().getLocation());
  143. when(parkingLotService.updateParkingLot(anyLong(), any())).thenReturn(storedParkingLot);
  144. result = parkingLotApiController.updateParkingLot(5L, storedParkingLot);
  145. assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode());
  146. }
  147. }