forked from pierront/mylibrary-template
Compare commits
18 Commits
main
..
73e066b593
| Author | SHA1 | Date | |
|---|---|---|---|
| 73e066b593 | |||
| 7e8a5522d8 | |||
| 69ce78a113 | |||
| 0ad7e71030 | |||
| 2ed99a901a | |||
| 98d41ea409 | |||
| d985e2fa8f | |||
| 2161a0cf01 | |||
| 4552585ef9 | |||
| ce27a686ef | |||
| 2c14ee5e0b | |||
| 213f7f64db | |||
| 8d33d16cb8 | |||
| 82164689cf | |||
| 67b5b83943 | |||
| e125a420a6 | |||
| 3bf4fcad07 | |||
| 6e94316e4b |
@@ -1 +0,0 @@
|
||||
Notre groupe est constitué de Marvin Aubert, Maxime Lebreton et de Patrick Felix Vimalaratnam
|
||||
@@ -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>
|
||||
|
||||
@@ -16,6 +16,8 @@ public class CommandeDTO {
|
||||
private String codePostal;
|
||||
private String pays;
|
||||
private String modePaiement;
|
||||
|
||||
|
||||
private UUID commandeId ;
|
||||
private double montantTotal ;
|
||||
private Integer pointsFideliteGagnes ;
|
||||
|
||||
+1
-27
@@ -15,18 +15,7 @@ public final class CommandeConverter {
|
||||
.ville(adressInfo.ville())
|
||||
.codePostal(adressInfo.codePostal())
|
||||
.pays(adressInfo.pays())
|
||||
.modePaiement(commandeInfo.modePayement())
|
||||
.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())
|
||||
.modePaiement(modePaiement)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -41,19 +30,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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-13
@@ -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));
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -1,13 +1,15 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CommandeRepository {
|
||||
public class ComandeRepository {
|
||||
|
||||
private final List<Commande> commande = new ArrayList<>();
|
||||
|
||||
-73
@@ -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,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) {
|
||||
}
|
||||
-30
@@ -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();
|
||||
}
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.exception;
|
||||
|
||||
public class NotValidReviewException extends RuntimeException {
|
||||
public NotValidReviewException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
-25
@@ -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());
|
||||
}
|
||||
}
|
||||
-79
@@ -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;
|
||||
}
|
||||
}
|
||||
-52
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
-17
@@ -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) {
|
||||
}
|
||||
-43
@@ -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();
|
||||
}
|
||||
}
|
||||
-12
@@ -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;
|
||||
}
|
||||
-44
@@ -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();
|
||||
}
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
public class NotValidPaymentMethodException extends RuntimeException {
|
||||
|
||||
public NotValidPaymentMethodException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
public class NotValidSubscriptionException extends RuntimeException {
|
||||
|
||||
public NotValidSubscriptionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
-21
@@ -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());
|
||||
}
|
||||
}
|
||||
-62
@@ -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);
|
||||
}
|
||||
}
|
||||
-110
@@ -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;
|
||||
}
|
||||
}
|
||||
-27
@@ -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);}
|
||||
}
|
||||
}
|
||||
-36
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
-50
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -17,14 +17,14 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class CommandeRepositoryTest {
|
||||
|
||||
private CommandeRepository repository;
|
||||
private ComandeRepository repository;
|
||||
private Commande commandeN1;
|
||||
private Commande commandeN2;
|
||||
private Commande commandeN3;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository = new CommandeRepository();
|
||||
repository = new ComandeRepository();
|
||||
|
||||
UUID clientId = UUID.randomUUID();
|
||||
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
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.CommandeInfo;
|
||||
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 fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.ComandeRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.usecase.CustomerUseCase;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CommandeUseCase {
|
||||
|
||||
@Mock
|
||||
private ComandeRepository comandeRepository;
|
||||
|
||||
|
||||
@InjectMocks
|
||||
private CommandeUseCase commandeUseCase;
|
||||
|
||||
private UUID commandeId;
|
||||
private UUID clientId;
|
||||
private Commande testCommande;
|
||||
private CommandeInfo validCommandeInfo;
|
||||
private LigneCommandeInfo validLigneCommandeInfo;
|
||||
private AdresseInfo validAdresseInfo;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
clientId = UUID.randomUUID();
|
||||
commandeId = 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();
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
-249
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
-68
@@ -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());
|
||||
}
|
||||
}
|
||||
-60
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
-98
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
-361
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
-318
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
-113
@@ -1,113 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.ReviewInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class ReviewValidatorTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should validate review with valid data")
|
||||
void testValidateValidReview() {
|
||||
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||
ReviewInfo validReview = new ReviewInfo(3, "Bof", purchaseDate);
|
||||
|
||||
assertDoesNotThrow(() -> ReviewValidator.validate(validReview));
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Note validation tests")
|
||||
class NoteValidationTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when note is lower than 1")
|
||||
void testValidateNoteLower1() {
|
||||
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||
ReviewInfo reviewWithNoteLower1 = new ReviewInfo(0, "Bof", purchaseDate);
|
||||
|
||||
NotValidReviewException exception = assertThrows(
|
||||
NotValidReviewException.class,
|
||||
() -> ReviewValidator.validate(reviewWithNoteLower1)
|
||||
);
|
||||
|
||||
assertEquals(ReviewValidator.NOTE_CANNOT_BE_LOWER_THAN_1, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when note is upper than 5")
|
||||
void testValidateNoteUpper5() {
|
||||
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||
ReviewInfo reviewWithNoteUpper5 = new ReviewInfo(6, "Bof", purchaseDate);
|
||||
|
||||
NotValidReviewException exception = assertThrows(
|
||||
NotValidReviewException.class,
|
||||
() -> ReviewValidator.validate(reviewWithNoteUpper5)
|
||||
);
|
||||
|
||||
assertEquals(ReviewValidator.NOTE_CANNOT_BE_UPPER_THAN_5, exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Comment validation tests")
|
||||
class CommentValidationTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when comment is blank")
|
||||
void testValidateBlankComment() {
|
||||
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||
ReviewInfo reviewWithBlankComment = new ReviewInfo(3, "", purchaseDate);
|
||||
|
||||
NotValidReviewException exception = assertThrows(
|
||||
NotValidReviewException.class,
|
||||
() -> ReviewValidator.validate(reviewWithBlankComment)
|
||||
);
|
||||
|
||||
assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||
@DisplayName("Should throw exception when last name contains only whitespace")
|
||||
void testValidateWhitespaceLastName(String whitespace) {
|
||||
LocalDate purchaseDate = LocalDate.of(2026, 3, 24);
|
||||
ReviewInfo reviewWithBlankComment = new ReviewInfo(3, whitespace, purchaseDate);
|
||||
|
||||
NotValidReviewException exception = assertThrows(
|
||||
NotValidReviewException.class,
|
||||
() -> ReviewValidator.validate(reviewWithBlankComment)
|
||||
);
|
||||
|
||||
assertEquals(ReviewValidator.COMMENT_CANNOT_BE_BLANK, exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Purchase date validation tests")
|
||||
class PurchaseDateValidationTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when purchase date is after the actual date")
|
||||
void testValidateFuturPurchaseDate() {
|
||||
LocalDate futurepurchaseDate = LocalDate.of(2026, 6, 24);
|
||||
ReviewInfo reviewWithFuturPurchaseDate = new ReviewInfo(2, "Bof", futurepurchaseDate);
|
||||
|
||||
NotValidReviewException exception = assertThrows(
|
||||
NotValidReviewException.class,
|
||||
() -> ReviewValidator.validate(reviewWithFuturPurchaseDate)
|
||||
);
|
||||
|
||||
assertEquals(ReviewValidator.PURCHASE_DATE_IS_NOT_VALID, exception.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
-125
@@ -1,125 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.converter;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.*;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.PaymentMethod;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class SubscriptionConverterTest {
|
||||
|
||||
@Nested
|
||||
@DisplayName("toDomain() method tests")
|
||||
class ToDomainTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should convert SubscriptionInfo to Subscription domain object with monthly amount initialized to 0")
|
||||
void shouldConvertSubscriptionInfoToDomain() {
|
||||
// Given
|
||||
PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "Maxime Lebreton");
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(DesiredSubscriptionDuration.THREE.getValue(), desiredStartDate);
|
||||
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
||||
|
||||
// When
|
||||
Subscription result = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo);
|
||||
|
||||
// Then
|
||||
assertNotNull(result);
|
||||
assertEquals(subscriptionInfo.desiredSubscriptionDuration(), result.getDesiredSubscriptionDuration());
|
||||
assertEquals(paymentMethodInfo.paymentType(), result.getPaymentMethod().getPaymentType());
|
||||
assertEquals(paymentMethodInfo.details(), result.getPaymentMethod().getDetails());
|
||||
assertEquals(subscriptionInfo.desiredStartDate(), result.getDesiredStartDate());
|
||||
assertEquals(subscriptionInfo.desiredStartDate(), result.getStartDate());
|
||||
assertEquals(estimateEndDate, result.getEndDate());
|
||||
assertEquals(0, result.getMonthlyAmount());
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("toDTO() method tests")
|
||||
class ToDTOTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should convert Subscription domain object to SubscriptionDTO with all fields mapped correctly")
|
||||
void shouldConvertSubscriptionToDTO() {
|
||||
PaymentMethod paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(PaymentType.CB.name())
|
||||
.details("Maxime Lebreton")
|
||||
.build();
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
||||
Subscription subscription = Subscription.builder()
|
||||
.subscriptionId(UUID.randomUUID())
|
||||
.customerId(UUID.randomUUID())
|
||||
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.startDate(desiredStartDate)
|
||||
.endDate(estimateEndDate)
|
||||
.monthlyAmount(12.03)
|
||||
.build();
|
||||
|
||||
SubscriptionDTO result = SubscriptionConverter.toDTO(subscription);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(subscription.getSubscriptionId(), result.getSubscriptionId());
|
||||
assertEquals(subscription.getCustomerId(), result.getCustomerId());
|
||||
assertEquals(subscription.getDesiredSubscriptionDuration(), result.getDesiredSubscriptionDuration());
|
||||
assertEquals(subscription.getPaymentMethod().getPaymentType(), result.getPaymentMethod().getPaymentType());
|
||||
assertEquals(subscription.getPaymentMethod().getDetails(), result.getPaymentMethod().getDetails());
|
||||
assertEquals(subscription.getDesiredStartDate(), result.getDesiredStartDate());
|
||||
assertEquals(subscription.getStartDate(), result.getStartDate());
|
||||
assertEquals(subscription.getEndDate(), result.getEndDate());
|
||||
assertEquals(12.03, result.getMonthlyAmount());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle null values properly when converting between objects")
|
||||
void shouldHandleNullValuesGracefully() {
|
||||
PaymentMethod paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(PaymentType.CB.name())
|
||||
.details(null)
|
||||
.build();
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
||||
Subscription subscription = Subscription.builder()
|
||||
.subscriptionId(UUID.randomUUID())
|
||||
.customerId(UUID.randomUUID())
|
||||
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.startDate(desiredStartDate)
|
||||
.endDate(estimateEndDate)
|
||||
.monthlyAmount(12.03)
|
||||
.build();
|
||||
|
||||
SubscriptionDTO result = SubscriptionConverter.toDTO(subscription);
|
||||
|
||||
assertNotNull(result);
|
||||
assertNull(result.getPaymentMethod().getDetails());
|
||||
assertEquals(desiredStartDate, result.getDesiredStartDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should preserve empty string values during conversion")
|
||||
void shouldPreserveEmptyStrings() {
|
||||
PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "");
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(DesiredSubscriptionDuration.THREE.getValue(), desiredStartDate);
|
||||
|
||||
Subscription domainResult = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo);
|
||||
SubscriptionDTO dtoResult = SubscriptionConverter.toDTO(domainResult);
|
||||
|
||||
assertEquals("", dtoResult.getPaymentMethod().getDetails());
|
||||
}
|
||||
}
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentType;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class PaymentMethodTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Builder should create a valid PaymentMethod instance")
|
||||
void testPaymentMethodBuilder() {
|
||||
String paymentType = PaymentType.CB.name();
|
||||
Object details = new Object();
|
||||
details = "Maxime Lebreton";
|
||||
|
||||
PaymentMethod paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(paymentType)
|
||||
.details(details)
|
||||
.build();
|
||||
|
||||
assertEquals(paymentType, paymentMethod.getPaymentType());
|
||||
assertEquals(details, paymentMethod.getDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Builder should create a valid PaymentMethod instance with details as an Integer")
|
||||
void testPaymentMethodDetailsInteger() {
|
||||
String paymentType = PaymentType.CB.name();
|
||||
Object details = new Object();
|
||||
details = 10;
|
||||
|
||||
PaymentMethod paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(paymentType)
|
||||
.details(details)
|
||||
.build();
|
||||
|
||||
assertEquals(paymentType, paymentMethod.getPaymentType());
|
||||
assertEquals(details, paymentMethod.getDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Builder should create a valid PaymentMethod instance with details as an boolean")
|
||||
void testPaymentMethodDetailsBoolean() {
|
||||
String paymentType = PaymentType.CB.name();
|
||||
Object details = new Object();
|
||||
details = true;
|
||||
|
||||
PaymentMethod paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(paymentType)
|
||||
.details(details)
|
||||
.build();
|
||||
|
||||
assertEquals(paymentType, paymentMethod.getPaymentType());
|
||||
assertEquals(details, paymentMethod.getDetails());
|
||||
}
|
||||
}
|
||||
-125
@@ -1,125 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentType;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.DesiredSubscriptionDuration;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class SubscriptionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Builder should create a valid Subscription instance")
|
||||
void testSubscriptionBuilder() {
|
||||
UUID subscriptionId = UUID.randomUUID();
|
||||
UUID customerId = UUID.randomUUID();
|
||||
Integer desiredSubscriptionDuration = DesiredSubscriptionDuration.THREE.getValue();
|
||||
String paymentType = PaymentType.CB.name();
|
||||
Object details = new Object();
|
||||
details = "Maxime Lebreton";
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
LocalDate startDate = desiredStartDate;
|
||||
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
||||
double monthlyAmount = 17.99;
|
||||
|
||||
PaymentMethod paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(paymentType)
|
||||
.details(details)
|
||||
.build();
|
||||
Subscription subscription = Subscription.builder()
|
||||
.subscriptionId(subscriptionId)
|
||||
.customerId(customerId)
|
||||
.desiredSubscriptionDuration(desiredSubscriptionDuration)
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.startDate(startDate)
|
||||
.endDate(estimateEndDate)
|
||||
.monthlyAmount(monthlyAmount)
|
||||
.build();
|
||||
|
||||
assertEquals(subscriptionId, subscription.getSubscriptionId());
|
||||
assertEquals(customerId, subscription.getCustomerId());
|
||||
assertEquals(desiredSubscriptionDuration, subscription.getDesiredSubscriptionDuration());
|
||||
assertEquals(paymentType, paymentMethod.getPaymentType());
|
||||
assertEquals(details, paymentMethod.getDetails());
|
||||
assertEquals(desiredStartDate, subscription.getDesiredStartDate());
|
||||
assertEquals(startDate, subscription.getStartDate());
|
||||
assertEquals(estimateEndDate, subscription.getEndDate());
|
||||
assertEquals(monthlyAmount, subscription.getMonthlyAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("setRandomSubscriptionUUID should change the ID to a new random UUID")
|
||||
void testSetRandomSubscriptionUUID() {
|
||||
Subscription subscription = Subscription.builder().build();
|
||||
UUID originalId = subscription.getSubscriptionId();
|
||||
|
||||
subscription.setRandomSubscriptionUUID();
|
||||
|
||||
assertNotNull(subscription.getSubscriptionId());
|
||||
assertNotEquals(originalId, subscription.getSubscriptionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("setRandomCustomerUUID should change the ID to a new random UUID")
|
||||
void testSetRandomCustomerUUID() {
|
||||
Subscription subscription = Subscription.builder().build();
|
||||
UUID originalId = subscription.getCustomerId();
|
||||
|
||||
subscription.setRandomCustomerUUID();
|
||||
|
||||
assertNotNull(subscription.getCustomerId());
|
||||
assertNotEquals(originalId, subscription.getCustomerId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("setStartDate should instantiate StartDate as the same value as the variable desiredStartDate")
|
||||
void testSetStartDate() {
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
Subscription subscription = Subscription.builder()
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.build();
|
||||
|
||||
subscription.setStartDate();
|
||||
|
||||
assertNotNull(subscription.getStartDate());
|
||||
assertEquals(desiredStartDate, subscription.getStartDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("setEndDate should instantiate EndDate as the same value as the variable desiredStartDate + desiredSubscriptionDuration")
|
||||
void testSetEndDate() {
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
||||
Subscription subscription = Subscription.builder()
|
||||
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.build();
|
||||
|
||||
subscription.setEndDate();
|
||||
|
||||
assertNotNull(subscription.getEndDate());
|
||||
assertEquals(estimateEndDate, subscription.getEndDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("setDateSubscription should instantiate StartDate and EndDate ")
|
||||
void testSetDateSubscription() {
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
||||
Subscription subscription = Subscription.builder()
|
||||
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.build();
|
||||
|
||||
subscription.setDateSubscription();
|
||||
|
||||
assertNotNull(subscription.getEndDate());
|
||||
assertEquals(estimateEndDate, subscription.getEndDate());
|
||||
}
|
||||
}
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class NotValidPaymentMethodEsceptionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be created with the provided message")
|
||||
void testExceptionCreation() {
|
||||
String errorMessage = "PaymentMethod data is not valid";
|
||||
|
||||
NotValidPaymentMethodException exception = new NotValidPaymentMethodException(errorMessage);
|
||||
|
||||
assertEquals(errorMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"paymentType is not valid"
|
||||
})
|
||||
@DisplayName("Exception should handle different validation messages")
|
||||
void testExceptionWithDifferentMessages(String errorMessage) {
|
||||
NotValidPaymentMethodException exception = new NotValidPaymentMethodException(errorMessage);
|
||||
|
||||
assertEquals(errorMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be properly thrown and caught")
|
||||
void testExceptionCanBeThrownAndCaught() {
|
||||
String errorMessage = "paymentType is not valid";
|
||||
|
||||
Exception exception = assertThrows(NotValidPaymentMethodException.class, () -> {
|
||||
throw new NotValidPaymentMethodException(errorMessage);
|
||||
});
|
||||
|
||||
assertEquals(errorMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be catchable as a general Exception")
|
||||
void testExceptionInheritance() {
|
||||
String errorMessage = "Invalid paymentMethod data";
|
||||
|
||||
try {
|
||||
throw new NotValidPaymentMethodException(errorMessage);
|
||||
} catch (Exception e) {
|
||||
assertEquals(NotValidPaymentMethodException.class, e.getClass());
|
||||
assertEquals(errorMessage, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
-60
@@ -1,60 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException;
|
||||
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 NotValidSubscriptionExceptionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be created with the provided message")
|
||||
void testExceptionCreation() {
|
||||
String errorMessage = "Subscription data is not valid";
|
||||
|
||||
NotValidSubscriptionException exception = new NotValidSubscriptionException(errorMessage);
|
||||
|
||||
assertEquals(errorMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"desiredSubscriptionDuration will be 3, 6 or 12 by the ENUM file SubscriptionDurationDesired",
|
||||
"desiredStartDate cannot be before today"
|
||||
})
|
||||
@DisplayName("Exception should handle different validation messages")
|
||||
void testExceptionWithDifferentMessages(String errorMessage) {
|
||||
NotValidSubscriptionException exception = new NotValidSubscriptionException(errorMessage);
|
||||
|
||||
assertEquals(errorMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be properly thrown and caught")
|
||||
void testExceptionCanBeThrownAndCaught() {
|
||||
String errorMessage = "desiredStartDate cannot be before today";
|
||||
|
||||
Exception exception = assertThrows(NotValidSubscriptionException.class, () -> {
|
||||
throw new NotValidSubscriptionException(errorMessage);
|
||||
});
|
||||
|
||||
assertEquals(errorMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be catchable as a general Exception")
|
||||
void testExceptionInheritance() {
|
||||
String errorMessage = "Invalid subscription data";
|
||||
|
||||
try {
|
||||
throw new NotValidSubscriptionException(errorMessage);
|
||||
} catch (Exception e) {
|
||||
assertEquals(NotValidSubscriptionException.class, e.getClass());
|
||||
assertEquals(errorMessage, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
-74
@@ -1,74 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.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 SubscriptionNotFoundExceptionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the UUID provided for customer")
|
||||
void testExceptionMessageContainsUUIDForCustomer() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||
|
||||
String expectedMessage = String.format("The subscriptions with the customer id %s does not exists", customerUUID);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the UUID provided for subscription")
|
||||
void testExceptionMessageContainsUUIDForSubscription() {
|
||||
UUID subscriptionUUID = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(Optional.empty(), Optional.of(subscriptionUUID));
|
||||
|
||||
String expectedMessage = String.format("The subscription with subscription id %s does not exists", subscriptionUUID);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format for customer")
|
||||
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||
|
||||
String expectedFormatWithPlaceholder = "The subscriptions with the customer id {0} does not exists";
|
||||
assertEquals(SubscriptionNotFoundException.THE_SUBSCRIPTION_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 subscription")
|
||||
void testExceptionUsesConstantMessageSubscriptionFormat() {
|
||||
UUID subscriptionUUID = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(Optional.empty(), Optional.of(subscriptionUUID));
|
||||
|
||||
String expectedFormatWithPlaceholder = "The subscription with subscription id {0} does not exists";
|
||||
assertEquals(SubscriptionNotFoundException.THE_SUBSCRIPTION_WITH_SUBSCRIPTION_ID_DOES_NOT_EXIST_MESSAGE,
|
||||
expectedFormatWithPlaceholder);
|
||||
assertTrue(exception.getMessage().contains(subscriptionUUID.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be properly thrown and caught")
|
||||
void testExceptionCanBeThrownAndCaught() {
|
||||
UUID subscriptionUUID = UUID.randomUUID();
|
||||
|
||||
try {
|
||||
throw new SubscriptionNotFoundException(Optional.empty(), Optional.of(subscriptionUUID));
|
||||
} catch (SubscriptionNotFoundException e) {
|
||||
String expectedMessage = String.format("The subscription with subscription id %s does not exists", subscriptionUUID);
|
||||
assertEquals(expectedMessage, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
-300
@@ -1,300 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.DesiredSubscriptionDuration;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentType;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.PaymentMethod;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||
import org.junit.jupiter.api.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.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 SubscriptionRepositoryTest {
|
||||
|
||||
private SubscriptionRepository repository;
|
||||
private PaymentMethod paymentMethod;
|
||||
private Subscription subscription1;
|
||||
private Subscription subscription2;
|
||||
private Subscription subscription3;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
repository = new SubscriptionRepository();
|
||||
|
||||
paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(PaymentType.CB.name())
|
||||
.details("Maxime Lebreton")
|
||||
.build();
|
||||
|
||||
LocalDate desiredStartDate1 = LocalDate.of(2026, 6, 24);
|
||||
subscription1 = Subscription.builder()
|
||||
.customerId(UUID.randomUUID())
|
||||
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate1)
|
||||
.monthlyAmount(12.03)
|
||||
.build();
|
||||
subscription1.setRandomSubscriptionUUID();
|
||||
subscription1.setDateSubscription();
|
||||
|
||||
UUID customCustomerId = UUID.randomUUID();
|
||||
|
||||
LocalDate desiredStartDate2 = LocalDate.of(2026, 7, 24);
|
||||
subscription2 = Subscription.builder()
|
||||
.customerId(customCustomerId)
|
||||
.desiredSubscriptionDuration(DesiredSubscriptionDuration.SIX.getValue())
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate2)
|
||||
.monthlyAmount(20)
|
||||
.build();
|
||||
subscription2.setRandomSubscriptionUUID();
|
||||
subscription2.setDateSubscription();
|
||||
|
||||
LocalDate desiredStartDate3 = LocalDate.of(2026, 8, 24);
|
||||
subscription3 = Subscription.builder()
|
||||
.customerId(customCustomerId)
|
||||
.desiredSubscriptionDuration(DesiredSubscriptionDuration.TWELVE.getValue())
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate3)
|
||||
.monthlyAmount(30)
|
||||
.build();
|
||||
subscription3.setRandomSubscriptionUUID();
|
||||
subscription3.setDateSubscription();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("New subecription should be empty")
|
||||
void testNewRepositoryIsEmpty() {
|
||||
List<Subscription> subscriptions = repository.findAll();
|
||||
|
||||
assertTrue(subscriptions.isEmpty());
|
||||
assertEquals(0, subscriptions.size());
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Save operations")
|
||||
class SaveOperations {
|
||||
|
||||
@Test
|
||||
@DisplayName("Save should add a new customer")
|
||||
void testSaveNewSubscription() {
|
||||
Subscription savedSubscription = repository.save(subscription1);
|
||||
|
||||
assertEquals(1, repository.findAll().size());
|
||||
assertEquals(subscription1.getSubscriptionId(), savedSubscription.getSubscriptionId());
|
||||
assertEquals(subscription1.getMonthlyAmount(), savedSubscription.getMonthlyAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Save should update existing subscription with same ID")
|
||||
void testSaveUpdatesExistingSubscription() {
|
||||
repository.save(subscription1);
|
||||
|
||||
UUID subscriptionId = subscription1.getSubscriptionId();
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
Subscription updatedSubscription = Subscription.builder()
|
||||
.subscriptionId(subscriptionId)
|
||||
.customerId(UUID.randomUUID())
|
||||
.desiredSubscriptionDuration(DesiredSubscriptionDuration.SIX.getValue())
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.monthlyAmount(20)
|
||||
.build();
|
||||
|
||||
Subscription savedCustomer = repository.save(updatedSubscription);
|
||||
|
||||
assertEquals(1, repository.findAll().size());
|
||||
assertEquals(subscriptionId, savedCustomer.getSubscriptionId());
|
||||
assertEquals(6, savedCustomer.getDesiredSubscriptionDuration());
|
||||
assertEquals(20, savedCustomer.getMonthlyAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Save multiple subscriptions should add all of them")
|
||||
void testSaveMultipleSubscriptions() {
|
||||
repository.save(subscription1);
|
||||
repository.save(subscription2);
|
||||
|
||||
List<Subscription> subscriptions = repository.findAll();
|
||||
|
||||
assertEquals(2, subscriptions.size());
|
||||
assertTrue(subscriptions.contains(subscription1));
|
||||
assertTrue(subscriptions.contains(subscription2));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Find operations")
|
||||
class FindOperations {
|
||||
|
||||
@BeforeEach
|
||||
void setUpSubscriptions() {
|
||||
repository.save(subscription1);
|
||||
repository.save(subscription2);
|
||||
repository.save(subscription3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("FindAll should return all subscriptions")
|
||||
void testFindAll() {
|
||||
List<Subscription> subscriptions = repository.findAll();
|
||||
|
||||
assertEquals(3, subscriptions.size());
|
||||
assertTrue(subscriptions.contains(subscription1));
|
||||
assertTrue(subscriptions.contains(subscription2));
|
||||
assertTrue(subscriptions.contains(subscription3));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("FindBySubscriptionId should return subscription with matching ID")
|
||||
void testFindBySubscriptionId() {
|
||||
Optional<Subscription> foundSubscription = repository.findBySubscriptionId(subscription1.getSubscriptionId());
|
||||
|
||||
assertTrue(foundSubscription.isPresent());
|
||||
assertEquals(subscription1.getStartDate(), foundSubscription.get().getStartDate());
|
||||
assertEquals(subscription1.getMonthlyAmount(), foundSubscription.get().getMonthlyAmount());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("FindBySubscriptionId should return empty Optional when ID doesn't exist")
|
||||
void testFindBySubscriptionIdNotFound() {
|
||||
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||
|
||||
Optional<Subscription> foundSubscription = repository.findBySubscriptionId(nonExistentSubscriptionId);
|
||||
|
||||
assertTrue(foundSubscription.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("FindByCustomerId should return subscription with matching ID")
|
||||
void testFindByCustomerId() {
|
||||
List<Subscription> foundSubscriptions = repository.FindByCustomerId(subscription2.getCustomerId());
|
||||
|
||||
assertFalse(foundSubscriptions.isEmpty());
|
||||
assertTrue(foundSubscriptions.contains(subscription2));
|
||||
assertTrue(foundSubscriptions.contains(subscription3));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("FindByCustomerId should return empty Optional when ID doesn't exist")
|
||||
void testFindByCustomerIdNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
|
||||
List<Subscription> foundSubscription = repository.FindByCustomerId(nonExistentCustomerId);
|
||||
|
||||
assertTrue(foundSubscription.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsBySubscriptionId should return true when ID exists")
|
||||
void testExistsBySubscriptionIdExists() {
|
||||
boolean exists = repository.existsBySubscriptionId(subscription1.getSubscriptionId());
|
||||
|
||||
assertTrue(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsBySubscriptionId should return false when ID doesn't exist")
|
||||
void testExistsBySubscriptionIdNotExists() {
|
||||
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||
|
||||
boolean exists = repository.existsBySubscriptionId(nonExistentSubscriptionId);
|
||||
|
||||
assertFalse(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsByCustomerId should return true when ID exists")
|
||||
void testExistsByCustomerIdExists() {
|
||||
boolean exists = repository.existsByCustomerId(subscription1.getCustomerId());
|
||||
|
||||
assertTrue(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsByCustomerId should return false when ID doesn't exist")
|
||||
void testExistsByCustomerIdNotExists() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
|
||||
boolean exists = repository.existsByCustomerId(nonExistentCustomerId);
|
||||
|
||||
assertFalse(exists);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Delete operations")
|
||||
class DeleteOperations {
|
||||
|
||||
@BeforeEach
|
||||
void setUpSubscriptions() {
|
||||
repository.save(subscription1);
|
||||
repository.save(subscription2);
|
||||
repository.save(subscription3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Delete should remove the specified subscription")
|
||||
void testDelete() {
|
||||
repository.delete(subscription1);
|
||||
|
||||
List<Subscription> subscriptions = repository.findAll();
|
||||
|
||||
assertEquals(2, subscriptions.size());
|
||||
assertFalse(subscriptions.contains(subscription1));
|
||||
assertTrue(subscriptions.contains(subscription2));
|
||||
assertTrue(subscriptions.contains(subscription3));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("DeleteAll should remove all subscriptions of a customer")
|
||||
void testDeleteAllSubscriptionOfACustomer() {
|
||||
repository.deleteByCustomerId(subscription2.getCustomerId());
|
||||
|
||||
List<Subscription> subscriptions = repository.findAll();
|
||||
|
||||
assertEquals(1, subscriptions.size());
|
||||
assertTrue(subscriptions.contains(subscription1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("DeleteAll should remove all subscriptions")
|
||||
void testDeleteAll() {
|
||||
repository.deleteAll();
|
||||
|
||||
List<Subscription> subscriptions = repository.findAll();
|
||||
|
||||
assertTrue(subscriptions.isEmpty());
|
||||
assertEquals(0, subscriptions.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Delete should not throw exception when customer doesn't exist")
|
||||
void testDeleteNonExistentCustomer() {
|
||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
Subscription nonExistentsubscription = Subscription.builder()
|
||||
.customerId(UUID.randomUUID())
|
||||
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.monthlyAmount(12.03)
|
||||
.build();
|
||||
nonExistentsubscription.setRandomSubscriptionUUID();
|
||||
nonExistentsubscription.setDateSubscription();
|
||||
|
||||
assertDoesNotThrow(() -> repository.delete(nonExistentsubscription));
|
||||
|
||||
assertEquals(3, repository.findAll().size());
|
||||
}
|
||||
}
|
||||
}
|
||||
-273
@@ -1,273 +0,0 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.*;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.PaymentMethod;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.SubscriptionNotFoundException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository.SubscriptionRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
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.*;
|
||||
import static org.mockito.Mockito.never;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class SubscriptionUseCaseTest {
|
||||
|
||||
@Mock
|
||||
private SubscriptionRepository subscriptionRepository;
|
||||
|
||||
@InjectMocks
|
||||
private SubscriptionUseCase subscriptionUseCase;
|
||||
|
||||
private UUID subscriptionId;
|
||||
private UUID customerId;
|
||||
private Integer desiredSubscriptionDuration;
|
||||
private PaymentMethod paymentMethod;
|
||||
private LocalDate desiredStartDate;
|
||||
private double monthlyAmount;
|
||||
private Subscription testSubscription;
|
||||
private SubscriptionInfo validSubscriptionInfo;
|
||||
private PaymentMethodInfo validPaymentMethodInfo;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
subscriptionId = UUID.randomUUID();
|
||||
customerId = UUID.randomUUID();
|
||||
desiredSubscriptionDuration = DesiredSubscriptionDuration.THREE.getValue();
|
||||
desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(PaymentType.CB.name())
|
||||
.details("Maxime Lebreton")
|
||||
.build();
|
||||
monthlyAmount = 12.99;
|
||||
|
||||
testSubscription = Subscription.builder()
|
||||
.subscriptionId(subscriptionId)
|
||||
.customerId(customerId)
|
||||
.desiredSubscriptionDuration(desiredSubscriptionDuration)
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.monthlyAmount(monthlyAmount)
|
||||
.build();
|
||||
|
||||
validPaymentMethodInfo = new PaymentMethodInfo(PaymentType.PAYPAL.name(),"");
|
||||
validSubscriptionInfo = new SubscriptionInfo(desiredSubscriptionDuration, desiredStartDate);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Register subscription tests")
|
||||
class RegisterSubscriptionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should register review when valid data is provided")
|
||||
void testRegisterSubscriptionithValidData() throws NotValidSubscriptionException {
|
||||
when(subscriptionRepository.save(any(Subscription.class))).thenReturn(testSubscription);
|
||||
|
||||
Map<String, Object> registeredSubscription = subscriptionUseCase.registerSubscription(validSubscriptionInfo, validPaymentMethodInfo);
|
||||
|
||||
assertNotNull(registeredSubscription);
|
||||
assertEquals(subscriptionId, (UUID) registeredSubscription.get("subscriptionId"));
|
||||
verify(subscriptionRepository, times(1)).save(any(Subscription.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when review data is not valid")
|
||||
void testRegisterSubscriptionWithInvalidData() {
|
||||
PaymentMethodInfo invalidPaymentMethodInfo = new PaymentMethodInfo("carte","");
|
||||
SubscriptionInfo invalidSubscriptionInfo = new SubscriptionInfo(0, desiredStartDate);
|
||||
|
||||
assertThrows(NotValidSubscriptionException.class,
|
||||
() -> subscriptionUseCase.registerSubscription(invalidSubscriptionInfo, invalidPaymentMethodInfo));
|
||||
|
||||
verify(subscriptionRepository, never()).save(any(Subscription.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Find subscription tests")
|
||||
class FindSubscriptionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return reviews when customer ID exists")
|
||||
void testFindSubscriptionByCustomerId() {
|
||||
when(subscriptionRepository.FindByCustomerId(customerId)).thenReturn(List.of(testSubscription));
|
||||
|
||||
List<SubscriptionDTO> foundSubscriptions = subscriptionUseCase.findSubscriptionByCustomerId(customerId);
|
||||
|
||||
assertFalse(foundSubscriptions.isEmpty());
|
||||
boolean allSameCustomer = foundSubscriptions.stream()
|
||||
.allMatch(review -> review.getCustomerId().equals(customerId));
|
||||
assertTrue(allSameCustomer);
|
||||
verify(subscriptionRepository, times(1)).FindByCustomerId(customerId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when customer ID doesn't exist")
|
||||
void testFindSubscriptionByCustomerIdNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
when(subscriptionRepository.FindByCustomerId(nonExistentCustomerId)).thenReturn(List.of());
|
||||
|
||||
List<SubscriptionDTO> foundSubscriptions = subscriptionUseCase.findSubscriptionByCustomerId(nonExistentCustomerId);
|
||||
|
||||
assertTrue(foundSubscriptions.isEmpty());
|
||||
verify(subscriptionRepository, times(1)).FindByCustomerId(nonExistentCustomerId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return subscription when Subscription ID exists")
|
||||
void testFindSubscriptionBySubscriptionId() {
|
||||
when(subscriptionRepository.findBySubscriptionId(subscriptionId)).thenReturn(Optional.of(testSubscription));
|
||||
|
||||
Optional<SubscriptionDTO> foundSubscription = subscriptionUseCase.findSubscriptionBySubscriptionId(subscriptionId);
|
||||
|
||||
assertTrue(foundSubscription.isPresent());
|
||||
assertEquals(testSubscription.getCustomerId(), foundSubscription.get().getCustomerId());
|
||||
assertEquals(testSubscription.getMonthlyAmount(), foundSubscription.get().getMonthlyAmount());
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(subscriptionId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when subscription ID doesn't exist")
|
||||
void testFindSubscriptionBySubscriptionIdNotFound() {
|
||||
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||
when(subscriptionRepository.findBySubscriptionId(nonExistentSubscriptionId)).thenReturn(Optional.empty());
|
||||
|
||||
Optional<SubscriptionDTO> foundSubscription = subscriptionUseCase.findSubscriptionBySubscriptionId(nonExistentSubscriptionId);
|
||||
|
||||
assertTrue(foundSubscription.isEmpty());
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(nonExistentSubscriptionId);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Update subscription tests")
|
||||
class UpdateSubscriptionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should update subscription when valid data is provided")
|
||||
void testUpdateSubscriptionWithValidData() throws SubscriptionNotFoundException, NotValidReviewException {
|
||||
when(subscriptionRepository.findBySubscriptionId(subscriptionId)).thenReturn(Optional.of(testSubscription));
|
||||
|
||||
LocalDate updateDdesiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
Subscription updatedSubscription = Subscription.builder()
|
||||
.subscriptionId(subscriptionId)
|
||||
.customerId(customerId)
|
||||
.desiredSubscriptionDuration(desiredSubscriptionDuration)
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.monthlyAmount(monthlyAmount)
|
||||
.build();
|
||||
|
||||
when(subscriptionRepository.save(any(Subscription.class))).thenReturn(updatedSubscription);
|
||||
|
||||
SubscriptionInfo updateInfo = new SubscriptionInfo(DesiredSubscriptionDuration.SIX.getValue(), updateDdesiredStartDate);
|
||||
|
||||
SubscriptionDTO result = subscriptionUseCase.updateSubscription(subscriptionId, updateInfo, validPaymentMethodInfo);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(customerId, result.getCustomerId());
|
||||
assertEquals(monthlyAmount, result.getMonthlyAmount());
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(subscriptionId);
|
||||
verify(subscriptionRepository, times(1)).save(any(Subscription.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when subscription ID doesn't exist")
|
||||
void testUpdateSubscriptionNotFound() {
|
||||
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||
when(subscriptionRepository.findBySubscriptionId(nonExistentSubscriptionId)).thenReturn(Optional.empty());
|
||||
|
||||
SubscriptionInfo updateInfo = new SubscriptionInfo(desiredSubscriptionDuration, desiredStartDate);
|
||||
|
||||
assertThrows(SubscriptionNotFoundException.class,
|
||||
() -> subscriptionUseCase.updateSubscription(nonExistentSubscriptionId, updateInfo, validPaymentMethodInfo));
|
||||
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(nonExistentSubscriptionId);
|
||||
verify(subscriptionRepository, never()).save(any(Subscription.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when update data is not valid")
|
||||
void testUpdateSubscriptionWithInvalidData() {
|
||||
SubscriptionInfo invalidUpdateInfo = new SubscriptionInfo(0, desiredStartDate);
|
||||
|
||||
assertThrows(NotValidSubscriptionException.class,
|
||||
() -> subscriptionUseCase.updateSubscription(subscriptionId, invalidUpdateInfo, validPaymentMethodInfo));
|
||||
|
||||
verify(subscriptionRepository, never()).findBySubscriptionId(any(UUID.class));
|
||||
verify(subscriptionRepository, never()).save(any(Subscription.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Delete subscription tests")
|
||||
class DeleteSubscriptionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should delete subscriptions when customer ID exists")
|
||||
void testDeleteSubscriptionsOfACustomer() throws SubscriptionNotFoundException {
|
||||
when(subscriptionRepository.FindByCustomerId(customerId)).thenReturn(List.of(testSubscription));
|
||||
doNothing().when(subscriptionRepository).delete(testSubscription);
|
||||
|
||||
subscriptionUseCase.deleteSubscriptionsOfACustomer(customerId);
|
||||
|
||||
verify(subscriptionRepository, times(1)).FindByCustomerId(customerId);
|
||||
verify(subscriptionRepository, times(1)).delete(testSubscription);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when customer ID doesn't exist")
|
||||
void testDeleteSubscriptionsOfACustomerNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
when(subscriptionRepository.FindByCustomerId(nonExistentCustomerId)).thenReturn(List.of());
|
||||
|
||||
assertThrows(SubscriptionNotFoundException.class,
|
||||
() -> subscriptionUseCase.deleteSubscriptionsOfACustomer(nonExistentCustomerId));
|
||||
|
||||
verify(subscriptionRepository, times(1)).FindByCustomerId(nonExistentCustomerId);
|
||||
verify(subscriptionRepository, never()).delete(any(Subscription.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should delete subscription when subscription ID exist")
|
||||
void testDeleteSubscription() throws SubscriptionNotFoundException {
|
||||
when(subscriptionRepository.findBySubscriptionId(subscriptionId)).thenReturn(Optional.of(testSubscription));
|
||||
doNothing().when(subscriptionRepository).delete(testSubscription);
|
||||
|
||||
subscriptionUseCase.deleteSubscription(subscriptionId);
|
||||
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(subscriptionId);
|
||||
verify(subscriptionRepository, times(1)).delete(testSubscription);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when subscription ID doesn't exist")
|
||||
void testDeleteSubscriptionNotFound() {
|
||||
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||
when(subscriptionRepository.findBySubscriptionId(nonExistentSubscriptionId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(SubscriptionNotFoundException.class,
|
||||
() -> subscriptionUseCase.deleteSubscription(nonExistentSubscriptionId));
|
||||
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(nonExistentSubscriptionId);
|
||||
verify(subscriptionRepository, never()).delete(any(Subscription.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user