Browse Source
Merge commit '38cdbc8fbb2753edb07ebd58b1d01be5882d7ae7' into HEAD
feature-pr-flight-management
Merge commit '38cdbc8fbb2753edb07ebd58b1d01be5882d7ae7' into HEAD
feature-pr-flight-management
JenkinsSonaImron
3 years ago
5 changed files with 152 additions and 1 deletions
-
36src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java
-
13src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java
-
5src/main/java/hs/fulda/de/ci/exam/project/Notification.java
-
37src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java
-
60src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java
@ -0,0 +1,36 @@ |
|||
package hs.fulda.de.ci.exam.project; |
|||
|
|||
import java.util.regex.Pattern; |
|||
|
|||
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; |
|||
} |
|||
System.out.println("Invalid Email Address"); |
|||
return false; |
|||
} |
|||
|
|||
private boolean isValidEmail(String email) { |
|||
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(); |
|||
} |
|||
} |
@ -0,0 +1,5 @@ |
|||
package hs.fulda.de.ci.exam.project; |
|||
|
|||
public abstract class Notification { |
|||
public abstract boolean sendNotification(); |
|||
} |
@ -0,0 +1,37 @@ |
|||
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; |
|||
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) { |
|||
String regexPattern = "(\\(?([\\d \\-\\)\\–\\+\\/\\(]+){6,}\\)?([ .\\-–\\/]?)([\\d]+))"; |
|||
return patternMatches(to, regexPattern); |
|||
} |
|||
|
|||
public static boolean patternMatches(String emailAddress, String regexPattern) { |
|||
return Pattern.compile(regexPattern) |
|||
.matcher(emailAddress) |
|||
.matches(); |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,60 @@ |
|||
package hs.fulda.de.ci.exam.project; |
|||
|
|||
import org.junit.Test; |
|||
|
|||
import static hs.fulda.de.ci.exam.project.ReservationStatus.Confirmed; |
|||
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() { |
|||
boolean result = email.sendNotification(); |
|||
boolean expected = true; |
|||
assertThat(expected).isEqualTo(result); |
|||
} |
|||
|
|||
@Test |
|||
public void emailValidiationShouldReturnFalseForInValidEmail() { |
|||
boolean result = emailInvalid.sendNotification(); |
|||
boolean expected = false; |
|||
assertThat(expected).isEqualTo(result); |
|||
} |
|||
|
|||
@Test |
|||
public void emailNotificationShouldBeSentWhenFlighReservationCompleted() { |
|||
reservation.setStatus(Confirmed); |
|||
boolean result = reservation.notifyUser("email", "test@gmail.com", "Reservation is Confirmed!"); |
|||
boolean expected = true; |
|||
assertThat(expected).isEqualTo(result); |
|||
} |
|||
|
|||
@Test |
|||
public void smsNotificationShouldBeSentWhenSmsTypeIsChosen() { |
|||
reservation.setStatus(Confirmed); |
|||
boolean result = reservation.notifyUser("sms", "01788370107", "Reservation is successfull"); |
|||
boolean expected = true; |
|||
assertThat(expected).isEqualTo(result); |
|||
} |
|||
|
|||
@Test |
|||
public void smsIsSentWhenNumberIsValid() { |
|||
boolean result = sms.sendNotification(); |
|||
boolean expected = true; |
|||
assertThat(expected).isEqualTo(result); |
|||
} |
|||
|
|||
@Test |
|||
public void smsShouldNotBeSentWhenNumberIsInvalid() { |
|||
boolean result = smsInvalid.sendNotification(); |
|||
boolean expected = false; |
|||
assertThat(expected).isEqualTo(result); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue