From 7debdccde60624b9e61d3ffacac5b4f8458037e4 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Wed, 9 Feb 2022 21:49:05 +0100 Subject: [PATCH] add flight as an admin mockito test --- .../hs/fulda/de/ci/exam/project/Admin.java | 17 ++++++++- .../hs/fulda/de/ci/exam/project/Flight.java | 4 ++ .../fulda/de/ci/exam/project/AdminTest.java | 37 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Admin.java b/src/main/java/hs/fulda/de/ci/exam/project/Admin.java index 84cea28..395007a 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Admin.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Admin.java @@ -1,5 +1,7 @@ package hs.fulda.de.ci.exam.project; +import java.io.IOException; + public class Admin extends Person{ 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 ); flightRepository.save(flight); + return flight; } public boolean blockUser(Account user){ diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Flight.java b/src/main/java/hs/fulda/de/ci/exam/project/Flight.java index 2859020..1d09e76 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Flight.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Flight.java @@ -45,4 +45,8 @@ public class Flight { return durationInMinutes; } + @Override + public String toString() { + return "Flight = {" + "flightNumber=" + flightNumber + '\'' + ", departure=" + departure + ", arrival='" + arrival + ", durationInMinutes=" + durationInMinutes + '}'; + } } diff --git a/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java b/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java index 47bde88..bbe7ff2 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java @@ -1,13 +1,20 @@ package hs.fulda.de.ci.exam.project; +import org.junit.Before; 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.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; 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; @ExtendWith(MockitoExtension.class) @@ -18,6 +25,11 @@ public class AdminTest { @InjectMocks Admin admin = new Admin("Max Muster", address1, "example@gmail.com", "012345678" ); + @Before + public void setUp() throws Exception { + + MockitoAnnotations.initMocks(this); + } @Mock private AccountRepository accountRepository; @@ -31,4 +43,29 @@ public class AdminTest { 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)); + } }