Skip to content
Snippets Groups Projects

21 race jpa tests

Merged Andrej Šimurka requested to merge 21_race_jpa_tests into milestone_2
2 files
+ 58
27
Compare changes
  • Side-by-side
  • Inline
Files
2
package cz.muni.pa165.race.rest;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.pa165.common_library.dtos.RaceDto;
import cz.muni.pa165.race.data.model.Season;
import cz.muni.pa165.race.data.repository.SeasonRepository;
import cz.muni.pa165.race.service.RaceServiceI;
import cz.muni.pa165.common_library.dtos.SeasonDto;
import cz.muni.pa165.race.service.SeasonServiceI;
import 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 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.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.any;
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.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.List;
@AutoConfigureMockMvc
@WebMvcTest(SeasonController.class)
@ExtendWith(MockitoExtension.class)
@@ -48,32 +37,70 @@ class SeasonControllerTest {
private ObjectMapper objectMapper;
@Test
void createSeason() {
var seasonDto = SeasonTestUtil.getDaoSeason()
given(raceServiceMock.postRace(raceDto)).willReturn(raceDto);
void createSeason() throws Exception {
var seasonDto = SeasonTestUtil.getDaoSeasonDto();
given(seasonServiceMock.postSeason(seasonDto)).willReturn(seasonDto);
String response = mockMvc.perform(post("/race/")
String response = mockMvc.perform(post("/season/")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(raceDto)))
.content(objectMapper.writeValueAsString(seasonDto)))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
RaceDto raceDtoResponse = objectMapper.readValue(response, RaceDto.class);
Assertions.assertEquals(raceDtoResponse, raceDto);
SeasonDto seasonDtoResponse = objectMapper.readValue(response, SeasonDto.class);
Assertions.assertEquals(seasonDtoResponse, seasonDto);
}
@Test
void getSeason() {
void getSeason() throws Exception {
var seasonDto = SeasonTestUtil.getDaoSeasonDto();
given(seasonServiceMock.getSeasonById(1L)).willReturn(seasonDto);
String response = mockMvc.perform(get("/season/id")
.param("seasonId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
SeasonDto seasonDtoResponse = objectMapper.readValue(response, SeasonDto.class);
Assertions.assertEquals(seasonDtoResponse, seasonDto);
}
@Test
void getAllSeasons() {
void getAllSeasons() throws Exception {
var seasonDto = SeasonTestUtil.getDaoSeasonDto();
given(seasonServiceMock.getAllSeasons()).willReturn(List.of(seasonDto));
String response = mockMvc.perform(get("/season/")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
List<SeasonDto> seasonDtoResponse = List.of(objectMapper.readValue(response, SeasonDto[].class));
Assertions.assertEquals(seasonDtoResponse, List.of(seasonDto));
}
@Test
void deleteSeason() {
void deleteSeason() throws Exception {
var seasonDto = SeasonTestUtil.getDaoSeasonDto();
given(seasonServiceMock.deleteById(1L)).willReturn("Season with id: " + 1L + "was succesfully deleted");
String response = mockMvc.perform(delete("/season/")
.param("seasonId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
Assertions.assertEquals(response, "Season with id: " + 1L + "was succesfully deleted");
}
@Test
void addRace() {
void addRace() throws Exception {
var seasonDto = SeasonTestUtil.getDaoSeasonDto();
given(seasonServiceMock.addRace(1L, 1L)).willReturn(seasonDto);
String response = mockMvc.perform(patch("/season/addRace")
.param("seasonId", String.valueOf(1L))
.param("raceId", String.valueOf(1L))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
SeasonDto seasonDtoResponse = objectMapper.readValue(response, SeasonDto.class);
Assertions.assertEquals(seasonDtoResponse, seasonDto);
}
}
\ No newline at end of file
Loading