Skip to content
Snippets Groups Projects
Commit 20c51293 authored by xkromm's avatar xkromm
Browse files

flight realization service tests

parent 35a1b383
No related branches found
No related tags found
1 merge request!13Flights tests
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);
}
}
......@@ -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());
......
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