You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
4.9 KiB
151 lines
4.9 KiB
package hs.fulda.de.ci.exam.project;
|
|
|
|
import org.junit.Before;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
import org.mockito.InjectMocks;
|
|
import org.mockito.Mock;
|
|
import org.mockito.MockitoAnnotations;
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
public class AdminTest {
|
|
|
|
Address address_fr;
|
|
Airport airport_fr;
|
|
Address address1_ist;
|
|
Airport airport1_ist;
|
|
Flight flight1;
|
|
Address address1;
|
|
Aircraft aircraft;
|
|
|
|
@BeforeEach
|
|
public void setup() {
|
|
aircraft = new Aircraft("Boeing", "787", 2003);
|
|
address_fr = new Address("Frankfurt str", "Frankfurt", "Hessen", "63023", "Germany");
|
|
airport_fr = new Airport("Fraport", address_fr, "1234");
|
|
address1_ist = new Address("Istanbul str", "Istanbul", "Fatih", "9019", "Turkey");
|
|
airport1_ist = new Airport("Istanbul", address1_ist, "5678");
|
|
flight1 = new Flight("1", airport_fr, airport1_ist, 140);
|
|
address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany");
|
|
}
|
|
@InjectMocks
|
|
Admin admin = new Admin("Max Muster", address1, "example@gmail.com", "012345678" );
|
|
|
|
@Mock
|
|
private AccountRepository accountRepository;
|
|
|
|
@Test
|
|
public void test_blockUser() {
|
|
ArrayList<Account> users = new ArrayList<>();
|
|
Account user = new Account("john5", "password", Account.AccountStatus.ACTIVE);
|
|
when(accountRepository.checkIfAccountAlreadyExist(user)).thenReturn(true);
|
|
|
|
admin.blockUser(user);
|
|
|
|
assertEquals(Account.AccountStatus.BLOCKED, user.getStatus(), "Status successfully changed");
|
|
}
|
|
|
|
@Mock
|
|
private FlightRepository flightRepository;
|
|
|
|
@Test
|
|
public void test_addFlight_returnsNewFlight() throws IOException {
|
|
|
|
try {
|
|
when(flightRepository.save(any(Flight.class))).thenReturn(true);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
try {
|
|
admin.addFlight("1", airport_fr, airport1_ist, 140);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
assertNotNull(admin.addFlight("1", airport_fr, airport1_ist, 140));
|
|
}
|
|
@Test
|
|
public void testAddFlight_throwsException() throws IOException {
|
|
|
|
when(flightRepository.save(any(Flight.class))).thenThrow(IOException.class);
|
|
assertThrows(IOException.class, () -> {
|
|
admin.addFlight("1", airport_fr, airport1_ist, 140);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void shouldThrowRuntimeExceptionWhenNull(){
|
|
|
|
assertThrows(RuntimeException.class, () -> {
|
|
admin.addFlight(null, airport_fr, airport1_ist, 140);
|
|
});
|
|
assertThrows(RuntimeException.class, () -> {
|
|
admin.addFlight("1", null, airport1_ist, 140);
|
|
});
|
|
assertThrows(RuntimeException.class, () -> {
|
|
admin.addFlight("1", airport_fr, null, 140);
|
|
});
|
|
assertThrows(RuntimeException.class, () -> {
|
|
admin.addFlight("1", airport_fr, airport1_ist, -40);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void test_searchFlightThrowsExceptionWhenBlank(){
|
|
when(flightRepository.findFlightByFlightNumber(any(String.class))).thenReturn("");
|
|
assertThrows(RuntimeException.class, () -> {
|
|
admin.searchFlights("1");
|
|
});
|
|
}
|
|
|
|
@Mock
|
|
private AircraftRepository aircraftRepository;
|
|
|
|
@Test
|
|
public void test_addAircraft_returnsNewAircraft() throws IOException {
|
|
|
|
try {
|
|
when(aircraftRepository.save(any(Aircraft.class))).thenReturn(true);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
assertNotNull(admin.addAircraft("Boeing", "787", 2003));
|
|
}
|
|
|
|
@Test
|
|
public void testAddAircraft_throwsException() throws IOException {
|
|
|
|
when(aircraftRepository.save(any(Aircraft.class))).thenThrow(IOException.class);
|
|
assertThrows(IOException.class, () -> {
|
|
admin.addAircraft("Boeing", "787", 2003);
|
|
});
|
|
}
|
|
@Test
|
|
public void addAircraftShouldThrowRuntimeExceptionWhenNull(){
|
|
|
|
assertThrows(RuntimeException.class, () -> {
|
|
admin.addAircraft(null, "787", 2003);
|
|
});
|
|
assertThrows(RuntimeException.class, () -> {
|
|
admin.addAircraft("Boeing", "", 2003);
|
|
});
|
|
assertThrows(RuntimeException.class, () -> {
|
|
admin.addAircraft("Boeing", "787", -2003);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void test_searchAircraft(){
|
|
when(aircraftRepository.findAircraftByAircraftName(any(String.class))).thenReturn(aircraft.toString());
|
|
assertNotNull(admin.searchAircraft("Boeing"));
|
|
assertEquals(admin.searchAircraft("Boeing"), aircraft.toString());
|
|
}
|
|
}
|