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

Added season controller tests

parent cf612a93
No related branches found
No related tags found
3 merge requests!60Docker,!5321 season component tests,!5221 race jpa tests
Pipeline #
package cz.muni.pa165.race.rest; package cz.muni.pa165.race.rest;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.pa165.common_library.dtos.RaceDto; import cz.muni.pa165.common_library.dtos.SeasonDto;
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.race.service.SeasonServiceI; import cz.muni.pa165.race.service.SeasonServiceI;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import static org.mockito.BDDMockito.given;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; 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.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; 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.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 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.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 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 static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.List;
@AutoConfigureMockMvc @AutoConfigureMockMvc
@WebMvcTest(SeasonController.class) @WebMvcTest(SeasonController.class)
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
...@@ -48,32 +37,70 @@ class SeasonControllerTest { ...@@ -48,32 +37,70 @@ class SeasonControllerTest {
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@Test @Test
void createSeason() { void createSeason() throws Exception {
var seasonDto = SeasonTestUtil.getDaoSeason() var seasonDto = SeasonTestUtil.getDaoSeasonDto();
given(raceServiceMock.postRace(raceDto)).willReturn(raceDto); given(seasonServiceMock.postSeason(seasonDto)).willReturn(seasonDto);
String response = mockMvc.perform(post("/race/") String response = mockMvc.perform(post("/season/")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(raceDto))) .content(objectMapper.writeValueAsString(seasonDto)))
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
RaceDto raceDtoResponse = objectMapper.readValue(response, RaceDto.class); SeasonDto seasonDtoResponse = objectMapper.readValue(response, SeasonDto.class);
Assertions.assertEquals(raceDtoResponse, raceDto); Assertions.assertEquals(seasonDtoResponse, seasonDto);
} }
@Test @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 @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 @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 @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
...@@ -17,6 +17,10 @@ public class SeasonTestUtil { ...@@ -17,6 +17,10 @@ public class SeasonTestUtil {
} }
public static SeasonDto getDaoSeasonDto() { public static SeasonDto getDaoSeasonDto() {
return SeasonDto.builder()
.id(1L)
.races(new ArrayList<>())
.year(2023)
.build();
} }
} }
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