From 0c42867e75021ce3160d51ab6a2da03898fe2351 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Tue, 8 Feb 2022 17:50:53 +0100 Subject: [PATCH 1/5] create Itinerary method testing --- .../de/ci/exam/project/FrontDeskOfficer.java | 41 +++++++++++++++++++ .../fulda/de/ci/exam/project/Itinerary.java | 9 ++++ .../ci/exam/project/FrontDeskOfficerTest.java | 26 ++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/main/java/hs/fulda/de/ci/exam/project/FrontDeskOfficer.java create mode 100644 src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FrontDeskOfficer.java b/src/main/java/hs/fulda/de/ci/exam/project/FrontDeskOfficer.java new file mode 100644 index 0000000..df0bd9a --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/FrontDeskOfficer.java @@ -0,0 +1,41 @@ +package hs.fulda.de.ci.exam.project; + +import java.util.Collection; +import java.util.Date; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class FrontDeskOfficer extends Person{ + + Map itineraryList = new ConcurrentHashMap(); + + public FrontDeskOfficer(String name, Address address, String email, String phone) { + super(name, address, email, phone); + } + + public void createItinerary(Airport start_airport, Airport final_airport, Date date){ + Itinerary itinerary = new Itinerary(start_airport, final_airport, date); + validateItinerary(itinerary); + checkIfItineraryAlreadyExist(itinerary); + itineraryList.put(generateKey(itinerary), itinerary); + } + + public Collection getAllItineraries() { + return itineraryList.values(); + } + + private void checkIfItineraryAlreadyExist(Itinerary itinerary){ + if(itineraryList.containsKey(generateKey(itinerary))){ + throw new RuntimeException("Itinerary Already Exists"); + } + } + + private String generateKey(Itinerary itinerary) { + return String.format("%s-%s", itinerary.getStart_airport(), itinerary.getFinal_airport()); + } + + public void validateItinerary(Itinerary itinerary){ + itinerary.validateStartAirport(); + itinerary.validateFinalAirport(); + } +} 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 d91c9fe..7663290 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 @@ -73,5 +73,14 @@ public class Itinerary { this.start_airport = start_airport; } + public void validateStartAirport() { + if(this.start_airport.getName().isBlank()) + throw new RuntimeException(("Starting Airport Cannot be null or empty")); + } + + public void validateFinalAirport() { + if(this.final_airport.getName().isBlank()) + throw new RuntimeException(("Destination Airport Cannot be null or empty")); + } } diff --git a/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java new file mode 100644 index 0000000..2293945 --- /dev/null +++ b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java @@ -0,0 +1,26 @@ +package hs.fulda.de.ci.exam.project; + +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; + +import java.util.Date; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class FrontDeskOfficerTest { + @Test + public void shouldCreateItinerary() { + 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"); + FrontDeskOfficer frontDeskOfficer = new FrontDeskOfficer("John", address1, "example@email.com", "0151238967"); + + frontDeskOfficer.createItinerary(airport_fr, airport_be, new Date()); + assertFalse(frontDeskOfficer.getAllItineraries().isEmpty()); + assertEquals(1, frontDeskOfficer.getAllItineraries().size()); + assertTrue(frontDeskOfficer.getAllItineraries().stream().filter(itinerary -> itinerary.getStart_airport().equals(airport_fr) && + itinerary.getFinal_airport().equals(airport_be)).findAny().isPresent()); + } +} From 32c81ede92b0b1cedde845cb34d56f876f398e9c Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Tue, 8 Feb 2022 18:38:02 +0100 Subject: [PATCH 2/5] should not create itinerary when starting airport is null --- .../ci/exam/project/FrontDeskOfficerTest.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java index 2293945..12cd3b4 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java @@ -1,26 +1,34 @@ package hs.fulda.de.ci.exam.project; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import java.util.Date; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class FrontDeskOfficerTest { + + 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 FrontDeskOfficer frontDeskOfficer = new FrontDeskOfficer("John", address1, "example@email.com", "0151238967"); + @Test public void shouldCreateItinerary() { - 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"); - FrontDeskOfficer frontDeskOfficer = new FrontDeskOfficer("John", address1, "example@email.com", "0151238967"); - frontDeskOfficer.createItinerary(airport_fr, airport_be, new Date()); assertFalse(frontDeskOfficer.getAllItineraries().isEmpty()); assertEquals(1, frontDeskOfficer.getAllItineraries().size()); assertTrue(frontDeskOfficer.getAllItineraries().stream().filter(itinerary -> itinerary.getStart_airport().equals(airport_fr) && itinerary.getFinal_airport().equals(airport_be)).findAny().isPresent()); } + @Test + @DisplayName("Should Not Create Itinerary when Starting Airport is null") + public void shouldThrowRuntimeExceptionWhenStartAirportIsNull(){ + assertThrows(RuntimeException.class, () -> { + frontDeskOfficer.createItinerary(null, airport_be, new Date()); + }); + } } From 4f8b1478e5a5db867b200c6afed5e68b9ba52353 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Tue, 8 Feb 2022 18:39:42 +0100 Subject: [PATCH 3/5] should not create itinerary when final airport is null --- .../hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java index 12cd3b4..e4e48f4 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java @@ -31,4 +31,12 @@ public class FrontDeskOfficerTest { frontDeskOfficer.createItinerary(null, airport_be, new Date()); }); } + + @Test + @DisplayName("Should Not Create Itinerary when Destination Airport is null") + public void shouldThrowRuntimeExceptionWhenFinalAirportIsNull(){ + assertThrows(RuntimeException.class, () -> { + frontDeskOfficer.createItinerary(airport_fr, null, new Date()); + }); + } } From 3b1430d10f87c254824262b8fb5e42a222dd1ca8 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Tue, 8 Feb 2022 18:44:12 +0100 Subject: [PATCH 4/5] validate creation date --- .../java/hs/fulda/de/ci/exam/project/FrontDeskOfficer.java | 1 + src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java | 4 ++++ .../hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FrontDeskOfficer.java b/src/main/java/hs/fulda/de/ci/exam/project/FrontDeskOfficer.java index df0bd9a..941be16 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/FrontDeskOfficer.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/FrontDeskOfficer.java @@ -37,5 +37,6 @@ public class FrontDeskOfficer extends Person{ public void validateItinerary(Itinerary itinerary){ itinerary.validateStartAirport(); itinerary.validateFinalAirport(); + itinerary.validateCreationDate(); } } 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 7663290..00f9b5a 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 @@ -82,5 +82,9 @@ public class Itinerary { if(this.final_airport.getName().isBlank()) throw new RuntimeException(("Destination Airport Cannot be null or empty")); } + public void validateCreationDate() { + if(this.creationDate.equals(null)) + throw new RuntimeException(("Creation Date should not be null or empty")); + } } diff --git a/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java index e4e48f4..4488bd3 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java @@ -36,7 +36,7 @@ public class FrontDeskOfficerTest { @DisplayName("Should Not Create Itinerary when Destination Airport is null") public void shouldThrowRuntimeExceptionWhenFinalAirportIsNull(){ assertThrows(RuntimeException.class, () -> { - frontDeskOfficer.createItinerary(airport_fr, null, new Date()); + frontDeskOfficer.createItinerary(airport_fr, airport_be, null); }); } } From 17942549338ddcbf627e088fa53fc98b29b9e943 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Tue, 8 Feb 2022 19:09:43 +0100 Subject: [PATCH 5/5] refactor FrontDeskOfficerTest --- .../ci/exam/project/FrontDeskOfficerTest.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java index 4488bd3..cfb742c 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/FrontDeskOfficerTest.java @@ -1,21 +1,32 @@ package hs.fulda.de.ci.exam.project; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import org.junit.Before; +import org.junit.jupiter.api.*; import org.mockito.InjectMocks; import java.util.Date; import static org.junit.jupiter.api.Assertions.*; +@TestInstance(TestInstance.Lifecycle.PER_CLASS) public class FrontDeskOfficerTest { - 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 FrontDeskOfficer frontDeskOfficer = new FrontDeskOfficer("John", address1, "example@email.com", "0151238967"); + Address address1; + Airport airport_fr; + Airport airport_be; + FrontDeskOfficer frontDeskOfficer; + @BeforeAll + public void setupAll(){ + System.out.println("Should Print Before All Tests"); + } + @BeforeEach + public void setup(){ + address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany"); + airport_fr = new Airport("Fraport", address1, "1234"); + airport_be = new Airport("Berlin", address1, "5678"); + frontDeskOfficer = new FrontDeskOfficer("John", address1, "example@email.com", "0151238967"); + } @Test public void shouldCreateItinerary() { frontDeskOfficer.createItinerary(airport_fr, airport_be, new Date());