forked from pierront/mylibrary-template
Feature/create subscription #3
+8
@@ -0,0 +1,8 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
public class NotValidPaymentMethodException extends RuntimeException {
|
||||
|
||||
public NotValidPaymentMethodException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
public class NotValidSubscriptionException extends RuntimeException {
|
||||
|
||||
public NotValidSubscriptionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentMethodInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidPaymentMethodException;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class PaymentMethodValidator {
|
||||
|
||||
public static final String MODE_PAIEMENT_IS_NOT_VALID = "paymentType is not valid";
|
||||
|
||||
private PaymentMethodValidator() {
|
||||
|
||||
}
|
||||
|
||||
public static void validate(PaymentMethodInfo newPaymentMethod)
|
||||
throws NotValidPaymentMethodException {
|
||||
validatePaymentType(newPaymentMethod);
|
||||
}
|
||||
|
||||
private static void validatePaymentType(PaymentMethodInfo newPaymentMethod)
|
||||
throws NotValidPaymentMethodException {
|
||||
Set<String> Valid_Payment_Type = Set.of("CB", "PAYPAL");
|
||||
if (!Valid_Payment_Type.contains(newPaymentMethod.paymentType())) {
|
||||
throw new NotValidPaymentMethodException(MODE_PAIEMENT_IS_NOT_VALID);}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Set;
|
||||
|
||||
public class SubscriptionValidator {
|
||||
|
||||
public static final String SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALID = "subscriptionDurationDesired will be 3, 6 or 12 by the ENUM file SubscriptionDurationDesired";
|
||||
public static final String DESIRED_START_DATE_CANNOT_BE_BEFORE_TODAY = "desiredStartDate cannot be before today";
|
||||
|
||||
private SubscriptionValidator() {
|
||||
|
||||
}
|
||||
|
||||
public static void validate(SubscriptionInfo newSubscription) throws NotValidSubscriptionException {
|
||||
validateDesiredStartDate(newSubscription);
|
||||
validateSubscriptionDurationDesired(newSubscription);
|
||||
}
|
||||
|
||||
private static void validateSubscriptionDurationDesired(SubscriptionInfo newSubscription)
|
||||
throws NotValidSubscriptionException {
|
||||
Set<Integer> VALID_DURATIONS = Set.of(3, 6, 12);
|
||||
if (!VALID_DURATIONS.contains(newSubscription.subscriptionDurationDesired())) {
|
||||
throw new NotValidSubscriptionException(SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALID);}
|
||||
}
|
||||
|
||||
private static void validateDesiredStartDate(SubscriptionInfo newSubscription)
|
||||
throws NotValidSubscriptionException {
|
||||
if (newSubscription.desiredStartDate().isBefore(LocalDate.now())) {
|
||||
throw new NotValidSubscriptionException(DESIRED_START_DATE_CANNOT_BE_BEFORE_TODAY);
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -2,6 +2,7 @@ package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentMethodInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentType;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidPaymentMethodException;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -44,7 +45,7 @@ public class PaymentMethodValidatorTest {
|
||||
() -> PaymentMethodValidator.validate(invalidPaymentMethod)
|
||||
);
|
||||
|
||||
assertEquals(PaymentMethodValidator.MODE_PAIEMENT_IS_NOT_VALIDE, exception.getMessage());
|
||||
assertEquals(PaymentMethodValidator.MODE_PAIEMENT_IS_NOT_VALID, exception.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -2,6 +2,7 @@ package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDurationDesired;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -50,7 +51,7 @@ public class SubscriptionValidatorTest {
|
||||
NotValidSubscriptionException.class,
|
||||
() -> SubscriptionValidator.validate(invalidSubscription)
|
||||
);
|
||||
assertEquals(SubscriptionValidator.SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALIDE, exception.getMessage());
|
||||
assertEquals(SubscriptionValidator.SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALID, exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user