forked from pierront/mylibrary-template
Add : Test concernant l'abonnement (structure de test)
This commit is contained in:
@@ -117,6 +117,12 @@
|
|||||||
<version>${mockito.version}</version>
|
<version>${mockito.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
+78
@@ -0,0 +1,78 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.*;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
@DisplayName("SubscriptionConverter Unit Tests")
|
||||||
|
class SubscriptionConverterTest {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDomain() method tests")
|
||||||
|
class ToDomainTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should convert SubscriptionInfo to Subscription domain object")
|
||||||
|
void shouldConvertSubscriptionInfoToDomain() {
|
||||||
|
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
new Date()
|
||||||
|
);
|
||||||
|
|
||||||
|
Subscription result = SubscriptionConverter.toDomain(subscriptionInfo);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(subscriptionInfo.clientId(), result.getClientId());
|
||||||
|
assertEquals(subscriptionInfo.duree(), result.getDuree());
|
||||||
|
assertEquals(subscriptionInfo.modePaiement().getType(), result.getModePaiement().getType());
|
||||||
|
assertEquals(subscriptionInfo.modePaiement().getDetails(), result.getModePaiement().getDetails());
|
||||||
|
assertEquals(subscriptionInfo.dateDebutSouhaitee(), result.getDateDebutSouhaitee());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDTO() method tests")
|
||||||
|
class ToDTOTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should convert Subscription domain object to SubscriptionDTO with all fields mapped correctly")
|
||||||
|
void shouldConvertSubscriptionToDTO() {
|
||||||
|
Subscription subscription = Subscription.builder()
|
||||||
|
.clientId(UUID.randomUUID())
|
||||||
|
.duree(SubscriptionDuree.M3)
|
||||||
|
.modePaiement(ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build())
|
||||||
|
.dateDebutSouhaitee(new Date())
|
||||||
|
.build();
|
||||||
|
subscription.setRandomUUID();
|
||||||
|
|
||||||
|
SubscriptionDTO result = SubscriptionConverter.toDTO(subscription);
|
||||||
|
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(subscription.getDateDebutSouhaitee());
|
||||||
|
cal.add(Calendar.MONTH, subscription.getDuree().getValue());
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(subscription.getAbonnementId(), result.getAbonnementId());
|
||||||
|
assertEquals(subscription.getDateDebutSouhaitee(), result.getDateDebut());
|
||||||
|
assertEquals(cal.getTime(), result.getDateFin());
|
||||||
|
assertEquals(subscription.getDuree().getMonthlyPricing(), result.getMontantMensuel());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+67
@@ -0,0 +1,67 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.ModePaiement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDuree;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.TypePaiement;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class SubscriptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Builder should create a valid Subscription instance")
|
||||||
|
void testSubscriptionBuilder() {
|
||||||
|
UUID id = UUID.randomUUID();
|
||||||
|
UUID id2 = UUID.randomUUID();
|
||||||
|
Date adj = new Date();
|
||||||
|
|
||||||
|
Subscription subscription = Subscription.builder()
|
||||||
|
.abonnementId(id)
|
||||||
|
.clientId(id2)
|
||||||
|
.duree(SubscriptionDuree.M3)
|
||||||
|
.modePaiement(ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build())
|
||||||
|
.dateDebutSouhaitee(adj)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
assertEquals(id, subscription.getAbonnementId());
|
||||||
|
assertEquals(id2, subscription.getClientId());
|
||||||
|
assertEquals(SubscriptionDuree.M3, subscription.getDuree());
|
||||||
|
assertEquals(TypePaiement.CB, subscription.getModePaiement().getType());
|
||||||
|
assertEquals("yes", subscription.getModePaiement().getDetails());
|
||||||
|
assertEquals(adj, subscription.getDateDebutSouhaitee());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("setRandomUUID should set a new non-null UUID")
|
||||||
|
void testSetRandomUUID() {
|
||||||
|
Subscription subscription = Subscription.builder().build();
|
||||||
|
UUID originalId = subscription.getAbonnementId();
|
||||||
|
|
||||||
|
subscription.setRandomUUID();
|
||||||
|
|
||||||
|
assertNotNull(subscription.getAbonnementId());
|
||||||
|
assertNotEquals(originalId, subscription.getAbonnementId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Two setRandomUUID calls should produce different UUIDs")
|
||||||
|
void testSetRandomUUIDTwice() {
|
||||||
|
Subscription subscription = Subscription.builder().build();
|
||||||
|
subscription.setRandomUUID();
|
||||||
|
UUID firstId = subscription.getAbonnementId();
|
||||||
|
|
||||||
|
subscription.setRandomUUID();
|
||||||
|
UUID secondId = subscription.getAbonnementId();
|
||||||
|
|
||||||
|
assertNotEquals(firstId, secondId);
|
||||||
|
}
|
||||||
|
}
|
||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
class SubscriptionNotFoundExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the UUID provided")
|
||||||
|
void testExceptionMessageContainsUUID() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
|
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(uuid);
|
||||||
|
|
||||||
|
String expectedMessage = String.format("The Subscription with id %s does not exist", uuid);
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should use the correct constant message format")
|
||||||
|
void testExceptionUsesConstantMessageFormat() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
|
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(uuid);
|
||||||
|
|
||||||
|
assertEquals("The Subscription with id {0} does not exist",
|
||||||
|
SubscriptionNotFoundException.THE_SUBSCRIPTION_WITH_ID_DOES_NOT_EXIST_MESSAGE);
|
||||||
|
assertTrue(exception.getMessage().contains(uuid.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new SubscriptionNotFoundException(uuid);
|
||||||
|
} catch (SubscriptionNotFoundException e) {
|
||||||
|
String expectedMessage = String.format("The Subscription with id %s does not exist", uuid);
|
||||||
|
assertEquals(expectedMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+194
@@ -0,0 +1,194 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.ModePaiement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDuree;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.TypePaiement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class SubscriptionRepositoryTest {
|
||||||
|
|
||||||
|
private SubscriptionRepository repository;
|
||||||
|
private Subscription subscription1;
|
||||||
|
private Subscription subscription2;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
repository = new SubscriptionRepository();
|
||||||
|
|
||||||
|
subscription1 = Subscription.builder()
|
||||||
|
.clientId(UUID.randomUUID())
|
||||||
|
.duree(SubscriptionDuree.M3)
|
||||||
|
.modePaiement(ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build())
|
||||||
|
.dateDebutSouhaitee(new Date())
|
||||||
|
.build();
|
||||||
|
subscription1.setRandomUUID();
|
||||||
|
|
||||||
|
subscription2 = Subscription.builder()
|
||||||
|
.clientId(UUID.randomUUID())
|
||||||
|
.duree(SubscriptionDuree.M6)
|
||||||
|
.modePaiement(ModePaiement.builder()
|
||||||
|
.type(TypePaiement.Paypal)
|
||||||
|
.details("No")
|
||||||
|
.build())
|
||||||
|
.dateDebutSouhaitee(new Date(0))
|
||||||
|
.build();
|
||||||
|
subscription2.setRandomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("New repository should be empty")
|
||||||
|
void testNewRepositoryIsEmpty() {
|
||||||
|
assertTrue(repository.findAll().isEmpty());
|
||||||
|
assertEquals(0, repository.findAll().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Save operations")
|
||||||
|
class SaveOperations {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should add a new subscription")
|
||||||
|
void testSaveNewSubscription() {
|
||||||
|
Subscription saved = repository.save(subscription1);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(subscription1.getAbonnementId(), saved.getAbonnementId());
|
||||||
|
assertEquals(subscription1.getClientId(), saved.getClientId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should update existing subscription with same ID")
|
||||||
|
void testSaveUpdatesExistingSubscription() {
|
||||||
|
repository.save(subscription1);
|
||||||
|
UUID uuid = subscription1.getAbonnementId();
|
||||||
|
|
||||||
|
Subscription updatedSubscription = Subscription.builder()
|
||||||
|
.abonnementId(uuid)
|
||||||
|
.clientId(UUID.randomUUID())
|
||||||
|
.duree(SubscriptionDuree.M12)
|
||||||
|
.modePaiement(ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("maybe")
|
||||||
|
.build())
|
||||||
|
.dateDebutSouhaitee(new Date(500000))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Subscription saved = repository.save(updatedSubscription);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(uuid, saved.getAbonnementId());
|
||||||
|
assertEquals(SubscriptionDuree.M12, saved.getDuree());
|
||||||
|
assertEquals("maybe", saved.getModePaiement().getDetails());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save multiple subscriptions should add all of them")
|
||||||
|
void testSaveMultipleSubscriptions() {
|
||||||
|
repository.save(subscription1);
|
||||||
|
repository.save(subscription2);
|
||||||
|
|
||||||
|
assertEquals(2, repository.findAll().size());
|
||||||
|
assertTrue(repository.findAll().contains(subscription1));
|
||||||
|
assertTrue(repository.findAll().contains(subscription2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find operations")
|
||||||
|
class FindOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpSubscriptions() {
|
||||||
|
repository.save(subscription1);
|
||||||
|
repository.save(subscription2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindAll should return all subscriptions")
|
||||||
|
void testFindAll() {
|
||||||
|
assertEquals(2, repository.findAll().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByUuid should return subscription with matching ID")
|
||||||
|
void testfindByUuid() {
|
||||||
|
Optional<Subscription> found = repository.findByClientUuid(subscription1.getClientId());
|
||||||
|
|
||||||
|
assertTrue(found.isPresent());
|
||||||
|
assertEquals(subscription1.getAbonnementId(), found.get().getAbonnementId());
|
||||||
|
assertEquals(subscription1.getDuree(), found.get().getDuree());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindById should return empty Optional when ID doesn't exist")
|
||||||
|
void testFindByIdNotFound() {
|
||||||
|
Optional<Subscription> found = repository.findByUuid(UUID.randomUUID());
|
||||||
|
|
||||||
|
assertTrue(found.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsById should return true when ID exists")
|
||||||
|
void testExistsByIdExists() {
|
||||||
|
assertTrue(repository.existsById(subscription1.getAbonnementId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsById should return false when ID doesn't exist")
|
||||||
|
void testExistsByIdNotExists() {
|
||||||
|
assertFalse(repository.existsById(UUID.randomUUID()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Delete operations")
|
||||||
|
class DeleteOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpSubscriptions() {
|
||||||
|
repository.save(subscription1);
|
||||||
|
repository.save(subscription2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should remove the specified subscription")
|
||||||
|
void testDelete() {
|
||||||
|
repository.delete(subscription1);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertFalse(repository.findAll().contains(subscription1));
|
||||||
|
assertTrue(repository.findAll().contains(subscription2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("DeleteAll should remove all subscriptions")
|
||||||
|
void testDeleteAll() {
|
||||||
|
repository.deleteAll();
|
||||||
|
|
||||||
|
assertTrue(repository.findAll().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should not throw exception when subscription doesn't exist")
|
||||||
|
void testDeleteNonExistentSubscription() {
|
||||||
|
Subscription nonExistent = Subscription.builder().clientId(UUID.randomUUID()).build();
|
||||||
|
nonExistent.setRandomUUID();
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> repository.delete(nonExistent));
|
||||||
|
assertEquals(2, repository.findAll().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+218
@@ -0,0 +1,218 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
class SubscriptionUseCaseTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private SubscriptionRepository subscriptionRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private SubscriptionUseCase subscriptionUseCase;
|
||||||
|
|
||||||
|
private UUID subscriptionId;
|
||||||
|
private UUID subscriptionClientId;
|
||||||
|
private Subscription testSubscription;
|
||||||
|
private SubscriptionInfo validSubscriptionInfo;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
subscriptionId = UUID.randomUUID();
|
||||||
|
subscriptionClientId = UUID.randomUUID();
|
||||||
|
|
||||||
|
Date adj = new Date();
|
||||||
|
|
||||||
|
testSubscription = Subscription.builder()
|
||||||
|
.abonnementId(subscriptionId)
|
||||||
|
.clientId(subscriptionClientId)
|
||||||
|
.duree(SubscriptionDuree.M3)
|
||||||
|
.modePaiement(ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build())
|
||||||
|
.dateDebutSouhaitee(new Date())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
validSubscriptionInfo = new SubscriptionInfo(
|
||||||
|
subscriptionClientId,
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
adj
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Register subscription tests")
|
||||||
|
class RegisterSubscriptionTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should register subscription when valid data is provided")
|
||||||
|
|
||||||
|
void testRegisterSubscriptionWithValidData() throws NotValidSubscriptionException {
|
||||||
|
when(subscriptionRepository.save(any(Subscription.class))).thenReturn(testSubscription);
|
||||||
|
|
||||||
|
SubscriptionDTO returnedSubscriptionDTO = subscriptionUseCase.registerSubscription(validSubscriptionInfo);
|
||||||
|
|
||||||
|
assertNotNull(returnedSubscriptionDTO);
|
||||||
|
assertEquals(subscriptionId, returnedSubscriptionDTO.getAbonnementId());
|
||||||
|
verify(subscriptionRepository, times(1)).save(any(Subscription.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when subscription data is not valid")
|
||||||
|
void testRegisterSubscriptionWithInvalidData() {
|
||||||
|
SubscriptionInfo invalidSubscriptionInfo = new SubscriptionInfo(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThrows(NotValidSubscriptionException.class,
|
||||||
|
() -> subscriptionUseCase.registerSubscription(invalidSubscriptionInfo));
|
||||||
|
|
||||||
|
verify(subscriptionRepository, never()).save(any(Subscription.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find subscription tests")
|
||||||
|
class FindSubscriptionTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return subscription when client UUID exists")
|
||||||
|
void testFindSubscriptionByUuid() {
|
||||||
|
when(subscriptionRepository.findByClientUuid(subscriptionClientId)).thenReturn(Optional.of(testSubscription));
|
||||||
|
|
||||||
|
Optional<SubscriptionDTO> foundSubscription = subscriptionUseCase.findSubscriptionByUuid(subscriptionClientId);
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isPresent());
|
||||||
|
assertEquals(testSubscription.getAbonnementId(), foundSubscription.get().getAbonnementId());
|
||||||
|
verify(subscriptionRepository, times(1)).findByClientUuid(subscriptionClientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty Optional when UUID doesn't exist")
|
||||||
|
void testFindSubscriptionByUuidNotFound() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
|
Optional<SubscriptionDTO> foundSubscription = subscriptionUseCase.findSubscriptionByUuid(uuid);
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Update subscription tests")
|
||||||
|
class UpdateSubscriptionTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should update subscription when valid data is provided")
|
||||||
|
void testUpdateSubscriptionWithValidData() throws SubscriptionNotFoundException, NotValidSubscriptionException {
|
||||||
|
when(subscriptionRepository.findByUuid(subscriptionId)).thenReturn(Optional.of(testSubscription));
|
||||||
|
|
||||||
|
Subscription updatedSubscription = Subscription.builder()
|
||||||
|
.abonnementId(subscriptionId)
|
||||||
|
.clientId(subscriptionClientId)
|
||||||
|
.duree(SubscriptionDuree.M6)
|
||||||
|
.modePaiement(ModePaiement.builder()
|
||||||
|
.type(TypePaiement.Paypal)
|
||||||
|
.details("no")
|
||||||
|
.build())
|
||||||
|
.dateDebutSouhaitee(new Date(0))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(subscriptionRepository.save(any(Subscription.class))).thenReturn(updatedSubscription);
|
||||||
|
|
||||||
|
SubscriptionInfo updateInfo = new SubscriptionInfo(
|
||||||
|
subscriptionClientId,
|
||||||
|
SubscriptionDuree.M6,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.Paypal)
|
||||||
|
.details("no")
|
||||||
|
.build(),
|
||||||
|
new Date(0)
|
||||||
|
);
|
||||||
|
|
||||||
|
SubscriptionDTO result = subscriptionUseCase.updateSubscription(subscriptionId, updateInfo);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(subscriptionId, result.getAbonnementId());
|
||||||
|
assertEquals(new Date(0), result.getDateDebut());
|
||||||
|
verify(subscriptionRepository, times(1)).findByUuid(subscriptionId);
|
||||||
|
verify(subscriptionRepository, times(1)).save(any(Subscription.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when subscription ID doesn't exist")
|
||||||
|
void testUpdateSubscriptionNotFound() {
|
||||||
|
UUID nonExistentId = UUID.randomUUID();
|
||||||
|
when(subscriptionRepository.findByUuid(nonExistentId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(SubscriptionNotFoundException.class,
|
||||||
|
() -> subscriptionUseCase.updateSubscription(nonExistentId, validSubscriptionInfo));
|
||||||
|
|
||||||
|
verify(subscriptionRepository, times(1)).findByUuid(nonExistentId);
|
||||||
|
verify(subscriptionRepository, never()).save(any(Subscription.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Delete subscription tests")
|
||||||
|
class DeleteSubscriptionTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should delete subscription when ID exists")
|
||||||
|
void testDeleteSubscription() throws SubscriptionNotFoundException {
|
||||||
|
when(subscriptionRepository.findByUuid(subscriptionId)).thenReturn(Optional.of(testSubscription));
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> subscriptionUseCase.deleteSubscription(subscriptionId));
|
||||||
|
|
||||||
|
verify(subscriptionRepository, times(1)).findByUuid(subscriptionId);
|
||||||
|
verify(subscriptionRepository, times(1)).delete(testSubscription);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when deleting unknown subscription")
|
||||||
|
void testDeleteSubscriptionNotFound() {
|
||||||
|
UUID nonExistentId = UUID.randomUUID();
|
||||||
|
when(subscriptionRepository.findByUuid(nonExistentId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(SubscriptionNotFoundException.class,
|
||||||
|
() -> subscriptionUseCase.deleteSubscription(nonExistentId));
|
||||||
|
|
||||||
|
verify(subscriptionRepository, times(1)).findByUuid(nonExistentId);
|
||||||
|
verify(subscriptionRepository, never()).delete(any(Subscription.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
+200
@@ -0,0 +1,200 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.ModePaiement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDuree;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.TypePaiement;
|
||||||
|
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 java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class SubscriptionValidatorTest {
|
||||||
|
|
||||||
|
private SubscriptionInfo validSubscription() {
|
||||||
|
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
Date adj = new Date();
|
||||||
|
|
||||||
|
return new SubscriptionInfo(
|
||||||
|
uuid,
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
adj
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should validate subscription with valid data")
|
||||||
|
void testValidateValidSubscription() {
|
||||||
|
assertDoesNotThrow(() -> SubscriptionValidator.validate(validSubscription()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("UUID validation tests")
|
||||||
|
class UuidValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should Be valid")
|
||||||
|
void testValidatenormalUUID() {
|
||||||
|
SubscriptionInfo subscription = new SubscriptionInfo(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
new Date(0)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> SubscriptionValidator.validate(subscription));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when UUID is blank")
|
||||||
|
void testValidateBlankUuid() {
|
||||||
|
SubscriptionInfo subscription = new SubscriptionInfo(
|
||||||
|
null,
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
new Date(0)
|
||||||
|
);
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscription));
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.Client_ID_NOT_VALID, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Date validation tests")
|
||||||
|
class DateValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should be good")
|
||||||
|
void testValidateTodayDate() {
|
||||||
|
SubscriptionInfo subscription = new SubscriptionInfo(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
new Date()
|
||||||
|
);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> SubscriptionValidator.validate(subscription));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when Date is null")
|
||||||
|
void testValidateNullDate() {
|
||||||
|
SubscriptionInfo subscription = new SubscriptionInfo(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscription));
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.LA_DATE_DE_DEBUT_N_A_PAS_ETE_RENSEIGNEE, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("ModePaiement validation tests")
|
||||||
|
class ModePaiementValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should be good")
|
||||||
|
void testValidateGoodModePaiement() {
|
||||||
|
SubscriptionInfo subscription = new SubscriptionInfo(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
new Date()
|
||||||
|
);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> SubscriptionValidator.validate(subscription));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when Date is null")
|
||||||
|
void testValidateNullModePaiement() {
|
||||||
|
SubscriptionInfo subscription = new SubscriptionInfo(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(null)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
new Date()
|
||||||
|
);
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscription));
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.LE_TYPE_DE_PAYEMENT_N_EST_PAS_PRIS_EN_CHARGE, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Durée validation tests")
|
||||||
|
class DureeValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should be good")
|
||||||
|
void testValidateGoodDuree() {
|
||||||
|
SubscriptionInfo subscription = new SubscriptionInfo(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
SubscriptionDuree.M3,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
new Date()
|
||||||
|
);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> SubscriptionValidator.validate(subscription));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when Date is null")
|
||||||
|
void testValidateNullDuree() {
|
||||||
|
SubscriptionInfo subscription = new SubscriptionInfo(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
null,
|
||||||
|
ModePaiement.builder()
|
||||||
|
.type(TypePaiement.CB)
|
||||||
|
.details("yes")
|
||||||
|
.build(),
|
||||||
|
new Date()
|
||||||
|
);
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscription));
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.LA_DUREE_RENSEIGNEE_N_EST_PAS_CORRECTE, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user