forked from pierront/mylibrary-template
Feat: Add Subscription Tests and the Subscription code
This commit is contained in:
@@ -117,6 +117,20 @@
|
|||||||
<version>${mockito.version}</version>
|
<version>${mockito.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-params</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public class ModePaiement {
|
||||||
|
private final TypePaiement type;
|
||||||
|
private final Object details;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Builder
|
||||||
|
@Getter
|
||||||
|
public class SubscriptionDTO {
|
||||||
|
private final UUID abonnementId;
|
||||||
|
private final Date dateDebut;
|
||||||
|
private final Date dateFin;
|
||||||
|
private final BigDecimal montantMensuel;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
public enum SubscriptionDuree {
|
||||||
|
M3,M6,M12;
|
||||||
|
|
||||||
|
private Integer[] subValues = {3,6,12};
|
||||||
|
private BigDecimal[] subPrices = {BigDecimal.valueOf(9.99), BigDecimal.valueOf(9.50),
|
||||||
|
BigDecimal.valueOf(8.99)};
|
||||||
|
|
||||||
|
public int getValue(){
|
||||||
|
return subValues[this.ordinal()];
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getMonthlyPricing(){
|
||||||
|
return subPrices[this.ordinal()];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record SubscriptionInfo (
|
||||||
|
UUID clientId,
|
||||||
|
SubscriptionDuree duree,
|
||||||
|
ModePaiement modePaiement,
|
||||||
|
Date dateDebutSouhaitee
|
||||||
|
) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||||
|
|
||||||
|
public enum TypePaiement {
|
||||||
|
CB,
|
||||||
|
Paypal
|
||||||
|
}
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
public final class SubscriptionConverter {
|
||||||
|
|
||||||
|
private SubscriptionConverter() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Subscription toDomain(SubscriptionInfo subscriptionInfo) {
|
||||||
|
Subscription sub = Subscription.builder()
|
||||||
|
.clientId(subscriptionInfo.clientId())
|
||||||
|
.duree(subscriptionInfo.duree())
|
||||||
|
.modePaiement(subscriptionInfo.modePaiement())
|
||||||
|
.dateDebutSouhaitee(subscriptionInfo.dateDebutSouhaitee())
|
||||||
|
.build();
|
||||||
|
sub.setRandomUUID();
|
||||||
|
return sub;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SubscriptionDTO toDTO(Subscription subscription) {
|
||||||
|
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(subscription.getDateDebutSouhaitee());
|
||||||
|
cal.add(Calendar.MONTH, subscription.getDuree().getValue());
|
||||||
|
|
||||||
|
return SubscriptionDTO.builder()
|
||||||
|
.abonnementId(subscription.getAbonnementId())
|
||||||
|
.dateDebut(subscription.getDateDebutSouhaitee())
|
||||||
|
.dateFin(cal.getTime())
|
||||||
|
.montantMensuel(subscription.getDuree().getMonthlyPricing())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
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 lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Builder
|
||||||
|
@Getter
|
||||||
|
public class Subscription {
|
||||||
|
private UUID abonnementId;
|
||||||
|
private UUID clientId;
|
||||||
|
private SubscriptionDuree duree;
|
||||||
|
private ModePaiement modePaiement;
|
||||||
|
private Date dateDebutSouhaitee;
|
||||||
|
|
||||||
|
public void setRandomUUID() {
|
||||||
|
this.abonnementId = UUID.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||||
|
|
||||||
|
public class NotValidSubscriptionException extends Exception {
|
||||||
|
|
||||||
|
public NotValidSubscriptionException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class SubscriptionNotFoundException extends Exception{
|
||||||
|
public static final String THE_SUBSCRIPTION_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The Subscription with id {0} does not exist";
|
||||||
|
|
||||||
|
public SubscriptionNotFoundException(UUID uuid) {
|
||||||
|
super(MessageFormat.format(THE_SUBSCRIPTION_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
|
||||||
|
}
|
||||||
|
}
|
||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
public final class SubscriptionRepository {
|
||||||
|
|
||||||
|
private final List<Subscription> subscriptions = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<Subscription> findAll() {
|
||||||
|
return subscriptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAll() {
|
||||||
|
subscriptions.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Subscription save(Subscription newSubscription) {
|
||||||
|
Optional<Subscription> optionalBookWithSameId = this.findByUuid(newSubscription.getAbonnementId());
|
||||||
|
optionalBookWithSameId.ifPresentOrElse(subscriptions::remove, newSubscription::setRandomUUID);
|
||||||
|
this.subscriptions.add(newSubscription);
|
||||||
|
return newSubscription;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Subscription> findByUuid(UUID uuid) {
|
||||||
|
return this.subscriptions.stream()
|
||||||
|
.filter(subscription -> subscription.getAbonnementId().equals(uuid))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsById(UUID uuid) {
|
||||||
|
return this.subscriptions.stream()
|
||||||
|
.anyMatch(subscription -> subscription.getAbonnementId().equals(uuid));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Subscription> findByClientUuid(UUID uuid) {
|
||||||
|
return this.subscriptions.stream()
|
||||||
|
.filter(subscription -> subscription.getClientId().equals(uuid))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(Subscription subscription) {
|
||||||
|
this.subscriptions.remove(subscription);
|
||||||
|
}
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.converter.SubscriptionConverter;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.SubscriptionNotFoundException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository.SubscriptionRepository;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator.SubscriptionValidator;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class SubscriptionUseCase {
|
||||||
|
|
||||||
|
private final SubscriptionRepository subscriptionRepository;
|
||||||
|
|
||||||
|
public SubscriptionUseCase(SubscriptionRepository subscriptionRepository) {
|
||||||
|
this.subscriptionRepository = subscriptionRepository;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public SubscriptionDTO registerSubscription(SubscriptionInfo subscriptionInfo) throws NotValidSubscriptionException {
|
||||||
|
SubscriptionValidator.validate(subscriptionInfo);
|
||||||
|
Subscription subscriptionToRegister = SubscriptionConverter.toDomain(subscriptionInfo);
|
||||||
|
Subscription registeredSubscription = subscriptionRepository.save(subscriptionToRegister);
|
||||||
|
return SubscriptionConverter.toDTO(registeredSubscription);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<SubscriptionDTO> findSubscriptionByUuid(UUID uuid) {
|
||||||
|
Optional<Subscription> optionalSubscription = subscriptionRepository.findByClientUuid(uuid);
|
||||||
|
return optionalSubscription.map(SubscriptionConverter::toDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SubscriptionDTO updateSubscription(UUID uuid, SubscriptionInfo subscriptionInfo) throws SubscriptionNotFoundException, NotValidSubscriptionException {
|
||||||
|
SubscriptionValidator.validate(subscriptionInfo);
|
||||||
|
Subscription existingSubscription = getSubscriptionIfNotFoundThrowException(uuid);
|
||||||
|
Subscription updatedSubscription = Subscription.builder()
|
||||||
|
.abonnementId(uuid)
|
||||||
|
.clientId(subscriptionInfo.clientId())
|
||||||
|
.duree(subscriptionInfo.duree())
|
||||||
|
.modePaiement(subscriptionInfo.modePaiement())
|
||||||
|
.dateDebutSouhaitee(subscriptionInfo.dateDebutSouhaitee())
|
||||||
|
.build();
|
||||||
|
Subscription saved = subscriptionRepository.save(updatedSubscription);
|
||||||
|
return SubscriptionConverter.toDTO(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteSubscription(UUID uuid) throws SubscriptionNotFoundException {
|
||||||
|
Subscription subscriptionToDelete = getSubscriptionIfNotFoundThrowException(uuid);
|
||||||
|
subscriptionRepository.delete(subscriptionToDelete);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Subscription getSubscriptionIfNotFoundThrowException(UUID uuid) throws SubscriptionNotFoundException {
|
||||||
|
Optional<Subscription> optionalSubscription = subscriptionRepository.findByUuid(uuid);
|
||||||
|
if (optionalSubscription.isEmpty()) {
|
||||||
|
throw new SubscriptionNotFoundException(uuid);
|
||||||
|
}
|
||||||
|
return optionalSubscription.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+51
@@ -0,0 +1,51 @@
|
|||||||
|
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.TypePaiement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||||
|
|
||||||
|
public final class SubscriptionValidator {
|
||||||
|
|
||||||
|
public static final String Client_ID_NOT_VALID = "l'identifient du client est incorect" ;
|
||||||
|
public static final String LA_DUREE_RENSEIGNEE_N_EST_PAS_CORRECTE = "La durée renseignée n'est pas correcte";
|
||||||
|
public static final String LE_TYPE_DE_PAYEMENT_N_EST_PAS_PRIS_EN_CHARGE = "Le type de payement n'est pas pris en charge";
|
||||||
|
public static final String LA_DATE_DE_DEBUT_N_A_PAS_ETE_RENSEIGNEE = "La date de début n'a pas été renseignée";
|
||||||
|
|
||||||
|
private SubscriptionValidator() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void validate(SubscriptionInfo bookInfo) throws NotValidSubscriptionException {
|
||||||
|
validateClientID(bookInfo);
|
||||||
|
validateDuree(bookInfo);
|
||||||
|
validateModePaiement(bookInfo);
|
||||||
|
validateDateDebut(bookInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateClientID(SubscriptionInfo bookInfo) throws NotValidSubscriptionException {
|
||||||
|
if (bookInfo.clientId() == null){
|
||||||
|
throw new NotValidSubscriptionException(Client_ID_NOT_VALID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateDuree(SubscriptionInfo bookInfo) throws NotValidSubscriptionException {
|
||||||
|
try {
|
||||||
|
if (bookInfo.duree().getValue() != 3 && bookInfo.duree().getValue() != 6 && bookInfo.duree().getValue() != 12){
|
||||||
|
throw new NotValidSubscriptionException(LA_DUREE_RENSEIGNEE_N_EST_PAS_CORRECTE);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new NotValidSubscriptionException(LA_DUREE_RENSEIGNEE_N_EST_PAS_CORRECTE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateModePaiement(SubscriptionInfo bookInfo) throws NotValidSubscriptionException{
|
||||||
|
if (bookInfo.modePaiement().getType() != TypePaiement.CB && bookInfo.modePaiement().getType() != TypePaiement.Paypal){
|
||||||
|
throw new NotValidSubscriptionException(LE_TYPE_DE_PAYEMENT_N_EST_PAS_PRIS_EN_CHARGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateDateDebut(SubscriptionInfo bookInfo) throws NotValidSubscriptionException{
|
||||||
|
if (bookInfo.dateDebutSouhaitee() == null){
|
||||||
|
throw new NotValidSubscriptionException(LA_DATE_DE_DEBUT_N_A_PAS_ETE_RENSEIGNEE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+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);
|
||||||
|
}
|
||||||
|
}
|
||||||
+61
@@ -0,0 +1,61 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
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 = {
|
||||||
|
"Client UUID must be set",
|
||||||
|
"Durée cannot be blank",
|
||||||
|
"ModePayement cannot be blank",
|
||||||
|
"DateDebutSouhaitee cannot be blank"
|
||||||
|
})
|
||||||
|
@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 = "Client UUID must be set";
|
||||||
|
|
||||||
|
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 = "Price must be positive";
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new NotValidSubscriptionException(errorMessage);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertEquals(NotValidSubscriptionException.class, e.getClass());
|
||||||
|
assertEquals(errorMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+211
@@ -0,0 +1,211 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.*;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.SubscriptionNotFoundException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository.SubscriptionRepository;
|
||||||
|
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.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
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