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

Car module controller unit and integration tests

parent 21cb4b6b
No related branches found
No related tags found
2 merge requests!60Docker,!5119 car tests
Pipeline #
Showing
with 553 additions and 1 deletion
...@@ -64,6 +64,11 @@ ...@@ -64,6 +64,11 @@
<artifactId>json</artifactId> <artifactId>json</artifactId>
<version>20210307</version> <version>20210307</version>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
......
...@@ -18,7 +18,7 @@ import org.springframework.web.client.RestTemplate; ...@@ -18,7 +18,7 @@ import org.springframework.web.client.RestTemplate;
* Main app. * Main app.
*/ */
@SpringBootApplication @SpringBootApplication
@EnableJpaRepositories(basePackages = {"cz.muni.pa165.car.data.repository"}) //@EnableJpaRepositories(basePackages = {"cz.muni.pa165.car.data.repository"})
@EnableTransactionManagement @EnableTransactionManagement
@EntityScan(basePackageClasses = {Car.class, Driver.class, CarComponent.class}) @EntityScan(basePackageClasses = {Car.class, Driver.class, CarComponent.class})
//@Import({RestExceptionHandler.class, ClientConfig.class}) //@Import({RestExceptionHandler.class, ClientConfig.class})
......
package cz.muni.pa165.car.rest.integration;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CarComponentPairControllerIntegrationTest {
// @BeforeEach
// void setUp() {
// }
//
// @Test
// void addComponent() {
// }
//
// @Test
// void removeComponent() {
// }
//
// @Test
// void getAllComponentsOfCar() {
// }
}
\ No newline at end of file
package cz.muni.pa165.car.rest.integration;
import cz.muni.pa165.car.data.model.Car;
import cz.muni.pa165.car.data.repository.CarRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@DataJpaTest
class DriverControllerItTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private CarRepository carRepository;
@Test
public void testSave() {
Car car = Car.builder()
.id(1L)
.components(List.of(2L, 3L, 4L))
.drivers(List.of(5L, 6L, 7L))
.mainDriverId(6L)
.build();
Car savedCar = carRepository.save(car);
Assertions.assertEquals(savedCar, car);
Assertions.assertEquals(entityManager.find(Car.class, 1L).getId(), 1);
}
}
package cz.muni.pa165.car.rest.integration;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CarDriverPairControllerIntegrationTest {
// @BeforeEach
// void setUp() {
// }
//
// @Test
// void assignDriverToCar() {
// }
//
// @Test
// void unassignDriverFromCar() {
// }
//
// @Test
// void getAllDriversOfCar() {
// }
//
// @Test
// void setMainDriver() {
// }
//
// @Test
// void removeMainDriver() {
// }
}
\ No newline at end of file
package cz.muni.pa165.car.rest.unit;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.pa165.car.rest.CarComponentPairController;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(controllers = CarComponentPairController.class)
@AutoConfigureMockMvc(addFilters = false)
@ExtendWith(MockitoExtension.class)
class CarComponentPairControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CarComponentPairController controller;
@Autowired
private ObjectMapper objectMapper;
// @BeforeEach
// void setUp() {
// }
//
// @AfterEach
// void tearDown() {
// }
//
// @Test
// void addComponent() {
// }
//
// @Test
// void removeComponent() {
// }
//
// @Test
// void getAllComponentsOfCar() {
// }
}
\ No newline at end of file
package cz.muni.pa165.car.rest.unit;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.pa165.car.rest.CarController;
import cz.muni.pa165.car.rest.CarDriverPairController;
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.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.when;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@WebMvcTest(CarController.class)
class CarControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CarService carServiceMock;
@Autowired
private ObjectMapper objectMapper;
private static List<Long> componentIds;
private static List<Long> driverIds;
private static CarRequestDto carRequestDto;
private static CarResponseDto carResponseDto;
@BeforeEach
void initializeTestObjects() {
componentIds = List.of(1L, 2L, 3L, 4L);
driverIds = List.of(11L, 22L, 33L, 44L);
carRequestDto = CarRequestDto.builder()
.componentIds(componentIds)
.driverIds(driverIds)
.mainDriverId(2L)
.build();
carResponseDto = CarResponseDto.builder()
.id(anyLong())
.componentIdsNames(componentIds)
.driverIdsNames(driverIds)
.mainDriverId(2L)
.build();
}
@Test
void notExistingPath() throws Exception {
mockMvc.perform(put("/invalidPath"))
.andExpect(status().isNotFound());
}
@Test
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());
//.andExpect(jsonPath("$.componentIdsNames").value(carResponseDto.getComponentIdsNames()))
//.andExpect(jsonPath("$.driverIdsNames").value(carResponseDto.getDriverIdsNames()))
//.andExpect(jsonPath("$.mainDriverId").value(carResponseDto.getMainDriverId()))
//.andReturn();
}
@Test
void deleteExistingCarTest() throws Exception {
given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto);
given(carServiceMock.deleteById(anyLong())).willReturn("");
mockMvc.perform(post("/car/")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(carRequestDto)))
.andExpect(status().isOk());
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(anyLong())).willReturn("");
mockMvc.perform(delete("/car/")
.param("carId", String.valueOf(-1L))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
@Test
void getCarTest() throws Exception {
given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto);
given(carServiceMock.getCarById(anyLong())).willReturn(carResponseDto);
mockMvc.perform(post("/car/")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(carRequestDto)))
.andExpect(status().isOk());
mockMvc.perform(get("/car/")
.param("carId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
@Test
void getAllCarsTest() throws Exception {
given(carServiceMock.getAllCars()).willReturn(List.of(carResponseDto));
mockMvc.perform(get("/car/all")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
\ No newline at end of file
package cz.muni.pa165.car.rest.unit;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.pa165.car.rest.CarController;
import cz.muni.pa165.car.rest.CarDriverPairController;
import org.junit.jupiter.api.AfterEach;
import static 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 org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(controllers = CarDriverPairController.class)
@AutoConfigureMockMvc(addFilters = false)
@ExtendWith(MockitoExtension.class)
class CarDriverPairControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CarDriverPairController controller;
@Autowired
private ObjectMapper objectMapper;
// @BeforeEach
// void setUp() {
// }
//
// @AfterEach
// void tearDown() {
// }
//
// @Test
// void assignDriverToCar() {
// }
//
// @Test
// void unassignDriverFromCar() {
// }
//
// @Test
// void getAllDriversOfCar() {
// }
//
// @Test
// void setMainDriver() {
// }
//
// @Test
// void removeMainDriver() {
// }
}
\ No newline at end of file
package cz.muni.pa165.car.service;
import static 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 org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class CarComponentPairServiceImplTest {
@BeforeEach
void setUp() {
}
@Test
void addComponent() {
}
@Test
void removeComponent() {
}
@Test
void getAllComponentsOfCar() {
}
@Test
void getComponentById() {
}
}
\ No newline at end of file
package cz.muni.pa165.car.service;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CarComponentPairServiceTest {
@BeforeEach
void setUp() {
}
@Test
void addComponent() {
}
@Test
void removeComponent() {
}
@Test
void getAllComponentsOfCar() {
}
}
\ No newline at end of file
package cz.muni.pa165.car.service;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CarDriverPairServiceImplTest {
@BeforeEach
void setUp() {
}
@Test
void assignDriverToCar() {
}
@Test
void unassignDriverFromCar() {
}
@Test
void getAllDriversOfCar() {
}
@Test
void setMainDriver() {
}
@Test
void removeMainDriver() {
}
@Test
void getDriverById() {
}
}
\ No newline at end of file
package cz.muni.pa165.car.service;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CarDriverPairServiceTest {
@BeforeEach
void setUp() {
}
@Test
void assignDriverToCar() {
}
@Test
void unassignDriverFromCar() {
}
@Test
void getAllDriversOfCar() {
}
@Test
void setMainDriver() {
}
@Test
void removeMainDriver() {
}
}
\ No newline at end of file
package cz.muni.pa165.car.service;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CarServiceImplTest {
@BeforeEach
void setUp() {
}
@Test
void postCar() {
}
@Test
void getCarById() {
}
@Test
void getAllCars() {
}
@Test
void deleteById() {
}
@Test
void carDtoConverter() {
}
@Test
void carConverterToDto() {
}
@Test
void getDriverById() {
}
@Test
void getComponentById() {
}
}
\ No newline at end of file
package cz.muni.pa165.car.service;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CarServiceTest {
@BeforeEach
void setUp() {
}
@Test
void postCar() {
}
@Test
void getCarById() {
}
@Test
void getAllCars() {
}
@Test
void deleteById() {
}
}
\ 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