forked from pierront/mylibrary-template
✅ création des tests de validator
This commit is contained in:
+50
@@ -0,0 +1,50 @@
|
|||||||
|
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 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_VALIDE, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+84
@@ -0,0 +1,84 @@
|
|||||||
|
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 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_VALIDE, 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user