diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/converter/SubscriptionConverterTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/converter/SubscriptionConverterTest.java new file mode 100644 index 0000000..4554a3c --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/converter/SubscriptionConverterTest.java @@ -0,0 +1,122 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.subscription.converter; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class SubscriptionConverterTest { + + @Nested + @DisplayName("toDomain() method tests") + class ToDomainTests { + + @Test + @DisplayName("Should convert SubscriptionInfo to Subscription domain object with monthly amount initialized to 0") + void shouldConvertSubscriptionInfoToDomain() { + // Given + PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "Maxime Lebreton"); + LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); + SubscriptionInfo subscriptionInfo = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate); + LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); + + // When + Subscription result = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo); + + // Then + assertNotNull(result); + assertEquals(subscriptionInfo.subscriptionDurationDesired(), result.getSubscriptionDurationDesired()); + assertEquals(paymentMethodInfo.paymentType(), result.getPaymentMethod().getPaymentType()); + assertEquals(paymentMethodInfo.details(), result.getPaymentMethod().getDetails()); + assertEquals(subscriptionInfo.desiredStartDate(), result.getDesiredStartDate()); + assertEquals(subscriptionInfo.desiredStartDate(), result.getStartDate()); + assertEquals(estimateEndDate, result.getEndDate()); + assertEquals(0, result.getMonthlyAmount()); + } + } + + @Nested + @DisplayName("toDTO() method tests") + class ToDTOTests { + + @Test + @DisplayName("Should convert Subscription domain object to SubscriptionDTO with all fields mapped correctly") + void shouldConvertSubscriptionToDTO() { + PaymentMethod paymentMethod = PaymentMethod.builder() + .paymentType(PaymentType.CB.name()) + .details("Maxime Lebreton") + .build(); + LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); + LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); + Subscription subscription = Subscription.builder() + .subscriptionId(UUID.randomUUID()) + .customerId(UUID.randomUUID()) + .subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue()) + .paymentMethod(paymentMethod) + .desiredStartDate(desiredStartDate) + .startDate(desiredStartDate) + .endDate(estimateEndDate) + .monthlyAmount(12.03) + .build(); + + SubscriptionDTO result = SubscriptionConverter.toDTO(subscription); + + assertNotNull(result); + assertEquals(subscription.getSubscriptionId(), result.getSubscriptionId()); + assertEquals(subscription.getCustomerId(), result.getCustomerId()); + assertEquals(subscription.getSubscriptionDurationDesired(), result.getSubscriptionDurationDesired()); + assertEquals(subscription.getPaymentMethod().getPaymentType(), result.getPaymentMethod().getPaymentType()); + assertEquals(subscription.getPaymentMethod().getDetails(), result.getPaymentMethod().getDetails()); + assertEquals(subscription.desiredStartDate(), result.getStartDateDesired()); + assertEquals(subscription.getStartDate(), result.getStartDate()); + assertEquals(subscription.getEndDate(), result.getEndDate()); + assertEquals(12.03, result.getMonthlyAmount()); + } + } + + @Test + @DisplayName("Should handle null values properly when converting between objects") + void shouldHandleNullValuesGracefully() { + PaymentMethod paymentMethod = PaymentMethod.builder() + .paymentType(PaymentType.CB.name()) + .details(null) + .build(); + LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); + LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); + Subscription subscription = Subscription.builder() + .subscriptionId(UUID.randomUUID()) + .customerId(UUID.randomUUID()) + .subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue()) + .paymentMethod(paymentMethod) + .desiredStartDate(desiredStartDate) + .startDate(desiredStartDate) + .endDate(estimateEndDate) + .monthlyAmount(12.03) + .build(); + + SubscriptionDTO result = SubscriptionConverter.toDTO(customer); + + assertNotNull(result); + assertNull(result.getPaymentMethod().getDetails()); + assertEquals(desiredStartDate, result.getDesiredStartDate()); + } + + @Test + @DisplayName("Should preserve empty string values during conversion") + void shouldPreserveEmptyStrings() { + PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), ""); + LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); + SubscriptionInfo subscriptionInfo = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate); + + Subscription domainResult = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo); + SubscriptionDTO dtoResult = SubscriptionConverter.toDTO(domainResult); + + assertEquals("", dtoResult.getPaymentMethod().getDetails()); + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/entity/PaymentMethodTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/entity/PaymentMethodTest.java new file mode 100644 index 0000000..bf95192 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/entity/PaymentMethodTest.java @@ -0,0 +1,57 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class PaymentMethodTest { + + @Test + @DisplayName("Builder should create a valid Subscription instance") + void testSubscriptionBuilder() { + String paymentType = PaymentType.CB.name(); + Object details = new Object(); + details = "Maxime Lebreton"; + + PaymentMethod paymentMethod = PaymentMethod.builder() + .paymentType(paymentType) + .details(details) + .build(); + + assertEquals(paymentType, paymentMethod.getPaymentType()); + assertEquals(details, paymentMethod.getDetails()); + } + + @Test + @DisplayName("Builder should create a valid Subscription instance with details as an Integer") + void testSubscriptionBuilder() { + String paymentType = PaymentType.CB.name(); + Object details = new Object(); + details = 10; + + PaymentMethod paymentMethod = PaymentMethod.builder() + .paymentType(paymentType) + .details(details) + .build(); + + assertEquals(paymentType, paymentMethod.getPaymentType()); + assertEquals(details, paymentMethod.getDetails()); + } + + @Test + @DisplayName("Builder should create a valid Subscription instance with details as an boolean") + void testSubscriptionBuilder() { + String paymentType = PaymentType.CB.name(); + Object details = new Object(); + details = true; + + PaymentMethod paymentMethod = PaymentMethod.builder() + .paymentType(paymentType) + .details(details) + .build(); + + assertEquals(paymentType, paymentMethod.getPaymentType()); + assertEquals(details, paymentMethod.getDetails()); + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/entity/SubscriptionTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/entity/SubscriptionTest.java new file mode 100644 index 0000000..40c2b45 --- /dev/null +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/subscription/entity/SubscriptionTest.java @@ -0,0 +1,123 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SubscriptionTest { + + @Test + @DisplayName("Builder should create a valid Subscription instance") + void testSubscriptionBuilder() { + UUID subscriptionId = UUID.randomUUID(); + UUID customerId = UUID.randomUUID(); + Integer subscriptionDurationDesired = SubscriptionDurationDesired.THREE.getValue(); + String paymentType = PaymentType.CB.name(); + Object details = new Object(); + details = "Maxime Lebreton"; + LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); + LocalDate startDate = desiredStartDate; + LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); + double monthlyAmount = 17.99; + + PaymentMethod paymentMethod = PaymentMethod.builder() + .paymentType(paymentType) + .details(details) + .build(); + Subscription subscription = Subscription.builder() + .subscriptionId(subscriptionId) + .customerId(customerId) + .subscriptionDurationDesired(subscriptionDurationDesired) + .paymentMethod(paymentMethod) + .desiredStartDate(desiredStartDate) + .startDate(startDate) + .endDate(estimateEndDate) + .monthlyAmount(monthlyAmount) + .build(); + + assertEquals(subscriptionId, subscription.getSubscriptionId()); + assertEquals(customerId, subscription.getCustomerId()); + assertEquals(subscriptionDurationDesired, subscription.getSubscriptionDurationDesired()); + assertEquals(paymentType, paymentMethod.getPaymentType()); + assertEquals(details, paymentMethod.getDetails()); + assertEquals(desiredStartDate, subscription.getDesiredStartDate()); + assertEquals(startDate, subscription.getStartDate()); + assertEquals(estimateEndDate, subscription.getEndDate()); + assertEquals(monthlyAmount, subscription.getMonthlyAmount()); + } + + @Test + @DisplayName("setRandomSubscriptionUUID should change the ID to a new random UUID") + void testSetRandomSubscriptionUUID() { + Subscription subscription = Subscription.builder().build(); + UUID originalId = subscription.getSubscriptionId(); + + subscription.setRandomSubscriptionUUID(); + + assertNotNull(subscription.getSubscriptionId()); + assertNotEquals(originalId, subscription.getSubscriptionId()); + } + + @Test + @DisplayName("setRandomCustomerUUID should change the ID to a new random UUID") + void testSetRandomCustomerUUID() { + Subscription subscription = Subscription.builder().build(); + UUID originalId = subscription.getCustomerId(); + + Subscription.setRandomCustomerUUID(); + + assertNotNull(subscription.getCustomerId()); + assertNotEquals(originalId, subscription.getCustomerId()); + } + + @Test + @DisplayName("setStartDate should instantiate StartDate as the same value as the variable desiredStartDate") + void testSetRandomCustomerUUID() { + LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); + Subscription subscription = Subscription.builder() + .desiredStartDate(desiredStartDate) + .build(); + + Subscription.setStartDate(); + + assertNotNull(subscription.getStartDate()); + assertEquals(desiredStartDate, subscription.getStartDate()); + } + + @Test + @DisplayName("setEndDate should instantiate EndDate as the same value as the variable desiredStartDate + subscriptionDurationDesired") + void testSetRandomCustomerUUID() { + LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); + LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); + Subscription subscription = Subscription.builder() + .subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue()) + .desiredStartDate(desiredStartDate) + .build(); + + Subscription.setEndDate(); + + assertNotNull(subscription.getEndDate()); + assertEquals(estimateEndDate, subscription.getEndDate()); + } + + @Test + @DisplayName("setDateSubscription should instantiate StartDate and EndDate ") + void testSetRandomCustomerUUID() { + LocalDate desiredStartDate = LocalDate.of(2026, 3, 24); + LocalDate estimateEndDate = LocalDate.of(2026, 6, 24); + Subscription subscription = Subscription.builder() + .subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue()) + .desiredStartDate(desiredStartDate) + .build(); + + Subscription.setDateSubscription(); + + assertNotNull(subscription.getEndDate()); + assertEquals(estimateEndDate, subscription.getEndDate()); + } +}