Skip to content
Snippets Groups Projects
Commit a49efaf1 authored by Diana Gulčíková's avatar Diana Gulčíková
Browse files

driver controller test

parent 4246aea4
No related branches found
No related tags found
2 merge requests!60Docker,!45Tests for driver
...@@ -11,7 +11,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; ...@@ -11,7 +11,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
* Main app. * Main app.
*/ */
@SpringBootApplication @SpringBootApplication
@EnableJpaRepositories(basePackages = {"cz.muni.pa165.driver.data.repository"}) //@EnableJpaRepositories(basePackages = {"cz.muni.pa165.driver.data.repository"})
@EntityScan(basePackages = {"cz.muni.pa165.driver.data.model"}) @EntityScan(basePackages = {"cz.muni.pa165.driver.data.model"})
@Import(RestExceptionHandler.class) @Import(RestExceptionHandler.class)
public class App { public class App {
......
package cz.muni.pa165.driver.rest;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
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.status;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.pa165.common_library.dtos.DriverAddDto;
import cz.muni.pa165.common_library.dtos.DriverInsightDto;
import cz.muni.pa165.common_library.dtos.DriverResponseDto;
import cz.muni.pa165.common_library.dtos.DriverUpdateDto;
import cz.muni.pa165.driver.service.DriverService;
import java.util.HashMap;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(controllers = DriverController.class)
class DriverControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private DriverService driverService;
@Test
void driverAddTest() throws Exception {
var driver = getAddDriver();
given(driverService.addDriver(driver)).willReturn(new DriverResponseDto(1L));
String response = mockMvc.perform(post("/driver/add")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(driver)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
DriverResponseDto driverResponseDto = objectMapper.readValue(response, DriverResponseDto.class);
Assertions.assertEquals(1L, driverResponseDto.id());
}
@Test
void badRequestTest() throws Exception {
var driver = getAddDriver();
given(driverService.addDriver(driver)).willReturn(new DriverResponseDto(1L));
mockMvc.perform(post("/driver/add")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString("INVALID CONTENT")))
.andExpect(status().isBadRequest());
}
@Test
void driverGetAllTest() throws Exception {
var driver = getAddDriver();
given(driverService.getAllDrivers()).willReturn(List.of(getInsightDriver()));
String response = mockMvc.perform(get("/driver/get/all"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
var driverResponseDto = objectMapper.readValue(response, DriverInsightDto[].class);
Assertions.assertAll(
() -> Assertions.assertEquals(1L, driverResponseDto[0].id()),
() -> Assertions.assertEquals(driver.name(), driverResponseDto[0].name()),
() -> Assertions.assertEquals(driver.surname(), driverResponseDto[0].surname()),
() -> Assertions.assertEquals(driver.characteristics(),
driverResponseDto[0].characteristics())
);
}
@Test
void driverDeleteTest() throws Exception {
given(driverService.removeDriverById(anyLong())).willReturn(new DriverResponseDto(1L));
String response = mockMvc.perform(delete("/driver/remove/id=1"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
var driverResponseDto = objectMapper.readValue(response, DriverResponseDto.class);
Assertions.assertEquals(1L, driverResponseDto.id());
}
@Test
void invalidPathTest() throws Exception {
mockMvc.perform(get("/invalidPath"))
.andExpect(status().isNotFound());
}
@Test
void driverGetByIdTest() throws Exception {
var driver = getAddDriver();
given(driverService.getDriverById(anyLong())).willReturn(getInsightDriver());
String response = mockMvc.perform(get("/driver/get/id=1"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
var driverResponseDto = objectMapper.readValue(response, DriverInsightDto.class);
Assertions.assertAll(
() -> Assertions.assertEquals(1L, driverResponseDto.id()),
() -> Assertions.assertEquals(driver.name(), driverResponseDto.name()),
() -> Assertions.assertEquals(driver.surname(), driverResponseDto.surname()),
() -> Assertions.assertEquals(driver.characteristics(), driverResponseDto.characteristics())
);
}
@Test
void driverUpdateByIdTest() throws Exception {
DriverUpdateDto driverUpdateDto = getUpdateDriver();
given(driverService.updateDriverById(1L, driverUpdateDto)).willReturn(new DriverResponseDto(1L));
String response = mockMvc.perform(put("/driver/update/id=1")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(driverUpdateDto)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
var driverResponseDto = objectMapper.readValue(response, DriverResponseDto.class);
Assertions.assertEquals(1L, driverResponseDto.id());
}
private DriverAddDto getAddDriver() {
var driver = new DriverAddDto("Name",
"Surname", "Nationality", new HashMap<>());
return driver;
}
private DriverInsightDto getInsightDriver() {
return new DriverInsightDto(1L, "Name",
"Surname", "Nationality", new HashMap<>());
}
private DriverUpdateDto getUpdateDriver() {
return new DriverUpdateDto("Name",
"Surname", "Nationality", new HashMap<>());
}
}
package cz.muni.pa165.driver.rest;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
/**
* Tests for driver initialization controller.
*/
@WebMvcTest(controllers = DriverController.class)
class DriverInitControllerUnitTest {
//
// @Autowired
// private MockMvc mockMvc;
//
// @MockBean
// private DriverInitFacade driverInitFacade;
//
// @Autowired
// private ObjectMapper objectMapper;
//
// Driver driver;
// long id = 1L;
// String driverName = "Tom";
// String driverSurname = "Marek";
// String driverNationality = "Czech";
//
// @BeforeEach
// void settingUp() {
// driver = new Driver();
// driver.setId(id);
// driver.setMain(true);
// driver.setName(driverName);
// driver.setSurname(driverSurname);
// driver.setNationality(driverNationality);
// }
//
//
// @Test
// void createDriverTest() throws Exception {
// given(driverInitFacade.addDriver(driver)).willReturn(
// driver);
// mockMvc.perform(post("/driver/add").contentType(MediaType.APPLICATION_JSON)
// .content(objectMapper.writeValueAsString(driver)))
// .andExpect(status().isOk());
// }
//
// @Test
// void createDriverWithAttributesTest() throws Exception {
// given(driverInitFacade.addDriver(driver)).willReturn(
// driver);
// mockMvc.perform(post("/driver/add").contentType(MediaType.APPLICATION_JSON)
// .content(objectMapper.writeValueAsString(driver)))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$.id").value(id))
// .andExpect(jsonPath("$.name").value(driverName))
// .andExpect(jsonPath("$.surname").value(driverSurname))
// .andExpect(jsonPath("$.main").value(true))
// .andExpect(jsonPath("$.nationality").value(driverNationality));
// }
//
// @Test
// void removeDriver() throws Exception {
// given(driverInitFacade.removeDriver(driver.getId())).willReturn(
// driver);
// mockMvc.perform(delete("/driver/remove")
// .queryParam("driverId", String.valueOf(driver.getId())))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$.id").value(id))
// .andExpect(jsonPath("$.name").value(driverName))
// .andExpect(jsonPath("$.surname").value(driverSurname))
// .andExpect(jsonPath("$.main").value(true))
// .andExpect(jsonPath("$.nationality").value(driverNationality));
// }
}
...@@ -13,7 +13,7 @@ import org.springframework.test.context.junit4.SpringRunner; ...@@ -13,7 +13,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@DataJpaTest @DataJpaTest
class DriverControllerItTest { class DriverRepositoryTest {
@Autowired @Autowired
private TestEntityManager entityManager; private TestEntityManager entityManager;
...@@ -22,7 +22,7 @@ class DriverControllerItTest { ...@@ -22,7 +22,7 @@ class DriverControllerItTest {
private DriverRepository driverRepository; private DriverRepository driverRepository;
@Test @Test
public void testSave() { void testSave() {
Driver driver = Driver.builder() Driver driver = Driver.builder()
.name("name") .name("name")
.surname("surname") .surname("surname")
......
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