Browse Source

should not make reservation when passenger passport number is invalid

feature-pr-PersonAndItinerary
Sona Markosyan 3 years ago
parent
commit
dac5318ef9
  1. 1
      src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java
  2. 15
      src/main/java/hs/fulda/de/ci/exam/project/Passenger.java
  3. 25
      src/test/java/hs/fulda/de/ci/exam/project/ItineraryTest.java

1
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();
}

15
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,8 +16,21 @@ 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");
}
}
}

25
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<String> passportNumberList() {
return Arrays.asList("A2", "000000", "AB231837%8");
}
}
Loading…
Cancel
Save