From e5dcacd334ef2e58f0d440e0082e72174dd53251 Mon Sep 17 00:00:00 2001 From: Imron Date: Wed, 16 Feb 2022 13:21:17 +0100 Subject: [PATCH] sms validation for false number --- .../fulda/de/ci/exam/project/SmsNotification.java | 14 +++++++++++++- .../fulda/de/ci/exam/project/NotificationTest.java | 11 +++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java b/src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java index 2be45eb..73f5d50 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java @@ -1,8 +1,11 @@ package hs.fulda.de.ci.exam.project; +import java.util.regex.Pattern; + public class SmsNotification extends Notification { String to; String content; + public SmsNotification(String to, String content) { super(); this.to = to; @@ -20,6 +23,15 @@ public class SmsNotification extends Notification { } private boolean isValidEmail(String to) { - return true; + String regexPattern = "(\\(?([\\d \\-\\)\\–\\+\\/\\(]+){6,}\\)?([ .\\-–\\/]?)([\\d]+))"; + return patternMatches(to, regexPattern); + } + + public static boolean patternMatches(String emailAddress, String regexPattern) { + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); } + + } diff --git a/src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java b/src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java index b57b57f..fb77bb6 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java @@ -29,7 +29,7 @@ public class NotificationTest { } @Test - public void smsNotificationShouldBeSentWhenSmsTypeIsChosen(){ + public void smsNotificationShouldBeSentWhenSmsTypeIsChosen() { FlightReservation reservation = new FlightReservation(); reservation.setStatus(Confirmed); boolean result = reservation.notifyUser("sms", "01788370107", "Reservation is successfull"); @@ -37,9 +37,16 @@ public class NotificationTest { } @Test - public void smsIsSentWhenNumberIsValid(){ + public void smsIsSentWhenNumberIsValid() { SmsNotification sms = new SmsNotification("01788370107", "Reservation confirmed"); boolean result = sms.sendNotification(); assertTrue(result); } + + @Test + public void smsShouldNotBeSentWhenNumberIsInvalid() { + SmsNotification sms = new SmsNotification("123", "Some Wrong Number"); + boolean result = sms.sendNotification(); + assertFalse(result); + } }