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 395007a..cd4cb6b 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 @@ -6,12 +6,18 @@ public class Admin extends Person{ AccountRepository accountRepository; FlightRepository flightRepository; + AircraftRepository aircraftRepository; public Admin(String name, Address address, String email, String phone) { super(name, address, email, phone); } - public void addAircraft(){ - + public Aircraft addAircraft(String name, String model, int manufacturerYear) throws IOException { + if(name.isBlank()) throw new RuntimeException("Name cannot be null or empty"); + if(model.isBlank()) throw new RuntimeException("Model cannot be null or empty"); + if(manufacturerYear < 0) throw new RuntimeException("Year cannot be zero"); + Aircraft aircraft = new Aircraft(name, model, manufacturerYear); + aircraftRepository.save(aircraft); + return aircraft; } public String searchFlights(String flightNumber){ diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Aircraft.java b/src/main/java/hs/fulda/de/ci/exam/project/Aircraft.java index 50c3b08..64489d6 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Aircraft.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Aircraft.java @@ -33,4 +33,9 @@ public class Aircraft { public void addFlight(Flight flight) { flights.add(flight); } + + @Override + public String toString() { + return "Aircraft = {" + "name=" + name + '\'' + ", model=" + model + ", manufacturingYear='" + manYear +'}'; + } } 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 659ad74..a78c0dc 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 @@ -7,9 +7,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.mockito.invocation.InvocationOnMock; import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.stubbing.Answer; import java.io.IOException; import java.util.ArrayList; @@ -26,9 +24,11 @@ public class AdminTest { 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"); @@ -39,11 +39,6 @@ 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; @@ -118,4 +113,17 @@ public class AdminTest { }); } + @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)); + } }