From ab409115b072b2dfa68fa7571278096772a73d10 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 21:24:46 +0100 Subject: [PATCH] refactor make reservation --- .../de/ci/exam/project/FlightReservation.java | 3 +-- .../fulda/de/ci/exam/project/Itinerary.java | 16 ++++++------- .../de/ci/exam/project/ItineraryTest.java | 23 +++++++++++++------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java index 2ccfa34..d110357 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java @@ -20,11 +20,10 @@ public class FlightReservation { this.status = status; } - public FlightReservation() { + public FlightReservation(){ } - public String getReservationNumber() { return reservationNumber; } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java b/src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java index cdfe540..2719856 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java @@ -1,6 +1,7 @@ package hs.fulda.de.ci.exam.project; import java.util.Date; +import java.util.HashMap; import java.util.List; public class Itinerary { @@ -15,10 +16,6 @@ public class Itinerary { this.creationDate = creationDate; } - public List getReservations(){ - return flightReservationRepository.findAll(); - } - public float makeDiscount(float fare, float discountRate){ fare = fare - (fare * discountRate/100); return fare; @@ -41,13 +38,14 @@ public class Itinerary { return false; } - public boolean makeReservation(Passenger passenger){ + public FlightReservation makeReservation(FlightSeat flightSeat, Passenger passenger){ + HashMap seatHashMap= new HashMap(); + seatHashMap.put(passenger, flightSeat); validatePassengerDetails(passenger); - List flightReservations = getReservations(); FlightReservation flight = new FlightReservation(); - flightReservationRepository.save(flight); - - return false; + flight.setSeatMap(seatHashMap); + flight.setStatus(ReservationStatus.Requested); + return flight; } public Airport getFinal_airport() { diff --git a/src/test/java/hs/fulda/de/ci/exam/project/ItineraryTest.java b/src/test/java/hs/fulda/de/ci/exam/project/ItineraryTest.java index 3b9f9da..c430585 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/ItineraryTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/ItineraryTest.java @@ -9,9 +9,11 @@ import org.junit.jupiter.params.provider.MethodSource; import org.mockito.InjectMocks; import org.mockito.Mock; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -19,22 +21,22 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.junit.jupiter.api.Assertions.assertEquals; - @ExtendWith(MockitoExtension.class) public class ItineraryTest { final Address address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany"); final Airport airport_fr = new Airport("Fraport", address1, "1234"); final Airport airport_be = new Airport("Berlin", address1, "5678"); + final Itinerary item2 = new Itinerary(airport_be, airport_fr, new Date()); @InjectMocks final Customer person1 = new Customer("Max Mustermann", address1, "max.mustermann@gmail.com", "015147890206"); final Itinerary item1 = new Itinerary(airport_fr, airport_be, new Date()); - final Itinerary item2 = new Itinerary(airport_be, airport_fr, new Date()); @Mock private ItineraryRepository itineraryRepo; + @Mock + private FlightReservationRepository flightReservationRepository; @Test public void test_makePayment() { @@ -62,7 +64,7 @@ public class ItineraryTest { public void shouldThrowRuntimeExceptionWhenNameIsNull() { String msg = null; try { - item1.makeReservation(new Passenger("", "Ab", new Date())); + item1.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("", "Ab", new Date())); } catch (Exception e) { msg = e.getMessage(); } @@ -75,7 +77,7 @@ public class ItineraryTest { public void shouldThrowRuntimeExceptionWhenPhoneNumberIsNull(String passportNumber) { String msg = null; try { - item1.makeReservation(new Passenger("John", passportNumber, new Date())); + item1.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("John", passportNumber, new Date())); } catch (Exception e) { msg = e.getMessage(); } @@ -90,7 +92,14 @@ public class ItineraryTest { @DisplayName("Should Not Make Reservation when Passenger Birthdate is null") public void shouldThrowRuntimeExceptionWhenDateIsNull() { assertThrows(RuntimeException.class, () -> { - item1.makeReservation(new Passenger("John", "AB127389", null)); + item1.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("John", "AB127389", null)); }); } + + @Test + public void test_makeReservation() { + FlightReservation flightreservation = item2.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("John", "AB2389", new Date())); + assertNotNull(flightreservation); + assertEquals(flightreservation.getStatus(), ReservationStatus.Requested); + } } \ No newline at end of file