From 54e36d19e690f56a2fcc9d150466485086c5221a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20=C5=A0imurka?= <492781@mail.muni.cz> Date: Sun, 16 Apr 2023 21:03:49 +0200 Subject: [PATCH] Reworked controllers unit tests --- .../CarComponentPairControllerUnitTest.java | 53 ++++++++--- .../car/rest/unit/CarControllerUnitTest.java | 71 +++++++++------ .../unit/CarDriverPairControllerUnitTest.java | 89 +++++++++++++++---- 3 files changed, 157 insertions(+), 56 deletions(-) diff --git a/car/src/test/java/cz/muni/pa165/car/rest/unit/CarComponentPairControllerUnitTest.java b/car/src/test/java/cz/muni/pa165/car/rest/unit/CarComponentPairControllerUnitTest.java index 09c0e78c..54b7c2c0 100644 --- a/car/src/test/java/cz/muni/pa165/car/rest/unit/CarComponentPairControllerUnitTest.java +++ b/car/src/test/java/cz/muni/pa165/car/rest/unit/CarComponentPairControllerUnitTest.java @@ -1,9 +1,11 @@ package cz.muni.pa165.car.rest.unit; +import com.fasterxml.jackson.databind.ObjectMapper; import cz.muni.pa165.car.rest.CarComponentPairController; import cz.muni.pa165.car.service.CarComponentPairService; import cz.muni.pa165.common_library.dtos.CarComponentDto; import cz.muni.pa165.common_library.dtos.CarResponseDto; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -32,16 +34,28 @@ public class CarComponentPairControllerUnitTest { @MockBean private CarComponentPairService carComponentPairServiceMock; + @Autowired + private ObjectMapper objectMapper; + private static CarResponseDto carResponseDto; + private static CarResponseDto carResponseDto2; private static CarComponentDto carComponentDto; + private static CarComponentDto carComponentDto2; @BeforeEach void initializeTestObjects() { carResponseDto = CarResponseDto.builder() + .id(1L) + .componentIdsNames(List.of(1L, 2L)) + .driverIdsNames(List.of(1L)) + .mainDriverId(1L) + .build(); + + carResponseDto2 = CarResponseDto.builder() .id(1L) .componentIdsNames(List.of()) - .driverIdsNames(List.of()) + .driverIdsNames(List.of(1L)) .mainDriverId(1L) .build(); @@ -52,6 +66,14 @@ public class CarComponentPairControllerUnitTest { .price(BigDecimal.TEN) .weight(BigDecimal.TEN) .build(); + + carComponentDto2 = CarComponentDto.builder() + .id(2L) + .name("") + .manufacturer("") + .price(BigDecimal.TEN) + .weight(BigDecimal.TEN) + .build(); } @@ -59,34 +81,43 @@ public class CarComponentPairControllerUnitTest { void addComponent() throws Exception { given(carComponentPairServiceMock.addComponent(1L, 1L)).willReturn(carResponseDto); - mockMvc.perform(put("/carcomponent/addcomponent") - .param("componentId", String.valueOf(2L)) + String response = mockMvc.perform(put("/carcomponent/addcomponent") + .param("componentId", String.valueOf(1L)) .param("carId", String.valueOf(1L)) .contentType(MediaType.APPLICATION_JSON) ) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + + CarResponseDto carResponseDto = objectMapper.readValue(response, CarResponseDto.class); + Assertions.assertEquals(1L, carResponseDto.getId()); + Assertions.assertEquals(List.of(1L, 2L), carResponseDto.getComponentIdsNames()); } @Test void removeComponent() throws Exception { - given(carComponentPairServiceMock.removeComponent(1L, 1L)).willReturn(carResponseDto); + given(carComponentPairServiceMock.removeComponent(1L, 1L)).willReturn(carResponseDto2); - mockMvc.perform(put("/carcomponent/removecomponent") - .param("componentId", String.valueOf(2L)) + String response = mockMvc.perform(put("/carcomponent/removecomponent") + .param("componentId", String.valueOf(1L)) .param("carId", String.valueOf(1L)) .contentType(MediaType.APPLICATION_JSON) ) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + CarResponseDto carResponseDto = objectMapper.readValue(response, CarResponseDto.class); + Assertions.assertEquals(1L, carResponseDto.getId()); + Assertions.assertEquals(List.of(), carResponseDto.getComponentIdsNames()); } @Test void getAllComponentsOfCar() throws Exception { - given(carComponentPairServiceMock.getAllComponentsOfCar(1L)).willReturn(List.of(carComponentDto)); + given(carComponentPairServiceMock.getAllComponentsOfCar(1L)).willReturn(List.of(carComponentDto, carComponentDto2)); - mockMvc.perform(put("/carcomponent/getcomponents") + String response = mockMvc.perform(put("/carcomponent/getcomponents") .param("carId", String.valueOf(1L)) .contentType(MediaType.APPLICATION_JSON) ) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + List<CarComponentDto> carComponentListResponse = List.of(objectMapper.readValue(response, CarComponentDto[].class)); + Assertions.assertEquals(List.of(carComponentDto, carComponentDto2), carComponentListResponse); } } \ No newline at end of file diff --git a/car/src/test/java/cz/muni/pa165/car/rest/unit/CarControllerUnitTest.java b/car/src/test/java/cz/muni/pa165/car/rest/unit/CarControllerUnitTest.java index 026d5d16..977915a6 100644 --- a/car/src/test/java/cz/muni/pa165/car/rest/unit/CarControllerUnitTest.java +++ b/car/src/test/java/cz/muni/pa165/car/rest/unit/CarControllerUnitTest.java @@ -5,9 +5,11 @@ import cz.muni.pa165.car.rest.CarController; import cz.muni.pa165.car.service.CarService; import cz.muni.pa165.common_library.dtos.CarRequestDto; import cz.muni.pa165.common_library.dtos.CarResponseDto; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.BDDMockito.given; import org.mockito.junit.jupiter.MockitoExtension; @@ -21,7 +23,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import java.util.List; @AutoConfigureMockMvc @@ -40,6 +42,9 @@ public class CarControllerUnitTest { private static CarRequestDto carRequestDto; private static CarResponseDto carResponseDto; + private static CarResponseDto carResponseDto2; + private static CarResponseDto carResponseDto3; + @BeforeEach void initializeTestObjects() { @@ -47,14 +52,28 @@ public class CarControllerUnitTest { carRequestDto = CarRequestDto.builder() .componentIds(List.of()) .driverIds(List.of()) - .mainDriverId(1L) + .mainDriverId(null) .build(); carResponseDto = CarResponseDto.builder() .id(1L) .componentIdsNames(List.of()) .driverIdsNames(List.of()) - .mainDriverId(1L) + .mainDriverId(null) + .build(); + + carResponseDto2 = CarResponseDto.builder() + .id(1L) + .componentIdsNames(List.of()) + .driverIdsNames(List.of()) + .mainDriverId(null) + .build(); + + carResponseDto3 = CarResponseDto.builder() + .id(2L) + .componentIdsNames(List.of()) + .driverIdsNames(List.of()) + .mainDriverId(null) .build(); } @@ -62,7 +81,7 @@ public class CarControllerUnitTest { void nonExistingPathPostTest() throws Exception { given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto); - mockMvc.perform(post("/car/invalidPath/")) + mockMvc.perform(put("/car/invalidPath/")) .andExpect(status().isNotFound()); } @@ -71,49 +90,45 @@ public class CarControllerUnitTest { void createCarTest() throws Exception { given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto); - mockMvc.perform(post("/car/") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(carRequestDto))) - .andExpect(status().isOk()); + String response = mockMvc.perform(post("/car/") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(carRequestDto))) + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + CarResponseDto carResponseDtoResponse = objectMapper.readValue(response, CarResponseDto.class); + Assertions.assertEquals(carResponseDtoResponse, carResponseDto); } @Test void deleteExistingCarTest() throws Exception { - given(carServiceMock.deleteById(1L)).willReturn(""); + given(carServiceMock.deleteById(1L)).willReturn("Car with id = " + 1L + " deleted!"); - mockMvc.perform(delete("/car/") + String response = mockMvc.perform(delete("/car/") .param("carId", String.valueOf(1L)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); - } - - @Test - void deleteNonExistingCarTest() throws Exception { - given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto); - given(carServiceMock.deleteById(1L)).willReturn(anyString()); - - mockMvc.perform(delete("/car/") - .param("carId", String.valueOf(-1L)) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + Assertions.assertEquals("Car with id = " + 1L + " deleted!", response); } @Test void getCarTest() throws Exception { - given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto); + given(carServiceMock.getCarById(1L)).willReturn(carResponseDto2); - mockMvc.perform(get("/car/") + String response = mockMvc.perform(get("/car/") .param("carId", String.valueOf(1L)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + CarResponseDto carResponseDtoResponse = objectMapper.readValue(response, CarResponseDto.class); + Assertions.assertEquals(carResponseDtoResponse, carResponseDto2); } @Test void getAllCarsTest() throws Exception { - given(carServiceMock.getAllCars()).willReturn(List.of()); + given(carServiceMock.getAllCars()).willReturn(List.of(carResponseDto2, carResponseDto3)); - mockMvc.perform(get("/car/all")) - .andExpect(status().isOk()); + String response = mockMvc.perform(get("/car/all")) + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + List<CarResponseDto> responseList = List.of(objectMapper.readValue(response, CarResponseDto[].class)); + Assertions.assertEquals(responseList, List.of(carResponseDto2, carResponseDto3)); } } \ No newline at end of file diff --git a/car/src/test/java/cz/muni/pa165/car/rest/unit/CarDriverPairControllerUnitTest.java b/car/src/test/java/cz/muni/pa165/car/rest/unit/CarDriverPairControllerUnitTest.java index 6de1df51..55846cd1 100644 --- a/car/src/test/java/cz/muni/pa165/car/rest/unit/CarDriverPairControllerUnitTest.java +++ b/car/src/test/java/cz/muni/pa165/car/rest/unit/CarDriverPairControllerUnitTest.java @@ -1,9 +1,11 @@ package cz.muni.pa165.car.rest.unit; +import com.fasterxml.jackson.databind.ObjectMapper; import cz.muni.pa165.car.rest.CarDriverPairController; import cz.muni.pa165.car.service.CarDriverPairService; import cz.muni.pa165.common_library.dtos.CarResponseDto; import cz.muni.pa165.common_library.dtos.DriverDto; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.mockito.BDDMockito.given; @@ -15,6 +17,7 @@ import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.util.ArrayList; import java.util.List; @WebMvcTest(CarDriverPairController.class) @@ -26,24 +29,59 @@ public class CarDriverPairControllerUnitTest { @MockBean private CarDriverPairService carDriverPairServiceMock; + @Autowired + private ObjectMapper objectMapper; + + private static DriverDto driverDto; + private static DriverDto driverDto2; private static CarResponseDto carResponseDto; + private static CarResponseDto carResponseDto2; + private static CarResponseDto carResponseDto3; + private static CarResponseDto carResponseDto4; @BeforeEach void beforeEach() { driverDto = DriverDto.builder() .id(1L) - .name("") - .surname("") + .name("Fernando") + .surname("Alonso") + .build(); + + driverDto2 = DriverDto.builder() + .id(2L) + .name("Max") + .surname("Verstappen") .build(); carResponseDto = CarResponseDto.builder() .id(1L) .componentIdsNames(List.of()) + .driverIdsNames(List.of(1L)) + .mainDriverId(null) + .build(); + + carResponseDto2 = CarResponseDto.builder() + .id(2L) + .componentIdsNames(List.of()) .driverIdsNames(List.of()) + .mainDriverId(null) + .build(); + + carResponseDto3 = CarResponseDto.builder() + .id(3L) + .componentIdsNames(List.of()) + .driverIdsNames(List.of(1L, 2L)) .mainDriverId(1L) .build(); + + carResponseDto4 = CarResponseDto.builder() + .id(1L) + .componentIdsNames(List.of()) + .driverIdsNames(List.of()) + .mainDriverId(null) + .build(); } @Test @@ -58,53 +96,70 @@ public class CarDriverPairControllerUnitTest { public void assignDriverToCar() throws Exception { given(carDriverPairServiceMock.assignDriverToCar(1L, 1L)).willReturn(carResponseDto); - mockMvc.perform(put("/cardriver/assign") + String response = mockMvc.perform(put("/cardriver/assign") .param("driverId", String.valueOf(1L)) .param("carId", String.valueOf(1L)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + + CarResponseDto carResponseDto = objectMapper.readValue(response, CarResponseDto.class); + Assertions.assertEquals(1L, carResponseDto.getId()); + Assertions.assertEquals(List.of(1L), carResponseDto.getDriverIdsNames()); + Assertions.assertNull(carResponseDto.getMainDriverId()); } @Test public void unassignDriverFromCar() throws Exception { - given(carDriverPairServiceMock.unassignDriverFromCar(1L, 1L)).willReturn(carResponseDto); + given(carDriverPairServiceMock.unassignDriverFromCar(1L, 1L)).willReturn(carResponseDto4); - mockMvc.perform(put("/cardriver/unassign") + String response = mockMvc.perform(put("/cardriver/unassign") .param("driverId", String.valueOf(1L)) .param("carId", String.valueOf(1L)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + + CarResponseDto carResponseDto = objectMapper.readValue(response, CarResponseDto.class); + Assertions.assertEquals(1L, carResponseDto.getId()); + Assertions.assertEquals(List.of(), carResponseDto.getDriverIdsNames()); + Assertions.assertNull(carResponseDto.getMainDriverId()); } @Test public void getAllDriversOfCar() throws Exception { - given(carDriverPairServiceMock.getAllDriversOfCar(1L)).willReturn(List.of(driverDto)); + given(carDriverPairServiceMock.getAllDriversOfCar(3L)).willReturn(List.of(driverDto, driverDto2)); - mockMvc.perform(put("/cardriver/alldrivers") - .param("carId", String.valueOf(1L)) + String response = mockMvc.perform(put("/cardriver/alldrivers") + .param("carId", String.valueOf(3L)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + + List<DriverDto> driverDtoListResponse = List.of(objectMapper.readValue(response, DriverDto[].class)); + Assertions.assertEquals(List.of(driverDto, driverDto2), driverDtoListResponse); } @Test public void setMainDriver() throws Exception { - given(carDriverPairServiceMock.setMainDriver(1L, 1L)).willReturn(carResponseDto); + given(carDriverPairServiceMock.setMainDriver(3L, 1L)).willReturn(carResponseDto3); - mockMvc.perform(put("/cardriver/setmain") + String response = mockMvc.perform(put("/cardriver/setmain") .param("driverId", String.valueOf(1L)) - .param("carId", String.valueOf(1L)) + .param("carId", String.valueOf(3L)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + CarResponseDto carResponseDto = objectMapper.readValue(response, CarResponseDto.class); + Assertions.assertEquals(1L, carResponseDto.getMainDriverId()); } @Test public void removeMainDriver() throws Exception { given(carDriverPairServiceMock.removeMainDriver(1L)).willReturn(carResponseDto); - mockMvc.perform(put("/cardriver/removemain") + String response = mockMvc.perform(put("/cardriver/removemain") .param("driverId", String.valueOf(1L)) .param("carId", String.valueOf(1L)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); + .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); + CarResponseDto carResponseDto = objectMapper.readValue(response, CarResponseDto.class); + Assertions.assertNull(carResponseDto.getMainDriverId()); } } \ No newline at end of file -- GitLab