Browse Source

add flight as an admin mockito test

feature-pr-Admin
Sona Markosyan 3 years ago
parent
commit
7debdccde6
  1. 17
      src/main/java/hs/fulda/de/ci/exam/project/Admin.java
  2. 4
      src/main/java/hs/fulda/de/ci/exam/project/Flight.java
  3. 37
      src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java

17
src/main/java/hs/fulda/de/ci/exam/project/Admin.java

@ -1,5 +1,7 @@
package hs.fulda.de.ci.exam.project; package hs.fulda.de.ci.exam.project;
import java.io.IOException;
public class Admin extends Person{ public class Admin extends Person{
AccountRepository accountRepository; AccountRepository accountRepository;
@ -12,9 +14,22 @@ public class Admin extends Person{
} }
public void addFlight(String flightNumber, Airport departure, Airport arrival, int durationInMinutes ){
public String searchFlights(String flightNumber){
String flightDetails = flightRepository.findFlightByFlightNumber(flightNumber);
if(flightDetails.isBlank()){
throw new RuntimeException("Flight does not exist.");
}
return flightDetails;
}
public Flight addFlight(String flightNumber, Airport departure, Airport arrival, int durationInMinutes ) throws IOException {
if(flightNumber.isBlank()) throw new RuntimeException("FlightNumber cannot be null or empty");
if(departure.equals(null)) throw new RuntimeException("Departure cannot be null or empty");
if(arrival.equals(null)) throw new RuntimeException("Arrival cannot be null or empty");
if(durationInMinutes < 0) throw new RuntimeException("Duration cannot be negative");
Flight flight = new Flight(flightNumber, departure, arrival, durationInMinutes ); Flight flight = new Flight(flightNumber, departure, arrival, durationInMinutes );
flightRepository.save(flight); flightRepository.save(flight);
return flight;
} }
public boolean blockUser(Account user){ public boolean blockUser(Account user){

4
src/main/java/hs/fulda/de/ci/exam/project/Flight.java

@ -45,4 +45,8 @@ public class Flight {
return durationInMinutes; return durationInMinutes;
} }
@Override
public String toString() {
return "Flight = {" + "flightNumber=" + flightNumber + '\'' + ", departure=" + departure + ", arrival='" + arrival + ", durationInMinutes=" + durationInMinutes + '}';
}
} }

37
src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java

@ -1,13 +1,20 @@
package hs.fulda.de.ci.exam.project; package hs.fulda.de.ci.exam.project;
import org.junit.Before;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
@ -18,6 +25,11 @@ public class AdminTest {
@InjectMocks @InjectMocks
Admin admin = new Admin("Max Muster", address1, "example@gmail.com", "012345678" ); Admin admin = new Admin("Max Muster", address1, "example@gmail.com", "012345678" );
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Mock @Mock
private AccountRepository accountRepository; private AccountRepository accountRepository;
@ -31,4 +43,29 @@ public class AdminTest {
assertEquals(Account.AccountStatus.BLOCKED, user.getStatus(), "Status successfully changed"); assertEquals(Account.AccountStatus.BLOCKED, user.getStatus(), "Status successfully changed");
} }
@Mock
private FlightRepository flightRepository;
@Test
public void test_addFlight_returnsNewFlight() throws IOException {
Address address_fr = new Address("Frankfurt str", "Frankfurt", "Hessen", "63023", "Germany");
Airport airport_fr = new Airport("Fraport", address_fr, "1234");
Address address1_ist = new Address("Istanbul str", "Istanbul", "Fatih", "9019", "Turkey");
Airport airport1_ist = new Airport("Istanbul", address1_ist, "5678");
Flight flight1 = new Flight("1", airport_fr, airport1_ist, 140);
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));
}
} }
Loading…
Cancel
Save