Browse Source

search all flights by criteria

feature-pr-PersonAndItinerary
Sona Markosyan 3 years ago
parent
commit
92b8141bd6
  1. 4
      src/main/java/hs/fulda/de/ci/exam/project/Airport.java
  2. 4
      src/main/java/hs/fulda/de/ci/exam/project/Flight.java
  3. 20
      src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java
  4. 8
      src/main/java/hs/fulda/de/ci/exam/project/Person.java
  5. 7
      src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java
  6. 38
      src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java

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

@ -15,6 +15,10 @@ public class Airport {
this.code = code;
}
public Airport() {
}
public String getName() {
return this.name;
}

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

@ -18,6 +18,10 @@ public class Flight {
this.durationInMinutes = durationInMinutes;
}
public Flight() {
}
public HashSet<FlightInstance> getInstances() {
return this.flightInstances;
}

20
src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java

@ -1,6 +1,7 @@
package hs.fulda.de.ci.exam.project;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class FlightRepository {
@ -29,4 +30,23 @@ public class FlightRepository {
}
return "";
}
public ArrayList<String> findbyDepartureArrival(Airport departure, Airport arrival) {
ArrayList<String> flights = new ArrayList<>();
File file = new File("flights.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.matches("(.*)"+departure+"(.*)") && line.matches("(.*)"+arrival+"(.*)")) {
flights.add(line);
}
}
} catch(FileNotFoundException e) {
System.out.println("There are no flights added yet. Please add a flight");
}
return flights;
}
}

8
src/main/java/hs/fulda/de/ci/exam/project/Person.java

@ -79,6 +79,14 @@ public class Person {
return flightDetails;
}
public ArrayList<String> searchAllFlightsByCriteria(Airport departure, Airport arrival){
ArrayList<String> flights = flightRepository.findbyDepartureArrival(departure, arrival);
if(flights.isEmpty()){
throw new RuntimeException("Flights do not exist.");
}
return flights;
}
public ArrayList<Itinerary> getItineraries() {
return itineraryRepository.findAll();
}

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

@ -98,13 +98,6 @@ public class AdminTest {
});
}
@Test
public void test_searchFlight(){
when(flightRepository.findFlightByFlightNumber(any(String.class))).thenReturn(flight1.toString());
assertNotNull(admin.searchFlights("1"));
assertEquals(admin.searchFlights("1"), flight1.toString());
}
@Test
public void test_searchFlightThrowsExceptionWhenBlank(){
when(flightRepository.findFlightByFlightNumber(any(String.class))).thenReturn("");

38
src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java

@ -2,15 +2,37 @@ package hs.fulda.de.ci.exam.project;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.ArrayList;
import static org.assertj.core.api.Assertions.assertThat;
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)
public class PersonTest {
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");
Address address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany");
@InjectMocks
Person person1 = new Person("Max Mustermann", address1, "max.mustermann@gmail.com", "015147890206");
@Mock
private FlightRepository flightRepository;
@Test
void test_getName() {
assertThat(person1.getName()).describedAs("get person name").isEqualTo("Max Mustermann");
@ -29,4 +51,20 @@ public class PersonTest {
void test_getPhone() {
assertThat(person1.getPhone()).describedAs("get person phone").isEqualTo("015147890206");
}
@Test
public void test_searchAllFlightsByCriteria(){
ArrayList<String> flights = new ArrayList<>();
flights.add("Flight1");
when(flightRepository.findbyDepartureArrival(airport_fr, airport1_ist)).thenReturn(flights);
assertNotNull(person1.searchAllFlightsByCriteria(airport_fr, airport1_ist));
}
@Test
public void test_searchFlight(){
Flight flight1 = new Flight("1", airport_fr, airport1_ist, 140);
when(flightRepository.findFlightByFlightNumber(any(String.class))).thenReturn(flight1.toString());
assertNotNull(person1.searchFlights("1"));
assertEquals(person1.searchFlights("1"), flight1.toString());
}
}
Loading…
Cancel
Save