Skip to content
Snippets Groups Projects
Commit 54e36d19 authored by Andrej Šimurka's avatar Andrej Šimurka
Browse files

Reworked controllers unit tests

parent bfb577c9
No related branches found
No related tags found
2 merge requests!60Docker,!5119 car tests
Pipeline #
package cz.muni.pa165.car.rest.unit; 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.rest.CarComponentPairController;
import cz.muni.pa165.car.service.CarComponentPairService; import cz.muni.pa165.car.service.CarComponentPairService;
import cz.muni.pa165.common_library.dtos.CarComponentDto; import cz.muni.pa165.common_library.dtos.CarComponentDto;
import cz.muni.pa165.common_library.dtos.CarResponseDto; 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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
...@@ -32,16 +34,28 @@ public class CarComponentPairControllerUnitTest { ...@@ -32,16 +34,28 @@ public class CarComponentPairControllerUnitTest {
@MockBean @MockBean
private CarComponentPairService carComponentPairServiceMock; private CarComponentPairService carComponentPairServiceMock;
@Autowired
private ObjectMapper objectMapper;
private static CarResponseDto carResponseDto; private static CarResponseDto carResponseDto;
private static CarResponseDto carResponseDto2;
private static CarComponentDto carComponentDto; private static CarComponentDto carComponentDto;
private static CarComponentDto carComponentDto2;
@BeforeEach @BeforeEach
void initializeTestObjects() { void initializeTestObjects() {
carResponseDto = CarResponseDto.builder() carResponseDto = CarResponseDto.builder()
.id(1L)
.componentIdsNames(List.of(1L, 2L))
.driverIdsNames(List.of(1L))
.mainDriverId(1L)
.build();
carResponseDto2 = CarResponseDto.builder()
.id(1L) .id(1L)
.componentIdsNames(List.of()) .componentIdsNames(List.of())
.driverIdsNames(List.of()) .driverIdsNames(List.of(1L))
.mainDriverId(1L) .mainDriverId(1L)
.build(); .build();
...@@ -52,6 +66,14 @@ public class CarComponentPairControllerUnitTest { ...@@ -52,6 +66,14 @@ public class CarComponentPairControllerUnitTest {
.price(BigDecimal.TEN) .price(BigDecimal.TEN)
.weight(BigDecimal.TEN) .weight(BigDecimal.TEN)
.build(); .build();
carComponentDto2 = CarComponentDto.builder()
.id(2L)
.name("")
.manufacturer("")
.price(BigDecimal.TEN)
.weight(BigDecimal.TEN)
.build();
} }
...@@ -59,34 +81,43 @@ public class CarComponentPairControllerUnitTest { ...@@ -59,34 +81,43 @@ public class CarComponentPairControllerUnitTest {
void addComponent() throws Exception { void addComponent() throws Exception {
given(carComponentPairServiceMock.addComponent(1L, 1L)).willReturn(carResponseDto); given(carComponentPairServiceMock.addComponent(1L, 1L)).willReturn(carResponseDto);
mockMvc.perform(put("/carcomponent/addcomponent") String response = mockMvc.perform(put("/carcomponent/addcomponent")
.param("componentId", String.valueOf(2L)) .param("componentId", String.valueOf(1L))
.param("carId", String.valueOf(1L)) .param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON) .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 @Test
void removeComponent() throws Exception { void removeComponent() throws Exception {
given(carComponentPairServiceMock.removeComponent(1L, 1L)).willReturn(carResponseDto); given(carComponentPairServiceMock.removeComponent(1L, 1L)).willReturn(carResponseDto2);
mockMvc.perform(put("/carcomponent/removecomponent") String response = mockMvc.perform(put("/carcomponent/removecomponent")
.param("componentId", String.valueOf(2L)) .param("componentId", String.valueOf(1L))
.param("carId", String.valueOf(1L)) .param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON) .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 @Test
void getAllComponentsOfCar() throws Exception { 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)) .param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON) .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
...@@ -5,9 +5,11 @@ import cz.muni.pa165.car.rest.CarController; ...@@ -5,9 +5,11 @@ import cz.muni.pa165.car.rest.CarController;
import cz.muni.pa165.car.service.CarService; import cz.muni.pa165.car.service.CarService;
import cz.muni.pa165.common_library.dtos.CarRequestDto; import cz.muni.pa165.common_library.dtos.CarRequestDto;
import cz.muni.pa165.common_library.dtos.CarResponseDto; 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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
...@@ -21,7 +23,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder ...@@ -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.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 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.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import java.util.List; import java.util.List;
@AutoConfigureMockMvc @AutoConfigureMockMvc
...@@ -40,6 +42,9 @@ public class CarControllerUnitTest { ...@@ -40,6 +42,9 @@ public class CarControllerUnitTest {
private static CarRequestDto carRequestDto; private static CarRequestDto carRequestDto;
private static CarResponseDto carResponseDto; private static CarResponseDto carResponseDto;
private static CarResponseDto carResponseDto2;
private static CarResponseDto carResponseDto3;
@BeforeEach @BeforeEach
void initializeTestObjects() { void initializeTestObjects() {
...@@ -47,14 +52,28 @@ public class CarControllerUnitTest { ...@@ -47,14 +52,28 @@ public class CarControllerUnitTest {
carRequestDto = CarRequestDto.builder() carRequestDto = CarRequestDto.builder()
.componentIds(List.of()) .componentIds(List.of())
.driverIds(List.of()) .driverIds(List.of())
.mainDriverId(1L) .mainDriverId(null)
.build(); .build();
carResponseDto = CarResponseDto.builder() carResponseDto = CarResponseDto.builder()
.id(1L) .id(1L)
.componentIdsNames(List.of()) .componentIdsNames(List.of())
.driverIdsNames(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(); .build();
} }
...@@ -62,7 +81,7 @@ public class CarControllerUnitTest { ...@@ -62,7 +81,7 @@ public class CarControllerUnitTest {
void nonExistingPathPostTest() throws Exception { void nonExistingPathPostTest() throws Exception {
given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto); given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto);
mockMvc.perform(post("/car/invalidPath/")) mockMvc.perform(put("/car/invalidPath/"))
.andExpect(status().isNotFound()); .andExpect(status().isNotFound());
} }
...@@ -71,49 +90,45 @@ public class CarControllerUnitTest { ...@@ -71,49 +90,45 @@ public class CarControllerUnitTest {
void createCarTest() throws Exception { void createCarTest() throws Exception {
given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto); given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto);
mockMvc.perform(post("/car/") String response = mockMvc.perform(post("/car/")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(carRequestDto))) .content(objectMapper.writeValueAsString(carRequestDto)))
.andExpect(status().isOk()); .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
CarResponseDto carResponseDtoResponse = objectMapper.readValue(response, CarResponseDto.class);
Assertions.assertEquals(carResponseDtoResponse, carResponseDto);
} }
@Test @Test
void deleteExistingCarTest() throws Exception { 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)) .param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()); .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
} Assertions.assertEquals("Car with id = " + 1L + " deleted!", response);
@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());
} }
@Test @Test
void getCarTest() throws Exception { 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)) .param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)) .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 @Test
void getAllCarsTest() throws Exception { void getAllCarsTest() throws Exception {
given(carServiceMock.getAllCars()).willReturn(List.of()); given(carServiceMock.getAllCars()).willReturn(List.of(carResponseDto2, carResponseDto3));
mockMvc.perform(get("/car/all")) String response = mockMvc.perform(get("/car/all"))
.andExpect(status().isOk()); .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
package cz.muni.pa165.car.rest.unit; 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.rest.CarDriverPairController;
import cz.muni.pa165.car.service.CarDriverPairService; import cz.muni.pa165.car.service.CarDriverPairService;
import cz.muni.pa165.common_library.dtos.CarResponseDto; import cz.muni.pa165.common_library.dtos.CarResponseDto;
import cz.muni.pa165.common_library.dtos.DriverDto; 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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
...@@ -15,6 +17,7 @@ import org.springframework.test.web.servlet.MockMvc; ...@@ -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.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@WebMvcTest(CarDriverPairController.class) @WebMvcTest(CarDriverPairController.class)
...@@ -26,24 +29,59 @@ public class CarDriverPairControllerUnitTest { ...@@ -26,24 +29,59 @@ public class CarDriverPairControllerUnitTest {
@MockBean @MockBean
private CarDriverPairService carDriverPairServiceMock; private CarDriverPairService carDriverPairServiceMock;
@Autowired
private ObjectMapper objectMapper;
private static DriverDto driverDto; private static DriverDto driverDto;
private static DriverDto driverDto2;
private static CarResponseDto carResponseDto; private static CarResponseDto carResponseDto;
private static CarResponseDto carResponseDto2;
private static CarResponseDto carResponseDto3;
private static CarResponseDto carResponseDto4;
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
driverDto = DriverDto.builder() driverDto = DriverDto.builder()
.id(1L) .id(1L)
.name("") .name("Fernando")
.surname("") .surname("Alonso")
.build();
driverDto2 = DriverDto.builder()
.id(2L)
.name("Max")
.surname("Verstappen")
.build(); .build();
carResponseDto = CarResponseDto.builder() carResponseDto = CarResponseDto.builder()
.id(1L) .id(1L)
.componentIdsNames(List.of()) .componentIdsNames(List.of())
.driverIdsNames(List.of(1L))
.mainDriverId(null)
.build();
carResponseDto2 = CarResponseDto.builder()
.id(2L)
.componentIdsNames(List.of())
.driverIdsNames(List.of()) .driverIdsNames(List.of())
.mainDriverId(null)
.build();
carResponseDto3 = CarResponseDto.builder()
.id(3L)
.componentIdsNames(List.of())
.driverIdsNames(List.of(1L, 2L))
.mainDriverId(1L) .mainDriverId(1L)
.build(); .build();
carResponseDto4 = CarResponseDto.builder()
.id(1L)
.componentIdsNames(List.of())
.driverIdsNames(List.of())
.mainDriverId(null)
.build();
} }
@Test @Test
...@@ -58,53 +96,70 @@ public class CarDriverPairControllerUnitTest { ...@@ -58,53 +96,70 @@ public class CarDriverPairControllerUnitTest {
public void assignDriverToCar() throws Exception { public void assignDriverToCar() throws Exception {
given(carDriverPairServiceMock.assignDriverToCar(1L, 1L)).willReturn(carResponseDto); 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("driverId", String.valueOf(1L))
.param("carId", String.valueOf(1L)) .param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)) .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 @Test
public void unassignDriverFromCar() throws Exception { 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("driverId", String.valueOf(1L))
.param("carId", String.valueOf(1L)) .param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)) .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 @Test
public void getAllDriversOfCar() throws Exception { 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") String response = mockMvc.perform(put("/cardriver/alldrivers")
.param("carId", String.valueOf(1L)) .param("carId", String.valueOf(3L))
.contentType(MediaType.APPLICATION_JSON)) .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 @Test
public void setMainDriver() throws Exception { 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("driverId", String.valueOf(1L))
.param("carId", String.valueOf(1L)) .param("carId", String.valueOf(3L))
.contentType(MediaType.APPLICATION_JSON)) .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 @Test
public void removeMainDriver() throws Exception { public void removeMainDriver() throws Exception {
given(carDriverPairServiceMock.removeMainDriver(1L)).willReturn(carResponseDto); given(carDriverPairServiceMock.removeMainDriver(1L)).willReturn(carResponseDto);
mockMvc.perform(put("/cardriver/removemain") String response = mockMvc.perform(put("/cardriver/removemain")
.param("driverId", String.valueOf(1L)) .param("driverId", String.valueOf(1L))
.param("carId", String.valueOf(1L)) .param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)) .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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment