Browse Source

Merge commit '38cdbc8fbb2753edb07ebd58b1d01be5882d7ae7' into HEAD

feature-pr-flight-management
JenkinsSonaImron 3 years ago
parent
commit
82813a62ad
  1. 36
      src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java
  2. 15
      src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java
  3. 5
      src/main/java/hs/fulda/de/ci/exam/project/Notification.java
  4. 37
      src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java
  5. 60
      src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java

36
src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.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();
}
}

15
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() {
}
@ -60,4 +60,17 @@ public class FlightReservation {
credit_card_payment.status = Completed;
status = Confirmed;
}
public boolean notifyUser(String type, String to, String content) {
Notification notification = null;
switch (type) {
case "email":
notification = new EmailNotification(to, content);
break;
case "sms":
notification = new SmsNotification(to, content);
break;
}
return notification.sendNotification();
}
}

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

37
src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java

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

60
src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java

@ -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);
}
}
Loading…
Cancel
Save