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 472c44ec700c8a0d069defa4d342875fcd7272aa..3cae69527725b456c818d6bcaf51249730204898 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 @@ -1,16 +1,27 @@ package cz.muni.fi.pa165.service; +import cz.muni.fi.pa165.dao.Flight; import cz.muni.fi.pa165.dao.FlightRealization; +import cz.muni.fi.pa165.exception.FlightNotFoundException; import cz.muni.fi.pa165.exception.FlightRealizationNotFoundException; import cz.muni.fi.pa165.repository.FlightRealizationRepository; import cz.muni.fi.pa165.repository.FlightRepository; +import cz.muni.fi.pa165.utils.TestDaoFactory; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import java.time.Duration; import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; @ExtendWith(MockitoExtension.class) public class FlightRealizationServiceTest { @@ -21,27 +32,87 @@ public class FlightRealizationServiceTest { private FlightRealizationService flightRealizationService; @Test - public void getAllFlightRealizations() { + public void getFlightRealization_flightRealizationFound_returnsFlight() { + // Prepare a sample flight ID + UUID id = UUID.randomUUID(); + + // Create a sample flight object + FlightRealization flight = TestDaoFactory.getflightRealizationEntity(); + + // Mocking the behavior of the flightRepository.findById() method + Mockito.when(flightRealizationRepository.findById(id)).thenReturn(Optional.of(flight)); + // Calling the method to be tested + FlightRealization foundFlight = flightRealizationService.getFlightRealizationById(id); + + // Assertions + Assertions.assertEquals(foundFlight, flight); // Check if the returned flight matches the one we created } @Test - public void getFlightRealizationById() { + void getById_flightRealizationNotFound_throwsResourceNotFoundException() { + // Prepare a random non-existent flight ID + UUID id = UUID.randomUUID(); + + // Mock the behavior of the flightRepository.findById() method to return an empty Optional + Mockito.when(flightRealizationRepository.findById(id)).thenReturn(Optional.empty()); + // Assert that calling flightService.getFlight(id) should throw FlightNotFoundException + assertThrows(FlightRealizationNotFoundException.class, () -> flightRealizationService.getFlightRealizationById(id)); } + @Test - public void createFlightRealization() { + void createFlightRealization() { + // Create a sample flight object + FlightRealization flight = TestDaoFactory.getflightRealizationEntity(); + + // Mocking the behavior of the flightRepository.save() method + Mockito.when(flightRealizationRepository.save(flight)).thenReturn(flight); + // Calling the method to be tested + FlightRealization createdFlight = flightRealizationService.createFlightRealization(flight); + + // Assertions + assertEquals(flight, createdFlight); // Check if returned flight matches the one we created + Mockito.verify(flightRealizationRepository, Mockito.times(1)).save(flight); // Verify that save method was called exactly once with the correct parameter } @Test - public void updateFlightRealization() { + void updateFlightRealization_flightRealizationNotFound_throwsFlightRealizationNotFoundException() { + UUID id = UUID.randomUUID(); // Generate a random flight ID + FlightRealization update = new FlightRealization(); // Prepare an update object + + // Assert that calling flightService.updateFlight(id, update) should throw FlightNotFoundException + assertThrows(FlightRealizationNotFoundException.class, () -> flightRealizationService.updateFlightRealization(id, update)); + // Verify that stewardRepository.save() was not called + Mockito.verify(flightRealizationRepository, Mockito.times(0)).save(Mockito.any()); } + @Test - public void deleteFlightRealization() { + void updateFlightRealization_flightFound() { + UUID id = UUID.randomUUID(); + + FlightRealization existingFlight = TestDaoFactory.getflightRealizationEntity(); + existingFlight.setId(id); + + FlightRealization update = new FlightRealization(); + update.setDuration(Duration.ofHours(2)); + + FlightRealization updatedFlight = TestDaoFactory.getflightRealizationEntity(); + + updatedFlight.setId(id); + updatedFlight.setDuration(Duration.ofHours(2)); + + Mockito.when(flightRealizationRepository.findById(id)).thenReturn(Optional.of(existingFlight)); + + Mockito.when(flightRealizationRepository.save(Mockito.any(FlightRealization.class))).thenReturn(updatedFlight); + + FlightRealization returnedFlight = flightRealizationService.updateFlightRealization(id, update); + assertEquals(updatedFlight, returnedFlight); + Mockito.verify(flightRealizationRepository, Mockito.times(1)).save(updatedFlight); } } diff --git a/airports-flight-service/src/test/java/cz/muni/fi/pa165/utils/TestApiFactory.java b/airports-flight-service/src/test/java/cz/muni/fi/pa165/utils/TestApiFactory.java index a478f64ba714b9c64b07ef6a48ca9dbb37d1a2ec..d3e5940f6a6dcc4014ecb1bcbe5badaa9d9ae1b1 100644 --- a/airports-flight-service/src/test/java/cz/muni/fi/pa165/utils/TestApiFactory.java +++ b/airports-flight-service/src/test/java/cz/muni/fi/pa165/utils/TestApiFactory.java @@ -24,7 +24,7 @@ public class TestApiFactory { } - public static FlightRealization getflightRealizationEntity() { + public static FlightRealization getFlightRealizationEntity() { UUID id = new UUID(0x1, 0xf); FlightRealization flightRealization = new FlightRealization(); flightRealization.setDepartureTime(LocalDateTime.now());