Compare commits

..

4 Commits

10 changed files with 347 additions and 14 deletions
@@ -0,0 +1,8 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
public class NotValidPaymentMethodException extends RuntimeException {
public NotValidPaymentMethodException(String message) {
super(message);
}
}
@@ -0,0 +1,8 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
public class NotValidSubscriptionException extends RuntimeException {
public NotValidSubscriptionException(String message) {
super(message);
}
}
@@ -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);}
}
}
@@ -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);
}
}
}
@@ -25,9 +25,9 @@ public class SubscriptionConverterTest {
void shouldConvertSubscriptionInfoToDomain() { void shouldConvertSubscriptionInfoToDomain() {
// Given // Given
PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "Maxime Lebreton"); PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "Maxime Lebreton");
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate); SubscriptionInfo subscriptionInfo = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
// When // When
Subscription result = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo); Subscription result = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo);
@@ -55,8 +55,8 @@ public class SubscriptionConverterTest {
.paymentType(PaymentType.CB.name()) .paymentType(PaymentType.CB.name())
.details("Maxime Lebreton") .details("Maxime Lebreton")
.build(); .build();
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
Subscription subscription = Subscription.builder() Subscription subscription = Subscription.builder()
.subscriptionId(UUID.randomUUID()) .subscriptionId(UUID.randomUUID())
.customerId(UUID.randomUUID()) .customerId(UUID.randomUUID())
@@ -90,8 +90,8 @@ public class SubscriptionConverterTest {
.paymentType(PaymentType.CB.name()) .paymentType(PaymentType.CB.name())
.details(null) .details(null)
.build(); .build();
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
Subscription subscription = Subscription.builder() Subscription subscription = Subscription.builder()
.subscriptionId(UUID.randomUUID()) .subscriptionId(UUID.randomUUID())
.customerId(UUID.randomUUID()) .customerId(UUID.randomUUID())
@@ -114,7 +114,7 @@ public class SubscriptionConverterTest {
@DisplayName("Should preserve empty string values during conversion") @DisplayName("Should preserve empty string values during conversion")
void shouldPreserveEmptyStrings() { void shouldPreserveEmptyStrings() {
PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), ""); PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "");
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate); SubscriptionInfo subscriptionInfo = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
Subscription domainResult = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo); Subscription domainResult = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo);
@@ -22,9 +22,9 @@ public class SubscriptionTest {
String paymentType = PaymentType.CB.name(); String paymentType = PaymentType.CB.name();
Object details = new Object(); Object details = new Object();
details = "Maxime Lebreton"; details = "Maxime Lebreton";
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
LocalDate startDate = desiredStartDate; LocalDate startDate = desiredStartDate;
LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
double monthlyAmount = 17.99; double monthlyAmount = 17.99;
PaymentMethod paymentMethod = PaymentMethod.builder() PaymentMethod paymentMethod = PaymentMethod.builder()
@@ -80,7 +80,7 @@ public class SubscriptionTest {
@Test @Test
@DisplayName("setStartDate should instantiate StartDate as the same value as the variable desiredStartDate") @DisplayName("setStartDate should instantiate StartDate as the same value as the variable desiredStartDate")
void testSetStartDate() { void testSetStartDate() {
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
Subscription subscription = Subscription.builder() Subscription subscription = Subscription.builder()
.desiredStartDate(desiredStartDate) .desiredStartDate(desiredStartDate)
.build(); .build();
@@ -94,8 +94,8 @@ public class SubscriptionTest {
@Test @Test
@DisplayName("setEndDate should instantiate EndDate as the same value as the variable desiredStartDate + subscriptionDurationDesired") @DisplayName("setEndDate should instantiate EndDate as the same value as the variable desiredStartDate + subscriptionDurationDesired")
void testSetEndDate() { void testSetEndDate() {
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
Subscription subscription = Subscription.builder() Subscription subscription = Subscription.builder()
.subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue()) .subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue())
.desiredStartDate(desiredStartDate) .desiredStartDate(desiredStartDate)
@@ -110,8 +110,8 @@ public class SubscriptionTest {
@Test @Test
@DisplayName("setDateSubscription should instantiate StartDate and EndDate ") @DisplayName("setDateSubscription should instantiate StartDate and EndDate ")
void testSetDateSubscription() { void testSetDateSubscription() {
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
Subscription subscription = Subscription.builder() Subscription subscription = Subscription.builder()
.subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue()) .subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue())
.desiredStartDate(desiredStartDate) .desiredStartDate(desiredStartDate)
@@ -0,0 +1,58 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NotValidPaymentMethodEsceptionTest {
@Test
@DisplayName("Exception should be created with the provided message")
void testExceptionCreation() {
String errorMessage = "PaymentMethod data is not valid";
NotValidPaymentMethodException exception = new NotValidPaymentMethodException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {
"paymentType is not valid"
})
@DisplayName("Exception should handle different validation messages")
void testExceptionWithDifferentMessages(String errorMessage) {
NotValidPaymentMethodException exception = new NotValidPaymentMethodException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "paymentType is not valid";
Exception exception = assertThrows(NotValidPaymentMethodException.class, () -> {
throw new NotValidPaymentMethodException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid paymentMethod data";
try {
throw new NotValidPaymentMethodException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidPaymentMethodException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -0,0 +1,60 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NotValidSubscriptionExceptionTest {
@Test
@DisplayName("Exception should be created with the provided message")
void testExceptionCreation() {
String errorMessage = "Subscription data is not valid";
NotValidSubscriptionException exception = new NotValidSubscriptionException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {
"subscriptionDurationDesired will be 3, 6 or 12 by the ENUM file SubscriptionDurationDesired",
"desiredStartDate cannot be before today"
})
@DisplayName("Exception should handle different validation messages")
void testExceptionWithDifferentMessages(String errorMessage) {
NotValidSubscriptionException exception = new NotValidSubscriptionException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "desiredStartDate cannot be before today";
Exception exception = assertThrows(NotValidSubscriptionException.class, () -> {
throw new NotValidSubscriptionException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid subscription data";
try {
throw new NotValidSubscriptionException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidSubscriptionException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -0,0 +1,51 @@
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;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.*;
public class PaymentMethodValidatorTest {
@Test
@DisplayName("Should validate payment method with valid data")
void testValidateValidPaymentMethod() {
PaymentMethodInfo validPaymentMethod = new PaymentMethodInfo(PaymentType.CB.name(), "Maxime Lebreton");
assertDoesNotThrow(() -> PaymentMethodValidator.validate(validPaymentMethod));
}
@Nested
@DisplayName("PaymentType validation tests")
class PaymentTypeValidationTests {
@ParameterizedTest
@EnumSource(value = PaymentType.class)
void testValidatePaymentType(PaymentType paymentType) {
String validPaymentType = paymentType.toString();
PaymentMethodInfo validPaymentMethod = new PaymentMethodInfo(validPaymentType, "Maxime Lebreton");
assertDoesNotThrow(() -> PaymentMethodValidator.validate(validPaymentMethod));
}
@ParameterizedTest
@ValueSource(strings = {"jambon", " ", ""})
void testNotValidatePaymentType(String invalidPaymentType) {
PaymentMethodInfo invalidPaymentMethod = new PaymentMethodInfo(invalidPaymentType, "Maxime Lebreton");
NotValidPaymentMethodException exception = assertThrows(
NotValidPaymentMethodException.class,
() -> PaymentMethodValidator.validate(invalidPaymentMethod)
);
assertEquals(PaymentMethodValidator.MODE_PAIEMENT_IS_NOT_VALID, exception.getMessage());
}
}
}
@@ -0,0 +1,85 @@
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;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class SubscriptionValidatorTest {
@Test
@DisplayName("Should validate subscription with valid data")
void testValidateValidSubscription() {
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
SubscriptionInfo validSubscription = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
assertDoesNotThrow(() -> SubscriptionValidator.validate(validSubscription));
}
@Nested
@DisplayName("SubscriptionDurationDesired validation tests")
class SubscriptionDurationDesiredValidationTests {
@ParameterizedTest
@EnumSource(value = SubscriptionDurationDesired.class)
void testValidateSubscriptionDurationDesired(SubscriptionDurationDesired subscriptionDurationDesired) {
Integer validSubscriptionDurationDesired = subscriptionDurationDesired.getValue();
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
SubscriptionInfo validSubscription = new SubscriptionInfo(validSubscriptionDurationDesired, desiredStartDate);
assertDoesNotThrow(() -> SubscriptionValidator.validate(validSubscription));
}
@Test
@DisplayName("Should throw exception when subscriptionDurationDesired is not 3, 6 or 12")
void testNotValidateSubscriptionDurationDesired() {
Integer notValidSubscriptionDurationDesired = 1 ;
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
SubscriptionInfo invalidSubscription = new SubscriptionInfo(notValidSubscriptionDurationDesired, desiredStartDate);
NotValidSubscriptionException exception = assertThrows(
NotValidSubscriptionException.class,
() -> SubscriptionValidator.validate(invalidSubscription)
);
assertEquals(SubscriptionValidator.SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALID, exception.getMessage());
}
}
@Nested
@DisplayName("DesiredStartDate validation tests")
class DesiredStartDateValidationTests {
@Test
@DisplayName("Should validate when desiredStartDate is before today")
void testValidateDesiredStartDate() {
LocalDate desiredStartDate = LocalDate.now();
SubscriptionInfo validSubscription = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
assertDoesNotThrow(() -> SubscriptionValidator.validate(validSubscription));
}
@Test
@DisplayName("Should throw exception when desiredStartDate is before today")
void testNotValidateDesiredStartDate() {
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24);
SubscriptionInfo validSubscription = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
NotValidSubscriptionException exception = assertThrows(
NotValidSubscriptionException.class,
() -> SubscriptionValidator.validate(validSubscription)
);
assertEquals(SubscriptionValidator.DESIRED_START_DATE_CANNOT_BE_BEFORE_TODAY, exception.getMessage());
}
}
}