From 8277f7fb6078e8ef76b67547e2bbfcbf7c64c432 Mon Sep 17 00:00:00 2001 From: Imron Date: Wed, 16 Feb 2022 12:52:07 +0100 Subject: [PATCH 1/7] email notification --- .../de/ci/exam/project/EmailNotification.java | 24 +++++++++++++++++++ .../de/ci/exam/project/FlightReservation.java | 8 +++++++ .../de/ci/exam/project/Notification.java | 5 ++++ .../de/ci/exam/project/NotificationTest.java | 16 +++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java create mode 100644 src/main/java/hs/fulda/de/ci/exam/project/Notification.java create mode 100644 src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java 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 new file mode 100644 index 0000000..ca37a9d --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java @@ -0,0 +1,24 @@ +package hs.fulda.de.ci.exam.project; + +public class EmailNotification extends Notification { + String email; + String content; + public EmailNotification(String email, String content) { + super(); + this.email = email; + this.content = content; + } + + @Override + public boolean sendNotification() { + if (isValidEmail(email)) { + System.out.println("Email is sent to " + email); + return true; + } + return false; + } + + private boolean isValidEmail(String email) { + return true; + } +} diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java index f288009..eaef200 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java @@ -60,4 +60,12 @@ public class FlightReservation { credit_card_payment.status = Completed; status = Confirmed; } + + public boolean notifyUser(String type, String email, String content) { + Notification notification = null; + switch (type) { + case "email": notification = new EmailNotification(email, content); + } + return notification.sendNotification(); + } } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Notification.java b/src/main/java/hs/fulda/de/ci/exam/project/Notification.java new file mode 100644 index 0000000..4821060 --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/Notification.java @@ -0,0 +1,5 @@ +package hs.fulda.de.ci.exam.project; + +public abstract class Notification { + public abstract boolean sendNotification(); +} 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 new file mode 100644 index 0000000..a23b63b --- /dev/null +++ b/src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java @@ -0,0 +1,16 @@ +package hs.fulda.de.ci.exam.project; + +import org.junit.Test; + +import static hs.fulda.de.ci.exam.project.ReservationStatus.Confirmed; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class NotificationTest { + @Test + public void emailNotificationShouldBeSentWhenFlighReservationCompleted() { + FlightReservation reservation = new FlightReservation(); + reservation.setStatus(Confirmed); + boolean result = reservation.notifyUser("email", "test@gmail.com","Reservation is Confirmed!"); + assertTrue(result); + } +} From 5f23abf6fe8efea20cd6853a75c14090bced7490 Mon Sep 17 00:00:00 2001 From: Imron Date: Wed, 16 Feb 2022 13:01:17 +0100 Subject: [PATCH 2/7] 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(); From 0801e2e8aac6e5a2986a3229f4fe27bc68e9dcf8 Mon Sep 17 00:00:00 2001 From: Imron Date: Wed, 16 Feb 2022 13:03:21 +0100 Subject: [PATCH 3/7] email validation --- .../hs/fulda/de/ci/exam/project/EmailNotification.java | 1 + .../hs/fulda/de/ci/exam/project/NotificationTest.java | 9 ++++++++- 2 files changed, 9 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 1e3352e..a72d30e 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 @@ -17,6 +17,7 @@ public class EmailNotification extends Notification { System.out.println("Email is sent to " + email); return true; } + System.out.println("Invalid Email Address"); return false; } 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 39fe4d1..2c2fced 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 @@ -3,7 +3,7 @@ package hs.fulda.de.ci.exam.project; import org.junit.Test; import static hs.fulda.de.ci.exam.project.ReservationStatus.Confirmed; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class NotificationTest { @Test @@ -13,6 +13,13 @@ public class NotificationTest { assertTrue(result); } + @Test + public void emailValidiationShouldReturnFalseForInValidEmail(){ + EmailNotification email = new EmailNotification("testgmail.com", "HelloWorld"); + boolean result = email.sendNotification(); + assertFalse(result); + } + @Test public void emailNotificationShouldBeSentWhenFlighReservationCompleted() { FlightReservation reservation = new FlightReservation(); From 7abce6392e8772b93591ade916c0d768d91d6537 Mon Sep 17 00:00:00 2001 From: Imron Date: Wed, 16 Feb 2022 13:10:31 +0100 Subject: [PATCH 4/7] sms notification --- .../de/ci/exam/project/EmailNotification.java | 1 + .../de/ci/exam/project/FlightReservation.java | 11 ++++++++--- .../fulda/de/ci/exam/project/SmsNotification.java | 12 ++++++++++++ .../fulda/de/ci/exam/project/NotificationTest.java | 14 +++++++++++--- 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java 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 a72d30e..0735a28 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 @@ -5,6 +5,7 @@ import java.util.regex.Pattern; public class EmailNotification extends Notification { String email; String content; + public EmailNotification(String email, String content) { super(); this.email = email; diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java index eaef200..55c0bb8 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java @@ -18,7 +18,7 @@ public class FlightReservation { this.status = status; } - public FlightReservation(){ + public FlightReservation() { } @@ -61,10 +61,15 @@ public class FlightReservation { status = Confirmed; } - public boolean notifyUser(String type, String email, String content) { + public boolean notifyUser(String type, String to, String content) { Notification notification = null; switch (type) { - case "email": notification = new EmailNotification(email, content); + case "email": + notification = new EmailNotification(to, content); + break; + case "sms": + notification = new SmsNotification(to, content); + break; } return notification.sendNotification(); } 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 new file mode 100644 index 0000000..82529d6 --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java @@ -0,0 +1,12 @@ +package hs.fulda.de.ci.exam.project; + +public class SmsNotification extends Notification { + public SmsNotification(String to, String content) { + super(); + } + + @Override + public boolean sendNotification() { + return true; + } +} 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 2c2fced..579666f 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 @@ -7,14 +7,14 @@ import static org.junit.jupiter.api.Assertions.*; public class NotificationTest { @Test - public void emailValidiationShouldReturnTrueForValidEmail(){ + public void emailValidiationShouldReturnTrueForValidEmail() { EmailNotification email = new EmailNotification("test@gmail.com", "HelloWorld"); boolean result = email.sendNotification(); assertTrue(result); } @Test - public void emailValidiationShouldReturnFalseForInValidEmail(){ + public void emailValidiationShouldReturnFalseForInValidEmail() { EmailNotification email = new EmailNotification("testgmail.com", "HelloWorld"); boolean result = email.sendNotification(); assertFalse(result); @@ -24,7 +24,15 @@ public class NotificationTest { public void emailNotificationShouldBeSentWhenFlighReservationCompleted() { FlightReservation reservation = new FlightReservation(); reservation.setStatus(Confirmed); - boolean result = reservation.notifyUser("email", "test@gmail.com","Reservation is Confirmed!"); + boolean result = reservation.notifyUser("email", "test@gmail.com", "Reservation is Confirmed!"); + assertTrue(result); + } + + @Test + public void smsNotificationShouldBeSentWhenSmsTypeIsChosen(){ + FlightReservation reservation = new FlightReservation(); + reservation.setStatus(Confirmed); + boolean result = reservation.notifyUser("sms", "01788370107", "Reservation is successfull"); assertTrue(result); } } From f4c587cf42fe0cfb0b2838637fab9f52caa42364 Mon Sep 17 00:00:00 2001 From: Imron Date: Wed, 16 Feb 2022 13:14:35 +0100 Subject: [PATCH 5/7] sms validation --- .../fulda/de/ci/exam/project/SmsNotification.java | 13 +++++++++++++ .../fulda/de/ci/exam/project/NotificationTest.java | 7 +++++++ 2 files changed, 20 insertions(+) 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 82529d6..2be45eb 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,12 +1,25 @@ package hs.fulda.de.ci.exam.project; public class SmsNotification extends Notification { + String to; + String content; public SmsNotification(String to, String content) { super(); + this.to = to; + this.content = content; } @Override public boolean sendNotification() { + if (isValidEmail(to)) { + System.out.println("SMS is sent to " + to); + return true; + } + System.out.println("Invalid Number"); + return false; + } + + private boolean isValidEmail(String to) { return true; } } 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 579666f..b57b57f 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 @@ -35,4 +35,11 @@ public class NotificationTest { boolean result = reservation.notifyUser("sms", "01788370107", "Reservation is successfull"); assertTrue(result); } + + @Test + public void smsIsSentWhenNumberIsValid(){ + SmsNotification sms = new SmsNotification("01788370107", "Reservation confirmed"); + boolean result = sms.sendNotification(); + assertTrue(result); + } } From e5dcacd334ef2e58f0d440e0082e72174dd53251 Mon Sep 17 00:00:00 2001 From: Imron Date: Wed, 16 Feb 2022 13:21:17 +0100 Subject: [PATCH 6/7] 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); + } } From 38cdbc8fbb2753edb07ebd58b1d01be5882d7ae7 Mon Sep 17 00:00:00 2001 From: Imron Date: Wed, 16 Feb 2022 13:27:56 +0100 Subject: [PATCH 7/7] refactor tests --- .../de/ci/exam/project/NotificationTest.java | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) 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 fb77bb6..b1600f2 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 @@ -3,50 +3,58 @@ package hs.fulda.de.ci.exam.project; import org.junit.Test; import static hs.fulda.de.ci.exam.project.ReservationStatus.Confirmed; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; public class NotificationTest { + + EmailNotification email = new EmailNotification("test@gmail.com", "HelloWorld"); + EmailNotification emailInvalid = new EmailNotification("testgmail.com", "HelloWorld"); + FlightReservation reservation = new FlightReservation(); + SmsNotification sms = new SmsNotification("01788370107", "Reservation confirmed"); + SmsNotification smsInvalid = new SmsNotification("123", "Some Wrong Number"); + + @Test public void emailValidiationShouldReturnTrueForValidEmail() { - EmailNotification email = new EmailNotification("test@gmail.com", "HelloWorld"); boolean result = email.sendNotification(); - assertTrue(result); + boolean expected = true; + assertThat(expected).isEqualTo(result); } @Test public void emailValidiationShouldReturnFalseForInValidEmail() { - EmailNotification email = new EmailNotification("testgmail.com", "HelloWorld"); - boolean result = email.sendNotification(); - assertFalse(result); + boolean result = emailInvalid.sendNotification(); + boolean expected = false; + assertThat(expected).isEqualTo(result); } @Test public void emailNotificationShouldBeSentWhenFlighReservationCompleted() { - FlightReservation reservation = new FlightReservation(); reservation.setStatus(Confirmed); boolean result = reservation.notifyUser("email", "test@gmail.com", "Reservation is Confirmed!"); - assertTrue(result); + boolean expected = true; + assertThat(expected).isEqualTo(result); } @Test public void smsNotificationShouldBeSentWhenSmsTypeIsChosen() { - FlightReservation reservation = new FlightReservation(); reservation.setStatus(Confirmed); boolean result = reservation.notifyUser("sms", "01788370107", "Reservation is successfull"); - assertTrue(result); + boolean expected = true; + assertThat(expected).isEqualTo(result); } @Test public void smsIsSentWhenNumberIsValid() { - SmsNotification sms = new SmsNotification("01788370107", "Reservation confirmed"); boolean result = sms.sendNotification(); - assertTrue(result); + boolean expected = true; + assertThat(expected).isEqualTo(result); } @Test public void smsShouldNotBeSentWhenNumberIsInvalid() { - SmsNotification sms = new SmsNotification("123", "Some Wrong Number"); - boolean result = sms.sendNotification(); - assertFalse(result); + boolean result = smsInvalid.sendNotification(); + boolean expected = false; + assertThat(expected).isEqualTo(result); } }