Compare commits

..

15 Commits

57 changed files with 50 additions and 3647 deletions
-1
View File
@@ -1 +0,0 @@
Notre groupe est constitué de Marvin Aubert, Maxime Lebreton et de Patrick Felix Vimalaratnam
-5
View File
@@ -117,11 +117,6 @@
<version>${mockito.version}</version> <version>${mockito.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@@ -0,0 +1,6 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
import java.util.List;
public record ComandeInfo(List<LigneCommandeInfo> listeLigne ,String modePayement) {
}
@@ -16,8 +16,4 @@ public class CommandeDTO {
private String codePostal; private String codePostal;
private String pays; private String pays;
private String modePaiement; private String modePaiement;
private UUID commandeId ;
private double montantTotal ;
private Integer pointsFideliteGagnes ;
} }
@@ -1,6 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
import java.util.List;
public record CommandeInfo(List<LigneCommandeInfo> listeLigne , String modePayement) {
}
@@ -1,36 +1,26 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter; package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.ComandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeDTO; import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande; import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
public final class CommandeConverter { public final class CommandeConverter {
public static Commande toDomain(CommandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) { public static Commande toDomain(ComandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
return Commande.builder() return Commande.builder()
.lignesCommande(commandeInfo.listeLigne()) .lignesCommande(commandeInfo.listeLigne())
.rue(adressInfo.rue()) .rue(adressInfo.rue())
.ville(adressInfo.ville()) .ville(adressInfo.ville())
.codePostal(adressInfo.codePostal()) .codePostal(adressInfo.codePostal())
.pays(adressInfo.pays()) .pays(adressInfo.pays())
.modePaiement(commandeInfo.modePayement()) .modePaiement(modePaiement)
.build(); .build();
} }
public static Commande toDomain(CommandeInfo commandeInfo, AdresseInfo adressInfo) { public static CommandeDTO toDTO(ComandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
return Commande.builder()
.lignesCommande(commandeInfo.listeLigne())
.rue(adressInfo.rue())
.ville(adressInfo.ville())
.codePostal(adressInfo.codePostal())
.pays(adressInfo.pays())
.modePaiement(commandeInfo.modePayement())
.build();
}
public static CommandeDTO toDTO(CommandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
return CommandeDTO.builder() return CommandeDTO.builder()
.lignesCommande(commandeInfo.listeLigne()) .lignesCommande(commandeInfo.listeLigne())
.rue(adressInfo.rue()) .rue(adressInfo.rue())
@@ -41,19 +31,4 @@ public final class CommandeConverter {
.build(); .build();
} }
public static CommandeDTO toDTO(Commande commande) {
return CommandeDTO.builder()
.lignesCommande(commande.getLignesCommande())
.rue(commande.getRue())
.ville(commande.getVille())
.codePostal(commande.getCodePostal())
.pays(commande.getPays())
.modePaiement(commande.getModePaiement())
.commandeId(commande.getCommandeId())
.clientId(commande.getClientId())
.montantTotal(commande.getMontantTotal())
.pointsFideliteGagnes(commande.getPointsFideliteGagnes())
.build();
}
} }
@@ -1,13 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
import java.text.MessageFormat;
import java.util.UUID;
public class CommandeNotFoundException extends RuntimeException {
public static final String THE_COMMANDE_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The commande with id {0} does not exist";
public CommandeNotFoundException(UUID uuid) {
super(MessageFormat.format(THE_COMMANDE_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
}
}
@@ -1,23 +1,20 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository; package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande; import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
public class CommandeRepository { public class ComandeRepository {
private final List<Commande> commande = new ArrayList<>(); private final List<Commande> commande = new ArrayList<>();
public List<Commande> findAll(){return commande;} public List<Commande> findAll(){return commande;}
public void deleteAll() {
commande.clear();
}
public Commande save(Commande newCommande){ public Commande save(Commande newCommande){
Optional<Commande> optionalCommandeWithSameIsbn = this.findById(newCommande.getCommandeId()); Optional<Commande> optionalCommandeWithSameIsbn = this.findById(newCommande.getCommandeId());
optionalCommandeWithSameIsbn.ifPresent(commande::remove); optionalCommandeWithSameIsbn.ifPresent(commande::remove);
@@ -36,12 +33,4 @@ public class CommandeRepository {
.anyMatch(customer -> customer.getCommandeId().equals(uuid)); .anyMatch(customer -> customer.getCommandeId().equals(uuid));
} }
public void delete(Commande commande) {
this.commande.remove(commande);
}
public void deleteById(UUID uuid) {
this.commande.removeIf(commande -> commande.getClientId().equals(uuid));
}
} }
@@ -1,73 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.converter.CommandeConverter;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.CommandeNotFoundException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.CommandeRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.validator.CommandeValidator;
import java.util.Optional;
import java.util.UUID;
public class CommandeUseCase {
private final CommandeRepository commandeRepository;
public CommandeUseCase(CommandeRepository commandeRepository) {
this.commandeRepository = commandeRepository;
}
public UUID registerCommande(CommandeInfo validCommandeInfo, AdresseInfo validAdresseInfo){
CommandeValidator.validate(validCommandeInfo);
CommandeValidator.validate(validAdresseInfo);
Commande commandeToRegister = CommandeConverter.toDomain(validCommandeInfo,validAdresseInfo);
Commande commandeToRegistered = commandeRepository.save(commandeToRegister) ;
return commandeToRegistered.getCommandeId();
}
public Optional<CommandeDTO> findCommandByUUID(UUID uuid) {
Optional<Commande> optionalCommande = commandeRepository.findById(uuid);
return optionalCommande.map(CommandeConverter::toDTO);
}
public CommandeDTO updateCommande(UUID uuid, CommandeInfo commandeInfo, AdresseInfo adresseInfo)
throws CommandeNotFoundException, NotValidCommandeException {
CommandeValidator.validate(commandeInfo);
CommandeValidator.validate(adresseInfo);
Commande commandeByUUID = getCommandeIfDoesNotExistThrowCommandeNotFoundException(
uuid);
Commande commande = Commande.builder()
.clientId(commandeByUUID.getClientId())
.lignesCommande(commandeInfo.listeLigne())
.rue(adresseInfo.rue())
.ville(adresseInfo.ville())
.codePostal(adresseInfo.codePostal())
.pays(adresseInfo.pays())
.modePaiement(commandeInfo.modePayement())
.commandeId(uuid)
.build();
Commande updatedCommande = commandeRepository.save(commande);
return CommandeConverter.toDTO(updatedCommande);
}
public void deleteCommande(UUID uuid) throws CommandeNotFoundException {
Commande commandeToDelete = getCommandeIfDoesNotExistThrowCommandeNotFoundException(uuid);
this.commandeRepository.delete(commandeToDelete);
}
private Commande getCommandeIfDoesNotExistThrowCommandeNotFoundException(UUID uuid)
throws CommandeNotFoundException {
Optional<Commande> optionalCommandeById = commandeRepository.findById(uuid);
if (optionalCommandeById.isEmpty()) {
throw new CommandeNotFoundException(uuid);
}
return optionalCommandeById.get();
}
}
@@ -1,12 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator; package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.ComandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException; import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException; import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException; import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public final class CommandeValidator { public final class CommandeValidator {
@@ -27,10 +28,10 @@ public final class CommandeValidator {
} }
} }
public static void validate(CommandeInfo commandeInfo) { public static void validate(ComandeInfo comandeInfo) {
validateModePaiement(commandeInfo); validateModePaiement(comandeInfo);
validateListLigneCommande(commandeInfo); validateListLigneCommande(comandeInfo);
} }
public static void validate(AdresseInfo adresseInfo) { public static void validate(AdresseInfo adresseInfo) {
@@ -42,16 +43,16 @@ public final class CommandeValidator {
private static void validateModePaiement(CommandeInfo commandeInfo) { private static void validateModePaiement(ComandeInfo comandeInfo) {
if(commandeInfo.modePayement()!="CB"&& commandeInfo.modePayement()!="PAYPAL"&& commandeInfo.modePayement()!="POINTS_FIDELITE"){ if(comandeInfo.modePayement()!="CB"&& comandeInfo.modePayement()!="PAYPAL"&&comandeInfo.modePayement()!="POINTS_FIDELITE"){
throw new NotValidCommandeException(MODE_PAIEMENT_IS_NOT_VALIDE); throw new NotValidCommandeException(MODE_PAIEMENT_IS_NOT_VALIDE);
} }
} }
private static void validateListLigneCommande(CommandeInfo commandeInfo) { private static void validateListLigneCommande(ComandeInfo comandeInfo) {
if (commandeInfo.listeLigne() == null if (comandeInfo.listeLigne() == null
|| commandeInfo.listeLigne().isEmpty() || comandeInfo.listeLigne().isEmpty()
|| commandeInfo.listeLigne().stream().anyMatch(Objects::isNull)){ || comandeInfo.listeLigne().stream().anyMatch(Objects::isNull)){
throw new NotValidCommandeException(LIST_LIGNE_COMANDE_IS_NOT_VALIDE); throw new NotValidCommandeException(LIST_LIGNE_COMANDE_IS_NOT_VALIDE);
} }
@@ -1,19 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review;
import lombok.Builder;
import lombok.Getter;
import java.time.LocalDate;
import java.util.UUID;
@Getter
@Builder
public class ReviewDTO {
private UUID reviewId;
private UUID customerId;
private UUID bookId;
private Integer note;
private String comment;
private LocalDate purchaseDate;
}
@@ -1,7 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review;
import java.time.LocalDate;
import java.util.UUID;
public record ReviewInfo(Integer note, String comment, LocalDate purchaseDate) {
}
@@ -1,30 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.converter;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
public class ReviewConverter {
private ReviewConverter() {
}
public static Review toDomain(ReviewInfo newReview) {
return Review.builder()
.note(newReview.note())
.comment(newReview.comment())
.purchaseDate(newReview.purchaseDate())
.build();
}
public static ReviewDTO toDTO(Review review) {
return ReviewDTO.builder()
.reviewId(review.getReviewId())
.customerId(review.getCustomerId())
.bookId(review.getBookId())
.note(review.getNote())
.comment(review.getComment())
.purchaseDate(review.getPurchaseDate())
.build();
}
}
@@ -1,29 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.entity;
import lombok.Builder;
import lombok.Getter;
import java.time.LocalDate;
import java.util.UUID;
@Getter
@Builder
public class Review {
private UUID reviewId;
private UUID customerId;
private UUID bookId;
private Integer note;
private String comment;
private LocalDate purchaseDate;
public void setRandomUUID() {
this.reviewId = UUID.randomUUID();
}
public void setRandomUUIDCustomerAndBook() {
this.customerId = UUID.randomUUID();
this.bookId = UUID.randomUUID();
}
}
@@ -1,7 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
public class NotValidReviewException extends RuntimeException {
public NotValidReviewException(String message) {
super(message);
}
}
@@ -1,25 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
import java.text.MessageFormat;
import java.util.Optional;
import java.util.UUID;
public class ReviewNotFoundException extends RuntimeException {
public static final String THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the customer id {0} does not exists";
public static final String THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE = "The reviews with the book id {0} does not exists";
public static final String THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE = "The review with review id {0} does not exists";
public ReviewNotFoundException(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> reviewUUID) {
super(buildMessage(customerUUID, bookUUID, reviewUUID));
}
private static String buildMessage(Optional<UUID> customerUUID, Optional<UUID> bookUUID, Optional<UUID> reviewUUID) {
if (customerUUID.isPresent()) {
return MessageFormat.format(THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE, customerUUID.get());
}else if (bookUUID.isPresent()) {
return MessageFormat.format(THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE, bookUUID.get());
}
return MessageFormat.format(THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE, reviewUUID.get());
}
}
@@ -1,79 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
public class ReviewRepository {
private final List<Review> reviews = new ArrayList<>();
public List<Review> findAll() {
return reviews;
}
public void deleteAll() {
reviews.clear();
}
public Review save(Review newReview) {
Optional<Review> optionalReviewWithSameReviewId = this.findByReviewId(newReview.getReviewId());
optionalReviewWithSameReviewId.ifPresent(reviews::remove);
this.reviews.add(newReview);
return newReview;
}
public ArrayList<Review> findByCustomerId(UUID customerUUID) {
return this.reviews.stream()
.filter(review -> review.getCustomerId().equals(customerUUID))
.collect(Collectors.toCollection(ArrayList::new));
}
public ArrayList<Review> findByBookId(UUID bookUUID) {
return this.reviews.stream()
.filter(review -> review.getBookId().equals(bookUUID))
.collect(Collectors.toCollection(ArrayList::new));
}
public Optional<Review> findByReviewId(UUID reviewUUID) {
return this.reviews.stream()
.filter(review -> review.getReviewId().equals(reviewUUID))
.findFirst();
}
public boolean existsByCustomerId(UUID customerUUID) {
return this.reviews.stream()
.anyMatch(review -> review.getCustomerId().equals(customerUUID));
}
public boolean existsByBookId(UUID bookUUID) {
return this.reviews.stream()
.anyMatch(review -> review.getBookId().equals(bookUUID));
}
public boolean existsByReviewId(UUID reviewUUID) {
return this.reviews.stream()
.anyMatch(review -> review.getReviewId().equals(reviewUUID));
}
public void deleteCustomerReviews(UUID customerUUID) {
this.reviews.removeIf(review -> review.getCustomerId().equals(customerUUID));
}
public void deleteBookReviews(UUID bookUUID) {
this.reviews.removeIf(review -> review.getBookId().equals(bookUUID));
}
public void delete(Review review) {
this.reviews.remove(review);
}
}
@@ -1,113 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.review.converter.ReviewConverter;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.ReviewNotFoundException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.review.validator.ReviewValidator;
import java.util.ArrayList;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
public class ReviewUseCase {
private final ReviewRepository reviewRepository;
public ReviewUseCase(ReviewRepository reviewRepository) {
this.reviewRepository = reviewRepository;
}
public UUID registerReview(ReviewInfo newReview) throws NotValidReviewException {
ReviewValidator.validate(newReview);
Review reviewToRegister = ReviewConverter.toDomain(newReview);
Review reviewToRegistered = reviewRepository.save(reviewToRegister);
return reviewToRegistered.getReviewId();
}
public ArrayList<ReviewDTO> findReviewByCustomerId(UUID customerId) {
ArrayList<Review> optionalReviews = reviewRepository.findByCustomerId(customerId);
return optionalReviews.stream()
.map(ReviewConverter::toDTO)
.collect(Collectors.toCollection(ArrayList::new));
}
public ArrayList<ReviewDTO> findReviewByBookId(UUID bookId) {
ArrayList<Review> optionalReviews = reviewRepository.findByBookId(bookId);
return optionalReviews.stream()
.map(ReviewConverter::toDTO)
.collect(Collectors.toCollection(ArrayList::new));
}
public Optional<ReviewDTO> findReviewByReviewId(UUID reviewId) {
Optional<Review> optionalReview = reviewRepository.findByReviewId(reviewId);
return optionalReview.map(ReviewConverter::toDTO);
}
public ReviewDTO updateReview(UUID reviewUUID, ReviewInfo reviewInfo)
throws ReviewNotFoundException, NotValidReviewException {
ReviewValidator.validate(reviewInfo);
Review reviewByReviewUUID = getReviewIfDoesNotExistThrowReviewNotFoundException(
reviewUUID);
Review review = Review.builder()
.reviewId(reviewUUID)
.customerId(reviewByReviewUUID.getCustomerId())
.bookId(reviewByReviewUUID.getBookId())
.note(reviewByReviewUUID.getNote())
.comment(reviewByReviewUUID.getComment())
.purchaseDate(reviewByReviewUUID.getPurchaseDate())
.build();
Review updatedReview = reviewRepository.save(review);
return ReviewConverter.toDTO(updatedReview);
}
public void deleteReview(UUID reviewUUID) throws ReviewNotFoundException {
Review reviewToDelete = getReviewIfDoesNotExistThrowReviewNotFoundException(reviewUUID);
this.reviewRepository.delete(reviewToDelete);
}
public void deleteCustomerReviews(UUID customerUUID) throws ReviewNotFoundException {
ArrayList<Review> reviewsToDelete = getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(customerUUID);
for (Review review : reviewsToDelete) {
reviewRepository.delete(review);
}
}
public void deleteBookReviews(UUID bookUUID) throws ReviewNotFoundException {
ArrayList<Review> reviewsToDelete = getReviewByBookIfDoesNotExistThrowReviewNotFoundException(bookUUID);
for (Review review : reviewsToDelete) {
reviewRepository.delete(review);
}
}
private Review getReviewIfDoesNotExistThrowReviewNotFoundException(UUID reviewUUID)
throws ReviewNotFoundException {
Optional<Review> optionalReviewByReviewId = reviewRepository.findByReviewId(reviewUUID);
if (optionalReviewByReviewId.isEmpty()) {
throw new ReviewNotFoundException(Optional.empty(), Optional.empty(),Optional.of(reviewUUID));
}
return optionalReviewByReviewId.get();
}
private ArrayList<Review> getReviewByCustomerIdIfDoesNotExistThrowReviewNotFoundException(UUID customerUUID)
throws ReviewNotFoundException {
ArrayList<Review> optionalReviewByReviewId = reviewRepository.findByCustomerId(customerUUID);
if (optionalReviewByReviewId.isEmpty()) {
throw new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(),Optional.empty());
}
return optionalReviewByReviewId;
}
private ArrayList<Review> getReviewByBookIfDoesNotExistThrowReviewNotFoundException(UUID bookUUID)
throws ReviewNotFoundException {
ArrayList<Review> optionalReviewByReviewId = reviewRepository.findByBookId(bookUUID);
if (optionalReviewByReviewId.isEmpty()) {
throw new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(bookUUID));
}
return optionalReviewByReviewId;
}
}
@@ -1,52 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
import java.time.LocalDate;
public class ReviewValidator {
public static final String NOTE_CANNOT_BE_LOWER_THAN_1 = "Note is greater than or equal to 1";
public static final String NOTE_CANNOT_BE_UPPER_THAN_5 = "Note is less than or equal to 5";
public static final String COMMENT_CANNOT_BE_BLANK = "Comment cannot be blank";
public static final String PURCHASE_DATE_IS_NOT_VALID = "Date is not valid";
public ReviewValidator() {
}
public static void validate(ReviewInfo newReview) throws NotValidReviewException {
validateNoteLower1(newReview);
validateNoteUpper5(newReview);
validateComment(newReview);
validatePurchaseDate(newReview);
}
private static void validateNoteLower1(ReviewInfo newReview)
throws NotValidReviewException {
if (newReview.note() <= 1) {
throw new NotValidReviewException(NOTE_CANNOT_BE_LOWER_THAN_1);
}
}
private static void validateNoteUpper5(ReviewInfo newReview)
throws NotValidReviewException {
if (newReview.note() >= 5) {
throw new NotValidReviewException("Note is less than or equal to 5");
}
}
private static void validateComment(ReviewInfo newReview) throws NotValidReviewException {
if (newReview.comment().isBlank()) {
throw new NotValidReviewException(COMMENT_CANNOT_BE_BLANK);
}
}
private static void validatePurchaseDate(ReviewInfo newReview) throws NotValidReviewException {
if (newReview.purchaseDate().isAfter(LocalDate.now())) {
throw new NotValidReviewException(PURCHASE_DATE_IS_NOT_VALID);
}
}
}
@@ -1,17 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
public enum DesiredSubscriptionDuration {
THREE(3),
SIX(6),
TWELVE(12);
private final Integer value;
DesiredSubscriptionDuration(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
}
@@ -1,4 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
public record PaymentMethodInfo(String paymentType, Object details) {
}
@@ -1,6 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
public enum PaymentType {
CB,
PAYPAL;
}
@@ -1,22 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.PaymentMethod;
import lombok.Builder;
import lombok.Getter;
import java.time.LocalDate;
import java.util.UUID;
@Builder
@Getter
public class SubscriptionDTO {
private UUID subscriptionId;
private UUID customerId;
private Integer desiredSubscriptionDuration;
private PaymentMethod paymentMethod;
private LocalDate desiredStartDate;
private LocalDate startDate;
private LocalDate endDate;
private double monthlyAmount;
}
@@ -1,6 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
import java.time.LocalDate;
public record SubscriptionInfo(Integer desiredSubscriptionDuration, LocalDate desiredStartDate) {
}
@@ -1,43 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.converter;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentMethodInfo;
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.PaymentMethod;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
public class SubscriptionConverter {
private SubscriptionConverter(){
}
public static Subscription toDomain(SubscriptionInfo newSubscription, PaymentMethodInfo newPaymentMethod) {
PaymentMethod paymentMethod = PaymentMethod.builder()
.paymentType(newPaymentMethod.paymentType())
.details(newPaymentMethod.details())
.build();
return Subscription.builder()
.desiredSubscriptionDuration(newSubscription.desiredSubscriptionDuration())
.paymentMethod(paymentMethod)
.desiredStartDate(newSubscription.desiredStartDate())
.startDate(newSubscription.desiredStartDate())
.endDate(newSubscription.desiredStartDate().plusMonths(newSubscription.desiredSubscriptionDuration()))
.monthlyAmount(0)
.build();
}
public static SubscriptionDTO toDTO(Subscription subscription) {
return SubscriptionDTO.builder()
.subscriptionId(subscription.getSubscriptionId())
.customerId(subscription.getCustomerId())
.desiredSubscriptionDuration(subscription.getDesiredSubscriptionDuration())
.paymentMethod(subscription.getPaymentMethod())
.desiredStartDate(subscription.getDesiredStartDate())
.startDate(subscription.getStartDate())
.endDate(subscription.getEndDate())
.monthlyAmount(subscription.getMonthlyAmount())
.build();
}
}
@@ -1,12 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
import lombok.Builder;
import lombok.Getter;
@Builder
@Getter
public class PaymentMethod {
private String paymentType;
private Object details;
}
@@ -1,44 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
import lombok.Builder;
import lombok.Getter;
import java.time.LocalDate;
import java.util.UUID;
@Builder
@Getter
public class Subscription {
private UUID subscriptionId;
private UUID customerId;
private Integer desiredSubscriptionDuration;
private PaymentMethod paymentMethod;
private LocalDate desiredStartDate;
private LocalDate startDate;
private LocalDate endDate;
private double monthlyAmount;
public void setRandomSubscriptionUUID() {
this.subscriptionId = UUID.randomUUID();
}
public void setRandomCustomerUUID() {
this.customerId = UUID.randomUUID();
}
public void setStartDate(){
this.startDate = this.desiredStartDate;
}
public void setEndDate(){
this.endDate = this.desiredStartDate.plusMonths(this.desiredSubscriptionDuration);
}
public void setDateSubscription(){
setStartDate();
setEndDate();
}
}
@@ -1,8 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
public class NotValidPaymentMethodException extends RuntimeException {
public NotValidPaymentMethodException(String message) {
super(message);
}
}
@@ -1,8 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
public class NotValidSubscriptionException extends RuntimeException {
public NotValidSubscriptionException(String message) {
super(message);
}
}
@@ -1,21 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
import java.text.MessageFormat;
import java.util.Optional;
import java.util.UUID;
public class SubscriptionNotFoundException extends RuntimeException {
public static final String THE_SUBSCRIPTION_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE = "The subscriptions with the customer id {0} does not exists";
public static final String THE_SUBSCRIPTION_WITH_SUBSCRIPTION_ID_DOES_NOT_EXIST_MESSAGE = "The subscription with subscription id {0} does not exists";
public SubscriptionNotFoundException(Optional<UUID> customerUUID, Optional<UUID> subscriptionUUID) {
super(buildMessage(customerUUID, subscriptionUUID));
}
private static String buildMessage(Optional<UUID> customerUUID, Optional<UUID> subscriptionUUID) {
if (customerUUID.isPresent()) {
return MessageFormat.format(THE_SUBSCRIPTION_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE, customerUUID.get());
}
return MessageFormat.format(THE_SUBSCRIPTION_WITH_SUBSCRIPTION_ID_DOES_NOT_EXIST_MESSAGE, subscriptionUUID.get());
}
}
@@ -1,62 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public 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> optionalSubscriptionWithSameSubscriptionId = this.findBySubscriptionId(newSubscription.getSubscriptionId());
optionalSubscriptionWithSameSubscriptionId.ifPresent(subscriptions::remove);
this.subscriptions.add(newSubscription);
return newSubscription;
}
public List<Subscription> FindByCustomerId(UUID customerUUID) {
return this.subscriptions.stream()
.filter(subscription -> subscription.getCustomerId().equals(customerUUID))
.toList();
}
public Optional<Subscription> findBySubscriptionId(UUID subscriptionUUID) {
return this.subscriptions.stream()
.filter(subscription -> subscription.getSubscriptionId().equals(subscriptionUUID))
.findFirst();
}
public boolean existsByCustomerId(UUID customerUUID) {
return this.subscriptions.stream()
.anyMatch(review -> review.getCustomerId().equals(customerUUID));
}
public boolean existsBySubscriptionId(UUID subscriptionUUID) {
return this.subscriptions.stream()
.anyMatch(subscription -> subscription.getSubscriptionId().equals(subscriptionUUID));
}
public void deleteByCustomerId(UUID customerUUID) {
this.subscriptions.removeIf(subscription -> subscription.getCustomerId().equals(customerUUID));
}
public void delete(Subscription subscription) {
this.subscriptions.remove(subscription);
}
}
@@ -1,110 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentMethodInfo;
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.PaymentMethod;
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.PaymentMethodValidator;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator.SubscriptionValidator;
import java.util.*;
public class SubscriptionUseCase {
private final SubscriptionRepository subscriptionRepository;
public SubscriptionUseCase(SubscriptionRepository subscriptionRepository) {
this.subscriptionRepository = subscriptionRepository;
}
private Map<String, Object> Output(Subscription subscription){
Map<String, Object> result = new HashMap<>();
result.put("subscriptionId", subscription.getSubscriptionId());
result.put("startDate", subscription.getStartDate());
result.put("endDate", subscription.getEndDate());
result.put("monthlyAmount", subscription.getMonthlyAmount());
return result;
}
public Map<String, Object> registerSubscription(SubscriptionInfo newSubscription, PaymentMethodInfo newPaymentMethod)
throws NotValidSubscriptionException {
SubscriptionValidator.validate(newSubscription);
PaymentMethodValidator.validate(newPaymentMethod);
Subscription subscriptionToRegister = SubscriptionConverter.toDomain(newSubscription, newPaymentMethod);
Subscription subscriptionToRegistered = subscriptionRepository.save(subscriptionToRegister);
return Output(subscriptionToRegistered);
}
public List<SubscriptionDTO> findSubscriptionByCustomerId(UUID customerId) {
List<Subscription> optionalSubscriptions = subscriptionRepository.FindByCustomerId(customerId);
return optionalSubscriptions.stream()
.map(SubscriptionConverter::toDTO)
.toList();
}
public Optional<SubscriptionDTO> findSubscriptionBySubscriptionId(UUID subscriptionId) {
Optional<Subscription> optionalSubscription = subscriptionRepository.findBySubscriptionId(subscriptionId);
return optionalSubscription.map(SubscriptionConverter::toDTO);
}
public SubscriptionDTO updateSubscription(UUID SubscriptionUUID, SubscriptionInfo subscriptionInfo, PaymentMethodInfo paymentMethodInfo)
throws SubscriptionNotFoundException, NotValidSubscriptionException {
SubscriptionValidator.validate(subscriptionInfo);
PaymentMethodValidator.validate(paymentMethodInfo);
Subscription subscriptionBySubscriptionUUID = getSubscriptionIfDoesNotExistThrowSubscriptionNotFoundException(
SubscriptionUUID);
PaymentMethod paymentMethod = PaymentMethod.builder()
.paymentType(paymentMethodInfo.paymentType())
.details(paymentMethodInfo.details())
.build();
Subscription subscription = Subscription.builder()
.subscriptionId(SubscriptionUUID)
.customerId(subscriptionBySubscriptionUUID.getCustomerId())
.desiredSubscriptionDuration(subscriptionInfo.desiredSubscriptionDuration())
.paymentMethod(paymentMethod)
.desiredStartDate(subscriptionInfo.desiredStartDate())
.monthlyAmount(subscriptionBySubscriptionUUID.getMonthlyAmount())
.build();
subscription.setDateSubscription();
Subscription updatedSubscription = subscriptionRepository.save(subscription);
return SubscriptionConverter.toDTO(updatedSubscription);
}
public void deleteSubscription(UUID subscriptionUUID) throws SubscriptionNotFoundException {
Subscription subscriptionToDelete = getSubscriptionIfDoesNotExistThrowSubscriptionNotFoundException(subscriptionUUID);
this.subscriptionRepository.delete(subscriptionToDelete);
}
public void deleteSubscriptionsOfACustomer(UUID customerUUID) throws SubscriptionNotFoundException {
List<Subscription> subscriptionsToDelete = getSubscriptionByCustomerIdIfDoesNotExistThrowSubscriptionNotFoundException(customerUUID);
for (Subscription subscription : subscriptionsToDelete) {
subscriptionRepository.delete(subscription);
}
}
private Subscription getSubscriptionIfDoesNotExistThrowSubscriptionNotFoundException(UUID subscriptionUUID)
throws SubscriptionNotFoundException {
Optional<Subscription> optionalSubscriptionBySubscriptionId = subscriptionRepository.findBySubscriptionId(subscriptionUUID);
if (optionalSubscriptionBySubscriptionId.isEmpty()) {
throw new SubscriptionNotFoundException(Optional.empty(),Optional.of(subscriptionUUID));
}
return optionalSubscriptionBySubscriptionId.get();
}
private List<Subscription> getSubscriptionByCustomerIdIfDoesNotExistThrowSubscriptionNotFoundException(UUID customerUUID)
throws SubscriptionNotFoundException {
List<Subscription> optionalSubscriptionBySubscriptionId = subscriptionRepository.FindByCustomerId(customerUUID);
if (optionalSubscriptionBySubscriptionId.isEmpty()) {
throw new SubscriptionNotFoundException(Optional.of(customerUUID),Optional.empty());
}
return optionalSubscriptionBySubscriptionId;
}
}
@@ -1,27 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentMethodInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidPaymentMethodException;
import java.util.Set;
public class PaymentMethodValidator {
public static final String MODE_PAIEMENT_IS_NOT_VALID = "paymentType is not valid";
private PaymentMethodValidator() {
}
public static void validate(PaymentMethodInfo newPaymentMethod)
throws NotValidPaymentMethodException {
validatePaymentType(newPaymentMethod);
}
private static void validatePaymentType(PaymentMethodInfo newPaymentMethod)
throws NotValidPaymentMethodException {
Set<String> Valid_Payment_Type = Set.of("CB", "PAYPAL");
if (!Valid_Payment_Type.contains(newPaymentMethod.paymentType())) {
throw new NotValidPaymentMethodException(MODE_PAIEMENT_IS_NOT_VALID);}
}
}
@@ -1,36 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
import java.time.LocalDate;
import java.util.Set;
public class SubscriptionValidator {
public static final String SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALID = "desiredSubscriptionDuration will be 3, 6 or 12 by the ENUM file SubscriptionDurationDesired";
public static final String DESIRED_START_DATE_CANNOT_BE_BEFORE_TODAY = "desiredStartDate cannot be before today";
private SubscriptionValidator() {
}
public static void validate(SubscriptionInfo newSubscription) throws NotValidSubscriptionException {
validateDesiredStartDate(newSubscription);
validateSubscriptionDurationDesired(newSubscription);
}
private static void validateSubscriptionDurationDesired(SubscriptionInfo newSubscription)
throws NotValidSubscriptionException {
Set<Integer> VALID_DURATIONS = Set.of(3, 6, 12);
if (!VALID_DURATIONS.contains(newSubscription.desiredSubscriptionDuration())) {
throw new NotValidSubscriptionException(SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALID);}
}
private static void validateDesiredStartDate(SubscriptionInfo newSubscription)
throws NotValidSubscriptionException {
if (newSubscription.desiredStartDate().isBefore(LocalDate.now())) {
throw new NotValidSubscriptionException(DESIRED_START_DATE_CANNOT_BE_BEFORE_TODAY);
}
}
}
@@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -21,7 +22,7 @@ public class CommandeConverterTest {
LigneCommandeInfo ligne2 = new LigneCommandeInfo(14); LigneCommandeInfo ligne2 = new LigneCommandeInfo(14);
listLigne.add(ligne); listLigne.add(ligne);
listLigne.add(ligne2); listLigne.add(ligne2);
CommandeInfo commandeInfo = new CommandeInfo(listLigne,modePaiement); ComandeInfo commandeInfo = new ComandeInfo(listLigne,modePaiement);
AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France"); AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France");
@@ -45,7 +46,7 @@ public class CommandeConverterTest {
LigneCommandeInfo ligne2 = new LigneCommandeInfo(14); LigneCommandeInfo ligne2 = new LigneCommandeInfo(14);
listLigne.add(ligne); listLigne.add(ligne);
listLigne.add(ligne2); listLigne.add(ligne2);
CommandeInfo commandeInfo = new CommandeInfo(listLigne,modePaiement); ComandeInfo commandeInfo = new ComandeInfo(listLigne,modePaiement);
AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France"); AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France");
@@ -0,0 +1,4 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
public class Test {
}
@@ -1,50 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.CommandeNotFoundException;
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;
public class CommandeNotFoundEsceptionTest {
@Test
@DisplayName("Exception message should contain the UUID provided")
void testExceptionMessageContainsUUID() {
UUID uuid = UUID.randomUUID();
CommandeNotFoundException exception = new CommandeNotFoundException(uuid);
String expectedMessage = String.format("The commande 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();
CommandeNotFoundException exception = new CommandeNotFoundException(uuid);
String expectedFormatWithPlaceholder = "The commande with id {0} does not exist";
assertEquals(CommandeNotFoundException.THE_COMMANDE_WITH_ID_DOES_NOT_EXIST_MESSAGE,
expectedFormatWithPlaceholder);
assertTrue(exception.getMessage().contains(uuid.toString()));
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
UUID uuid = UUID.randomUUID();
try {
throw new CommandeNotFoundException(uuid);
} catch (CommandeNotFoundException e) {
String expectedMessage = String.format("The commande with id %s does not exist", uuid);
assertEquals(expectedMessage, e.getMessage());
}
}
}
@@ -3,6 +3,8 @@ package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement; import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande; import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
@@ -13,18 +15,19 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; 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.assertTrue;
public class CommandeRepositoryTest { public class CommandeRepositoryTest {
private CommandeRepository repository; private ComandeRepository repository;
private Commande commandeN1; private Commande commandeN1;
private Commande commandeN2; private Commande commandeN2;
private Commande commandeN3; private Commande commandeN3;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
repository = new CommandeRepository(); repository = new ComandeRepository();
UUID clientId = UUID.randomUUID(); UUID clientId = UUID.randomUUID();
List<LigneCommandeInfo> lignesCommande = new ArrayList<>(); List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
@@ -65,6 +68,7 @@ public class CommandeRepositoryTest {
.pays(pays) .pays(pays)
.modePaiement(modePayement) .modePaiement(modePayement)
.build(); .build();
commandeN2.setRandomUUID();
commandeN2.setRandomUUIDCommande(); commandeN2.setRandomUUIDCommande();
List<LigneCommandeInfo> lignesCommande3 = new ArrayList<>(); List<LigneCommandeInfo> lignesCommande3 = new ArrayList<>();
@@ -216,8 +220,14 @@ public class CommandeRepositoryTest {
UUID nonExistentId = UUID.randomUUID(); UUID nonExistentId = UUID.randomUUID();
boolean exists = repository.existsById(nonExistentId); boolean exists = repository.existsById(nonExistentId);
assertFalse(exists); assertTrue(exists);
} }
} }
@@ -226,119 +236,6 @@ public class CommandeRepositoryTest {
@Nested @Nested
@DisplayName("Delete operations") @DisplayName("Delete operations")
class DeleteOperations { class DeleteOperations {
@BeforeEach
void setUpCustomers() {
repository.save(commandeN1);
repository.save(commandeN2);
repository.save(commandeN3);
}
@Test
@DisplayName("Delete should remove the specified customer")
void testDelete() {
repository.delete(commandeN1);
List<Commande> commande = repository.findAll();
assertEquals(2, commande.size());
assertFalse(commande.contains(commandeN1));
assertTrue(commande.contains(commandeN2));
assertTrue(commande.contains(commandeN3));
}
@Test
@DisplayName("DeleteAll should remove all customers")
void testDeleteAll() {
repository.deleteAll();
List<Commande> commande = repository.findAll();
assertTrue(commande.isEmpty());
assertEquals(0, commande.size());
}
@Test
@DisplayName("Delete should not throw exception when customer doesn't exist")
void testDeleteNonExistentCustomer() {
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
LigneCommandeInfo commande1 = new LigneCommandeInfo(321);
LigneCommandeInfo commande2 = new LigneCommandeInfo(3);
lignesCommande.add(commande1);
lignesCommande.add(commande2);
Commande nonExistentCommande = Commande.builder()
.lignesCommande(lignesCommande)
.rue("non")
.ville("non")
.codePostal("Existent")
.pays("0000")
.modePaiement(ModePaiement.PAYPAL.name())
.build();
nonExistentCommande.setRandomUUIDCommande();
assertDoesNotThrow(() -> repository.delete(nonExistentCommande));
assertEquals(3, repository.findAll().size());
}
@Test
void TestDeleteAllCommandeByCustomerID() {
System.out.println(commandeN1.getClientId());
System.out.println(commandeN2.getClientId());
System.out.println(commandeN3.getClientId());
repository.deleteById(commandeN1.getClientId());
List<Commande> commande = repository.findAll();
assertEquals(1, commande.size());
assertFalse(commande.contains(commandeN1));
assertFalse(commande.contains(commandeN2));
assertTrue(commande.contains(commandeN3));
}
@Test
void TestDeleteAllCommandeByNonExistentCustomerID() {
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
LigneCommandeInfo commande1 = new LigneCommandeInfo(321);
LigneCommandeInfo commande2 = new LigneCommandeInfo(3);
lignesCommande.add(commande1);
lignesCommande.add(commande2);
Commande nonExistentCommande = Commande.builder()
.lignesCommande(lignesCommande)
.rue("non")
.ville("non")
.codePostal("Existent")
.pays("0000")
.modePaiement(ModePaiement.PAYPAL.name())
.build();
nonExistentCommande.setRandomUUIDCommande();
repository.deleteById(nonExistentCommande.getClientId());
assertDoesNotThrow(() -> repository.findAll());
assertEquals(3, repository.findAll().size());
}
} }
@@ -1,249 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.*;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.CommandeNotFoundException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.CommandeRepository;
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.ArrayList;
import java.util.List;
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)
public class TestCommandeUseCase {
@Mock
private CommandeRepository commandeRepository;
@InjectMocks
private CommandeUseCase commandeUseCase;
private UUID commandeId;
private UUID clientId;
private Commande testCommande;
private CommandeInfo validCommandeInfo;
private LigneCommandeInfo validLigneCommandeInfo;
private AdresseInfo validAdresseInfo;
private List<LigneCommandeInfo> lignesCommande;
private LigneCommandeInfo commande1;
private LigneCommandeInfo commande2;
private String rue;
private String ville;
private String codePostal;
private String pays;
private String modePayement;
@BeforeEach
void setUp() {
clientId = UUID.randomUUID();
commandeId = UUID.randomUUID();
lignesCommande = new ArrayList<>();
commande1 = new LigneCommandeInfo(12);
commande2 = new LigneCommandeInfo(13);
lignesCommande.add(commande1);
lignesCommande.add(commande2);
rue = "rue du chien";
ville = "LKa Rochette";
codePostal = "7700";
pays = "France";
modePayement = ModePaiement.CB.name();
testCommande = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande)
.rue(rue)
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.modePaiement(modePayement)
.commandeId(commandeId)
.build();
validCommandeInfo = new CommandeInfo(lignesCommande,ModePaiement.CB.name());
validLigneCommandeInfo= new LigneCommandeInfo(12);
validAdresseInfo = new AdresseInfo("rue du chien","LKa Rochette","7700","France");
}
@Nested
class RegisterCommandeTest{
@Test
void testRegisterCommandeWhitValidData(){
when(commandeRepository.save(any(Commande.class))).thenReturn(testCommande);
UUID registeredIsbn = commandeUseCase.registerCommande(validCommandeInfo,validAdresseInfo);
assertNotNull(registeredIsbn);
assertEquals(commandeId, registeredIsbn);
verify(commandeRepository, times(1)).save(any(Commande.class));
}
@Test
void testRegisterCommandeWhitInvalidData(){
CommandeInfo invalidCommandeInfo = new CommandeInfo(new ArrayList<LigneCommandeInfo>(),"pas bon ");
AdresseInfo invalidAdressInfo = new AdresseInfo(""," "," ","");
assertThrows(NotValidCommandeException.class,
()->commandeUseCase.registerCommande(invalidCommandeInfo,invalidAdressInfo));
verify(commandeRepository, never()).save(any(Commande.class));
}
}
@Nested
class FindCommandeTests {
@Test
@DisplayName("Should return book when isbn exists")
void testFindCommandByUUID() {
when(commandeRepository.findById(testCommande.getCommandeId())).thenReturn(Optional.of(testCommande));
Optional<CommandeDTO> foundCommande = commandeUseCase.findCommandByUUID(testCommande.getCommandeId());
assertTrue(foundCommande.isPresent());
assertEquals(testCommande.getCommandeId(), foundCommande.get().getCommandeId());
verify(commandeRepository, times(1)).findById(testCommande.getCommandeId());
}
@Test
@DisplayName("Should return empty Optional when isbn doesn't exist")
void testFindCommandByUUIDNotFound() {
UUID uuid = UUID.randomUUID();
when(commandeRepository.findById(uuid)).thenReturn(Optional.empty());
Optional<CommandeDTO> foundCommande = commandeUseCase.findCommandByUUID(uuid);
assertTrue(foundCommande.isEmpty());
verify(commandeRepository, times(1)).findById(uuid);
}
}
@Nested
class UpdateCommandeTests {
@Test
@DisplayName("Should update commande when valid data is provided")
void testUpdateCommandeWithValidData() throws CommandeNotFoundException, NotValidCommandeException {
when(commandeRepository.findById(commandeId)).thenReturn(Optional.of(testCommande));
Commande updatedCommande = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande)
.rue(rue)
.ville("Fontainebleau")
.codePostal("77300")
.pays(pays)
.modePaiement(modePayement)
.commandeId(commandeId)
.build();
when(commandeRepository.save(any(Commande.class))).thenReturn(updatedCommande);
List<LigneCommandeInfo> updatelignesCommande = new ArrayList<>();
LigneCommandeInfo updateLigneCommandeInfo= new LigneCommandeInfo(12);
updatelignesCommande.add(updateLigneCommandeInfo);
CommandeInfo updateInfo = new CommandeInfo(updatelignesCommande,ModePaiement.CB.name());
AdresseInfo updateAdresseInfo = new AdresseInfo("rue du chien","Fontainebleau","77300","France");
CommandeDTO result = commandeUseCase.updateCommande(commandeId, updateInfo, updateAdresseInfo);
assertNotNull(result);
assertEquals(commandeId, result.getCommandeId());
assertEquals("Fontainebleau", result.getVille());
assertEquals("77300", result.getCodePostal());
verify(commandeRepository, times(1)).findById(commandeId);
verify(commandeRepository, times(1)).save(any(Commande.class));
}
@Test
@DisplayName("Should throw exception when commande ID doesn't exist")
void testUpdateCommandeNotFound() {
UUID nonExistentId = UUID.randomUUID();
when(commandeRepository.findById(nonExistentId)).thenReturn(Optional.empty());
List<LigneCommandeInfo> updatelignesCommande = new ArrayList<>();
LigneCommandeInfo updateLigneCommandeInfo= new LigneCommandeInfo(12);
updatelignesCommande.add(updateLigneCommandeInfo);
CommandeInfo updateInfo = new CommandeInfo(updatelignesCommande,ModePaiement.CB.name());
AdresseInfo updateAdresseInfo = new AdresseInfo("rue du chien","Fontainebleau","77300","France");
assertThrows(CommandeNotFoundException.class,
() -> commandeUseCase.updateCommande(nonExistentId, updateInfo, updateAdresseInfo));
verify(commandeRepository, times(1)).findById(nonExistentId);
verify(commandeRepository, never()).save(any(Commande.class));
}
@Test
@DisplayName("Should throw exception when update data is not valid")
void testUpdateCommandeWithInvalidData() {
List<LigneCommandeInfo> updatelignesCommande = new ArrayList<>();
LigneCommandeInfo updateLigneCommandeInfo= new LigneCommandeInfo(12);
updatelignesCommande.add(updateLigneCommandeInfo);
CommandeInfo invalidUpdateInfo = new CommandeInfo(updatelignesCommande, "CARTE");
AdresseInfo updateAdresseInfo = new AdresseInfo("rue du chien","Fontainebleau","77300","France");
assertThrows(NotValidCommandeException.class,
() -> commandeUseCase.updateCommande(commandeId, invalidUpdateInfo, updateAdresseInfo));
verify(commandeRepository, never()).findById(any(UUID.class));
verify(commandeRepository, never()).save(any(Commande.class));
}
}
@Nested
class DeleteCommandeTests {
@Test
@DisplayName("Should delete commande when ID exists")
void testDeleteCommande() throws CommandeNotFoundException {
when(commandeRepository.findById(commandeId)).thenReturn(Optional.of(testCommande));
doNothing().when(commandeRepository).delete(testCommande);
commandeUseCase.deleteCommande(commandeId);
verify(commandeRepository, times(1)).findById(commandeId);
verify(commandeRepository, times(1)).delete(testCommande);
}
@Test
@DisplayName("Should throw exception when commande ID doesn't exist")
void testDeleteCustomerNotFound() {
UUID nonExistentId = UUID.randomUUID();
when(commandeRepository.findById(nonExistentId)).thenReturn(Optional.empty());
assertThrows(CommandeNotFoundException.class,
() -> commandeUseCase.deleteCommande(nonExistentId));
verify(commandeRepository, times(1)).findById(nonExistentId);
verify(commandeRepository, never()).delete(any(Commande.class));
}
}
}
@@ -1,7 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator; package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.ComandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement; import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException; import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
@@ -47,7 +47,7 @@ public class CommandeValidatorTest {
List<LigneCommandeInfo> listeLigne = new ArrayList<>(); List<LigneCommandeInfo> listeLigne = new ArrayList<>();
listeLigne.add(ligneCommandeValide); listeLigne.add(ligneCommandeValide);
String modePaiementValide = modePaiement.toString(); String modePaiementValide = modePaiement.toString();
CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide); ComandeInfo commandeValid = new ComandeInfo(listeLigne, modePaiementValide);
assertDoesNotThrow(() -> CommandeValidator.validate(commandeValid)); assertDoesNotThrow(() -> CommandeValidator.validate(commandeValid));
} }
@@ -59,7 +59,7 @@ public class CommandeValidatorTest {
List<LigneCommandeInfo> listeLigne = new ArrayList<>(); List<LigneCommandeInfo> listeLigne = new ArrayList<>();
listeLigne.add(ligneCommandeValide); listeLigne.add(ligneCommandeValide);
String modePaiementValide = modePaiementError; String modePaiementValide = modePaiementError;
CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide); ComandeInfo commandeValid = new ComandeInfo(listeLigne, modePaiementValide);
NotValidCommandeException exception = assertThrows( NotValidCommandeException exception = assertThrows(
NotValidCommandeException.class, NotValidCommandeException.class,
@@ -72,7 +72,7 @@ public class CommandeValidatorTest {
@MethodSource("provideInvalidOrderLists") @MethodSource("provideInvalidOrderLists")
void testValidateNotValideListLigneCommande(List<LigneCommandeInfo> listeLigne) { void testValidateNotValideListLigneCommande(List<LigneCommandeInfo> listeLigne) {
String modePaiementValide = ModePaiement.CB.toString(); String modePaiementValide = ModePaiement.CB.toString();
CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide); ComandeInfo commandeValid = new ComandeInfo(listeLigne, modePaiementValide);
NotValidCommandeException exception = assertThrows( NotValidCommandeException exception = assertThrows(
NotValidCommandeException.class, NotValidCommandeException.class,
() -> CommandeValidator.validate(commandeValid) () -> CommandeValidator.validate(commandeValid)
@@ -1,68 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.converter;
import java.time.LocalDate;
import java.util.UUID;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@DisplayName("ReviewConverterTest Unit Tests")
public class ReviewConverterTest {
@Nested
@DisplayName("toDomain() method tests")
class ToDomainTests {
@Test
@DisplayName("Should convert ReviewInfo to Review domain object")
void shouldConvertReviewInfoToDomain() {
// Given
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo reviewInfo = new ReviewInfo(5, "tres bon livre", purchaseDate);
// When
Review result = ReviewConverter.toDomain(reviewInfo);
// Then
assertNotNull(result);
assertEquals(reviewInfo.note(), result.getNote());
assertEquals(reviewInfo.comment(), result.getComment());
assertEquals(reviewInfo.purchaseDate(), result.getPurchaseDate());
}
}
@Nested
@DisplayName("toDTO() method tests")
class ToDTOTests {
@Test
@DisplayName("Should convert Review domain object to ReviewDTO with all fields mapped correctly")
void shouldConvertReviewToDTO() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
Review review = Review.builder()
.reviewId(UUID.randomUUID())
.customerId(UUID.randomUUID())
.bookId(UUID.randomUUID())
.note(5)
.comment("très bon livre")
.purchaseDate(purchaseDate)
.build();
ReviewDTO result = ReviewConverter.toDTO(review);
assertNotNull(result);
assertEquals(review.getCustomerId(), result.getCustomerId());
assertEquals(review.getBookId(), result.getBookId());
assertEquals(review.getNote(), result.getNote());
assertEquals(review.getComment(), result.getComment());
assertEquals(review.getPurchaseDate(), result.getPurchaseDate());
}
}
}
@@ -1,55 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.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.*;
public class ReviewTest {
@Test
@DisplayName("Builder should create a valid review instance")
void testReviewBuilder() {
UUID customerId = UUID.randomUUID();
UUID bookId = UUID.randomUUID();
Integer note = 5;
String comment = "très bon livre";
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
Review review = Review.builder()
.customerId(customerId)
.bookId(bookId)
.note(note)
.comment(comment)
.purchaseDate(purchaseDate)
.build();
assertEquals(customerId, review.getCustomerId());
assertEquals(bookId, review.getBookId());
assertEquals(note, review.getNote());
assertEquals(comment, review.getComment());
assertEquals(purchaseDate, review.getPurchaseDate());
}
@Test
@DisplayName("setRandomUUID should change the ID to a new random UUID")
void testSetRandomUUID() {
Review review = Review.builder().build();
UUID originalReviewId = review.getReviewId();
UUID originalCustomerId = review.getCustomerId();
UUID originalBookId = review.getCustomerId();
review.setRandomUUID();
review.setRandomUUIDCustomerAndBook();
assertNotNull(review.getReviewId());
assertNotNull(review.getCustomerId());
assertNotNull(review.getBookId());
assertNotEquals(originalReviewId, review.getReviewId());
assertNotEquals(originalCustomerId, review.getCustomerId());
assertNotEquals(originalBookId, review.getBookId());
}
}
@@ -1,60 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NotValidReviewExcpetionTest {
@Test
@DisplayName("Exception should be created with the provided message")
void testExceptionCreation() {
String errorMessage = "Review data is not valid";
NotValidReviewException exception = new NotValidReviewException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {
"Note is greater than or equal to 1",
"Note is less than or equal to 1",
"Comment cannot be empty"
})
@DisplayName("Exception should handle different validation messages")
void testExceptionWithDifferentMessages(String errorMessage) {
NotValidReviewException exception = new NotValidReviewException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "Comment field is empty";
Exception exception = assertThrows(NotValidReviewException.class, () -> {
throw new NotValidReviewException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid review data";
try {
throw new NotValidReviewException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidReviewException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -1,98 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ReviewNotFoundExceptionTest {
@Test
@DisplayName("Exception message should contain the UUID provided for customer")
void testExceptionMessageContainsUUIDForCustomer() {
UUID customerUUID = UUID.randomUUID();
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(), Optional.empty());
String expectedMessage = String.format("The reviews with the customer id %s does not exists", customerUUID);
assertEquals(expectedMessage, exception.getMessage());
}
@Test
@DisplayName("Exception message should contain the UUID provided for book")
void testExceptionMessageContainsUUIDForBook() {
UUID bookUUID = UUID.randomUUID();
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID), Optional.empty());
String expectedMessage = String.format("The reviews with the book id %s does not exists", bookUUID);
assertEquals(expectedMessage, exception.getMessage());
}
@Test
@DisplayName("Exception message should contain the UUID provided for customer and book")
void testExceptionMessageContainsUUIDForCustomerAndBook() {
UUID reviewUUID = UUID.randomUUID();
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(reviewUUID));
String expectedMessage = String.format("The review with review id %s does not exists", reviewUUID);
assertEquals(expectedMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should use the correct constant message format for customer")
void testExceptionUsesConstantMessageCustomerFormat() {
UUID customerUUID = UUID.randomUUID();
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.of(customerUUID), Optional.empty(), Optional.empty());
String expectedFormatWithPlaceholder = "The reviews with the customer id {0} does not exists";
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE,
expectedFormatWithPlaceholder);
assertTrue(exception.getMessage().contains(customerUUID.toString()));
}
@Test
@DisplayName("Exception should use the correct constant message format for book")
void testExceptionUsesConstantMessageBookFormat() {
UUID bookUUID = UUID.randomUUID();
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.of(bookUUID), Optional.empty());
String expectedFormatWithPlaceholder = "The reviews with the book id {0} does not exists";
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_BOOK_ID_DOES_NOT_EXIST_MESSAGE,
expectedFormatWithPlaceholder);
assertTrue(exception.getMessage().contains(bookUUID.toString()));
}
@Test
@DisplayName("Exception should use the correct constant message format for review")
void testExceptionUsesConstantMessageReviewFormat() {
UUID reviewUUID = UUID.randomUUID();
ReviewNotFoundException exception = new ReviewNotFoundException(Optional.empty(), Optional.empty(), Optional.of(reviewUUID));
String expectedFormatWithPlaceholder = "The review with review id {0} does not exists";
assertEquals(ReviewNotFoundException.THE_REVIEWS_WITH_REVIEW_ID_DOES_NOT_EXIST_MESSAGE,
expectedFormatWithPlaceholder);
assertTrue(exception.getMessage().contains(reviewUUID.toString()));
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
UUID reviewUUID = UUID.randomUUID();
try {
throw new ReviewNotFoundException(Optional.empty(),Optional.empty(), Optional.of(reviewUUID));
} catch (ReviewNotFoundException e) {
String expectedMessage = String.format("The review with review id %s does not exists", reviewUUID);
assertEquals(expectedMessage, e.getMessage());
}
}
}
@@ -1,361 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
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.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ReviewRepositoryTest {
private ReviewRepository repository;
private Review review1;
private Review review2;
private Review review3;
private Review review4;
@BeforeEach
void setUp() {
repository = new ReviewRepository();
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
review1 = Review.builder()
.note(1)
.comment("nul")
.purchaseDate(purchaseDate)
.build();
review1.setRandomUUID();
review1.setRandomUUIDCustomerAndBook();
UUID customerId = UUID.randomUUID();
UUID bookId = UUID.randomUUID();
review2 = Review.builder()
.customerId(customerId)
.bookId(bookId)
.note(1)
.comment("nul")
.purchaseDate(purchaseDate)
.build();
review2.setRandomUUID();
UUID bookId3 = UUID.randomUUID();
review3 = Review.builder()
.customerId(customerId)
.bookId(bookId3)
.note(2)
.comment("ça passe")
.purchaseDate(purchaseDate)
.build();
review3.setRandomUUID();
UUID customerId4 = UUID.randomUUID();
review4 = Review.builder()
.customerId(customerId4)
.bookId(bookId)
.note(2)
.comment("ça passe")
.purchaseDate(purchaseDate)
.build();
review4.setRandomUUID();
}
@Test
@DisplayName("New repository should be empty")
void testNewRepositoryIsEmpty() {
List<Review> reviews = repository.findAll();
assertTrue(reviews.isEmpty());
assertEquals(0, reviews.size());
}
@Nested
@DisplayName("Save operations")
class SaveOperations {
@Test
@DisplayName("Save should add a new review")
void testSaveNewReview() {
Review savedReview = repository.save(review1);
assertEquals(1, repository.findAll().size());
assertEquals(review1.getCustomerId(), savedReview.getCustomerId());
assertEquals(review1.getBookId(), savedReview.getBookId());
}
@Test
@DisplayName("Save should update existing review with same customer and book ID")
void testSaveUpdatesExistingReview() {
repository.save(review1);
LocalDate purchaseDate = LocalDate.of(2026, 5, 24);
UUID reviewId = review1.getReviewId();
UUID customerId = UUID.randomUUID();
UUID bookId = UUID.randomUUID();
Review updatedReview = Review.builder()
.reviewId(reviewId)
.customerId(customerId)
.bookId(bookId)
.note(4)
.comment("pas mal")
.purchaseDate(purchaseDate)
.build();
Review savedReview = repository.save(updatedReview);
assertEquals(1, repository.findAll().size());
assertEquals(reviewId, savedReview.getReviewId());
assertEquals(customerId, savedReview.getCustomerId());
assertEquals(bookId, savedReview.getBookId());
assertEquals(4, savedReview.getNote());
assertEquals("pas mal", savedReview.getComment());
assertEquals(purchaseDate, savedReview.getPurchaseDate());
}
@Test
@DisplayName("Save multiple review should add all of them")
void testSaveMultipleReviews() {
repository.save(review1);
repository.save(review2);
List<Review> reviews = repository.findAll();
assertEquals(2, reviews.size());
assertTrue(reviews.contains(review1));
assertTrue(reviews.contains(review2));
}
}
@Nested
@DisplayName("Find operations")
class FindOperations {
@BeforeEach
void setUpReviews() {
repository.save(review1);
repository.save(review2);
repository.save(review3);
repository.save(review4);
}
@Test
@DisplayName("FindAll should return all reviews")
void testFindAll() {
List<Review> reviews = repository.findAll();
assertEquals(4, reviews.size());
assertTrue(reviews.contains(review1));
assertTrue(reviews.contains(review2));
assertTrue(reviews.contains(review3));
assertTrue(reviews.contains(review4));
}
@Test
@DisplayName("findByCustomerId should return review with matching customer ID")
void testFindByCustomerId() {
ArrayList<Review> foundreviews = repository.findByCustomerId(review2.getCustomerId());
assertTrue(!foundreviews.isEmpty());
boolean allSameCustomer = foundreviews.stream()
.allMatch(review -> review.getCustomerId().equals(review2.getCustomerId()));
assertTrue(allSameCustomer);
}
@Test
@DisplayName("findByCustomerId should return empty Optional when a review with customer ID doesn't exist")
void testFindByCustomerIdNotFound() {
UUID nonExistentCustomerId = UUID.randomUUID();
ArrayList<Review> foundreview = repository.findByCustomerId(nonExistentCustomerId);
assertTrue(foundreview.isEmpty());
}
@Test
@DisplayName("findByBookId should return review with matching book ID")
void testFindByBookId() {
ArrayList<Review> foundreviews = repository.findByBookId(review2.getBookId());
assertTrue(!foundreviews.isEmpty());
boolean allSameCustomer = foundreviews.stream()
.allMatch(review -> review.getBookId().equals(review2.getBookId()));
assertTrue(allSameCustomer);
}
@Test
@DisplayName("findByBookId should return empty Optional when a review with book ID doesn't exist")
void testFindByBookIdNotFound() {
UUID nonExistentBookId = UUID.randomUUID();
ArrayList<Review> foundreview = repository.findByBookId(nonExistentBookId);
assertTrue(foundreview.isEmpty());
}
@Test
@DisplayName("findByReviewId should return review with matching review ID")
void testFindByReviewId() {
Optional<Review> foundreview = repository.findByReviewId(review1.getReviewId());
assertTrue(foundreview.isPresent());
assertEquals(review1.getNote(), foundreview.get().getNote());
assertEquals(review1.getComment(), foundreview.get().getComment());
}
@Test
@DisplayName("findByReviewId should return empty Optional when a review with review ID doesn't exist")
void testFindByReviewIdNotFound() {
UUID nonExistentReviewId = UUID.randomUUID();
Optional<Review> foundreview = repository.findByReviewId(nonExistentReviewId);
assertTrue(foundreview.isEmpty());
}
@Test
@DisplayName("existsByCustomerId should return true when a review with customer ID exists")
void testExistsByCustomerIdExists() {
boolean exists = repository.existsByCustomerId(review1.getCustomerId());
assertTrue(exists);
}
@Test
@DisplayName("existsByCustomerId should return false when a review with customer ID doesn't exist")
void testExistsByCustomerIdNotExists() {
UUID nonExistentCustomerId = UUID.randomUUID();
boolean exists = repository.existsByCustomerId(nonExistentCustomerId);
assertFalse(exists);
}
@Test
@DisplayName("existsByBookId should return true when a review with book ID exists")
void testExistsByBookIdExists() {
boolean exists = repository.existsByBookId(review1.getBookId());
assertTrue(exists);
}
@Test
@DisplayName("existsByBookId should return false when a review with book ID doesn't exist")
void testExistsByBookIdNotExists() {
UUID nonExistentBookId = UUID.randomUUID();
boolean exists = repository.existsByBookId(nonExistentBookId);
assertFalse(exists);
}
@Test
@DisplayName("existsByReviewId should return true when a review with review ID exists")
void testExistsByReviewIdExists() {
boolean exists = repository.existsByReviewId(review1.getReviewId());
assertTrue(exists);
}
@Test
@DisplayName("existsByReviewId should return false when review ID doesn't exist")
void testExistsByReviewIdNotExists() {
UUID nonExistentReviewId = UUID.randomUUID();
boolean exists = repository.existsByReviewId(nonExistentReviewId);
assertFalse(exists);
}
}
@Nested
@DisplayName("Delete operations")
class DeleteOperations {
@BeforeEach
void setUpReviews() {
repository.save(review1);
repository.save(review2);
repository.save(review3);
repository.save(review4);
}
@Test
@DisplayName("Delete should remove all reviews of a customer")
void testDeleteCustomerReviews() {
repository.deleteCustomerReviews(review2.getCustomerId());
List<Review> reviews = repository.findAll();
assertEquals(2, reviews.size());
assertTrue(reviews.contains(review1));
assertFalse(reviews.contains(review2));
assertFalse(reviews.contains(review3));
assertTrue(reviews.contains(review4));
}
@Test
@DisplayName("Delete should remove all reviews of a book")
void testDeleteBookReviews() {
repository.deleteBookReviews(review2.getBookId());
List<Review> reviews = repository.findAll();
assertEquals(2, reviews.size());
assertTrue(reviews.contains(review1));
assertFalse(reviews.contains(review2));
assertTrue(reviews.contains(review3));
assertFalse(reviews.contains(review4));
}
@Test
@DisplayName("Delete should remove the specified review")
void testDelete() {
repository.delete(review1);
List<Review> reviews = repository.findAll();
assertEquals(3, reviews.size());
assertFalse(reviews.contains(review1));
assertTrue(reviews.contains(review2));
assertTrue(reviews.contains(review3));
assertTrue(reviews.contains(review4));
}
@Test
@DisplayName("DeleteAll should remove all reviews")
void testDeleteAll() {
repository.deleteAll();
List<Review> reviews = repository.findAll();
assertTrue(reviews.isEmpty());
assertEquals(0, reviews.size());
}
@Test
@DisplayName("Delete should not throw exception when review doesn't exist")
void testDeleteNonExistentReview() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
Review nonExistentReview = Review.builder()
.note(1)
.comment("nul")
.purchaseDate(purchaseDate)
.build();
nonExistentReview.setRandomUUID();
assertDoesNotThrow(() -> repository.delete(nonExistentReview));
assertEquals(4, repository.findAll().size());
}
}
}
@@ -1,318 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.ReviewNotFoundException;
import fr.iut_fbleau.but3.dev62.mylibrary.review.repository.ReviewRepository;
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.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.never;
@ExtendWith(MockitoExtension.class)
public class ReviewUseCaseTest {
@Mock
private ReviewRepository reviewRepository;
@InjectMocks
private ReviewUseCase reviewUseCase;
private UUID reviewId;
private UUID customerId;
private UUID bookId;
private LocalDate purchaseDate;
private Review testReview;
private ReviewInfo validReviewInfo;
@BeforeEach
void setUp() {
reviewId = UUID.randomUUID();
customerId = UUID.randomUUID();
bookId = UUID.randomUUID();
purchaseDate = LocalDate.of(2026, 5, 24);
testReview = Review.builder()
.reviewId(reviewId)
.customerId(customerId)
.bookId(bookId)
.note(2)
.comment("plutôt mauvais")
.purchaseDate(purchaseDate)
.build();
validReviewInfo = new ReviewInfo(2, "plutôt mauvais", purchaseDate);
}
@Nested
@DisplayName("Register review tests")
class RegisterReviewTests {
@Test
@DisplayName("Should register review when valid data is provided")
void testRegisterReviewWithValidData() throws NotValidReviewException {
when(reviewRepository.save(any(Review.class))).thenReturn(testReview);
UUID registeredId = reviewUseCase.registerReview(validReviewInfo);
assertNotNull(registeredId);
assertEquals(reviewId, registeredId);
verify(reviewRepository, times(1)).save(any(Review.class));
}
@Test
@DisplayName("Should throw exception when review data is not valid")
void testRegisterReviewWithInvalidData() {
ReviewInfo invalidReviewInfo = new ReviewInfo(0, "plutôt mauvais", purchaseDate);
assertThrows(NotValidReviewException.class,
() -> reviewUseCase.registerReview(invalidReviewInfo));
verify(reviewRepository, never()).save(any(Review.class));
}
}
@Nested
@DisplayName("Find review tests")
class FindReviewTests {
@Test
@DisplayName("Should return reviews when customer ID exists")
void testFindReviewByCustomerId() {
when(reviewRepository.findByCustomerId(customerId)).thenReturn(new ArrayList<Review>(List.of(testReview)));
ArrayList<ReviewDTO> foundReviews = reviewUseCase.findReviewByCustomerId(customerId);
assertTrue(!foundReviews.isEmpty());
boolean allSameCustomer = foundReviews.stream()
.allMatch(review -> review.getCustomerId().equals(customerId));
assertTrue(allSameCustomer);
verify(reviewRepository, times(1)).findByCustomerId(customerId);
}
@Test
@DisplayName("Should return empty Optional when customer ID doesn't exist")
void testFindReviewByCustomerIdNotFound() {
UUID nonExistentCustomerId = UUID.randomUUID();
when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(new ArrayList<Review>());
ArrayList<ReviewDTO> foundReviews = reviewUseCase.findReviewByCustomerId(nonExistentCustomerId);
assertTrue(foundReviews.isEmpty());
verify(reviewRepository, times(1)).findByCustomerId(nonExistentCustomerId);
}
@Test
@DisplayName("Should return reviews when book ID exists")
void testFindReviewByBookId() {
when(reviewRepository.findByBookId(bookId)).thenReturn(new ArrayList<Review>(List.of(testReview)));
ArrayList<ReviewDTO> foundReviews = reviewUseCase.findReviewByBookId(bookId);
assertTrue(!foundReviews.isEmpty());
boolean allSameCustomer = foundReviews.stream()
.allMatch(review -> review.getBookId().equals(bookId));
assertTrue(allSameCustomer);
verify(reviewRepository, times(1)).findByBookId(bookId);
}
@Test
@DisplayName("Should return empty Optional when book ID doesn't exist")
void testFindReviewByBookIdNotFound() {
UUID nonExistentBookId = UUID.randomUUID();
when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(new ArrayList<Review>());
ArrayList<ReviewDTO> foundReviews = reviewUseCase.findReviewByBookId(nonExistentBookId);
assertTrue(foundReviews.isEmpty());
verify(reviewRepository, times(1)).findByBookId(nonExistentBookId);
}
@Test
@DisplayName("Should return review when Review ID exists")
void testFindReviewByReviewId() {
when(reviewRepository.findByReviewId(reviewId)).thenReturn(Optional.of(testReview));
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByReviewId(reviewId);
assertTrue(foundReview.isPresent());
assertEquals(testReview.getBookId(), foundReview.get().getBookId());
assertEquals(testReview.getNote(), foundReview.get().getNote());
verify(reviewRepository, times(1)).findByReviewId(reviewId);
}
@Test
@DisplayName("Should return empty Optional when review ID doesn't exist")
void testFindReviewByReviewIdNotFound() {
UUID nonExistentReviewId = UUID.randomUUID();
when(reviewRepository.findByReviewId(nonExistentReviewId)).thenReturn(Optional.empty());
Optional<ReviewDTO> foundReview = reviewUseCase.findReviewByReviewId(nonExistentReviewId);
assertTrue(foundReview.isEmpty());
verify(reviewRepository, times(1)).findByReviewId(nonExistentReviewId);
}
}
@Nested
@DisplayName("Update review tests")
class UpdateReviewTests {
@Test
@DisplayName("Should update review when valid data is provided")
void testUpdateReviewWithValidData() throws ReviewNotFoundException, NotValidReviewException {
when(reviewRepository.findByReviewId(reviewId)).thenReturn(Optional.of(testReview));
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 30);
Review updatedReview = Review.builder()
.reviewId(reviewId)
.customerId(customerId)
.bookId(bookId)
.note(4)
.comment("en fait c'est bien")
.purchaseDate(updatePurchaseDate)
.build();
when(reviewRepository.save(any(Review.class))).thenReturn(updatedReview);
ReviewInfo updateInfo = new ReviewInfo(4, "en fait c'est bien", updatePurchaseDate);
ReviewDTO result = reviewUseCase.updateReview(reviewId, updateInfo);
assertNotNull(result);
assertEquals(customerId, result.getCustomerId());
assertEquals(4, result.getNote());
assertEquals("en fait c'est bien", result.getComment());
verify(reviewRepository, times(1)).findByReviewId(reviewId);
verify(reviewRepository, times(1)).save(any(Review.class));
}
@Test
@DisplayName("Should throw exception when review ID doesn't exist")
void testUpdateReviewNotFound() {
UUID nonExistentReviewId = UUID.randomUUID();
when(reviewRepository.findByReviewId(nonExistentReviewId)).thenReturn(Optional.empty());
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24);
ReviewInfo updateInfo = new ReviewInfo(3, "moyen", updatePurchaseDate);
assertThrows(ReviewNotFoundException.class,
() -> reviewUseCase.updateReview(nonExistentReviewId, updateInfo));
verify(reviewRepository, times(1)).findByReviewId(nonExistentReviewId);
verify(reviewRepository, never()).save(any(Review.class));
}
@Test
@DisplayName("Should throw exception when update data is not valid")
void testUpdateReviewWithInvalidData() {
LocalDate updatePurchaseDate = LocalDate.of(2026, 5, 24);
ReviewInfo invalidUpdateInfo = new ReviewInfo(0, "éclaté au sol", updatePurchaseDate);
assertThrows(NotValidReviewException.class,
() -> reviewUseCase.updateReview(reviewId, invalidUpdateInfo));
verify(reviewRepository, never()).findByReviewId(any(UUID.class));
verify(reviewRepository, never()).save(any(Review.class));
}
}
@Nested
@DisplayName("Delete review tests")
class DeleteReviewTests {
@Test
@DisplayName("Should delete reviews when customer ID exists")
void testDeleteCustomerReviews() throws ReviewNotFoundException {
when(reviewRepository.findByCustomerId(customerId)).thenReturn(new ArrayList<Review>(List.of(testReview)));
doNothing().when(reviewRepository).delete(testReview);
reviewUseCase.deleteCustomerReviews(customerId);
verify(reviewRepository, times(1)).findByCustomerId(customerId);
verify(reviewRepository, times(1)).delete(testReview);
}
@Test
@DisplayName("Should throw exception when customer ID doesn't exist")
void testDeleteCustomerReviewsNotFound() {
UUID nonExistentCustomerId = UUID.randomUUID();
when(reviewRepository.findByCustomerId(nonExistentCustomerId)).thenReturn(new ArrayList<Review>());
assertThrows(ReviewNotFoundException.class,
() -> reviewUseCase.deleteCustomerReviews(nonExistentCustomerId));
verify(reviewRepository, times(1)).findByCustomerId(nonExistentCustomerId);
verify(reviewRepository, never()).delete(any(Review.class));
}
@Test
@DisplayName("Should delete reviews when book ID exists")
void testDeleteBookReviews() throws ReviewNotFoundException {
when(reviewRepository.findByBookId(bookId)).thenReturn(new ArrayList<Review>(List.of(testReview)));
doNothing().when(reviewRepository).delete(testReview);
reviewUseCase.deleteBookReviews(bookId);
verify(reviewRepository, times(1)).findByBookId(bookId);
verify(reviewRepository, times(1)).delete(testReview);
}
@Test
@DisplayName("Should throw exception when book ID doesn't exist")
void testDeleteBookReviewsNotFound() {
UUID nonExistentBookId = UUID.randomUUID();
when(reviewRepository.findByBookId(nonExistentBookId)).thenReturn(new ArrayList<Review>());
assertThrows(ReviewNotFoundException.class,
() -> reviewUseCase.deleteBookReviews(nonExistentBookId));
verify(reviewRepository, times(1)).findByBookId(nonExistentBookId);
verify(reviewRepository, never()).delete(any(Review.class));
}
@Test
@DisplayName("Should delete review when review ID exists")
void testDeleteReview() throws ReviewNotFoundException {
when(reviewRepository.findByReviewId(reviewId)).thenReturn(Optional.of(testReview));
doNothing().when(reviewRepository).delete(testReview);
reviewUseCase.deleteReview(reviewId);
verify(reviewRepository, times(1)).findByReviewId(reviewId);
verify(reviewRepository, times(1)).delete(testReview);
}
@Test
@DisplayName("Should throw exception when review ID doesn't exist")
void testDeleteReviewNotFound() {
UUID nonExistentReviewId = UUID.randomUUID();
when(reviewRepository.findByReviewId(nonExistentReviewId)).thenReturn(Optional.empty());
assertThrows(ReviewNotFoundException.class,
() -> reviewUseCase.deleteReview(nonExistentReviewId));
verify(reviewRepository, times(1)).findByReviewId(nonExistentReviewId);
verify(reviewRepository, never()).delete(any(Review.class));
}
}
}
@@ -1,113 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class ReviewValidatorTest {
@Test
@DisplayName("Should validate review with valid data")
void testValidateValidReview() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo validReview = new ReviewInfo(3, "Bof", purchaseDate);
assertDoesNotThrow(() -> ReviewValidator.validate(validReview));
}
@Nested
@DisplayName("Note validation tests")
class NoteValidationTests {
@Test
@DisplayName("Should throw exception when note is lower than 1")
void testValidateNoteLower1() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo reviewWithNoteLower1 = new ReviewInfo(0, "Bof", purchaseDate);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithNoteLower1)
);
assertEquals(ReviewValidator.NOTE_CANNOT_BE_LOWER_THAN_1, exception.getMessage());
}
@Test
@DisplayName("Should throw exception when note is upper than 5")
void testValidateNoteUpper5() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo reviewWithNoteUpper5 = new ReviewInfo(6, "Bof", purchaseDate);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithNoteUpper5)
);
assertEquals(ReviewValidator.NOTE_CANNOT_BE_UPPER_THAN_5, exception.getMessage());
}
}
@Nested
@DisplayName("Comment validation tests")
class CommentValidationTests {
@Test
@DisplayName("Should throw exception when comment is blank")
void testValidateBlankComment() {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo reviewWithBlankComment = new ReviewInfo(3, "", purchaseDate);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithBlankComment)
);
assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {" ", " ", "\t", "\n"})
@DisplayName("Should throw exception when last name contains only whitespace")
void testValidateWhitespaceLastName(String whitespace) {
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
ReviewInfo reviewWithBlankComment = new ReviewInfo(3, whitespace, purchaseDate);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithBlankComment)
);
assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage());
}
}
@Nested
@DisplayName("Purchase date validation tests")
class PurchaseDateValidationTests {
@Test
@DisplayName("Should throw exception when purchase date is after the actual date")
void testValidateFuturPurchaseDate() {
LocalDate futurepurchaseDate = LocalDate.of(2026, 6, 24);
ReviewInfo reviewWithFuturPurchaseDate = new ReviewInfo(2, "Bof", futurepurchaseDate);
NotValidReviewException exception = assertThrows(
NotValidReviewException.class,
() -> ReviewValidator.validate(reviewWithFuturPurchaseDate)
);
assertEquals(ReviewValidator.PURCHASE_DATE_IS_NOT_VALID, exception.getMessage());
}
}
}
@@ -1,125 +0,0 @@
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.PaymentMethod;
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.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, 6, 24);
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(DesiredSubscriptionDuration.THREE.getValue(), desiredStartDate);
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
// When
Subscription result = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo);
// Then
assertNotNull(result);
assertEquals(subscriptionInfo.desiredSubscriptionDuration(), result.getDesiredSubscriptionDuration());
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, 6, 24);
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
Subscription subscription = Subscription.builder()
.subscriptionId(UUID.randomUUID())
.customerId(UUID.randomUUID())
.desiredSubscriptionDuration(DesiredSubscriptionDuration.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.getDesiredSubscriptionDuration(), result.getDesiredSubscriptionDuration());
assertEquals(subscription.getPaymentMethod().getPaymentType(), result.getPaymentMethod().getPaymentType());
assertEquals(subscription.getPaymentMethod().getDetails(), result.getPaymentMethod().getDetails());
assertEquals(subscription.getDesiredStartDate(), result.getDesiredStartDate());
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, 6, 24);
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
Subscription subscription = Subscription.builder()
.subscriptionId(UUID.randomUUID())
.customerId(UUID.randomUUID())
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
.paymentMethod(paymentMethod)
.desiredStartDate(desiredStartDate)
.startDate(desiredStartDate)
.endDate(estimateEndDate)
.monthlyAmount(12.03)
.build();
SubscriptionDTO result = SubscriptionConverter.toDTO(subscription);
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, 6, 24);
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(DesiredSubscriptionDuration.THREE.getValue(), desiredStartDate);
Subscription domainResult = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo);
SubscriptionDTO dtoResult = SubscriptionConverter.toDTO(domainResult);
assertEquals("", dtoResult.getPaymentMethod().getDetails());
}
}
@@ -1,58 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentType;
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 PaymentMethod instance")
void testPaymentMethodBuilder() {
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 PaymentMethod instance with details as an Integer")
void testPaymentMethodDetailsInteger() {
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 PaymentMethod instance with details as an boolean")
void testPaymentMethodDetailsBoolean() {
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());
}
}
@@ -1,125 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentType;
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.DesiredSubscriptionDuration;
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 desiredSubscriptionDuration = DesiredSubscriptionDuration.THREE.getValue();
String paymentType = PaymentType.CB.name();
Object details = new Object();
details = "Maxime Lebreton";
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
LocalDate startDate = desiredStartDate;
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
double monthlyAmount = 17.99;
PaymentMethod paymentMethod = PaymentMethod.builder()
.paymentType(paymentType)
.details(details)
.build();
Subscription subscription = Subscription.builder()
.subscriptionId(subscriptionId)
.customerId(customerId)
.desiredSubscriptionDuration(desiredSubscriptionDuration)
.paymentMethod(paymentMethod)
.desiredStartDate(desiredStartDate)
.startDate(startDate)
.endDate(estimateEndDate)
.monthlyAmount(monthlyAmount)
.build();
assertEquals(subscriptionId, subscription.getSubscriptionId());
assertEquals(customerId, subscription.getCustomerId());
assertEquals(desiredSubscriptionDuration, subscription.getDesiredSubscriptionDuration());
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 testSetStartDate() {
LocalDate desiredStartDate = LocalDate.of(2026, 6, 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 + desiredSubscriptionDuration")
void testSetEndDate() {
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
Subscription subscription = Subscription.builder()
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
.desiredStartDate(desiredStartDate)
.build();
subscription.setEndDate();
assertNotNull(subscription.getEndDate());
assertEquals(estimateEndDate, subscription.getEndDate());
}
@Test
@DisplayName("setDateSubscription should instantiate StartDate and EndDate ")
void testSetDateSubscription() {
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
Subscription subscription = Subscription.builder()
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
.desiredStartDate(desiredStartDate)
.build();
subscription.setDateSubscription();
assertNotNull(subscription.getEndDate());
assertEquals(estimateEndDate, subscription.getEndDate());
}
}

Some files were not shown because too many files have changed in this diff Show More