Skip to content
Snippets Groups Projects

21 race jpa tests

Merged Andrej Šimurka requested to merge 21_race_jpa_tests into milestone_2
Files
3
package cz.muni.pa165.race.rest;
import cz.muni.pa165.race.data.model.Race;
import cz.muni.pa165.race.data.repository.RaceRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
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.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@AutoConfigureMockMvc
class RaceServiceTest {
private String bodyContent = """
{
"id": 1,
"raceInfo": {
"location": "MONACO",
"name": "Monaco Grand Prix 2023",
"prizePool": 30000000
}
}
""";
private String expectedMessagePost = "{\"id\":1,\"raceInfo\":{\"location\":\"MONACO\",\"name\":\"Monaco Grand Prix 2023\",\"prizePool\":30000000},\"driverOne\":{\"driverId\":1,\"carId\":1,\"position\":null},\"driverTwo\":{\"driverId\":2,\"carId\":2,\"position\":null}}";
@Autowired
private MockMvc mockMvc;
@MockBean
private RaceRepository raceRepository;
@BeforeEach
void setup() {
Race raceDao = RaceTestUtil.getDaoRace();
given(raceRepository.save(any(Race.class))).willReturn(
raceDao);
given(raceRepository.findById(anyLong())).willReturn(
Optional.of(raceDao));
given(raceRepository.findAll()).willReturn(List.of(raceDao));
}
@Test
void createRace() throws Exception {
var request = post("/race/")
.content(bodyContent)
.contentType(MediaType.APPLICATION_JSON_VALUE);
this.mockMvc.perform(request)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(expectedMessagePost));
}
@Test
void deleteRace() throws Exception {
String expectedMessage = "Race with id: 1was succesfully deleted";
var requestDelete = delete("/race/")
.param("raceId", "1")
.contentType(MediaType.APPLICATION_JSON_VALUE);
this.mockMvc.perform(requestDelete)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(expectedMessage));
}
@Test
void getExistingRace() throws Exception {
String expectedMessage = "{\"id\":1,\"raceInfo\":{\"location\":\"MONACO\",\"name\":\"Monaco Grand Prix 2023\",\"prizePool\":30000000},\"driverOne\":{\"driverId\":1,\"carId\":1,\"position\":null},\"driverTwo\":{\"driverId\":2,\"carId\":2,\"position\":null}}";
var requestGet = get("/race/id")
.param("raceId", "1")
.contentType(MediaType.APPLICATION_JSON_VALUE);
this.mockMvc.perform(requestGet)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(expectedMessage));
}
@Test
void getAllRaces() throws Exception {
var requestGet = get("/race/");
String expectedMessage = "[{\"id\":1,\"raceInfo\":{\"location\":\"MONACO\",\"name\":\"Monaco Grand Prix 2023\",\"prizePool\":30000000},\"driverOne\":{\"driverId\":1,\"carId\":1,\"position\":null},\"driverTwo\":{\"driverId\":2,\"carId\":2,\"position\":null}}]";
this.mockMvc.perform(requestGet)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(expectedMessage));
}
@Test
void assignDriverOne() throws Exception {
var requestAssign = patch("/race/assignDriverOne")
.param("driverOneId", "1")
.param("raceId", "1")
.param("carId", "2")
.contentType(MediaType.APPLICATION_JSON_VALUE);
this.mockMvc.perform(requestAssign)
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(expectedMessagePost));
}
}
Loading