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

Test fixed

parent 6112fe8a
No related branches found
No related tags found
2 merge requests!60Docker,!5119 car tests
Pipeline #
package cz.muni.pa165.car.rest;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.pa165.car.rest.CarDriverPairController;
import cz.muni.pa165.car.restemplate.DbGetter;
import cz.muni.pa165.car.service.CarComponentPairService;
import cz.muni.pa165.car.service.CarDriverPairService;
import cz.muni.pa165.common_library.dtos.CarComponentResponseDto;
import cz.muni.pa165.common_library.dtos.CarResponseDto;
import cz.muni.pa165.common_library.dtos.DriverDto;
import java.math.BigDecimal;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.BDDMockito.given;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
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.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@WebMvcTest(CarComponentPairController.class)
@Import(TestConfig.class)
public class CarComponentPairControllerTest {
private static CarResponseDto carResponseDto;
private static CarResponseDto carResponseDto2;
private static CarComponentResponseDto carComponentResponseDto;
private static CarComponentResponseDto carComponentResponseDto2;
@Autowired
private MockMvc mockMvc;
@MockBean
private CarComponentPairService carComponentPairServiceMock;
@Autowired
private ObjectMapper objectMapper;
private static CarResponseDto carResponseDto;
private static CarResponseDto carResponseDto2;
// private static CarComponentRequestDto carComponentRequestDto;
// private static CarComponentRequestDto carComponentRequestDto2;
private static CarComponentResponseDto carComponentResponseDto;
private static CarComponentResponseDto carComponentResponseDto2;
@BeforeEach
void initializeTestObjects() {
......@@ -69,20 +53,6 @@ public class CarComponentPairControllerTest {
.mainDriverId(1L)
.build();
// carComponentRequestDto = CarComponentRequestDto.builder()
// .name("")
// .weight(BigDecimal.TEN)
// .manufacturer("")
// .price(BigDecimal.TEN)
// .build();
//
// carComponentRequestDto2 = CarComponentRequestDto.builder()
// .name("")
// .manufacturer("")
// .price(BigDecimal.TEN)
// .weight(BigDecimal.TEN)
// .build();
carComponentResponseDto = CarComponentResponseDto.builder()
.id(1L)
.name("")
......@@ -106,10 +76,10 @@ public class CarComponentPairControllerTest {
given(carComponentPairServiceMock.addComponent(1L, 1L)).willReturn(carResponseDto);
String response = mockMvc.perform(put("/carcomponent/addcomponent")
.param("componentId", String.valueOf(1L))
.param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)
)
.param("componentId", String.valueOf(1L))
.param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
CarResponseDto carResponseDto = objectMapper.readValue(response, CarResponseDto.class);
......@@ -122,10 +92,10 @@ public class CarComponentPairControllerTest {
given(carComponentPairServiceMock.removeComponent(1L, 1L)).willReturn(carResponseDto2);
String response = mockMvc.perform(put("/carcomponent/removecomponent")
.param("componentId", String.valueOf(1L))
.param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)
)
.param("componentId", String.valueOf(1L))
.param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
CarResponseDto carResponseDto = objectMapper.readValue(response, CarResponseDto.class);
Assertions.assertEquals(1L, carResponseDto.getId());
......@@ -136,10 +106,10 @@ public class CarComponentPairControllerTest {
void getAllComponentsOfCar() throws Exception {
given(carComponentPairServiceMock.getAllComponentsOfCar(1L)).willReturn(List.of(carComponentResponseDto, carComponentResponseDto2));
String response = mockMvc.perform(put("/carcomponent/getcomponents")
.param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)
)
String response = mockMvc.perform(get("/carcomponent/getcomponents")
.param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
List<CarComponentResponseDto> carComponentListResponse = List.of(objectMapper.readValue(response, CarComponentResponseDto[].class));
Assertions.assertEquals(List.of(carComponentResponseDto, carComponentResponseDto2), carComponentListResponse);
......
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