diff --git a/airports-flight-service/src/main/java/cz/muni/fi/pa165/rest/FlightController.java b/airports-flight-service/src/main/java/cz/muni/fi/pa165/rest/FlightController.java index 606ec5a0c6b3e4d6cd0f767d18e60930be6a8f83..57cf35aa220da94362b8f684d7f2b66c0b69c40a 100644 --- a/airports-flight-service/src/main/java/cz/muni/fi/pa165/rest/FlightController.java +++ b/airports-flight-service/src/main/java/cz/muni/fi/pa165/rest/FlightController.java @@ -3,6 +3,8 @@ package cz.muni.fi.pa165.rest; import cz.muni.fi.pa165.api.flight.Flight; import cz.muni.fi.pa165.api.flight.requests.FlightRequest; //import cz.muni.fi.pa165.dao.Flight; +import cz.muni.fi.pa165.exception.FlightNotFoundException; +import cz.muni.fi.pa165.exception.FlightRealizationNotFoundException; import cz.muni.fi.pa165.facade.FlightFacade; import cz.muni.fi.pa165.repository.FlightRepository; import lombok.RequiredArgsConstructor; @@ -56,4 +58,9 @@ public class FlightController { flightFacade.deleteFlight(id); return new ResponseEntity<>("Flight deleted sucessfully", HttpStatus.OK); } + + @ExceptionHandler(FlightNotFoundException.class) + public ResponseEntity<String> handleIssueReportNotFoundException(FlightNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Flight not found"); + } } diff --git a/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/FlightFacadeTest.java b/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/FlightFacadeTest.java index 70d5705491112d65d7ce2467b915f731a53741fa..81c2a6b1bb7aca2870a035c50d8d9aaf40556a5b 100644 --- a/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/FlightFacadeTest.java +++ b/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/FlightFacadeTest.java @@ -54,18 +54,4 @@ public class FlightFacadeTest { assertThrows(FlightNotFoundException.class, () -> flightFacade.getFlight(id)); } - @Test - public void createFlight() { - - } - - @Test - public void updateFlight() { - - } - - @Test - public void deleteFlight() { - - } } diff --git a/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/FlightRealizationFacadeTest.java b/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/FlightRealizationFacadeTest.java index b96f9d6bef5b7d29339385feb780e87a5e3a6165..7e41d0668a401ca9beb1d155ac79db05bc860c4e 100644 --- a/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/FlightRealizationFacadeTest.java +++ b/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/FlightRealizationFacadeTest.java @@ -17,33 +17,4 @@ public class FlightRealizationFacadeTest { @InjectMocks private FlightFacade flightFacade; - @BeforeEach - public void simulate_post_construct() { - - } - - @Test - public void getFlightRealizations() { - - } - - @Test - public void getFlightRealization() { - - } - - @Test - public void createFlightRealization() { - - } - - @Test - public void updateFlightRealization() { - - } - - @Test - public void deleteFlightRealization() { - - } } diff --git a/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/IssueReportFacadeTest.java b/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/IssueReportFacadeTest.java index e6a23d2915ecbd62de451f95b95f45853501e62d..bad358e80a5dcff416836a9017ff9320f7e3fec3 100644 --- a/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/IssueReportFacadeTest.java +++ b/airports-flight-service/src/test/java/cz/muni/fi/pa165/facade/IssueReportFacadeTest.java @@ -33,16 +33,6 @@ public class IssueReportFacadeTest { issueReportFacade.init(); } - @Test - public void getAllIssueReports() { - - } - - @Test - public void getIssueReport() { - - } - @Test public void createIssueReport() { cz.muni.fi.pa165.api.flight.IssueReport issueReport = TestApiFactory.getIssueReport(); @@ -56,14 +46,4 @@ public class IssueReportFacadeTest { assertEquals(issueReport, issueReportFacade.createIssueReport(issueReportRequest)); Mockito.verify(issueReportService, Mockito.times(1)).createIssueReport(daoIssueReport); } - - @Test - public void updateIssueReport() { - - } - - @Test - public void deleteIssueReport() { - - } } diff --git a/airports-flight-service/src/test/java/cz/muni/fi/pa165/rest/FlightControllerTest.java b/airports-flight-service/src/test/java/cz/muni/fi/pa165/rest/FlightControllerTest.java index 69716f3c2cdf4cc25dd6fd8f15b730a35fd4ad86..f34ae20da256d2ba7cee464a59fc1ef53d078883 100644 --- a/airports-flight-service/src/test/java/cz/muni/fi/pa165/rest/FlightControllerTest.java +++ b/airports-flight-service/src/test/java/cz/muni/fi/pa165/rest/FlightControllerTest.java @@ -22,6 +22,7 @@ import org.springframework.test.web.servlet.MockMvc; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.UUID; @@ -30,6 +31,8 @@ import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -43,38 +46,58 @@ public class FlightControllerTest { private FlightFacade flightFacade; @Test - void findById_flightFound_returnflight() throws Exception { - UUID id = new UUID(0x1, 0xf); - UUID planeId = new UUID(0x1, 0xf); - UUID destination = new UUID(0x2, 0xf); - UUID origin = new UUID(0x3, 0xf); -// UUID id = UUID.fromString("00000000-0000-0001-0000-00000000000f"); - Mockito.when(flightFacade.getFlight(id)).thenReturn(TestApiFactory.getFlightEntity()); - - RequestBuilder request = MockMvcRequestBuilders.get("/flights/{id}", id).accept(MediaType.APPLICATION_JSON_VALUE); - String responseJson = mockMvc.perform(request) + void getAllFlights_noFlightsFound_returnsNoContent() throws Exception { + Mockito.when(flightFacade.getFlights(0, 10)).thenReturn(Collections.emptyList()); + + mockMvc.perform(get("/flights") + .param("page", "0") + .param("size", "10") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } + + @Test + void getAllFlights_flightsFound_returnsFlights() throws Exception { + Flight flight = TestApiFactory.getFlightEntity(); + Mockito.when(flightFacade.getFlights(0, 10)).thenReturn(Collections.singletonList(flight)); + + String responseJson = mockMvc.perform(get("/flights") + .param("page", "0") + .param("size", "10") + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn() .getResponse() - .getContentAsString(StandardCharsets.UTF_8); - Flight response = ObjectConverter.convertJsonToObject(responseJson, Flight.class); - assertThat(responseJson).isEqualTo("{" + - "\"id\":\""+ id +"\"," + - "\"planeId\":\"" + planeId + "\"," + - "\"pilotIds\":null," + - "\"stewardIds\":null," + - "\"origin\":\"" +origin+ "\"," + - "\"destination\":\"" + destination + "\"" + - "}" - ); - assertThat(response).isEqualTo(TestApiFactory.getFlightEntity()); + .getContentAsString(); + + Flight[] response = ObjectConverter.convertJsonToObject(responseJson, Flight[].class); + + assertThat(response).containsExactly(flight); } + @Test + void getFlight_flightFound_returnsFlight() throws Exception { + UUID id = UUID.randomUUID(); + Flight flight = TestApiFactory.getFlightEntity(); + Mockito.when(flightFacade.getFlight(id)).thenReturn(flight); + + String responseJson = mockMvc.perform(get("/flights/{id}", id) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn() + .getResponse() + .getContentAsString(); + + Flight response = ObjectConverter.convertJsonToObject(responseJson, Flight.class); + + assertThat(response).isEqualTo(flight); + } @Test - void findById_flightNotFound_throws404() throws Exception { + void getFlight_flightNotFound_throws404() throws Exception { UUID id = UUID.randomUUID(); Mockito.when(flightFacade.getFlight(id)).thenThrow(new FlightNotFoundException()); + mockMvc.perform(get("/flights/{id}", id) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); @@ -82,59 +105,55 @@ public class FlightControllerTest { Mockito.verify(flightFacade, Mockito.times(1)).getFlight(id); } - @Test - void updateflight_flightFound_returnsflight() throws Exception { - UUID id = new UUID(0x1, 0xf); - UUID planeId = new UUID(0x1, 0xf); - UUID destination = new UUID(0x2, 0xf); - UUID origin = new UUID(0x3, 0xf); - cz.muni.fi.pa165.api.flight.Flight updatedFlight = TestApiFactory.getFlightEntity(); - Mockito.when(flightFacade.updateFlight( Mockito.eq(id), Mockito.any(FlightRequest.class))).thenReturn(updatedFlight); - - String requestJson = - "{" + - "\"planeId\":\"" + planeId + "\"," + - "\"pilotIds\":null," + - "\"stewardIds\":null," + - "\"origin\":\"" +origin+ "\"," + - "\"destination\":\"" + destination + "\"" + - "}"; - - - String responseJson = mockMvc.perform(put("/flights/{id}", id) + void createFlight_flightCreated_returnsFlight() throws Exception { + FlightRequest flightRequest = TestApiFactory.getFlightRequest(); + Flight flight = TestApiFactory.getFlightEntity(); + Mockito.when(flightFacade.createFlight(Mockito.any(FlightRequest.class))).thenReturn(flight); + + String requestJson = ObjectConverter.convertObjectToJson(flightRequest); + String responseJson = mockMvc.perform(post("/flights") .contentType(MediaType.APPLICATION_JSON) .content(requestJson)) .andExpect(status().isOk()) .andReturn() .getResponse() - .getContentAsString(StandardCharsets.UTF_8); + .getContentAsString(); Flight response = ObjectConverter.convertJsonToObject(responseJson, Flight.class); - assertThat(response).isEqualTo(updatedFlight); - Mockito.verify(flightFacade, Mockito.times(1)).updateFlight(Mockito.eq(id), Mockito.any(FlightRequest.class)); + + assertThat(response).isEqualTo(flight); } + @Test + void updateFlight_flightUpdated_returnsFlight() throws Exception { + UUID id = UUID.randomUUID(); + FlightRequest flightRequest = TestApiFactory.getFlightRequest(); + Flight flight = TestApiFactory.getFlightEntity(); + Mockito.when(flightFacade.updateFlight(Mockito.eq(id), Mockito.any(FlightRequest.class))).thenReturn(flight); + + String requestJson = ObjectConverter.convertObjectToJson(flightRequest); + String responseJson = mockMvc.perform(post("/flights/{id}", id) + .contentType(MediaType.APPLICATION_JSON) + .content(requestJson)) + .andExpect(status().isOk()) + .andReturn() + .getResponse() + .getContentAsString(); + + Flight response = ObjectConverter.convertJsonToObject(responseJson, Flight.class); + + assertThat(response).isEqualTo(flight); + } @Test - public void updateflight_flightNotFound_throws404() throws Exception { + void updateFlight_flightNotFound_throws404() throws Exception { UUID id = UUID.randomUUID(); - UUID planeId = new UUID(0x1, 0xf); - UUID destination = new UUID(0x2, 0xf); - UUID origin = new UUID(0x3, 0xf); - - Mockito.when(flightFacade.updateFlight(Mockito.eq(id), Mockito.any())).thenThrow(new FlightNotFoundException()); - RequestBuilder request = MockMvcRequestBuilders.delete("/flights/{id}", id).accept(MediaType.APPLICATION_JSON); - String requestJson = - "{" + - "\"planeId\":\"" + planeId + "\"," + - "\"pilotIds\":null," + - "\"stewardIds\":null," + - "\"origin\":\"" +origin+ "\"," + - "\"destination\":\"" + destination + "\"" + - "}"; - - mockMvc.perform(put("/flights/{id}", id) + FlightRequest flightRequest = TestApiFactory.getFlightRequest(); + Mockito.when(flightFacade.updateFlight(Mockito.eq(id), Mockito.any(FlightRequest.class))).thenThrow(new FlightNotFoundException()); + + String requestJson = ObjectConverter.convertObjectToJson(flightRequest); + mockMvc.perform(post("/flights/{id}", id) .contentType(MediaType.APPLICATION_JSON) .content(requestJson)) .andExpect(status().isNotFound()); @@ -143,22 +162,13 @@ public class FlightControllerTest { } @Test - public void deleteflight_flightFound() throws Exception { + void deleteFlight_flightDeleted_returnsOk() throws Exception { UUID id = UUID.randomUUID(); - Flight flight = TestApiFactory.getFlightEntity(); Mockito.doNothing().when(flightFacade).deleteFlight(id); - RequestBuilder request = MockMvcRequestBuilders.delete("/flights/{id}", id).accept(MediaType.APPLICATION_JSON); - mockMvc.perform(request).andExpect(status().isOk()); - Mockito.verify(flightFacade, Mockito.times(1)).deleteFlight(id); - } - - @Test - public void Deleteflight_flightNotFound_404() throws Exception { - UUID id = UUID.randomUUID(); -// Mockito.when(flightFacade.deleteFlight(id)).thenThrow(new FlightNotFoundException()); - RequestBuilder request = MockMvcRequestBuilders.delete("/flights/{id}", id).accept(MediaType.APPLICATION_JSON); - mockMvc.perform(request).andExpect(status().isNotFound()); + mockMvc.perform(delete("/flights/{id}", id) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); Mockito.verify(flightFacade, Mockito.times(1)).deleteFlight(id); } diff --git a/airports-flight-service/src/test/java/cz/muni/fi/pa165/rest/FlightRealizationControllerTest.java b/airports-flight-service/src/test/java/cz/muni/fi/pa165/rest/FlightRealizationControllerTest.java index 97d7fb0e638ba99f9a2ce4eebbce5d799815fad8..a891344340469ca907e405b72e5d475c5ce86f6b 100644 --- a/airports-flight-service/src/test/java/cz/muni/fi/pa165/rest/FlightRealizationControllerTest.java +++ b/airports-flight-service/src/test/java/cz/muni/fi/pa165/rest/FlightRealizationControllerTest.java @@ -1,13 +1,8 @@ package cz.muni.fi.pa165.rest; -import cz.muni.fi.pa165.api.flight.IssueReport; -import cz.muni.fi.pa165.api.flight.requests.FlightRealizationRequest; -import cz.muni.fi.pa165.api.flight.requests.FlightRequest; -import cz.muni.fi.pa165.api.flight.requests.IssueReportRequest; import cz.muni.fi.pa165.api.flight.FlightRealization; +import cz.muni.fi.pa165.api.flight.requests.FlightRealizationRequest; import cz.muni.fi.pa165.exception.FlightRealizationNotFoundException; -import cz.muni.fi.pa165.exception.IssueReportNotFoundException; -import cz.muni.fi.pa165.facade.FlightFacade; import cz.muni.fi.pa165.facade.FlightRealizationFacade; import cz.muni.fi.pa165.utils.ObjectConverter; import cz.muni.fi.pa165.utils.TestApiFactory; @@ -16,7 +11,6 @@ import org.mockito.Mockito; 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.context.ApplicationContext; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.RequestBuilder; @@ -28,11 +22,9 @@ import java.time.LocalDateTime; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; - +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest(controllers = {FlightRealizationController.class}) @@ -59,23 +51,21 @@ public class FlightRealizationControllerTest { FlightRealization response = ObjectConverter.convertJsonToObject(responseJson, FlightRealization.class); assertThat(responseJson).isEqualTo("{" + - "\"id\":\""+ id +"\"," + + "\"id\":\"" + id + "\"," + "\"flightId\":\"" + new UUID(0x2, 0xf) + "\"," + "\"report\":\"report\"," + - "\"duration\":\""+ Duration.ofHours(1) +"\"," + + "\"duration\":\"" + Duration.ofHours(1) + "\"," + "\"departureTime\":\"" + LocalDateTime.of(2002, 2, 2, 2, 2, 2) + "\"," + "\"arrivalTime\":\"" + LocalDateTime.of(2002, 2, 2, 2, 2, 2) + "\"," + - "\"kilometersFlown\":"+ 850.0 + + "\"kilometersFlown\":" + 850.0 + "}" ); - assertThat(response).isEqualTo(TestApiFactory.getFlightRealizationEntity()); } - @Test void findById_flightRealizationNotFound_throws404() throws Exception { UUID id = UUID.randomUUID(); @@ -91,47 +81,46 @@ public class FlightRealizationControllerTest { @Test void updateflightRealization_flightRealizationFound_returnsIssueReport() throws Exception { UUID id = new UUID(0x1, 0xf); - cz.muni.fi.pa165.api.flight.FlightRealization updatedFlightRealization = TestApiFactory.getFlightRealizationEntity(); - Mockito.when(flightRealizationFacade.updateFlightRealization(Mockito.eq(id), Mockito.any(FlightRealizationRequest.class))).thenReturn(updatedFlightRealization); - - String requestJson = "{" + - "\"id\":\""+ id +"\"," + - "\"flightId\":\"" + new UUID(0x2, 0xf) + "\"," + - "\"report\":\"report\"," + - "\"duration\":\""+ Duration.ofHours(1) +"\"," + - "\"departureTime\":\"" + LocalDateTime.of(2002, 2, 2, 2, 2, 2) + "\"," + - "\"arrivalTime\":\"" + LocalDateTime.of(2002, 2, 2, 2, 2, 2) + "\"," + - "\"kilometersFlown\":"+ 850.0 + - "}"; - String responseJson = mockMvc.perform(put("/flightrealization/{id}", id) + cz.muni.fi.pa165.api.flight.FlightRealization updatedFlightRealization = TestApiFactory.getFlightRealizationEntity(); + Mockito.when(flightRealizationFacade.updateFlightRealization(Mockito.eq(id), Mockito.any(FlightRealizationRequest.class))).thenReturn(updatedFlightRealization); + + String requestJson = "{" + + "\"id\":\"" + id + "\"," + + "\"flightId\":\"" + new UUID(0x2, 0xf) + "\"," + + "\"report\":\"report\"," + + "\"duration\":\"" + Duration.ofHours(1) + "\"," + + "\"departureTime\":\"" + LocalDateTime.of(2002, 2, 2, 2, 2, 2) + "\"," + + "\"arrivalTime\":\"" + LocalDateTime.of(2002, 2, 2, 2, 2, 2) + "\"," + + "\"kilometersFlown\":" + 850.0 + + "}"; + String responseJson = mockMvc.perform(put("/flightrealization/{id}", id) .contentType(MediaType.APPLICATION_JSON) - .content(requestJson) - ) + .content(requestJson) + ) .andExpect(status().isOk()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); - FlightRealization response = ObjectConverter.convertJsonToObject(responseJson, FlightRealization.class); - assertThat(response).isEqualTo(updatedFlightRealization); - Mockito.verify(flightRealizationFacade, Mockito.times(1)) + FlightRealization response = ObjectConverter.convertJsonToObject(responseJson, FlightRealization.class); + assertThat(response).isEqualTo(updatedFlightRealization); + Mockito.verify(flightRealizationFacade, Mockito.times(1)) .updateFlightRealization(Mockito.eq(id), Mockito.any(FlightRealizationRequest.class)); } - @Test public void updateflightRealization_flightRealizationNotFound_throws404() throws Exception { UUID id = UUID.randomUUID(); String requestJson = "{" + - "\"id\":\""+ id +"\"," + + "\"id\":\"" + id + "\"," + "\"flightId\":\"" + new UUID(0x2, 0xf) + "\"," + "\"report\":\"report\"," + - "\"duration\":\""+ Duration.ofHours(1) +"\"," + + "\"duration\":\"" + Duration.ofHours(1) + "\"," + "\"departureTime\":\"" + LocalDateTime.of(2002, 2, 2, 2, 2, 2) + "\"," + "\"arrivalTime\":\"" + LocalDateTime.of(2002, 2, 2, 2, 2, 2) + "\"," + - "\"kilometersFlown\":"+ 850.0 + + "\"kilometersFlown\":" + 850.0 + "}"; Mockito.when(flightRealizationFacade.updateFlightRealization(Mockito.eq(id), Mockito.any(FlightRealizationRequest.class))).thenThrow(new FlightRealizationNotFoundException()); @@ -145,8 +134,6 @@ public class FlightRealizationControllerTest { } - - @Test public void deleteflightRealization_flightRealizationFound() throws Exception { UUID id = UUID.randomUUID(); diff --git a/airports-flight-service/src/test/java/cz/muni/fi/pa165/service/FlightRealizationServiceTest.java b/airports-flight-service/src/test/java/cz/muni/fi/pa165/service/FlightRealizationServiceTest.java index 805810936a48b31347bab4e958536bc1ab48e112..2549a4d7288aba7f78506b2d60ac3b5f6eed4507 100644 --- a/airports-flight-service/src/test/java/cz/muni/fi/pa165/service/FlightRealizationServiceTest.java +++ b/airports-flight-service/src/test/java/cz/muni/fi/pa165/service/FlightRealizationServiceTest.java @@ -90,26 +90,21 @@ public class FlightRealizationServiceTest { @Test void updateFlightRealization_flighrealizationtFound() { - UUID id = UUID.randomUUID(); + UUID id = new UUID(0x1, 0xf); FlightRealization existingFlightRealization = TestDaoFactory.getflightRealizationEntity(); - existingFlightRealization.setId(id); - FlightRealization update = new FlightRealization(); + FlightRealization update = TestDaoFactory.getflightRealizationEntity(); update.setDuration(Duration.ofHours(2)); + update.setFlightId(id); FlightRealization updatedFlightRealization = TestDaoFactory.getflightRealizationEntity(); - - updatedFlightRealization.setId(id); updatedFlightRealization.setDuration(Duration.ofHours(2)); Mockito.when(flightRealizationRepository.findById(id)).thenReturn(Optional.of(existingFlightRealization)); + Mockito.when(flightRealizationRepository.save(Mockito.any(FlightRealization.class))).thenAnswer(invocation -> invocation.getArgument(0)); - Mockito.when(flightRealizationRepository.save(Mockito.any(FlightRealization.class))).thenReturn(updatedFlightRealization); - - FlightRealization returnedFlightRealization = flightRealizationService.updateFlightRealization(id, update); - - assertEquals(updatedFlightRealization, returnedFlightRealization); + assertEquals(updatedFlightRealization, flightRealizationService.updateFlightRealization(id, update)); Mockito.verify(flightRealizationRepository, Mockito.times(1)).save(updatedFlightRealization); } } diff --git a/airports-flight-service/src/test/java/cz/muni/fi/pa165/service/FlightServiceTest.java b/airports-flight-service/src/test/java/cz/muni/fi/pa165/service/FlightServiceTest.java index c49a29db9a5dbda8f01e39aaf8b8b440da285257..5236e30e559aa2f4067f558757477eaeb4d3b70f 100644 --- a/airports-flight-service/src/test/java/cz/muni/fi/pa165/service/FlightServiceTest.java +++ b/airports-flight-service/src/test/java/cz/muni/fi/pa165/service/FlightServiceTest.java @@ -13,6 +13,7 @@ import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import java.time.LocalDateTime; +import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -90,29 +91,21 @@ public class FlightServiceTest { @Test void updateFlight_flightFound() { - UUID id = UUID.randomUUID(); - var destId = UUID.randomUUID(); + UUID id = new UUID(0x1, 0xf); + var destId = new UUID(0x2, 0xf); Flight existingFlight = TestDaoFactory.getFlightEntity(); - existingFlight.setId(id); - Flight update = new Flight(); -// update.setDestination(destId.toString()); - update.setDestination(destId); + Flight update = TestDaoFactory.getFlightEntity(); Flight updatedFlight = TestDaoFactory.getFlightEntity(); - updatedFlight.setId(id); -// updatedFlight.setDestination(destId.toString()); - updatedFlight.setDestination(destId); Mockito.when(flightRepository.findById(id)).thenReturn(Optional.of(existingFlight)); + Mockito.when(flightRepository.save(Mockito.any(Flight.class))).thenAnswer(invocation -> invocation.getArgument(0)); - Mockito.when(flightRepository.save(Mockito.any(Flight.class))).thenReturn(updatedFlight); - - Flight returnedFlight = flightService.updateFlight(id, update); - assertEquals(updatedFlight, returnedFlight); + assertEquals(updatedFlight, flightService.updateFlight(id, update)); Mockito.verify(flightRepository, Mockito.times(1)).save(updatedFlight); } diff --git a/airports-flight-service/src/test/java/cz/muni/fi/pa165/utils/TestDaoFactory.java b/airports-flight-service/src/test/java/cz/muni/fi/pa165/utils/TestDaoFactory.java index 17b1933158bf6cb539c6b4c90b4955c6f751d086..cdffb6c85aabd2f6d0698e02d980fd21691aabe2 100644 --- a/airports-flight-service/src/test/java/cz/muni/fi/pa165/utils/TestDaoFactory.java +++ b/airports-flight-service/src/test/java/cz/muni/fi/pa165/utils/TestDaoFactory.java @@ -15,20 +15,20 @@ import java.util.UUID; public class TestDaoFactory { public static Flight getFlightEntity() { - UUID id = new UUID(0x1, 0xf); Flight flight = new Flight(); - flight.setPlaneId(id); - flight.setOrigin(new UUID(0x1, 0xf)); - flight.setDestination(new UUID(0x1, 0xf)); + flight.setId(new UUID(0x1, 0xf)); + flight.setPlaneId(new UUID(0x2, 0xf)); + flight.setOrigin(new UUID(0x3, 0xf)); + flight.setDestination(new UUID(0x4, 0xf)); Set<UUID> pilots = new HashSet<>(); - pilots.add(UUID.randomUUID()); - pilots.add(UUID.randomUUID()); + pilots.add(new UUID(0x5, 0xf)); + pilots.add(new UUID(0x6, 0xf)); flight.setPilotIds(pilots); Set<UUID> stewards = new HashSet<>(); - stewards.add(UUID.randomUUID()); - stewards.add(UUID.randomUUID()); + stewards.add(new UUID(0x7, 0xf)); + stewards.add(new UUID(0x8, 0xf)); flight.setStewardIds(stewards); return flight;