From f0fa2901b64ae75b1abab256f3e550e8e8e28adc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20=C5=A0imurka?= <492781@mail.muni.cz>
Date: Mon, 17 Apr 2023 02:33:13 +0200
Subject: [PATCH] lost tests

---
 .../rest/CarManagerControllerUnitTest.java    |  21 --
 .../pa165/race/rest/RaceControllerTest.java   | 243 ++++++++++--------
 .../muni/pa165/race/rest/RaceServiceTest.java | 154 +++++++++++
 .../pa165/race/rest/SeasonControllerTest.java | 158 +++++-------
 .../pa165/race/rest/SeasonServiceTest.java    | 124 +++++++++
 5 files changed, 483 insertions(+), 217 deletions(-)
 delete mode 100644 race/src/test/java/cz/muni/pa165/race/rest/CarManagerControllerUnitTest.java
 create mode 100644 race/src/test/java/cz/muni/pa165/race/rest/RaceServiceTest.java
 create mode 100644 race/src/test/java/cz/muni/pa165/race/rest/SeasonServiceTest.java

diff --git a/race/src/test/java/cz/muni/pa165/race/rest/CarManagerControllerUnitTest.java b/race/src/test/java/cz/muni/pa165/race/rest/CarManagerControllerUnitTest.java
deleted file mode 100644
index 05055698..00000000
--- a/race/src/test/java/cz/muni/pa165/race/rest/CarManagerControllerUnitTest.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package cz.muni.pa165.race.rest;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
-import org.springframework.test.web.servlet.MockMvc;
-
-/**
- * Car manager unit tester.
- */
-@WebMvcTest
-class CarManagerControllerUnitTest {
-
-  @Autowired
-  private MockMvc mockMvc;
-
-
-  @Autowired
-  private ObjectMapper objectMapper;
-
-}
diff --git a/race/src/test/java/cz/muni/pa165/race/rest/RaceControllerTest.java b/race/src/test/java/cz/muni/pa165/race/rest/RaceControllerTest.java
index 39665d91..933a5802 100644
--- a/race/src/test/java/cz/muni/pa165/race/rest/RaceControllerTest.java
+++ b/race/src/test/java/cz/muni/pa165/race/rest/RaceControllerTest.java
@@ -1,154 +1,181 @@
 package cz.muni.pa165.race.rest;
 
-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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import cz.muni.pa165.common_library.dtos.CarRequestDto;
 import cz.muni.pa165.common_library.dtos.CarResponseDto;
-import cz.muni.pa165.common_library.dtos.DriverDto;
-import cz.muni.pa165.race.data.model.Race;
-import cz.muni.pa165.race.data.repository.RaceRepository;
-import cz.muni.pa165.race.service.DbGetter;
-import java.util.List;
-import java.util.Optional;
+import cz.muni.pa165.common_library.dtos.Location;
+import cz.muni.pa165.common_library.dtos.RaceDto;
+import cz.muni.pa165.race.service.RaceServiceI;
+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.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+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.context.SpringBootTest;
+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.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.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
+import java.util.List;
+import java.util.Set;
 
-@SpringBootTest
 @AutoConfigureMockMvc
+@WebMvcTest(RaceController.class)
+@ExtendWith(MockitoExtension.class)
 class RaceControllerTest {
 
-  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}}";
-  private String expectedMessageAssignTwo = "{\"id\":1,\"raceInfo\":{\"location\":"
-      + "\"MONACO\",\"name\":\"Monaco Grand Prix 2023\",\"prizePool\":30000000},"
-      + "\"driverOne\":{\"driverId\":1,\"carId\":1,\"position\":null},\"driverTwo\""
-      + ":{\"driverId\":1,\"carId\":1,\"position\":null}}";
-  private String expectedMessageGet = "{\"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}}";
-  private String expectedMessageGetAll = "[{\"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;
+  private RaceServiceI raceServiceMock;
 
-  @MockBean
-  private DbGetter dbGetter;
+  @Autowired
+  private ObjectMapper objectMapper;
 
   @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));
-    CarResponseDto carDao = RaceTestUtil.getCarDao();
-    given(dbGetter.getCar(anyLong())).willReturn(carDao);
-    DriverDto driverDto = RaceTestUtil.getDriverDao();
-    given(dbGetter.getDriver(anyLong())).willReturn(driverDto);
+  void initialize() {
   }
 
-
   @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));
+    var raceDto = RaceTestUtil.getDaoRaceDto();
+    given(raceServiceMock.postRace(raceDto)).willReturn(raceDto);
+
+    String response = mockMvc.perform(post("/race/")
+            .contentType(MediaType.APPLICATION_JSON)
+            .content(objectMapper.writeValueAsString(raceDto)))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    RaceDto raceDtoResponse = objectMapper.readValue(response, RaceDto.class);
+    Assertions.assertEquals(raceDtoResponse, raceDto);
   }
 
   @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));
+    var raceDto = RaceTestUtil.getDaoRaceDto();
+    given(raceServiceMock.deleteRace(1L)).willReturn(
+        "Race with id: " + 1L + "was succesfully deleted");
+
+    String response = mockMvc.perform(delete("/race/")
+            .param("raceId", String.valueOf(1L))
+            .contentType(MediaType.APPLICATION_JSON))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    Assertions.assertEquals(response, "Race with id: " + 1L + "was succesfully deleted");
   }
 
   @Test
-  void getExistingRace() throws Exception {
-    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(expectedMessageGet));
+  void getRace() throws Exception {
+    var raceDto = RaceTestUtil.getDaoRaceDto();
+    given(raceServiceMock.findRaceById(1L)).willReturn(raceDto);
+
+    String response = mockMvc.perform(get("/race/id")
+            .param("raceId", String.valueOf(1L))
+            .contentType(MediaType.APPLICATION_JSON))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    RaceDto raceDtoResponse = objectMapper.readValue(response, RaceDto.class);
+    Assertions.assertEquals(raceDtoResponse, raceDto);
   }
 
+
   @Test
-  void getAllRaces() throws Exception {
-    var requestGet = get("/race/");
-    this.mockMvc.perform(requestGet)
-        .andDo(print())
-        .andExpect(status().isOk())
-        .andExpect(content().string(expectedMessageGetAll));
+  void getRaces() throws Exception {
+    var raceDto = RaceTestUtil.getDaoRaceDto();
+    given(raceServiceMock.findRaces()).willReturn(List.of(raceDto));
+
+    String response = mockMvc.perform(get("/race/")
+            .contentType(MediaType.APPLICATION_JSON))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    List<RaceDto> raceDtoResponse = List.of(objectMapper.readValue(response, RaceDto[].class));
+    Assertions.assertEquals(raceDtoResponse.get(0), raceDto);
   }
 
   @Test
   void assignDriverOne() throws Exception {
-    var requestAssign = patch("/race/assignDriverOne")
-        .param("driverOneId", "1")
-        .param("raceId", "1")
-        .param("carId", "1")
-        .contentType(MediaType.APPLICATION_JSON_VALUE);
-    this.mockMvc.perform(requestAssign)
-        .andDo(print())
-        .andExpect(status().isOk())
-        .andExpect(content().string(expectedMessagePost));
+    var raceDto = RaceTestUtil.getDaoRaceDto();
+    given(raceServiceMock.assignDriverOne(1L, 1L, 1L)).willReturn(raceDto);
+
+    String response = mockMvc.perform(patch("/race/assignDriverOne")
+            .param("driverOneId", String.valueOf(1L))
+            .param("carId", String.valueOf(1L))
+            .param("raceId", String.valueOf(1L))
+            .contentType(MediaType.APPLICATION_JSON))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    RaceDto raceDtoResponse = objectMapper.readValue(response, RaceDto.class);
+    Assertions.assertEquals(raceDtoResponse, raceDto);
   }
 
   @Test
   void assignDriverTwo() throws Exception {
-    var requestAssign = patch("/race/assignDriverTwo")
-        .param("driverTwoId", "2")
-        .param("raceId", "2")
-        .param("carId", "2")
-        .contentType(MediaType.APPLICATION_JSON_VALUE);
-    this.mockMvc.perform(requestAssign)
-        .andDo(print())
-        .andExpect(status().isOk())
-        .andExpect(content().string(expectedMessageAssignTwo));
+    var raceDto = RaceTestUtil.getDaoRaceDto();
+    given(raceServiceMock.assignDriverTwo(2L, 1L, 2L)).willReturn(raceDto);
+
+    String response = mockMvc.perform(patch("/race/assignDriverTwo")
+            .param("driverTwoId", String.valueOf(2L))
+            .param("carId", String.valueOf(2L))
+            .param("raceId", String.valueOf(1L))
+            .contentType(MediaType.APPLICATION_JSON))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    RaceDto raceDtoResponse = objectMapper.readValue(response, RaceDto.class);
+    Assertions.assertEquals(raceDtoResponse, raceDto);
   }
 
+  @Test
+  void assignPositionDriverTwo() throws Exception {
+    var raceDto = RaceTestUtil.getDaoRaceDto();
+    given(raceServiceMock.assignPositionForDriverTwo(1L, 2)).willReturn(raceDto);
+
+    String response = mockMvc.perform(patch("/race/assignPointsDriverTwo")
+            .param("position", String.valueOf(2))
+            .param("raceId", String.valueOf(1L))
+            .contentType(MediaType.APPLICATION_JSON))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    RaceDto raceDtoResponse = objectMapper.readValue(response, RaceDto.class);
+    Assertions.assertEquals(raceDtoResponse, raceDto);
+  }
+
+  @Test
+  void assignPositionDriverOne() throws Exception {
+    var raceDto = RaceTestUtil.getDaoRaceDto();
+    given(raceServiceMock.assignPositionForDriverOne(1L, 1)).willReturn(raceDto);
+
+    String response = mockMvc.perform(patch("/race/assignPointsDriverOne")
+            .param("position", String.valueOf(1))
+            .param("raceId", String.valueOf(1L))
+            .contentType(MediaType.APPLICATION_JSON))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    RaceDto raceDtoResponse = objectMapper.readValue(response, RaceDto.class);
+    Assertions.assertEquals(raceDtoResponse, raceDto);
+  }
+
+  @Test
+  void getMostSuitableDriversForLocation() throws Exception {
+    given(raceServiceMock.findMostSuitableDriver(Location.MONACO)).willReturn(Set.of(1L));
+
+    String response = mockMvc.perform(get("/race/findMostSuitableDriversForLocation")
+            .param("location", String.valueOf(Location.MONACO))
+            .contentType(MediaType.APPLICATION_JSON))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    var responseLongs = Set.of(objectMapper.readValue(response, Long[].class));
+    Assertions.assertEquals(Set.of(1L), responseLongs);
+  }
 }
diff --git a/race/src/test/java/cz/muni/pa165/race/rest/RaceServiceTest.java b/race/src/test/java/cz/muni/pa165/race/rest/RaceServiceTest.java
new file mode 100644
index 00000000..17e3a47b
--- /dev/null
+++ b/race/src/test/java/cz/muni/pa165/race/rest/RaceServiceTest.java
@@ -0,0 +1,154 @@
+package cz.muni.pa165.race.rest;
+
+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 cz.muni.pa165.common_library.dtos.CarResponseDto;
+import cz.muni.pa165.common_library.dtos.DriverDto;
+import cz.muni.pa165.race.data.model.Race;
+import cz.muni.pa165.race.data.repository.RaceRepository;
+import cz.muni.pa165.race.service.DbGetter;
+import java.util.List;
+import java.util.Optional;
+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;
+
+@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}}";
+  private String expectedMessageAssignTwo = "{\"id\":1,\"raceInfo\":{\"location\":"
+      + "\"MONACO\",\"name\":\"Monaco Grand Prix 2023\",\"prizePool\":30000000},"
+      + "\"driverOne\":{\"driverId\":1,\"carId\":1,\"position\":null},\"driverTwo\""
+      + ":{\"driverId\":1,\"carId\":1,\"position\":null}}";
+  private String expectedMessageGet = "{\"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}}";
+  private String expectedMessageGetAll = "[{\"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;
+
+  @MockBean
+  private DbGetter dbGetter;
+
+  @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));
+    CarResponseDto carDao = RaceTestUtil.getCarDao();
+    given(dbGetter.getCar(anyLong())).willReturn(carDao);
+    DriverDto driverDto = RaceTestUtil.getDriverDao();
+    given(dbGetter.getDriver(anyLong())).willReturn(driverDto);
+  }
+
+
+  @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 {
+    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(expectedMessageGet));
+  }
+
+  @Test
+  void getAllRaces() throws Exception {
+    var requestGet = get("/race/");
+    this.mockMvc.perform(requestGet)
+        .andDo(print())
+        .andExpect(status().isOk())
+        .andExpect(content().string(expectedMessageGetAll));
+  }
+
+  @Test
+  void assignDriverOne() throws Exception {
+    var requestAssign = patch("/race/assignDriverOne")
+        .param("driverOneId", "1")
+        .param("raceId", "1")
+        .param("carId", "1")
+        .contentType(MediaType.APPLICATION_JSON_VALUE);
+    this.mockMvc.perform(requestAssign)
+        .andDo(print())
+        .andExpect(status().isOk())
+        .andExpect(content().string(expectedMessagePost));
+  }
+
+  @Test
+  void assignDriverTwo() throws Exception {
+    var requestAssign = patch("/race/assignDriverTwo")
+        .param("driverTwoId", "2")
+        .param("raceId", "2")
+        .param("carId", "2")
+        .contentType(MediaType.APPLICATION_JSON_VALUE);
+    this.mockMvc.perform(requestAssign)
+        .andDo(print())
+        .andExpect(status().isOk())
+        .andExpect(content().string(expectedMessageAssignTwo));
+  }
+
+}
diff --git a/race/src/test/java/cz/muni/pa165/race/rest/SeasonControllerTest.java b/race/src/test/java/cz/muni/pa165/race/rest/SeasonControllerTest.java
index b03b6430..627b6429 100644
--- a/race/src/test/java/cz/muni/pa165/race/rest/SeasonControllerTest.java
+++ b/race/src/test/java/cz/muni/pa165/race/rest/SeasonControllerTest.java
@@ -1,124 +1,106 @@
 package cz.muni.pa165.race.rest;
 
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyLong;
+import com.fasterxml.jackson.databind.ObjectMapper;
+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.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.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 cz.muni.pa165.race.data.model.Race;
-import cz.muni.pa165.race.data.model.Season;
-import cz.muni.pa165.race.data.repository.RaceRepository;
-import cz.muni.pa165.race.data.repository.SeasonRepository;
 import java.util.List;
-import java.util.Optional;
-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;
 
-/**
- * Tests for season controller.
- */
-@SpringBootTest
 @AutoConfigureMockMvc
+@WebMvcTest(SeasonController.class)
+@ExtendWith(MockitoExtension.class)
 class SeasonControllerTest {
-  private String bodyContent = """
-          {
-          "id": 1,
-          "year": 2020,
-          "races": []
-        }
-      """;
-  private String expectedMessagePost = "{\"id\":1,\"year\":2023,\"races\":[]}";
-  private String expectedMessageDelete = "Season with id: 1was succesfully deleted";
-  private String expectedMessageGet = "{\"id\":1,\"year\":2023,\"races\":[]}";
-  private String expectedMessageGetAll = "[{\"id\":1,\"year\":2023,\"races\":[]}]";
-  private String expectedMessageAddRace = "{\"id\":1,\"year\":2023,\"races\":[{\""
-      + "id\":1,\"name\":\"Monaco Grand Prix 2023\"}]}";
 
   @Autowired
   private MockMvc mockMvc;
 
   @MockBean
-  private SeasonRepository seasonRepository;
-
-  @MockBean
-  private RaceRepository raceRepository;
-
-  @BeforeEach
-  void setup() {
-    Season seasonDao = SeasonTestUtil.getDaoSeason();
-    Race raceDao = RaceTestUtil.getDaoRace();
-    given(seasonRepository.save(any(Season.class))).willReturn(
-        seasonDao);
-    given(seasonRepository.findById(anyLong())).willReturn(
-        Optional.of(seasonDao));
-    given(seasonRepository.findAll()).willReturn(List.of(seasonDao));
-    given(raceRepository.findById(anyLong())).willReturn(
-        Optional.of(raceDao));
-  }
+  private SeasonServiceI seasonServiceMock;
 
+  @Autowired
+  private ObjectMapper objectMapper;
 
   @Test
   void createSeason() throws Exception {
-    var request = post("/season/")
-        .content(bodyContent)
-        .contentType(MediaType.APPLICATION_JSON_VALUE);
-    this.mockMvc.perform(request)
-        .andDo(print())
-        .andExpect(status().isOk())
-        .andExpect(content().string(expectedMessagePost));
+    var seasonDto = SeasonTestUtil.getDaoSeasonDto();
+    given(seasonServiceMock.postSeason(seasonDto)).willReturn(seasonDto);
+
+    String response = mockMvc.perform(post("/season/")
+            .contentType(MediaType.APPLICATION_JSON)
+            .content(objectMapper.writeValueAsString(seasonDto)))
+        .andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
+
+    SeasonDto seasonDtoResponse = objectMapper.readValue(response, SeasonDto.class);
+    Assertions.assertEquals(seasonDtoResponse, seasonDto);
   }
 
   @Test
-  void deleteRace() throws Exception {
-    var requestDelete = delete("/season/")
-        .param("seasonId", "1")
-        .contentType(MediaType.APPLICATION_JSON_VALUE);
-    this.mockMvc.perform(requestDelete)
-        .andDo(print())
-        .andExpect(status().isOk())
-        .andExpect(content().string(expectedMessageDelete));
+  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 getExistingRace() throws Exception {
-    var requestGet = get("/season/id")
-        .param("seasonId", "1")
-        .contentType(MediaType.APPLICATION_JSON_VALUE);
-    this.mockMvc.perform(requestGet)
-        .andDo(print())
-        .andExpect(status().isOk())
-        .andExpect(content().string(expectedMessageGet));
+  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 getAllRaces() throws Exception {
-    var requestGet = get("/season/");
-    this.mockMvc.perform(requestGet)
-        .andDo(print())
-        .andExpect(status().isOk())
-        .andExpect(content().string(expectedMessageGetAll));
+  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() throws Exception {
-    var requestPatch = patch("/season/addRace")
-        .param("seasonId", "1")
-        .param("raceId", "1");
-    this.mockMvc.perform(requestPatch)
-        .andDo(print())
-        .andExpect(status().isOk())
-        .andExpect(content().string(expectedMessageAddRace));
+    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
+}
diff --git a/race/src/test/java/cz/muni/pa165/race/rest/SeasonServiceTest.java b/race/src/test/java/cz/muni/pa165/race/rest/SeasonServiceTest.java
new file mode 100644
index 00000000..5c6a6859
--- /dev/null
+++ b/race/src/test/java/cz/muni/pa165/race/rest/SeasonServiceTest.java
@@ -0,0 +1,124 @@
+package cz.muni.pa165.race.rest;
+
+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 cz.muni.pa165.race.data.model.Race;
+import cz.muni.pa165.race.data.model.Season;
+import cz.muni.pa165.race.data.repository.RaceRepository;
+import cz.muni.pa165.race.data.repository.SeasonRepository;
+import java.util.List;
+import java.util.Optional;
+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;
+
+/**
+ * Tests for season controller.
+ */
+@SpringBootTest
+@AutoConfigureMockMvc
+class SeasonServiceTest {
+  private String bodyContent = """
+          {
+          "id": 1,
+          "year": 2020,
+          "races": []
+        }
+      """;
+  private String expectedMessagePost = "{\"id\":1,\"year\":2023,\"races\":[]}";
+  private String expectedMessageDelete = "Season with id: 1was succesfully deleted";
+  private String expectedMessageGet = "{\"id\":1,\"year\":2023,\"races\":[]}";
+  private String expectedMessageGetAll = "[{\"id\":1,\"year\":2023,\"races\":[]}]";
+  private String expectedMessageAddRace = "{\"id\":1,\"year\":2023,\"races\":[{\""
+      + "id\":1,\"name\":\"Monaco Grand Prix 2023\"}]}";
+
+  @Autowired
+  private MockMvc mockMvc;
+
+  @MockBean
+  private SeasonRepository seasonRepository;
+
+  @MockBean
+  private RaceRepository raceRepository;
+
+  @BeforeEach
+  void setup() {
+    Season seasonDao = SeasonTestUtil.getDaoSeason();
+    Race raceDao = RaceTestUtil.getDaoRace();
+    given(seasonRepository.save(any(Season.class))).willReturn(
+        seasonDao);
+    given(seasonRepository.findById(anyLong())).willReturn(
+        Optional.of(seasonDao));
+    given(seasonRepository.findAll()).willReturn(List.of(seasonDao));
+    given(raceRepository.findById(anyLong())).willReturn(
+        Optional.of(raceDao));
+  }
+
+
+  @Test
+  void createSeason() throws Exception {
+    var request = post("/season/")
+        .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 {
+    var requestDelete = delete("/season/")
+        .param("seasonId", "1")
+        .contentType(MediaType.APPLICATION_JSON_VALUE);
+    this.mockMvc.perform(requestDelete)
+        .andDo(print())
+        .andExpect(status().isOk())
+        .andExpect(content().string(expectedMessageDelete));
+  }
+
+  @Test
+  void getExistingRace() throws Exception {
+    var requestGet = get("/season/id")
+        .param("seasonId", "1")
+        .contentType(MediaType.APPLICATION_JSON_VALUE);
+    this.mockMvc.perform(requestGet)
+        .andDo(print())
+        .andExpect(status().isOk())
+        .andExpect(content().string(expectedMessageGet));
+  }
+
+  @Test
+  void getAllRaces() throws Exception {
+    var requestGet = get("/season/");
+    this.mockMvc.perform(requestGet)
+        .andDo(print())
+        .andExpect(status().isOk())
+        .andExpect(content().string(expectedMessageGetAll));
+  }
+
+  @Test
+  void addRace() throws Exception {
+    var requestPatch = patch("/season/addRace")
+        .param("seasonId", "1")
+        .param("raceId", "1");
+    this.mockMvc.perform(requestPatch)
+        .andDo(print())
+        .andExpect(status().isOk())
+        .andExpect(content().string(expectedMessageAddRace));
+  }
+}
\ No newline at end of file
-- 
GitLab