Skip to content
Snippets Groups Projects
Commit e2761781 authored by Adam Krídl's avatar Adam Krídl
Browse files

Merge branch 'core-airport-services-unit-tests' into 'xkridl-core-airport'

Core airport services unit tests

See merge request xmachac5/pa165-airport-project!22
parents dbccb14e 58795e1b
No related branches found
No related tags found
No related merge requests found
Pipeline #
package cz.muni.fi.pa165.core.service.airport;
import cz.muni.fi.pa165.core.data.domain.Airport;
import cz.muni.fi.pa165.core.data.domain.City;
import cz.muni.fi.pa165.core.data.repository.airport.AirportRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class AirportServiceImplTest {
private AirportServiceImpl airportService;
private AirportRepository airportRepository;
@BeforeEach
void setUp() {
airportRepository = mock(AirportRepository.class);
airportService = new AirportServiceImpl(airportRepository);
}
@Test
void findByNameTestSuccess() {
var airportName = "Paris Charles de Gaulle";
var airport = new Airport();
airport.setName(airportName);
when(airportRepository.findByName(airportName))
.thenReturn(Optional.of(airport));
var foundCityByNameOpt = airportService.findByName(airportName);
assertTrue(foundCityByNameOpt.isPresent());
assertEquals(airport.getName(), foundCityByNameOpt.get().getName());
assertEquals(airport, foundCityByNameOpt.get());
}
@Test
void findByCodeTestSuccess() {
var airportCode = "JFK";
var airport = new Airport();
airport.setCode(airportCode);
when(airportRepository.findByCode(airportCode))
.thenReturn(Optional.of(airport));
var foundCityByCodeOpt = airportService.findByCode(airportCode);
assertTrue(foundCityByCodeOpt.isPresent());
assertEquals(airport.getName(), foundCityByCodeOpt.get().getName());
assertEquals(airport, foundCityByCodeOpt.get());
}
@Test
void findByCityTestSuccess() {
var city = new City();
city.setName("Prague");
var airport = new Airport();
airport.setCity(city);
when(airportRepository.findByCity(city))
.thenReturn(List.of(airport));
var foundAirportsByCity = airportService.findByCity(city);
assertFalse(foundAirportsByCity.isEmpty());
assertEquals(airport.getName(), foundAirportsByCity.get(0).getName());
assertEquals(airport, foundAirportsByCity.get(0));
}
@Test
void findByCityTestFindsNoAirports() {
var city = new City();
city.setName("Namestovo");
when(airportRepository.findByCity(city))
.thenReturn(new ArrayList<>());
var foundAirportsByCity = airportService.findByCity(city);
assertTrue(foundAirportsByCity.isEmpty());
}
@Test
void updateTestSuccess() {
var airportToUpdate = new Airport();
airportToUpdate.setId(1L);
var heathrow = "Heathrow";
airportToUpdate.setName(heathrow);
airportToUpdate.setCity(new City());
airportToUpdate.getCity().setName("London");
var lhr = "LHR";
airportToUpdate.setCode(lhr);
var updatedAirportRequest = new Airport();
var stansted = "Stansted";
updatedAirportRequest.setName(stansted);
var stn = "STN";
updatedAirportRequest.setCode(stn);
when(airportRepository.findById(1L))
.thenReturn(Optional.of(airportToUpdate));
when(airportRepository.save(airportToUpdate))
.thenReturn(updatedAirportRequest);
var updatedCountry = airportService.update(1L, updatedAirportRequest);
assertEquals(airportToUpdate.getName(), updatedCountry.getName());
assertEquals(airportToUpdate, updatedCountry);
assertEquals(stansted, airportToUpdate.getName());
assertNotEquals(heathrow, airportToUpdate.getName());
assertEquals(stn, airportToUpdate.getCode());
assertNotEquals(lhr, airportToUpdate.getCode());
}
}
\ No newline at end of file
package cz.muni.fi.pa165.core.service.city;
import cz.muni.fi.pa165.core.data.domain.City;
import cz.muni.fi.pa165.core.data.repository.city.CityRepository;
import cz.muni.fi.pa165.core.exceptions.ResourceNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class CityServiceImplTest {
private CityServiceImpl cityService;
private CityRepository cityRepository;
@BeforeEach
void setUp() {
cityRepository = mock(CityRepository.class);
cityService = new CityServiceImpl(cityRepository);
}
@Test
void findByNameTestSuccess() {
var cityName = "Berlin";
var city = new City();
city.setId(1L);
city.setName(cityName);
when(cityRepository.findByName(cityName))
.thenReturn(Optional.of(city));
var foundCityByNameOpt = cityService.findByName(cityName);
assertTrue(foundCityByNameOpt.isPresent());
assertEquals(city.getName(), foundCityByNameOpt.get().getName());
assertEquals(city, foundCityByNameOpt.get());
}
@Test
void findByNameTestFindsEmpty() {
var cityName = "Prague";
when(cityRepository.findByName(cityName))
.thenReturn(Optional.empty());
var foundCityByNameOpt = cityService.findByName(cityName);
assertTrue(foundCityByNameOpt.isEmpty());
}
@Test
void updateTestNotFoundByIdThrowsException() {
var city = new City();
city.setId(-1L);
city.setName("Madrid");
when(cityService.findById(-1L))
.thenReturn(Optional.empty());
assertThrows(ResourceNotFoundException.class, () -> cityService.update(-1L, city));
}
@Test
void updateTestSuccess() {
var cityToUpdate = new City();
cityToUpdate.setId(1L);
var amsterdam = "Amsterdam";
cityToUpdate.setName(amsterdam);
var updatedCityRequest = new City();
var antwerp = "Antwerp";
updatedCityRequest.setName(antwerp);
when(cityRepository.findById(1L))
.thenReturn(Optional.of(cityToUpdate));
when(cityRepository.save(cityToUpdate))
.thenReturn(updatedCityRequest);
var updatedCountry = cityService.update(1L, updatedCityRequest);
assertEquals(cityToUpdate.getName(), updatedCountry.getName());
assertEquals(cityToUpdate, updatedCountry);
assertEquals(antwerp, cityToUpdate.getName());
assertNotEquals(amsterdam, cityToUpdate.getName());
}
}
\ No newline at end of file
package cz.muni.fi.pa165.core.service.country;
import cz.muni.fi.pa165.core.data.domain.Country;
import cz.muni.fi.pa165.core.data.repository.country.CountryRepository;
import cz.muni.fi.pa165.core.exceptions.ResourceNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class CountryServiceImplTest {
private CountryServiceImpl countryService;
private CountryRepository countryRepository;
@BeforeEach
void setUp() {
countryRepository = mock(CountryRepository.class);
countryService = new CountryServiceImpl(countryRepository);
}
@Test
void findByNameTestSuccess() {
var countryName = "Italy";
var country = new Country();
country.setId(1L);
country.setName(countryName);
when(countryRepository.findByName(countryName))
.thenReturn(Optional.of(country));
var foundCountryByNameOpt = countryService.findByName(countryName);
assertTrue(foundCountryByNameOpt.isPresent());
assertEquals(country.getName(), foundCountryByNameOpt.get().getName());
assertEquals(country, foundCountryByNameOpt.get());
}
@Test
void updateTestNotFoundByIdThrowsException() {
var country = new Country();
country.setId(-1L);
country.setName("France");
when(countryRepository.findById(-1L))
.thenReturn(Optional.empty());
assertThrows(ResourceNotFoundException.class, () -> countryService.update(-1L, country));
}
@Test
void updateTestSuccess() {
var countryToUpdate = new Country();
countryToUpdate.setId(1L);
countryToUpdate.setName("France");
var updatedCountryRequest = new Country();
updatedCountryRequest.setName("Switzerland");
when(countryRepository.findById(1L))
.thenReturn(Optional.of(countryToUpdate));
when(countryRepository.save(countryToUpdate))
.thenReturn(updatedCountryRequest);
var updatedCountry = countryService.update(1L, updatedCountryRequest);
assertEquals(countryToUpdate.getName(), updatedCountry.getName());
assertEquals(countryToUpdate, updatedCountry);
}
}
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