diff --git a/car/pom.xml b/car/pom.xml index 8472300c295cb65a215a847dd2aa75eb8297d815..5e0c3a091c665b99ea17a4b1668d5cbea9cc7050 100644 --- a/car/pom.xml +++ b/car/pom.xml @@ -55,8 +55,10 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> + <version>4.12</version> <scope>test</scope> </dependency> + </dependencies> <properties> <maven.compiler.source>17</maven.compiler.source> diff --git a/car/src/main/java/cz/muni/pa165/car/App.java b/car/src/main/java/cz/muni/pa165/car/App.java index af2d25f5e666eb2af9d28fea87c26bf030d755eb..e2e4b1a19994735e998bd5c89b5357c84d3ca9c6 100644 --- a/car/src/main/java/cz/muni/pa165/car/App.java +++ b/car/src/main/java/cz/muni/pa165/car/App.java @@ -1,24 +1,21 @@ package cz.muni.pa165.car; -import cz.muni.pa165.car.data.model.Car; import cz.muni.pa165.common_library.exception.RestExceptionHandler; +import jakarta.transaction.Transactional; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.client.RestTemplate; /** * Main app. */ @SpringBootApplication -//@EnableJpaRepositories(basePackages = {"cz.muni.pa165.car.data.repository"}) -@EnableTransactionManagement -@EntityScan(basePackageClasses = {Car.class}) -//@Import({RestExceptionHandler.class, ClientConfig.class}) +@Transactional +@EntityScan(basePackages = {"cz.muni.pa165.car.data.model"}) @Import(RestExceptionHandler.class) public class App { diff --git a/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairService.java b/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairService.java index 19e2cdddc5bc605e7d33486faa56ad3b83ac7a41..fe30d471d2b315044e7716718298aed3a230d51f 100644 --- a/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairService.java +++ b/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairService.java @@ -33,4 +33,5 @@ public interface CarComponentPairService { * @return Components of the cat. */ List<CarComponentDto> getAllComponentsOfCar(Long carId); + } diff --git a/car/src/test/java/cz/muni/pa165/car/data/repository/CarRepositoryTest.java b/car/src/test/java/cz/muni/pa165/car/data/repository/CarRepositoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..30da90767a559287bf6fb429cda3a4d586ff3012 --- /dev/null +++ b/car/src/test/java/cz/muni/pa165/car/data/repository/CarRepositoryTest.java @@ -0,0 +1,147 @@ +package cz.muni.pa165.car.data.repository; + +import cz.muni.pa165.car.data.model.Car; +import org.junit.jupiter.api.Assertions; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +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.Set; + +@RunWith(SpringRunner.class) +@DataJpaTest +class CarRepositoryTest { + + @Autowired + private TestEntityManager entityManager; + + @Autowired + private CarRepository carRepository; + + @Test + public void saveTest() { + Car car = Car.builder() + .id(1L) + .components(Set.of(2L, 3L, 4L)) + .drivers(Set.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); + } + + @Test + public void addComponentAndSaveTest() { + Car car = Car.builder() + .id(1L) + .components(Set.of(2L, 3L, 4L)) + .drivers(Set.of(6L)) + .mainDriverId(6L) + .build(); + + Car savedCar = carRepository.save(car); + savedCar.getComponents().add(111L); + Car carWithAddedComponent = carRepository.save(savedCar); + assertTrue(carWithAddedComponent.getComponents().contains(111L)); + } + + @Test + public void removeComponentAndSaveTest() { + Car car = Car.builder() + .id(1L) + .components(Set.of(2L, 3L, 4L)) + .drivers(Set.of(6L)) + .mainDriverId(6L) + .build(); + + Car savedCar = carRepository.save(car); + savedCar.getComponents().remove(111L); + Car carWithAddedComponent = carRepository.save(savedCar); + assertFalse(carWithAddedComponent.getComponents().contains(111L)); + } + + @Test + public void getAllComponentsTest() { + Car car = Car.builder() + .id(1L) + .components(Set.of(2L, 3L, 4L)) + .drivers(Set.of(6L)) + .mainDriverId(6L) + .build(); + + Car savedCar = carRepository.save(car); + assertEquals(savedCar.getComponents(), Set.of(2L, 3L, 4L)); + } + + @Test + public void addDriverAndSaveTest() { + Car car = Car.builder() + .id(1L) + .components(Set.of()) + .drivers(Set.of(5L, 6L, 7L)) + .mainDriverId(6L) + .build(); + + Car savedCar = carRepository.save(car); + savedCar.getDrivers().add(111L); + Car carWithAddedDriver = carRepository.save(savedCar); + assertTrue(carWithAddedDriver.getDrivers().contains(111L)); + } + + @Test + public void removeDriverAndSaveTest() { + Car car = Car.builder() + .id(1L) + .components(Set.of()) + .drivers(Set.of(5L, 6L, 7L)) + .mainDriverId(6L) + .build(); + + Car savedCar = carRepository.save(car); + savedCar.getDrivers().remove(6L); + Car carWithRemovedDriver = carRepository.save(savedCar); + assertFalse(carWithRemovedDriver.getDrivers().contains(6L)); + } + + @Test + public void setDriverAsMainAndSaveTest() { + Car car = Car.builder() + .id(1L) + .components(Set.of()) + .drivers(Set.of(5L, 6L, 7L)) + .mainDriverId(6L) + .build(); + + Car savedCar = carRepository.save(car); + assertEquals(6L, (long) savedCar.getMainDriverId()); + savedCar.setMainDriverId(7L); + Car carWithReplacedMainDriver = carRepository.save(savedCar); + assertEquals(7L, (long) savedCar.getMainDriverId()); + } + + @Test + public void removeDriverAsMainAndSaveTest() { + Car car = Car.builder() + .id(1L) + .components(Set.of()) + .drivers(Set.of(5L, 6L, 7L)) + .mainDriverId(6L) + .build(); + + Car savedCar = carRepository.save(car); + assertEquals(6L, (long) savedCar.getMainDriverId()); + savedCar.setMainDriverId(null); + Car carWithRemovedMainDriver = carRepository.save(savedCar); + assertNull(carWithRemovedMainDriver.getMainDriverId()); + } +} diff --git a/car/src/test/java/cz/muni/pa165/car/rest/DriverManagerControllerItTest.java b/car/src/test/java/cz/muni/pa165/car/rest/DriverManagerControllerItTest.java deleted file mode 100644 index 2189c623ba3debacb01935f833b86c46fd80c2cc..0000000000000000000000000000000000000000 --- a/car/src/test/java/cz/muni/pa165/car/rest/DriverManagerControllerItTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package cz.muni.pa165.car.rest; - -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -@AutoConfigureMockMvc -class DriverManagerControllerItTest { - -// @Autowired -// private MockMvc mockMvc; -// -// @Autowired -// private ObjectMapper objectMapper; -// -// @Autowired -// DriverManagerService driverManagerService; -// -// @Test -// void test() throws Exception { -// long id = 1L; -// -// Car car = driverManagerService.assignDriverToCar(id, id); -// -// String response = mockMvc.perform(put("/driver/assign").queryParam("driverId", -// String.valueOf(id)).queryParam("carId", String.valueOf(id))) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// Car carResponse = objectMapper.readValue(response, Car.class); -// Assertions.assertAll( -// () -> Assertions.assertEquals(car.getId(), carResponse.getId()), -// () -> Assertions.assertEquals(car.getDrivers(), carResponse.getDrivers()) -// ); -// } -} diff --git a/car/src/test/java/cz/muni/pa165/car/rest/DriverManagerControllerUnitTest.java b/car/src/test/java/cz/muni/pa165/car/rest/DriverManagerControllerUnitTest.java deleted file mode 100644 index c41ee39994cad075f06e2259345775a984d44cc1..0000000000000000000000000000000000000000 --- a/car/src/test/java/cz/muni/pa165/car/rest/DriverManagerControllerUnitTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package cz.muni.pa165.car.rest; - -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; - -@WebMvcTest() -class DriverManagerControllerUnitTest { - -// @Autowired -// private MockMvc mockMvc; -// -// @MockBean -// private DriverManagerFacade mockDriverManagerFacade; -// -// @Autowired -// private ObjectMapper objectMapper; -// -// @Test -// void assignDriverValid() throws Exception { -// long id = 2L; -// var drivers = new HashSet<Driver>(); -// Car car = new Car(); -// car.setId(id); -// car.setDrivers(drivers); -// -// given(mockDriverManagerFacade.assignDriverToCar(anyLong(), anyLong())).willReturn( -// car); -// mockMvc.perform(put("/driver/assign").queryParam("driverId", -// String.valueOf(id)).queryParam("carId", String.valueOf(id))) -// .andExpect(status().isOk()) -// .andExpect(jsonPath("$.id").value(id)); -// } -// -// @Test -// void notExistingPath() throws Exception { -// mockMvc.perform(put("/invalidPath")) -// .andExpect(status().isNotFound()); -// } -// -// @Test -// void assignInvalid() throws Exception { -// long id = -1L; -// var drivers = new HashSet<Driver>(); -// Car car = new Car(); -// car.setId(id); -// car.setDrivers(drivers); -// -// given(mockDriverManagerFacade.assignDriverToCar(anyLong(), anyLong())).willReturn( -// car); -// mockMvc.perform(put("/driver/assign").queryParam("driverId", -// "invalidNumber").queryParam("carId", "invalidNumber")) -// .andExpect(status().isBadRequest()); -// } -} diff --git a/car/src/test/java/cz/muni/pa165/car/rest/integration/CarComponentPairControllerIntegrationTest.java b/car/src/test/java/cz/muni/pa165/car/rest/integration/CarComponentPairControllerIntegrationTest.java index 87ddb902b2b7c0e3638fd78ba9e7458ed0ccda0f..d96f132e8dd7b2fbcf7e69d9cd43c7719dc92296 100644 --- a/car/src/test/java/cz/muni/pa165/car/rest/integration/CarComponentPairControllerIntegrationTest.java +++ b/car/src/test/java/cz/muni/pa165/car/rest/integration/CarComponentPairControllerIntegrationTest.java @@ -1,24 +1,28 @@ 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; +import org.junit.runner.RunWith; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit4.SpringRunner; +@RunWith(SpringRunner.class) +@DataJpaTest class CarComponentPairControllerIntegrationTest { -// @BeforeEach -// void setUp() { -// } -// -// @Test -// void addComponent() { -// } -// -// @Test -// void removeComponent() { -// } -// -// @Test -// void getAllComponentsOfCar() { -// } + @BeforeEach + void setUp() { + } + + @Test + void addComponent() { + } + + @Test + void removeComponent() { + } + + @Test + void getAllComponentsOfCar() { + } } \ No newline at end of file diff --git a/car/src/test/java/cz/muni/pa165/car/rest/integration/CarControllerIntegrationTest.java b/car/src/test/java/cz/muni/pa165/car/rest/integration/CarControllerIntegrationTest.java deleted file mode 100644 index babbe28743b608f12ec09f1d9489772da6e9c882..0000000000000000000000000000000000000000 --- a/car/src/test/java/cz/muni/pa165/car/rest/integration/CarControllerIntegrationTest.java +++ /dev/null @@ -1,40 +0,0 @@ -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); - } - -} diff --git a/car/src/test/java/cz/muni/pa165/car/rest/integration/CarDriverPairControllerIntegrationTest.java b/car/src/test/java/cz/muni/pa165/car/rest/integration/CarDriverPairControllerIntegrationTest.java index 4c510a3c275af7261da767c166bc4546da7cee41..e69b0e82b333d3ccd144463391df8def8260dcfb 100644 --- a/car/src/test/java/cz/muni/pa165/car/rest/integration/CarDriverPairControllerIntegrationTest.java +++ b/car/src/test/java/cz/muni/pa165/car/rest/integration/CarDriverPairControllerIntegrationTest.java @@ -1,32 +1,36 @@ 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; +import org.junit.runner.RunWith; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit4.SpringRunner; +@RunWith(SpringRunner.class) +@DataJpaTest class CarDriverPairControllerIntegrationTest { -// @BeforeEach -// void setUp() { -// } -// -// @Test -// void assignDriverToCar() { -// } -// -// @Test -// void unassignDriverFromCar() { -// } -// -// @Test -// void getAllDriversOfCar() { -// } -// -// @Test -// void setMainDriver() { -// } -// -// @Test -// void removeMainDriver() { -// } + @BeforeEach + void setUp() { + } + + @Test + void assignDriverToCar() { + } + + @Test + void unassignDriverFromCar() { + } + + @Test + void getAllDriversOfCar() { + } + + @Test + void setMainDriver() { + } + + @Test + void removeMainDriver() { + } } \ No newline at end of file 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 017d1c883423cc8b88be467190af0c611202c13e..09c0e78cfb6f2274258b378e0a8d614f7e446ce4 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,49 +1,92 @@ 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 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.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import static org.mockito.BDDMockito.given; 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.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; -@WebMvcTest(controllers = CarComponentPairController.class) -@AutoConfigureMockMvc(addFilters = false) +import java.math.BigDecimal; +import java.util.List; + +@AutoConfigureMockMvc +@WebMvcTest(CarComponentPairController.class) @ExtendWith(MockitoExtension.class) -class CarComponentPairControllerUnitTest { +public class CarComponentPairControllerUnitTest { @Autowired private MockMvc mockMvc; @MockBean - private CarComponentPairController controller; + private CarComponentPairService carComponentPairServiceMock; - @Autowired - private ObjectMapper objectMapper; - -// @BeforeEach -// void setUp() { -// } -// -// @AfterEach -// void tearDown() { -// } -// -// @Test -// void addComponent() { -// } -// -// @Test -// void removeComponent() { -// } -// -// @Test -// void getAllComponentsOfCar() { -// } + private static CarResponseDto carResponseDto; + private static CarComponentDto carComponentDto; + + @BeforeEach + void initializeTestObjects() { + + carResponseDto = CarResponseDto.builder() + .id(1L) + .componentIdsNames(List.of()) + .driverIdsNames(List.of()) + .mainDriverId(1L) + .build(); + + carComponentDto = CarComponentDto.builder() + .id(1L) + .name("") + .manufacturer("") + .price(BigDecimal.TEN) + .weight(BigDecimal.TEN) + .build(); + } + + + @Test + void addComponent() throws Exception { + given(carComponentPairServiceMock.addComponent(1L, 1L)).willReturn(carResponseDto); + + mockMvc.perform(put("/carcomponent/addcomponent") + .param("componentId", String.valueOf(2L)) + .param("carId", String.valueOf(1L)) + .contentType(MediaType.APPLICATION_JSON) + ) + .andExpect(status().isOk()); + } + + @Test + void removeComponent() throws Exception { + given(carComponentPairServiceMock.removeComponent(1L, 1L)).willReturn(carResponseDto); + + mockMvc.perform(put("/carcomponent/removecomponent") + .param("componentId", String.valueOf(2L)) + .param("carId", String.valueOf(1L)) + .contentType(MediaType.APPLICATION_JSON) + ) + .andExpect(status().isOk()); + } + + @Test + void getAllComponentsOfCar() throws Exception { + given(carComponentPairServiceMock.getAllComponentsOfCar(1L)).willReturn(List.of(carComponentDto)); + + mockMvc.perform(put("/carcomponent/getcomponents") + .param("carId", String.valueOf(1L)) + .contentType(MediaType.APPLICATION_JSON) + ) + .andExpect(status().isOk()); + } } \ 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 7e409a3e02484a35e61343714d8146bdc03a399f..026d5d16a6789ce0b438085fed07a33feaa1f3d9 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 @@ -1,45 +1,33 @@ 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.ArgumentMatchers.anyString; 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) +@AutoConfigureMockMvc @WebMvcTest(CarController.class) -class CarControllerUnitTest { +@ExtendWith(MockitoExtension.class) +public class CarControllerUnitTest { @Autowired private MockMvc mockMvc; @@ -50,36 +38,35 @@ class CarControllerUnitTest { @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) + .componentIds(List.of()) + .driverIds(List.of()) + .mainDriverId(1L) .build(); carResponseDto = CarResponseDto.builder() - .id(anyLong()) - .componentIdsNames(componentIds) - .driverIdsNames(driverIds) - .mainDriverId(2L) + .id(1L) + .componentIdsNames(List.of()) + .driverIdsNames(List.of()) + .mainDriverId(1L) .build(); } @Test - void notExistingPath() throws Exception { - mockMvc.perform(put("/invalidPath")) + void nonExistingPathPostTest() throws Exception { + given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto); + + mockMvc.perform(post("/car/invalidPath/")) .andExpect(status().isNotFound()); } + @Test void createCarTest() throws Exception { given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto); @@ -88,21 +75,12 @@ class CarControllerUnitTest { .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()); + given(carServiceMock.deleteById(1L)).willReturn(""); mockMvc.perform(delete("/car/") .param("carId", String.valueOf(1L)) @@ -113,7 +91,7 @@ class CarControllerUnitTest { @Test void deleteNonExistingCarTest() throws Exception { given(carServiceMock.postCar(carRequestDto)).willReturn(carResponseDto); - given(carServiceMock.deleteById(anyLong())).willReturn(""); + given(carServiceMock.deleteById(1L)).willReturn(anyString()); mockMvc.perform(delete("/car/") .param("carId", String.valueOf(-1L)) @@ -124,12 +102,6 @@ class CarControllerUnitTest { @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)) @@ -139,10 +111,9 @@ class CarControllerUnitTest { @Test void getAllCarsTest() throws Exception { - given(carServiceMock.getAllCars()).willReturn(List.of(carResponseDto)); + given(carServiceMock.getAllCars()).willReturn(List.of()); - mockMvc.perform(get("/car/all") - .contentType(MediaType.APPLICATION_JSON)) + mockMvc.perform(get("/car/all")) .andExpect(status().isOk()); } } \ 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 efb97a2ed8b158c1ec61f469fe2298a8ac65fd79..6de1df51e2b4cb40f4eef567776cce58d1a1a7d2 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,59 +1,110 @@ 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 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.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; +import static org.mockito.BDDMockito.given; 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.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; -@WebMvcTest(controllers = CarDriverPairController.class) -@AutoConfigureMockMvc(addFilters = false) -@ExtendWith(MockitoExtension.class) -class CarDriverPairControllerUnitTest { +import java.util.List; + +@WebMvcTest(CarDriverPairController.class) +public class CarDriverPairControllerUnitTest { @Autowired private MockMvc mockMvc; @MockBean - private CarDriverPairController controller; + private CarDriverPairService carDriverPairServiceMock; - @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() { -// } + private static DriverDto driverDto; + private static CarResponseDto carResponseDto; + + @BeforeEach + void beforeEach() { + + driverDto = DriverDto.builder() + .id(1L) + .name("") + .surname("") + .build(); + + carResponseDto = CarResponseDto.builder() + .id(1L) + .componentIdsNames(List.of()) + .driverIdsNames(List.of()) + .mainDriverId(1L) + .build(); + } + + @Test + public void nonExistingPathPostTest() throws Exception { + given(carDriverPairServiceMock.assignDriverToCar(1L, 1L)).willReturn(carResponseDto); + + mockMvc.perform(put("/cardriver/invalidpath")) + .andExpect(status().isNotFound()); + } + + @Test + public void assignDriverToCar() throws Exception { + given(carDriverPairServiceMock.assignDriverToCar(1L, 1L)).willReturn(carResponseDto); + + mockMvc.perform(put("/cardriver/assign") + .param("driverId", String.valueOf(1L)) + .param("carId", String.valueOf(1L)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + public void unassignDriverFromCar() throws Exception { + given(carDriverPairServiceMock.unassignDriverFromCar(1L, 1L)).willReturn(carResponseDto); + + mockMvc.perform(put("/cardriver/unassign") + .param("driverId", String.valueOf(1L)) + .param("carId", String.valueOf(1L)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + public void getAllDriversOfCar() throws Exception { + given(carDriverPairServiceMock.getAllDriversOfCar(1L)).willReturn(List.of(driverDto)); + + mockMvc.perform(put("/cardriver/alldrivers") + .param("carId", String.valueOf(1L)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + public void setMainDriver() throws Exception { + given(carDriverPairServiceMock.setMainDriver(1L, 1L)).willReturn(carResponseDto); + + mockMvc.perform(put("/cardriver/setmain") + .param("driverId", String.valueOf(1L)) + .param("carId", String.valueOf(1L)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + public void removeMainDriver() throws Exception { + given(carDriverPairServiceMock.removeMainDriver(1L)).willReturn(carResponseDto); + + mockMvc.perform(put("/cardriver/removemain") + .param("driverId", String.valueOf(1L)) + .param("carId", String.valueOf(1L)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } } \ No newline at end of file diff --git a/car/src/test/java/cz/muni/pa165/car/service/CarComponentPairServiceImplTest.java b/car/src/test/java/cz/muni/pa165/car/service/CarComponentPairServiceImplTest.java deleted file mode 100644 index 639d83795c8b410ac23ef20be2078dc103c34d3c..0000000000000000000000000000000000000000 --- a/car/src/test/java/cz/muni/pa165/car/service/CarComponentPairServiceImplTest.java +++ /dev/null @@ -1,31 +0,0 @@ -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 diff --git a/car/src/test/java/cz/muni/pa165/car/service/CarDriverPairServiceImplTest.java b/car/src/test/java/cz/muni/pa165/car/service/CarDriverPairServiceImplTest.java deleted file mode 100644 index 0d36f0436a2ae9f17f8bde721d4f027579d3145a..0000000000000000000000000000000000000000 --- a/car/src/test/java/cz/muni/pa165/car/service/CarDriverPairServiceImplTest.java +++ /dev/null @@ -1,36 +0,0 @@ -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 diff --git a/car/src/test/java/cz/muni/pa165/car/service/CarServiceImplTest.java b/car/src/test/java/cz/muni/pa165/car/service/CarServiceImplTest.java deleted file mode 100644 index 8b1b91c0864758733e9427d1f21843ed85f4f97a..0000000000000000000000000000000000000000 --- a/car/src/test/java/cz/muni/pa165/car/service/CarServiceImplTest.java +++ /dev/null @@ -1,44 +0,0 @@ -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 diff --git a/car/src/test/java/cz/muni/pa165/car/service/CarComponentPairServiceTest.java b/car/src/test/java/cz/muni/pa165/car/service/unit/CarComponentPairServiceTest.java similarity index 76% rename from car/src/test/java/cz/muni/pa165/car/service/CarComponentPairServiceTest.java rename to car/src/test/java/cz/muni/pa165/car/service/unit/CarComponentPairServiceTest.java index dcf039e309315a8487aadb4ba8e7e695847657b9..f1b6b2ebdd5054c8c670f9cbda931c150ef063b3 100644 --- a/car/src/test/java/cz/muni/pa165/car/service/CarComponentPairServiceTest.java +++ b/car/src/test/java/cz/muni/pa165/car/service/unit/CarComponentPairServiceTest.java @@ -1,6 +1,5 @@ -package cz.muni.pa165.car.service; +package cz.muni.pa165.car.service.unit; -import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/car/src/test/java/cz/muni/pa165/car/service/CarDriverPairServiceTest.java b/car/src/test/java/cz/muni/pa165/car/service/unit/CarDriverPairServiceTest.java similarity index 80% rename from car/src/test/java/cz/muni/pa165/car/service/CarDriverPairServiceTest.java rename to car/src/test/java/cz/muni/pa165/car/service/unit/CarDriverPairServiceTest.java index 691b7bc75feeb75fa96046557e7d876c5d603786..428600546689d0d4e2ea7a76192e0d82617ad62f 100644 --- a/car/src/test/java/cz/muni/pa165/car/service/CarDriverPairServiceTest.java +++ b/car/src/test/java/cz/muni/pa165/car/service/unit/CarDriverPairServiceTest.java @@ -1,6 +1,5 @@ -package cz.muni.pa165.car.service; +package cz.muni.pa165.car.service.unit; -import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/car/src/test/java/cz/muni/pa165/car/service/CarServiceTest.java b/car/src/test/java/cz/muni/pa165/car/service/unit/CarServiceTest.java similarity index 76% rename from car/src/test/java/cz/muni/pa165/car/service/CarServiceTest.java rename to car/src/test/java/cz/muni/pa165/car/service/unit/CarServiceTest.java index 309b6388aa2962b9d4efd13438eb804d9acf03d0..1f724e1ceb367bf984b2bfae27fb02d6a929ea1c 100644 --- a/car/src/test/java/cz/muni/pa165/car/service/CarServiceTest.java +++ b/car/src/test/java/cz/muni/pa165/car/service/unit/CarServiceTest.java @@ -1,6 +1,5 @@ -package cz.muni.pa165.car.service; +package cz.muni.pa165.car.service.unit; -import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test;