From dac5318ef9f368d66717ad93bee6efa2338ee104 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 19:35:06 +0100 Subject: [PATCH] should not make reservation when passenger passport number is invalid --- .../fulda/de/ci/exam/project/Itinerary.java | 1 + .../fulda/de/ci/exam/project/Passenger.java | 15 +++++++++++ .../de/ci/exam/project/ItineraryTest.java | 25 ++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) 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 81d9a26..71ebcb9 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 @@ -90,6 +90,7 @@ public class Itinerary { public void validatePassengerDetails(Passenger passenger){ passenger.validateName(); + passenger.validatePassportNumber(); } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Passenger.java b/src/main/java/hs/fulda/de/ci/exam/project/Passenger.java index 69b2093..d4ea2dd 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Passenger.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Passenger.java @@ -1,6 +1,8 @@ package hs.fulda.de.ci.exam.project; import java.util.Date; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Passenger { private String name; @@ -14,7 +16,20 @@ public class Passenger { } public void validateName() { + if(name.isBlank()) { throw new RuntimeException(("Name cannot be null or empty")); + } + } + + public void validatePassportNumber() { + Pattern pattern = Pattern.compile("^(?!^0+$)[a-zA-Z0-9]{3,20}$"); + Matcher matcher = pattern.matcher(this.passportNumber); + if(passportNumber.isBlank()){ + throw new RuntimeException("Passport number cannot be blank"); + } + if(!matcher.matches()){ + throw new RuntimeException("Passport number cannot be null or empty"); + } } 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 bbaa039..dc2a480 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 @@ -4,6 +4,8 @@ import org.junit.Assert; 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.MethodSource; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -11,7 +13,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.*; import org.mockito.junit.jupiter.MockitoExtension; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -60,6 +66,23 @@ public class ItineraryTest { } catch (Exception e) { msg = e.getMessage(); } - assertEquals(msg, "Name cannot be null or empty"); + assertEquals("Name cannot be null or empty", msg); + } + + @DisplayName("Should Not Make Reservation when Passenger passport number is invalid") + @ParameterizedTest + @MethodSource("passportNumberList") + public void shouldThrowRuntimeExceptionWhenPhoneNumberIsNull(String passportNumber){ + String msg = null; + try { + item1.makeReservation(new Passenger("John", passportNumber, new Date())); + } catch (Exception e) { + msg = e.getMessage(); + } + assertEquals("Passport number cannot be null or empty", msg); + } + + private static List passportNumberList() { + return Arrays.asList("A2", "000000", "AB231837%8"); } } \ No newline at end of file