From 5f23abf6fe8efea20cd6853a75c14090bced7490 Mon Sep 17 00:00:00 2001 From: Imron Date: Wed, 16 Feb 2022 13:01:17 +0100 Subject: [PATCH] email validation --- .../fulda/de/ci/exam/project/EmailNotification.java | 12 +++++++++++- .../fulda/de/ci/exam/project/NotificationTest.java | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java b/src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java index ca37a9d..1e3352e 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java @@ -1,5 +1,7 @@ package hs.fulda.de.ci.exam.project; +import java.util.regex.Pattern; + public class EmailNotification extends Notification { String email; String content; @@ -19,6 +21,14 @@ public class EmailNotification extends Notification { } private boolean isValidEmail(String email) { - return true; + String regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" + + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; + return patternMatches(email, 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 a23b63b..39fe4d1 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 @@ -6,6 +6,13 @@ import static hs.fulda.de.ci.exam.project.ReservationStatus.Confirmed; import static org.junit.jupiter.api.Assertions.assertTrue; public class NotificationTest { + @Test + public void emailValidiationShouldReturnTrueForValidEmail(){ + EmailNotification email = new EmailNotification("test@gmail.com", "HelloWorld"); + boolean result = email.sendNotification(); + assertTrue(result); + } + @Test public void emailNotificationShouldBeSentWhenFlighReservationCompleted() { FlightReservation reservation = new FlightReservation();