Compare commits

..

6 Commits

61 changed files with 31 additions and 4145 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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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 pays;
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;
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.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
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()
.lignesCommande(commandeInfo.listeLigne())
.rue(adressInfo.rue())
.ville(adressInfo.ville())
.codePostal(adressInfo.codePostal())
.pays(adressInfo.pays())
.modePaiement(commandeInfo.modePayement())
.modePaiement(modePaiement)
.build();
}
public static Commande toDomain(CommandeInfo commandeInfo, AdresseInfo adressInfo) {
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) {
public static CommandeDTO toDTO(ComandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
return CommandeDTO.builder()
.lignesCommande(commandeInfo.listeLigne())
.rue(adressInfo.rue())
@@ -41,19 +31,4 @@ public final class CommandeConverter {
.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();
}
}
@@ -19,26 +19,8 @@ public class Commande {
private String pays;
private String modePaiement;
private UUID commandeId ;
private double montantTotal ;
private Integer pointsFideliteGagnes ;
public void setRandomUUID() {
this.clientId = UUID.randomUUID();
}
public void setRandomUUIDCommande() {
this.commandeId = UUID.randomUUID();
}
public int convertPointsFidelite(){
if (this.montantTotal==0|| this.montantTotal<=0){
return 0;
}
return (int) Math.ceil(this.getMontantTotal());
}
// conversion point de fidéliter
}
@@ -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,47 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public class CommandeRepository {
private final List<Commande> commande = new ArrayList<>();
public List<Commande> findAll(){return commande;}
public void deleteAll() {
commande.clear();
}
public Commande save(Commande newCommande){
Optional<Commande> optionalCommandeWithSameIsbn = this.findById(newCommande.getCommandeId());
optionalCommandeWithSameIsbn.ifPresent(commande::remove);
this.commande.add(newCommande);
return newCommande;
}
public Optional<Commande> findById(UUID uuid){
return this.commande.stream()
.filter(customer -> customer.getCommandeId().equals(uuid))
.findFirst();
}
public boolean existsById(UUID uuid) {
return this.commande.stream()
.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;
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.exception.NotValidAdressException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
import java.util.List;
import java.util.Objects;
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);
validateListLigneCommande(commandeInfo);
validateModePaiement(comandeInfo);
validateListLigneCommande(comandeInfo);
}
public static void validate(AdresseInfo adresseInfo) {
@@ -42,16 +43,16 @@ public final class CommandeValidator {
private static void validateModePaiement(CommandeInfo commandeInfo) {
if(commandeInfo.modePayement()!="CB"&& commandeInfo.modePayement()!="PAYPAL"&& commandeInfo.modePayement()!="POINTS_FIDELITE"){
private static void validateModePaiement(ComandeInfo comandeInfo) {
if(comandeInfo.modePayement()!="CB"&& comandeInfo.modePayement()!="PAYPAL"&&comandeInfo.modePayement()!="POINTS_FIDELITE"){
throw new NotValidCommandeException(MODE_PAIEMENT_IS_NOT_VALIDE);
}
}
private static void validateListLigneCommande(CommandeInfo commandeInfo) {
if (commandeInfo.listeLigne() == null
|| commandeInfo.listeLigne().isEmpty()
|| commandeInfo.listeLigne().stream().anyMatch(Objects::isNull)){
private static void validateListLigneCommande(ComandeInfo comandeInfo) {
if (comandeInfo.listeLigne() == null
|| comandeInfo.listeLigne().isEmpty()
|| comandeInfo.listeLigne().stream().anyMatch(Objects::isNull)){
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.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -21,7 +22,7 @@ public class CommandeConverterTest {
LigneCommandeInfo ligne2 = new LigneCommandeInfo(14);
listLigne.add(ligne);
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");
@@ -45,7 +46,7 @@ public class CommandeConverterTest {
LigneCommandeInfo ligne2 = new LigneCommandeInfo(14);
listLigne.add(ligne);
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");
@@ -1,11 +1,9 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
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.customer.entity.Customer;
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 java.util.ArrayList;
import java.util.List;
@@ -27,7 +25,6 @@ public class CommandeTest {
String ville = "LKa Rochette" ;
String codePostal = "7700" ;
String pays = "France" ;
String modePayement = ModePaiement.CB.name() ;
Commande commande = Commande.builder()
.clientId(clientId)
@@ -36,7 +33,6 @@ public class CommandeTest {
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.modePaiement(modePayement)
.build();
assertEquals(clientId, commande.getClientId());
@@ -45,7 +41,6 @@ public class CommandeTest {
assertEquals(ville, commande.getVille());
assertEquals(codePostal, commande.getCodePostal());
assertEquals(pays, commande.getPays());
assertEquals(modePayement, commande.getModePaiement());
}
@@ -62,40 +57,6 @@ public class CommandeTest {
assertNotEquals(originalId, commande.getClientId());
}
@Test
@DisplayName("setRandomUUID should change the ID to a new random UUID")
void testSetRandomUUIDForCommande() {
Commande commande = Commande.builder().build();
UUID originalId = commande.getCommandeId();
commande.setRandomUUIDCommande();
assertNotNull(commande.getCommandeId());
assertNotEquals(originalId, commande.getCommandeId());
}
@Test
void testConvertPointsFideliteGagnes(){
Commande commande = Commande.builder()
.montantTotal(14.34)
.build();
Integer point = commande.convertPointsFidelite();
assertEquals(point,(int) Math.ceil(commande.getMontantTotal()));
}
@ParameterizedTest
@ValueSource(doubles = {-1.9,0.0})
void testConvertPointsFideliteGagnesWhenMontantTotalIsNotGood(){
Commande commande = Commande.builder()
.montantTotal(14.34)
.build();
Integer point = commande.convertPointsFidelite();
assertEquals(point,(int) Math.ceil(commande.getMontantTotal()));
}
@@ -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());
}
}
}
@@ -1,67 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
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 NotValidAdressExceptionTest {
@Test
@DisplayName("Exception should be created with the provided message")
void testExceptionCreation() {
String errorMessage = "Adresse is not valid";
NotValidAdressException exception = new NotValidAdressException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {
"rue is not valide",
"city is not valide",
"code postal is not valide",
"Country is not valide"
})
@DisplayName("Exception should handle different validation messages")
void testExceptionWithDifferentMessages(String errorMessage) {
NotValidAdressException exception = new NotValidAdressException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "Required field is missing";
Exception exception = assertThrows(NotValidAdressException.class, () -> {
throw new NotValidAdressException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid Adresse ";
try {
throw new NotValidAdressException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidAdressException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -1,61 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
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 NotValidCommandeExceptionTest {
@Test
@DisplayName("Exception should be created with the provided message")
void testExceptionCreation() {
String errorMessage = "Commande is not valid";
NotValidCommandeException exception = new NotValidCommandeException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {
"Mode Paiement is not valide",
"List ligne commande is not valide"
})
@DisplayName("Exception should handle different validation messages")
void testExceptionWithDifferentMessages(String errorMessage) {
NotValidCommandeException exception = new NotValidCommandeException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "Required field is missing";
Exception exception = assertThrows(NotValidCommandeException.class, () -> {
throw new NotValidCommandeException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid Commande ";
try {
throw new NotValidCommandeException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidCommandeException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -1,47 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NotValidLigneCommandeExceptionTest {
@Test
@DisplayName("Exception should handle different validation messages")
void testExceptionCreation() {
String errorMessage = "Ligne commande is not valide";
NotValidLigneCommandeException exception = new NotValidLigneCommandeException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "Required field is missing";
Exception exception = assertThrows(NotValidLigneCommandeException.class, () -> {
throw new NotValidLigneCommandeException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid Commande ";
try {
throw new NotValidLigneCommandeException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidLigneCommandeException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -1,349 +0,0 @@
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.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
public class CommandeRepositoryTest {
private CommandeRepository repository;
private Commande commandeN1;
private Commande commandeN2;
private Commande commandeN3;
@BeforeEach
void setUp() {
repository = new CommandeRepository();
UUID clientId = UUID.randomUUID();
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
LigneCommandeInfo commande1 = new LigneCommandeInfo(12);
LigneCommandeInfo commande2 = new LigneCommandeInfo(13);
lignesCommande.add(commande1);
lignesCommande.add(commande2);
String rue = "rue du chien";
String ville = "LKa Rochette";
String codePostal = "7700";
String pays = "France";
String modePayement = ModePaiement.CB.name();
commandeN1 = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande)
.rue(rue)
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.modePaiement(modePayement)
.build();
commandeN1.setRandomUUIDCommande();
List<LigneCommandeInfo> lignesCommande2 = new ArrayList<>();
LigneCommandeInfo commande3 = new LigneCommandeInfo(43);
LigneCommandeInfo commande4 = new LigneCommandeInfo(1);
lignesCommande2.add(commande3);
lignesCommande2.add(commande4);
commandeN2 = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande2)
.rue(rue)
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.modePaiement(modePayement)
.build();
commandeN2.setRandomUUIDCommande();
List<LigneCommandeInfo> lignesCommande3 = new ArrayList<>();
LigneCommandeInfo commande6 = new LigneCommandeInfo(12);
LigneCommandeInfo commande7 = new LigneCommandeInfo(13);
lignesCommande3.add(commande6);
lignesCommande3.add(commande7);
String rue2 = "rue du chevre chaud ";
String ville2 = "jsp";
String codePostal2 = "77";
String pays2 = "Lune ";
String modePayement2 = ModePaiement.PAYPAL.name();
commandeN3 = Commande.builder()
.lignesCommande(lignesCommande3)
.rue(rue2)
.ville(ville2)
.codePostal(codePostal2)
.pays(pays2)
.modePaiement(modePayement2)
.build();
commandeN3.setRandomUUID();
commandeN3.setRandomUUIDCommande();
}
@Test
@DisplayName("New repository should be empty")
void testNewRepositoryIsEmpty() {
List<Commande> commande = repository.findAll();
assertTrue(commande.isEmpty());
assertEquals(0, commande.size());
}
@Nested
@DisplayName("Save operations")
class SaveOperations {
@Test
@DisplayName("Save should add a new comande")
void testSaveNewCommande() {
Commande savedCommande = repository.save(commandeN1);
assertEquals(1, repository.findAll().size());
assertEquals(commandeN1.getCommandeId(), savedCommande.getCommandeId());
assertEquals(commandeN1.getClientId(), savedCommande.getClientId());
}
@Test
@DisplayName("Save should update existing commande with same ID")
void testSaveUpdatesExistingCommande() {
repository.save(commandeN1);
List<LigneCommandeInfo> lignesCommande2 = new ArrayList<>();
LigneCommandeInfo commande3 = new LigneCommandeInfo(43);
LigneCommandeInfo commande4 = new LigneCommandeInfo(1);
lignesCommande2.add(commande3);
lignesCommande2.add(commande4);
UUID id = commandeN1.getCommandeId();
Commande updatedCommande = Commande.builder()
.commandeId(id)
.lignesCommande(lignesCommande2)
.rue("rue")
.ville("moi")
.codePostal("1")
.pays("Russie")
.modePaiement(ModePaiement.POINTS_FIDELITE.name())
.build();
Commande savedCommande = repository.save(updatedCommande);
assertEquals(1, repository.findAll().size());
assertEquals(id, savedCommande.getCommandeId());
assertEquals("rue", savedCommande.getRue());
assertEquals("moi", savedCommande.getVille());
assertEquals("1", savedCommande.getCodePostal());
assertEquals("Russie", savedCommande.getPays());
assertEquals(ModePaiement.POINTS_FIDELITE.name(), savedCommande.getModePaiement());
}
@Test
@DisplayName("Save multiple customers should add all of them")
void testSaveMultipleCommande() {
repository.save(commandeN1);
repository.save(commandeN2);
List<Commande> commande = repository.findAll();
assertEquals(2, commande.size());
assertTrue(commande.contains(commandeN1));
assertTrue(commande.contains(commandeN2));
}
}
@Nested
@DisplayName("Find operations")
class FindOperations {
@BeforeEach
void setUpCommande() {
repository.save(commandeN1);
repository.save(commandeN2);
}
@Test
@DisplayName("FindAll should return all commande")
void testFindAll() {
List<Commande> commande = repository.findAll();
assertEquals(2, commande.size());
assertTrue(commande.contains(commandeN1));
assertTrue(commande.contains(commandeN2));
}
@Test
@DisplayName("FindById should return commande with matching ID")
void testFindById() {
Optional<Commande> foundCommande = repository.findById(commandeN1.getCommandeId());
assertTrue(foundCommande.isPresent());
assertEquals(commandeN1.getCommandeId(), foundCommande.get().getCommandeId());
assertEquals(commandeN1.getClientId(), foundCommande.get().getClientId());
}
@Test
void testFindByIdNotFound(){
UUID nonExistentId = UUID.randomUUID();
Optional<Commande> foundCommande = repository.findById(nonExistentId);
assertTrue(foundCommande.isEmpty());
}
@Test
void testExistsByIdExists(){
boolean exists = repository.existsById(commandeN1.getCommandeId());
assertTrue(exists);
}
@Test
void testExistsByIdNotExists(){
UUID nonExistentId = UUID.randomUUID();
boolean exists = repository.existsById(nonExistentId);
assertFalse(exists);
}
}
@Nested
@DisplayName("Delete operations")
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,9 +1,10 @@
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.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.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.LigneCommande;
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.NotValidLigneCommandeException;
@@ -47,7 +48,7 @@ public class CommandeValidatorTest {
List<LigneCommandeInfo> listeLigne = new ArrayList<>();
listeLigne.add(ligneCommandeValide);
String modePaiementValide = modePaiement.toString();
CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide);
ComandeInfo commandeValid = new ComandeInfo(listeLigne, modePaiementValide);
assertDoesNotThrow(() -> CommandeValidator.validate(commandeValid));
}
@@ -59,7 +60,7 @@ public class CommandeValidatorTest {
List<LigneCommandeInfo> listeLigne = new ArrayList<>();
listeLigne.add(ligneCommandeValide);
String modePaiementValide = modePaiementError;
CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide);
ComandeInfo commandeValid = new ComandeInfo(listeLigne, modePaiementValide);
NotValidCommandeException exception = assertThrows(
NotValidCommandeException.class,
@@ -72,7 +73,7 @@ public class CommandeValidatorTest {
@MethodSource("provideInvalidOrderLists")
void testValidateNotValideListLigneCommande(List<LigneCommandeInfo> listeLigne) {
String modePaiementValide = ModePaiement.CB.toString();
CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide);
ComandeInfo commandeValid = new ComandeInfo(listeLigne, modePaiementValide);
NotValidCommandeException exception = assertThrows(
NotValidCommandeException.class,
() -> 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));
}
}
}

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