forked from pierront/mylibrary-template
Compare commits
28 Commits
2c33798c83
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| d0c7514624 | |||
| fd9863097c | |||
| c582b54a42 | |||
| a828b0db95 | |||
| 4511e4e961 | |||
| 889f17d806 | |||
| 8fc037e4b1 | |||
| 0d2a88f4a0 | |||
| a5c26c62e3 | |||
| 1ac8896253 | |||
| 4d7079c585 | |||
| 5b7e413a44 | |||
| f0704e4039 | |||
| 7ba85a0ae8 | |||
| 9bdccbb954 | |||
| 01e92154e7 | |||
| 42b0073297 | |||
| aa40ad0e3b | |||
| 76b60297c1 | |||
| 3ace5810f8 | |||
| 28a923c126 | |||
| a93ea4fc31 | |||
| 45d4016960 | |||
| 7c2eb19e13 | |||
| 49ddb77fb2 | |||
| 3deb17a4c7 | |||
| 655fad8fb2 | |||
| 5adb10101f |
@@ -19,6 +19,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"PasserCommande": {
|
"PasserCommande": {
|
||||||
"input": {
|
"input": {
|
||||||
"clientId": "uuid",
|
"clientId": "uuid",
|
||||||
@@ -40,6 +46,12 @@
|
|||||||
"pointsFideliteGagnes": "integer"
|
"pointsFideliteGagnes": "integer"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"GererAvis": {
|
"GererAvis": {
|
||||||
"input": {
|
"input": {
|
||||||
"clientId": "uuid",
|
"clientId": "uuid",
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||||
|
|
||||||
|
public record AdresseInfo(String rue, String ville, String codePostal, String pays) {}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public class CommandeDTO {
|
||||||
|
private UUID clientId;
|
||||||
|
private List<LigneCommandeInfo> lignesCommande;
|
||||||
|
private String rue;
|
||||||
|
private String ville;
|
||||||
|
private String codePostal;
|
||||||
|
private String pays;
|
||||||
|
private String modePaiement;
|
||||||
|
private UUID commandeId ;
|
||||||
|
private double montantTotal ;
|
||||||
|
private Integer pointsFideliteGagnes ;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record CommandeInfo(List<LigneCommandeInfo> listeLigne , String modePayement) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public class LigneCommandeDTO {
|
||||||
|
|
||||||
|
int quantite;
|
||||||
|
UUID id;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record LigneCommandeInfo(int quantite) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
|
||||||
|
|
||||||
|
public enum ModePaiement {
|
||||||
|
CB,
|
||||||
|
PAYPAL,
|
||||||
|
POINTS_FIDELITE,
|
||||||
|
}
|
||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||||
|
|
||||||
|
public final class CommandeConverter {
|
||||||
|
|
||||||
|
|
||||||
|
public static Commande toDomain(CommandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
|
||||||
|
return Commande.builder()
|
||||||
|
.lignesCommande(commandeInfo.listeLigne())
|
||||||
|
.rue(adressInfo.rue())
|
||||||
|
.ville(adressInfo.ville())
|
||||||
|
.codePostal(adressInfo.codePostal())
|
||||||
|
.pays(adressInfo.pays())
|
||||||
|
.modePaiement(commandeInfo.modePayement())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Commande toDomain(CommandeInfo commandeInfo, AdresseInfo adressInfo) {
|
||||||
|
return Commande.builder()
|
||||||
|
.lignesCommande(commandeInfo.listeLigne())
|
||||||
|
.rue(adressInfo.rue())
|
||||||
|
.ville(adressInfo.ville())
|
||||||
|
.codePostal(adressInfo.codePostal())
|
||||||
|
.pays(adressInfo.pays())
|
||||||
|
.modePaiement(commandeInfo.modePayement())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CommandeDTO toDTO(CommandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
|
||||||
|
return CommandeDTO.builder()
|
||||||
|
.lignesCommande(commandeInfo.listeLigne())
|
||||||
|
.rue(adressInfo.rue())
|
||||||
|
.ville(adressInfo.ville())
|
||||||
|
.codePostal(adressInfo.codePostal())
|
||||||
|
.pays(adressInfo.pays())
|
||||||
|
.modePaiement(modePaiement)
|
||||||
|
.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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.LigneCommande;
|
||||||
|
|
||||||
|
public final class LigneCommandeConverter {
|
||||||
|
|
||||||
|
public static LigneCommande toDomain(LigneCommandeInfo ligneCommandeInfo) {
|
||||||
|
return LigneCommande.builder()
|
||||||
|
.quantite(ligneCommandeInfo.quantite())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LigneCommandeDTO toDTO(LigneCommandeInfo ligneCommandeInfo) {
|
||||||
|
return LigneCommandeDTO.builder()
|
||||||
|
.quantite(ligneCommandeInfo.quantite())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public class Commande {
|
||||||
|
private UUID clientId;
|
||||||
|
private List<LigneCommandeInfo> lignesCommande;
|
||||||
|
private String rue;
|
||||||
|
private String ville;
|
||||||
|
private String codePostal;
|
||||||
|
private String pays;
|
||||||
|
private String modePaiement;
|
||||||
|
|
||||||
|
private UUID commandeId ;
|
||||||
|
private double montantTotal ;
|
||||||
|
private Integer pointsFideliteGagnes ;
|
||||||
|
|
||||||
|
public void setRandomUUID() {
|
||||||
|
this.clientId = UUID.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRandomUUIDCommande() {
|
||||||
|
this.commandeId = UUID.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int convertPointsFidelite(){
|
||||||
|
if (this.montantTotal==0|| this.montantTotal<=0){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return (int) Math.ceil(this.getMontantTotal());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// conversion point de fidéliter
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public class LigneCommande {
|
||||||
|
|
||||||
|
int quantite;
|
||||||
|
UUID livreId;
|
||||||
|
|
||||||
|
public void setRandomUUID() {
|
||||||
|
this.livreId = UUID.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
|
||||||
|
|
||||||
|
public class NotValidAdressException extends RuntimeException {
|
||||||
|
public NotValidAdressException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
|
||||||
|
|
||||||
|
public class NotValidCommandeException extends RuntimeException {
|
||||||
|
public NotValidCommandeException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
|
||||||
|
|
||||||
|
public class NotValidLigneCommandeException extends RuntimeException {
|
||||||
|
public NotValidLigneCommandeException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+47
@@ -0,0 +1,47 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class CommandeRepository {
|
||||||
|
|
||||||
|
private final List<Commande> commande = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<Commande> findAll(){return commande;}
|
||||||
|
|
||||||
|
|
||||||
|
public void deleteAll() {
|
||||||
|
commande.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Commande save(Commande newCommande){
|
||||||
|
Optional<Commande> optionalCommandeWithSameIsbn = this.findById(newCommande.getCommandeId());
|
||||||
|
optionalCommandeWithSameIsbn.ifPresent(commande::remove);
|
||||||
|
this.commande.add(newCommande);
|
||||||
|
return newCommande;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Commande> findById(UUID uuid){
|
||||||
|
return this.commande.stream()
|
||||||
|
.filter(customer -> customer.getCommandeId().equals(uuid))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsById(UUID uuid) {
|
||||||
|
return this.commande.stream()
|
||||||
|
.anyMatch(customer -> customer.getCommandeId().equals(uuid));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(Commande commande) {
|
||||||
|
this.commande.remove(commande);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void deleteById(UUID uuid) {
|
||||||
|
this.commande.removeIf(commande -> commande.getClientId().equals(uuid));
|
||||||
|
}
|
||||||
|
}
|
||||||
+73
@@ -0,0 +1,73 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
+87
@@ -0,0 +1,87 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public final class CommandeValidator {
|
||||||
|
|
||||||
|
public static final String LIGNE_COMMANDE_IS_NOT_VALIDE = "Ligne commande is not valide";
|
||||||
|
public static final String MODE_PAIEMENT_IS_NOT_VALIDE = "Mode Paiement is not valide";
|
||||||
|
public static final String LIST_LIGNE_COMANDE_IS_NOT_VALIDE = "List ligne commande is not valide";
|
||||||
|
public static final String STREET_IS_NOT_VALIDE = "rue is not valide";
|
||||||
|
|
||||||
|
public static final String CITY_IS_NOT_VALIDE = "city is not valide";
|
||||||
|
public static final String CODE_POSTAL_IS_NOT_VALIDE = "code postal is not valide";
|
||||||
|
public static final String COUNTRY_IS_NOT_VALIDE = "Country is not valide";
|
||||||
|
|
||||||
|
|
||||||
|
public static void validate(LigneCommandeInfo ligneCommandeInfo) {
|
||||||
|
if (ligneCommandeInfo.quantite()<=0){
|
||||||
|
throw new NotValidLigneCommandeException(LIGNE_COMMANDE_IS_NOT_VALIDE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void validate(CommandeInfo commandeInfo) {
|
||||||
|
|
||||||
|
validateModePaiement(commandeInfo);
|
||||||
|
validateListLigneCommande(commandeInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void validate(AdresseInfo adresseInfo) {
|
||||||
|
validateStreet(adresseInfo);
|
||||||
|
validateCity(adresseInfo);
|
||||||
|
validateCodePostal(adresseInfo);
|
||||||
|
validateCountry(adresseInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static void validateModePaiement(CommandeInfo commandeInfo) {
|
||||||
|
if(commandeInfo.modePayement()!="CB"&& commandeInfo.modePayement()!="PAYPAL"&& commandeInfo.modePayement()!="POINTS_FIDELITE"){
|
||||||
|
throw new NotValidCommandeException(MODE_PAIEMENT_IS_NOT_VALIDE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateListLigneCommande(CommandeInfo commandeInfo) {
|
||||||
|
if (commandeInfo.listeLigne() == null
|
||||||
|
|| commandeInfo.listeLigne().isEmpty()
|
||||||
|
|| commandeInfo.listeLigne().stream().anyMatch(Objects::isNull)){
|
||||||
|
throw new NotValidCommandeException(LIST_LIGNE_COMANDE_IS_NOT_VALIDE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateStreet(AdresseInfo adresseInfo) {
|
||||||
|
if (adresseInfo.rue() == null || adresseInfo.rue().isBlank()){
|
||||||
|
throw new NotValidAdressException(STREET_IS_NOT_VALIDE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static void validateCity(AdresseInfo adresseInfo) {
|
||||||
|
if (adresseInfo.ville() == null || adresseInfo.ville().isBlank()){
|
||||||
|
throw new NotValidAdressException(CITY_IS_NOT_VALIDE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateCodePostal(AdresseInfo adresseInfo) {
|
||||||
|
if (adresseInfo.codePostal() == null || adresseInfo.codePostal().isBlank()){
|
||||||
|
throw new NotValidAdressException(CODE_POSTAL_IS_NOT_VALIDE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateCountry(AdresseInfo adresseInfo) {
|
||||||
|
if (adresseInfo.pays() == null || adresseInfo.pays().isBlank()){
|
||||||
|
throw new NotValidAdressException(COUNTRY_IS_NOT_VALIDE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+2
-2
@@ -1,13 +1,13 @@
|
|||||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||||
|
|
||||||
public enum SubscriptionDurationDesired {
|
public enum DesiredSubscriptionDuration {
|
||||||
THREE(3),
|
THREE(3),
|
||||||
SIX(6),
|
SIX(6),
|
||||||
TWELVE(12);
|
TWELVE(12);
|
||||||
|
|
||||||
private final Integer value;
|
private final Integer value;
|
||||||
|
|
||||||
SubscriptionDurationDesired(Integer value) {
|
DesiredSubscriptionDuration(Integer value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ public class SubscriptionDTO {
|
|||||||
|
|
||||||
private UUID subscriptionId;
|
private UUID subscriptionId;
|
||||||
private UUID customerId;
|
private UUID customerId;
|
||||||
private Integer subscriptionDurationDesired;
|
private Integer desiredSubscriptionDuration;
|
||||||
private PaymentMethod paymentMethod;
|
private PaymentMethod paymentMethod;
|
||||||
private LocalDate desiredStartDate;
|
private LocalDate desiredStartDate;
|
||||||
private LocalDate startDate;
|
private LocalDate startDate;
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
|||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
public record SubscriptionInfo(Integer subscriptionDurationDesired, LocalDate desiredStartDate) {
|
public record SubscriptionInfo(Integer desiredSubscriptionDuration, LocalDate desiredStartDate) {
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -19,11 +19,11 @@ public class SubscriptionConverter {
|
|||||||
.details(newPaymentMethod.details())
|
.details(newPaymentMethod.details())
|
||||||
.build();
|
.build();
|
||||||
return Subscription.builder()
|
return Subscription.builder()
|
||||||
.subscriptionDurationDesired(newSubscription.subscriptionDurationDesired())
|
.desiredSubscriptionDuration(newSubscription.desiredSubscriptionDuration())
|
||||||
.paymentMethod(paymentMethod)
|
.paymentMethod(paymentMethod)
|
||||||
.desiredStartDate(newSubscription.desiredStartDate())
|
.desiredStartDate(newSubscription.desiredStartDate())
|
||||||
.startDate(newSubscription.desiredStartDate())
|
.startDate(newSubscription.desiredStartDate())
|
||||||
.endDate(newSubscription.desiredStartDate().plusMonths(newSubscription.subscriptionDurationDesired()))
|
.endDate(newSubscription.desiredStartDate().plusMonths(newSubscription.desiredSubscriptionDuration()))
|
||||||
.monthlyAmount(0)
|
.monthlyAmount(0)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ public class SubscriptionConverter {
|
|||||||
return SubscriptionDTO.builder()
|
return SubscriptionDTO.builder()
|
||||||
.subscriptionId(subscription.getSubscriptionId())
|
.subscriptionId(subscription.getSubscriptionId())
|
||||||
.customerId(subscription.getCustomerId())
|
.customerId(subscription.getCustomerId())
|
||||||
.subscriptionDurationDesired(subscription.getSubscriptionDurationDesired())
|
.desiredSubscriptionDuration(subscription.getDesiredSubscriptionDuration())
|
||||||
.paymentMethod(subscription.getPaymentMethod())
|
.paymentMethod(subscription.getPaymentMethod())
|
||||||
.desiredStartDate(subscription.getDesiredStartDate())
|
.desiredStartDate(subscription.getDesiredStartDate())
|
||||||
.startDate(subscription.getStartDate())
|
.startDate(subscription.getStartDate())
|
||||||
|
|||||||
+2
-2
@@ -12,7 +12,7 @@ public class Subscription {
|
|||||||
|
|
||||||
private UUID subscriptionId;
|
private UUID subscriptionId;
|
||||||
private UUID customerId;
|
private UUID customerId;
|
||||||
private Integer subscriptionDurationDesired;
|
private Integer desiredSubscriptionDuration;
|
||||||
private PaymentMethod paymentMethod;
|
private PaymentMethod paymentMethod;
|
||||||
private LocalDate desiredStartDate;
|
private LocalDate desiredStartDate;
|
||||||
private LocalDate startDate;
|
private LocalDate startDate;
|
||||||
@@ -34,7 +34,7 @@ public class Subscription {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setEndDate(){
|
public void setEndDate(){
|
||||||
this.endDate = this.desiredStartDate.plusMonths(this.subscriptionDurationDesired);
|
this.endDate = this.desiredStartDate.plusMonths(this.desiredSubscriptionDuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDateSubscription(){
|
public void setDateSubscription(){
|
||||||
|
|||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
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
@@ -0,0 +1,62 @@
|
|||||||
|
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
@@ -0,0 +1,110 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -8,7 +8,7 @@ import java.util.Set;
|
|||||||
|
|
||||||
public class SubscriptionValidator {
|
public class SubscriptionValidator {
|
||||||
|
|
||||||
public static final String SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALID = "subscriptionDurationDesired will be 3, 6 or 12 by the ENUM file SubscriptionDurationDesired";
|
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";
|
public static final String DESIRED_START_DATE_CANNOT_BE_BEFORE_TODAY = "desiredStartDate cannot be before today";
|
||||||
|
|
||||||
private SubscriptionValidator() {
|
private SubscriptionValidator() {
|
||||||
@@ -23,7 +23,7 @@ public class SubscriptionValidator {
|
|||||||
private static void validateSubscriptionDurationDesired(SubscriptionInfo newSubscription)
|
private static void validateSubscriptionDurationDesired(SubscriptionInfo newSubscription)
|
||||||
throws NotValidSubscriptionException {
|
throws NotValidSubscriptionException {
|
||||||
Set<Integer> VALID_DURATIONS = Set.of(3, 6, 12);
|
Set<Integer> VALID_DURATIONS = Set.of(3, 6, 12);
|
||||||
if (!VALID_DURATIONS.contains(newSubscription.subscriptionDurationDesired())) {
|
if (!VALID_DURATIONS.contains(newSubscription.desiredSubscriptionDuration())) {
|
||||||
throw new NotValidSubscriptionException(SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALID);}
|
throw new NotValidSubscriptionException(SUBSCRIPTION_DURATION_DESIRED_IS_NOT_VALID);}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+65
@@ -0,0 +1,65 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.*;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
public class CommandeConverterTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestConvertCommandeToDomain(){
|
||||||
|
String modePaiement = ModePaiement.CB.name();
|
||||||
|
List<LigneCommandeInfo> listLigne = new ArrayList<>();
|
||||||
|
LigneCommandeInfo ligne = new LigneCommandeInfo(12);
|
||||||
|
LigneCommandeInfo ligne2 = new LigneCommandeInfo(14);
|
||||||
|
listLigne.add(ligne);
|
||||||
|
listLigne.add(ligne2);
|
||||||
|
CommandeInfo commandeInfo = new CommandeInfo(listLigne,modePaiement);
|
||||||
|
AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France");
|
||||||
|
|
||||||
|
|
||||||
|
Commande result = CommandeConverter.toDomain(commandeInfo,adressInfo,modePaiement);
|
||||||
|
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(commandeInfo.listeLigne(), result.getLignesCommande());
|
||||||
|
assertEquals(adressInfo.rue() , result.getRue());
|
||||||
|
assertEquals(adressInfo.ville() , result.getVille());
|
||||||
|
assertEquals(adressInfo.codePostal() , result.getCodePostal());
|
||||||
|
assertEquals(adressInfo.pays() , result.getPays());
|
||||||
|
assertEquals( modePaiement,result.getModePaiement());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestConvertCommandeToDTO(){
|
||||||
|
String modePaiement = ModePaiement.CB.name();
|
||||||
|
List<LigneCommandeInfo> listLigne = new ArrayList<>();
|
||||||
|
LigneCommandeInfo ligne = new LigneCommandeInfo(12);
|
||||||
|
LigneCommandeInfo ligne2 = new LigneCommandeInfo(14);
|
||||||
|
listLigne.add(ligne);
|
||||||
|
listLigne.add(ligne2);
|
||||||
|
CommandeInfo commandeInfo = new CommandeInfo(listLigne,modePaiement);
|
||||||
|
AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France");
|
||||||
|
|
||||||
|
|
||||||
|
CommandeDTO result = CommandeConverter.toDTO(commandeInfo,adressInfo,modePaiement);
|
||||||
|
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(commandeInfo.listeLigne(), result.getLignesCommande());
|
||||||
|
assertEquals(adressInfo.rue() , result.getRue());
|
||||||
|
assertEquals(adressInfo.ville() , result.getVille());
|
||||||
|
assertEquals(adressInfo.codePostal() , result.getCodePostal());
|
||||||
|
assertEquals(adressInfo.pays() , result.getPays());
|
||||||
|
assertEquals( modePaiement,result.getModePaiement());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.LigneCommande;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class LigneCommandeConverterTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestConvertLigneCommandeToDomain(){
|
||||||
|
LigneCommandeInfo ligne = new LigneCommandeInfo(12);
|
||||||
|
|
||||||
|
LigneCommande result = LigneCommandeConverter.toDomain(ligne);
|
||||||
|
|
||||||
|
|
||||||
|
assertEquals(ligne.quantite(),result.getQuantite());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestConvertLigneCommandeToDTO(){
|
||||||
|
LigneCommandeInfo ligne = new LigneCommandeInfo(12);
|
||||||
|
|
||||||
|
LigneCommandeDTO result = LigneCommandeConverter.toDTO(ligne);
|
||||||
|
|
||||||
|
|
||||||
|
assertEquals(ligne.quantite(),result.getQuantite());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class CommandeTest {
|
||||||
|
@Test
|
||||||
|
public void commandeTest(){
|
||||||
|
|
||||||
|
UUID clientId = UUID.randomUUID();
|
||||||
|
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
|
||||||
|
LigneCommandeInfo commande1 = new LigneCommandeInfo(12);
|
||||||
|
LigneCommandeInfo commande2 = new LigneCommandeInfo(13);
|
||||||
|
lignesCommande.add(commande1);
|
||||||
|
lignesCommande.add(commande2);
|
||||||
|
String rue = "rue du chien" ;
|
||||||
|
String ville = "LKa Rochette" ;
|
||||||
|
String codePostal = "7700" ;
|
||||||
|
String pays = "France" ;
|
||||||
|
String modePayement = ModePaiement.CB.name() ;
|
||||||
|
|
||||||
|
Commande commande = Commande.builder()
|
||||||
|
.clientId(clientId)
|
||||||
|
.lignesCommande(lignesCommande)
|
||||||
|
.rue(rue)
|
||||||
|
.ville(ville)
|
||||||
|
.codePostal(codePostal)
|
||||||
|
.pays(pays)
|
||||||
|
.modePaiement(modePayement)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals(clientId, commande.getClientId());
|
||||||
|
assertEquals(lignesCommande, commande.getLignesCommande());
|
||||||
|
assertEquals(rue, commande.getRue());
|
||||||
|
assertEquals(ville, commande.getVille());
|
||||||
|
assertEquals(codePostal, commande.getCodePostal());
|
||||||
|
assertEquals(pays, commande.getPays());
|
||||||
|
assertEquals(modePayement, commande.getModePaiement());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("setRandomUUID should change the ID to a new random UUID")
|
||||||
|
void testSetRandomUUID() {
|
||||||
|
Commande commande = Commande.builder().build();
|
||||||
|
UUID originalId = commande.getClientId();
|
||||||
|
|
||||||
|
commande.setRandomUUID();
|
||||||
|
|
||||||
|
assertNotNull(commande.getClientId());
|
||||||
|
assertNotEquals(originalId, commande.getClientId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("setRandomUUID should change the ID to a new random UUID")
|
||||||
|
void testSetRandomUUIDForCommande() {
|
||||||
|
Commande commande = Commande.builder().build();
|
||||||
|
UUID originalId = commande.getCommandeId();
|
||||||
|
|
||||||
|
commande.setRandomUUIDCommande();
|
||||||
|
|
||||||
|
assertNotNull(commande.getCommandeId());
|
||||||
|
assertNotEquals(originalId, commande.getCommandeId());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConvertPointsFideliteGagnes(){
|
||||||
|
Commande commande = Commande.builder()
|
||||||
|
.montantTotal(14.34)
|
||||||
|
.build();
|
||||||
|
Integer point = commande.convertPointsFidelite();
|
||||||
|
|
||||||
|
assertEquals(point,(int) Math.ceil(commande.getMontantTotal()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(doubles = {-1.9,0.0})
|
||||||
|
void testConvertPointsFideliteGagnesWhenMontantTotalIsNotGood(){
|
||||||
|
Commande commande = Commande.builder()
|
||||||
|
.montantTotal(14.34)
|
||||||
|
.build();
|
||||||
|
Integer point = commande.convertPointsFidelite();
|
||||||
|
|
||||||
|
assertEquals(point,(int) Math.ceil(commande.getMontantTotal()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+41
@@ -0,0 +1,41 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class LigneCommandeTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Builder should create a valid LigneCommande instance")
|
||||||
|
void TestLigneCommande() {
|
||||||
|
UUID id = UUID.randomUUID();
|
||||||
|
int quantite = 12;
|
||||||
|
|
||||||
|
|
||||||
|
LigneCommande ligneCommande = LigneCommande.builder()
|
||||||
|
.livreId(id)
|
||||||
|
.quantite(quantite)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals(id, ligneCommande.getLivreId());
|
||||||
|
assertEquals(quantite, ligneCommande.getQuantite());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("setRandomUUID should change the ID to a new random UUID")
|
||||||
|
void testSetRandomUUID() {
|
||||||
|
LigneCommande ligneCommande = LigneCommande.builder().build();
|
||||||
|
UUID originalId = ligneCommande.getLivreId();
|
||||||
|
|
||||||
|
ligneCommande.setRandomUUID();
|
||||||
|
|
||||||
|
assertNotNull(ligneCommande.getLivreId());
|
||||||
|
assertNotEquals(originalId, ligneCommande.getLivreId());
|
||||||
|
}
|
||||||
|
}
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+67
@@ -0,0 +1,67 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class NotValidAdressExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be created with the provided message")
|
||||||
|
void testExceptionCreation() {
|
||||||
|
String errorMessage = "Adresse is not valid";
|
||||||
|
|
||||||
|
NotValidAdressException exception = new NotValidAdressException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {
|
||||||
|
"rue is not valide",
|
||||||
|
"city is not valide",
|
||||||
|
"code postal is not valide",
|
||||||
|
"Country is not valide"
|
||||||
|
})
|
||||||
|
@DisplayName("Exception should handle different validation messages")
|
||||||
|
void testExceptionWithDifferentMessages(String errorMessage) {
|
||||||
|
NotValidAdressException exception = new NotValidAdressException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
String errorMessage = "Required field is missing";
|
||||||
|
|
||||||
|
Exception exception = assertThrows(NotValidAdressException.class, () -> {
|
||||||
|
throw new NotValidAdressException(errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be catchable as a general Exception")
|
||||||
|
void testExceptionInheritance() {
|
||||||
|
String errorMessage = "Invalid Adresse ";
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new NotValidAdressException(errorMessage);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertEquals(NotValidAdressException.class, e.getClass());
|
||||||
|
assertEquals(errorMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+61
@@ -0,0 +1,61 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class NotValidCommandeExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be created with the provided message")
|
||||||
|
void testExceptionCreation() {
|
||||||
|
String errorMessage = "Commande is not valid";
|
||||||
|
|
||||||
|
NotValidCommandeException exception = new NotValidCommandeException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {
|
||||||
|
"Mode Paiement is not valide",
|
||||||
|
"List ligne commande is not valide"
|
||||||
|
})
|
||||||
|
@DisplayName("Exception should handle different validation messages")
|
||||||
|
void testExceptionWithDifferentMessages(String errorMessage) {
|
||||||
|
NotValidCommandeException exception = new NotValidCommandeException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
String errorMessage = "Required field is missing";
|
||||||
|
|
||||||
|
Exception exception = assertThrows(NotValidCommandeException.class, () -> {
|
||||||
|
throw new NotValidCommandeException(errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be catchable as a general Exception")
|
||||||
|
void testExceptionInheritance() {
|
||||||
|
String errorMessage = "Invalid Commande ";
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new NotValidCommandeException(errorMessage);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertEquals(NotValidCommandeException.class, e.getClass());
|
||||||
|
assertEquals(errorMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+47
@@ -0,0 +1,47 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class NotValidLigneCommandeExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should handle different validation messages")
|
||||||
|
void testExceptionCreation() {
|
||||||
|
String errorMessage = "Ligne commande is not valide";
|
||||||
|
NotValidLigneCommandeException exception = new NotValidLigneCommandeException(errorMessage);
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
String errorMessage = "Required field is missing";
|
||||||
|
|
||||||
|
Exception exception = assertThrows(NotValidLigneCommandeException.class, () -> {
|
||||||
|
throw new NotValidLigneCommandeException(errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertEquals(errorMessage, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be catchable as a general Exception")
|
||||||
|
void testExceptionInheritance() {
|
||||||
|
String errorMessage = "Invalid Commande ";
|
||||||
|
|
||||||
|
try {
|
||||||
|
throw new NotValidLigneCommandeException(errorMessage);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertEquals(NotValidLigneCommandeException.class, e.getClass());
|
||||||
|
assertEquals(errorMessage, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+349
@@ -0,0 +1,349 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class CommandeRepositoryTest {
|
||||||
|
|
||||||
|
private CommandeRepository repository;
|
||||||
|
private Commande commandeN1;
|
||||||
|
private Commande commandeN2;
|
||||||
|
private Commande commandeN3;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
repository = new CommandeRepository();
|
||||||
|
|
||||||
|
UUID clientId = UUID.randomUUID();
|
||||||
|
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
|
||||||
|
LigneCommandeInfo commande1 = new LigneCommandeInfo(12);
|
||||||
|
LigneCommandeInfo commande2 = new LigneCommandeInfo(13);
|
||||||
|
lignesCommande.add(commande1);
|
||||||
|
lignesCommande.add(commande2);
|
||||||
|
String rue = "rue du chien";
|
||||||
|
String ville = "LKa Rochette";
|
||||||
|
String codePostal = "7700";
|
||||||
|
String pays = "France";
|
||||||
|
String modePayement = ModePaiement.CB.name();
|
||||||
|
|
||||||
|
commandeN1 = Commande.builder()
|
||||||
|
.clientId(clientId)
|
||||||
|
.lignesCommande(lignesCommande)
|
||||||
|
.rue(rue)
|
||||||
|
.ville(ville)
|
||||||
|
.codePostal(codePostal)
|
||||||
|
.pays(pays)
|
||||||
|
.modePaiement(modePayement)
|
||||||
|
.build();
|
||||||
|
commandeN1.setRandomUUIDCommande();
|
||||||
|
|
||||||
|
|
||||||
|
List<LigneCommandeInfo> lignesCommande2 = new ArrayList<>();
|
||||||
|
LigneCommandeInfo commande3 = new LigneCommandeInfo(43);
|
||||||
|
LigneCommandeInfo commande4 = new LigneCommandeInfo(1);
|
||||||
|
lignesCommande2.add(commande3);
|
||||||
|
lignesCommande2.add(commande4);
|
||||||
|
|
||||||
|
commandeN2 = Commande.builder()
|
||||||
|
.clientId(clientId)
|
||||||
|
.lignesCommande(lignesCommande2)
|
||||||
|
.rue(rue)
|
||||||
|
.ville(ville)
|
||||||
|
.codePostal(codePostal)
|
||||||
|
.pays(pays)
|
||||||
|
.modePaiement(modePayement)
|
||||||
|
.build();
|
||||||
|
commandeN2.setRandomUUIDCommande();
|
||||||
|
|
||||||
|
List<LigneCommandeInfo> lignesCommande3 = new ArrayList<>();
|
||||||
|
LigneCommandeInfo commande6 = new LigneCommandeInfo(12);
|
||||||
|
LigneCommandeInfo commande7 = new LigneCommandeInfo(13);
|
||||||
|
lignesCommande3.add(commande6);
|
||||||
|
lignesCommande3.add(commande7);
|
||||||
|
String rue2 = "rue du chevre chaud ";
|
||||||
|
String ville2 = "jsp";
|
||||||
|
String codePostal2 = "77";
|
||||||
|
String pays2 = "Lune ";
|
||||||
|
String modePayement2 = ModePaiement.PAYPAL.name();
|
||||||
|
|
||||||
|
commandeN3 = Commande.builder()
|
||||||
|
.lignesCommande(lignesCommande3)
|
||||||
|
.rue(rue2)
|
||||||
|
.ville(ville2)
|
||||||
|
.codePostal(codePostal2)
|
||||||
|
.pays(pays2)
|
||||||
|
.modePaiement(modePayement2)
|
||||||
|
.build();
|
||||||
|
commandeN3.setRandomUUID();
|
||||||
|
commandeN3.setRandomUUIDCommande();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("New repository should be empty")
|
||||||
|
void testNewRepositoryIsEmpty() {
|
||||||
|
List<Commande> commande = repository.findAll();
|
||||||
|
|
||||||
|
assertTrue(commande.isEmpty());
|
||||||
|
assertEquals(0, commande.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Save operations")
|
||||||
|
class SaveOperations {
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should add a new comande")
|
||||||
|
void testSaveNewCommande() {
|
||||||
|
Commande savedCommande = repository.save(commandeN1);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(commandeN1.getCommandeId(), savedCommande.getCommandeId());
|
||||||
|
assertEquals(commandeN1.getClientId(), savedCommande.getClientId());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should update existing commande with same ID")
|
||||||
|
void testSaveUpdatesExistingCommande() {
|
||||||
|
repository.save(commandeN1);
|
||||||
|
|
||||||
|
|
||||||
|
List<LigneCommandeInfo> lignesCommande2 = new ArrayList<>();
|
||||||
|
LigneCommandeInfo commande3 = new LigneCommandeInfo(43);
|
||||||
|
LigneCommandeInfo commande4 = new LigneCommandeInfo(1);
|
||||||
|
lignesCommande2.add(commande3);
|
||||||
|
lignesCommande2.add(commande4);
|
||||||
|
|
||||||
|
|
||||||
|
UUID id = commandeN1.getCommandeId();
|
||||||
|
Commande updatedCommande = Commande.builder()
|
||||||
|
.commandeId(id)
|
||||||
|
.lignesCommande(lignesCommande2)
|
||||||
|
.rue("rue")
|
||||||
|
.ville("moi")
|
||||||
|
.codePostal("1")
|
||||||
|
.pays("Russie")
|
||||||
|
.modePaiement(ModePaiement.POINTS_FIDELITE.name())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Commande savedCommande = repository.save(updatedCommande);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(id, savedCommande.getCommandeId());
|
||||||
|
assertEquals("rue", savedCommande.getRue());
|
||||||
|
assertEquals("moi", savedCommande.getVille());
|
||||||
|
assertEquals("1", savedCommande.getCodePostal());
|
||||||
|
assertEquals("Russie", savedCommande.getPays());
|
||||||
|
assertEquals(ModePaiement.POINTS_FIDELITE.name(), savedCommande.getModePaiement());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save multiple customers should add all of them")
|
||||||
|
void testSaveMultipleCommande() {
|
||||||
|
repository.save(commandeN1);
|
||||||
|
repository.save(commandeN2);
|
||||||
|
|
||||||
|
List<Commande> commande = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, commande.size());
|
||||||
|
assertTrue(commande.contains(commandeN1));
|
||||||
|
assertTrue(commande.contains(commandeN2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find operations")
|
||||||
|
class FindOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpCommande() {
|
||||||
|
repository.save(commandeN1);
|
||||||
|
repository.save(commandeN2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindAll should return all commande")
|
||||||
|
void testFindAll() {
|
||||||
|
List<Commande> commande = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, commande.size());
|
||||||
|
assertTrue(commande.contains(commandeN1));
|
||||||
|
assertTrue(commande.contains(commandeN2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindById should return commande with matching ID")
|
||||||
|
void testFindById() {
|
||||||
|
Optional<Commande> foundCommande = repository.findById(commandeN1.getCommandeId());
|
||||||
|
|
||||||
|
assertTrue(foundCommande.isPresent());
|
||||||
|
assertEquals(commandeN1.getCommandeId(), foundCommande.get().getCommandeId());
|
||||||
|
assertEquals(commandeN1.getClientId(), foundCommande.get().getClientId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFindByIdNotFound(){
|
||||||
|
UUID nonExistentId = UUID.randomUUID();
|
||||||
|
Optional<Commande> foundCommande = repository.findById(nonExistentId);
|
||||||
|
|
||||||
|
assertTrue(foundCommande.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExistsByIdExists(){
|
||||||
|
boolean exists = repository.existsById(commandeN1.getCommandeId());
|
||||||
|
|
||||||
|
assertTrue(exists);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExistsByIdNotExists(){
|
||||||
|
UUID nonExistentId = UUID.randomUUID();
|
||||||
|
boolean exists = repository.existsById(nonExistentId);
|
||||||
|
|
||||||
|
assertFalse(exists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Delete operations")
|
||||||
|
class DeleteOperations {
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpCustomers() {
|
||||||
|
repository.save(commandeN1);
|
||||||
|
repository.save(commandeN2);
|
||||||
|
repository.save(commandeN3);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should remove the specified customer")
|
||||||
|
void testDelete() {
|
||||||
|
repository.delete(commandeN1);
|
||||||
|
|
||||||
|
List<Commande> commande = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, commande.size());
|
||||||
|
assertFalse(commande.contains(commandeN1));
|
||||||
|
assertTrue(commande.contains(commandeN2));
|
||||||
|
assertTrue(commande.contains(commandeN3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("DeleteAll should remove all customers")
|
||||||
|
void testDeleteAll() {
|
||||||
|
repository.deleteAll();
|
||||||
|
|
||||||
|
List<Commande> commande = repository.findAll();
|
||||||
|
|
||||||
|
assertTrue(commande.isEmpty());
|
||||||
|
assertEquals(0, commande.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should not throw exception when customer doesn't exist")
|
||||||
|
void testDeleteNonExistentCustomer() {
|
||||||
|
|
||||||
|
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
|
||||||
|
LigneCommandeInfo commande1 = new LigneCommandeInfo(321);
|
||||||
|
LigneCommandeInfo commande2 = new LigneCommandeInfo(3);
|
||||||
|
lignesCommande.add(commande1);
|
||||||
|
lignesCommande.add(commande2);
|
||||||
|
|
||||||
|
|
||||||
|
Commande nonExistentCommande = Commande.builder()
|
||||||
|
.lignesCommande(lignesCommande)
|
||||||
|
.rue("non")
|
||||||
|
.ville("non")
|
||||||
|
.codePostal("Existent")
|
||||||
|
.pays("0000")
|
||||||
|
.modePaiement(ModePaiement.PAYPAL.name())
|
||||||
|
.build();
|
||||||
|
nonExistentCommande.setRandomUUIDCommande();
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> repository.delete(nonExistentCommande));
|
||||||
|
|
||||||
|
assertEquals(3, repository.findAll().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestDeleteAllCommandeByCustomerID() {
|
||||||
|
System.out.println(commandeN1.getClientId());
|
||||||
|
System.out.println(commandeN2.getClientId());
|
||||||
|
System.out.println(commandeN3.getClientId());
|
||||||
|
repository.deleteById(commandeN1.getClientId());
|
||||||
|
|
||||||
|
|
||||||
|
List<Commande> commande = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(1, commande.size());
|
||||||
|
assertFalse(commande.contains(commandeN1));
|
||||||
|
assertFalse(commande.contains(commandeN2));
|
||||||
|
assertTrue(commande.contains(commandeN3));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void TestDeleteAllCommandeByNonExistentCustomerID() {
|
||||||
|
|
||||||
|
|
||||||
|
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
|
||||||
|
LigneCommandeInfo commande1 = new LigneCommandeInfo(321);
|
||||||
|
LigneCommandeInfo commande2 = new LigneCommandeInfo(3);
|
||||||
|
lignesCommande.add(commande1);
|
||||||
|
lignesCommande.add(commande2);
|
||||||
|
|
||||||
|
|
||||||
|
Commande nonExistentCommande = Commande.builder()
|
||||||
|
.lignesCommande(lignesCommande)
|
||||||
|
.rue("non")
|
||||||
|
.ville("non")
|
||||||
|
.codePostal("Existent")
|
||||||
|
.pays("0000")
|
||||||
|
.modePaiement(ModePaiement.PAYPAL.name())
|
||||||
|
.build();
|
||||||
|
nonExistentCommande.setRandomUUIDCommande();
|
||||||
|
|
||||||
|
|
||||||
|
repository.deleteById(nonExistentCommande.getClientId());
|
||||||
|
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> repository.findAll());
|
||||||
|
|
||||||
|
assertEquals(3, repository.findAll().size());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
+249
@@ -0,0 +1,249 @@
|
|||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+165
@@ -0,0 +1,165 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class CommandeValidatorTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateValideLigneCommande () {
|
||||||
|
LigneCommandeInfo ligneCommandeValide = new LigneCommandeInfo(5);
|
||||||
|
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> CommandeValidator.validate(ligneCommandeValide));
|
||||||
|
}
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(ints = {-5,0 })
|
||||||
|
void testValidateNotValideLigneCommande (int notValiteQuantite) {
|
||||||
|
LigneCommandeInfo ligneCommandeNotValide = new LigneCommandeInfo(notValiteQuantite);
|
||||||
|
NotValidLigneCommandeException exception = assertThrows(
|
||||||
|
NotValidLigneCommandeException.class,
|
||||||
|
() -> CommandeValidator.validate(ligneCommandeNotValide)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(CommandeValidator.LIGNE_COMMANDE_IS_NOT_VALIDE, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@EnumSource(value = ModePaiement.class, names = {"CB", "PAYPAL", "POINTS_FIDELITE"})
|
||||||
|
void testValidateValideCommande(ModePaiement modePaiement) {
|
||||||
|
LigneCommandeInfo ligneCommandeValide = new LigneCommandeInfo(5);
|
||||||
|
List<LigneCommandeInfo> listeLigne = new ArrayList<>();
|
||||||
|
listeLigne.add(ligneCommandeValide);
|
||||||
|
String modePaiementValide = modePaiement.toString();
|
||||||
|
CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> CommandeValidator.validate(commandeValid));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"jambon", " ", ""})
|
||||||
|
void testValidateNotValideModePaiement(String modePaiementError) {
|
||||||
|
LigneCommandeInfo ligneCommandeValide = new LigneCommandeInfo(5);
|
||||||
|
List<LigneCommandeInfo> listeLigne = new ArrayList<>();
|
||||||
|
listeLigne.add(ligneCommandeValide);
|
||||||
|
String modePaiementValide = modePaiementError;
|
||||||
|
CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide);
|
||||||
|
|
||||||
|
NotValidCommandeException exception = assertThrows(
|
||||||
|
NotValidCommandeException.class,
|
||||||
|
() -> CommandeValidator.validate(commandeValid)
|
||||||
|
);
|
||||||
|
assertEquals(CommandeValidator.MODE_PAIEMENT_IS_NOT_VALIDE, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("provideInvalidOrderLists")
|
||||||
|
void testValidateNotValideListLigneCommande(List<LigneCommandeInfo> listeLigne) {
|
||||||
|
String modePaiementValide = ModePaiement.CB.toString();
|
||||||
|
CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide);
|
||||||
|
NotValidCommandeException exception = assertThrows(
|
||||||
|
NotValidCommandeException.class,
|
||||||
|
() -> CommandeValidator.validate(commandeValid)
|
||||||
|
);
|
||||||
|
assertEquals(CommandeValidator.LIST_LIGNE_COMANDE_IS_NOT_VALIDE, exception.getMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
static Stream<Arguments> provideInvalidOrderLists() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(Collections.emptyList()),
|
||||||
|
Arguments.of(Arrays.asList(new LigneCommandeInfo(5), null, null, new LigneCommandeInfo(12))),
|
||||||
|
Arguments.of((List<LigneCommandeInfo>) null)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class TestAdress{
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateValideLigneCommande () {
|
||||||
|
AdresseInfo adress = new AdresseInfo("rue du chien","La Rochette","7700","France");
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> CommandeValidator.validate(adress));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullSource
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
void testValidateNotValideStreet(String notvalideStreet) {
|
||||||
|
AdresseInfo adress = new AdresseInfo(notvalideStreet,"La Rochette","7700","France");
|
||||||
|
NotValidAdressException exception = assertThrows(
|
||||||
|
NotValidAdressException.class,
|
||||||
|
() -> CommandeValidator.validate(adress)
|
||||||
|
);
|
||||||
|
assertEquals(CommandeValidator.STREET_IS_NOT_VALIDE, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullSource
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
void testValidateNotValideCity(String notvalideCity) {
|
||||||
|
AdresseInfo adress = new AdresseInfo("rue du chien",notvalideCity,"7700","France");
|
||||||
|
NotValidAdressException exception = assertThrows(
|
||||||
|
NotValidAdressException.class,
|
||||||
|
() -> CommandeValidator.validate(adress)
|
||||||
|
);
|
||||||
|
assertEquals(CommandeValidator.CITY_IS_NOT_VALIDE, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullSource
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
void testValidateNotValideCodePostal(String notvalideCodePostal) {
|
||||||
|
AdresseInfo adress = new AdresseInfo("rue du chien","La Rochette",notvalideCodePostal,"France");
|
||||||
|
NotValidAdressException exception = assertThrows(
|
||||||
|
NotValidAdressException.class,
|
||||||
|
() -> CommandeValidator.validate(adress)
|
||||||
|
);
|
||||||
|
assertEquals(CommandeValidator.CODE_POSTAL_IS_NOT_VALIDE, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@NullSource
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
void testValidateNotValideCountry(String notvalideCountry) {
|
||||||
|
AdresseInfo adress = new AdresseInfo("rue du chien","La Rochette","7700",notvalideCountry);
|
||||||
|
NotValidAdressException exception = assertThrows(
|
||||||
|
NotValidAdressException.class,
|
||||||
|
() -> CommandeValidator.validate(adress)
|
||||||
|
);
|
||||||
|
assertEquals(CommandeValidator.COUNTRY_IS_NOT_VALIDE, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+6
-6
@@ -26,7 +26,7 @@ public class SubscriptionConverterTest {
|
|||||||
// Given
|
// Given
|
||||||
PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "Maxime Lebreton");
|
PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "Maxime Lebreton");
|
||||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||||
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
|
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(DesiredSubscriptionDuration.THREE.getValue(), desiredStartDate);
|
||||||
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
@@ -34,7 +34,7 @@ public class SubscriptionConverterTest {
|
|||||||
|
|
||||||
// Then
|
// Then
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(subscriptionInfo.subscriptionDurationDesired(), result.getSubscriptionDurationDesired());
|
assertEquals(subscriptionInfo.desiredSubscriptionDuration(), result.getDesiredSubscriptionDuration());
|
||||||
assertEquals(paymentMethodInfo.paymentType(), result.getPaymentMethod().getPaymentType());
|
assertEquals(paymentMethodInfo.paymentType(), result.getPaymentMethod().getPaymentType());
|
||||||
assertEquals(paymentMethodInfo.details(), result.getPaymentMethod().getDetails());
|
assertEquals(paymentMethodInfo.details(), result.getPaymentMethod().getDetails());
|
||||||
assertEquals(subscriptionInfo.desiredStartDate(), result.getDesiredStartDate());
|
assertEquals(subscriptionInfo.desiredStartDate(), result.getDesiredStartDate());
|
||||||
@@ -60,7 +60,7 @@ public class SubscriptionConverterTest {
|
|||||||
Subscription subscription = Subscription.builder()
|
Subscription subscription = Subscription.builder()
|
||||||
.subscriptionId(UUID.randomUUID())
|
.subscriptionId(UUID.randomUUID())
|
||||||
.customerId(UUID.randomUUID())
|
.customerId(UUID.randomUUID())
|
||||||
.subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue())
|
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||||
.paymentMethod(paymentMethod)
|
.paymentMethod(paymentMethod)
|
||||||
.desiredStartDate(desiredStartDate)
|
.desiredStartDate(desiredStartDate)
|
||||||
.startDate(desiredStartDate)
|
.startDate(desiredStartDate)
|
||||||
@@ -73,7 +73,7 @@ public class SubscriptionConverterTest {
|
|||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(subscription.getSubscriptionId(), result.getSubscriptionId());
|
assertEquals(subscription.getSubscriptionId(), result.getSubscriptionId());
|
||||||
assertEquals(subscription.getCustomerId(), result.getCustomerId());
|
assertEquals(subscription.getCustomerId(), result.getCustomerId());
|
||||||
assertEquals(subscription.getSubscriptionDurationDesired(), result.getSubscriptionDurationDesired());
|
assertEquals(subscription.getDesiredSubscriptionDuration(), result.getDesiredSubscriptionDuration());
|
||||||
assertEquals(subscription.getPaymentMethod().getPaymentType(), result.getPaymentMethod().getPaymentType());
|
assertEquals(subscription.getPaymentMethod().getPaymentType(), result.getPaymentMethod().getPaymentType());
|
||||||
assertEquals(subscription.getPaymentMethod().getDetails(), result.getPaymentMethod().getDetails());
|
assertEquals(subscription.getPaymentMethod().getDetails(), result.getPaymentMethod().getDetails());
|
||||||
assertEquals(subscription.getDesiredStartDate(), result.getDesiredStartDate());
|
assertEquals(subscription.getDesiredStartDate(), result.getDesiredStartDate());
|
||||||
@@ -95,7 +95,7 @@ public class SubscriptionConverterTest {
|
|||||||
Subscription subscription = Subscription.builder()
|
Subscription subscription = Subscription.builder()
|
||||||
.subscriptionId(UUID.randomUUID())
|
.subscriptionId(UUID.randomUUID())
|
||||||
.customerId(UUID.randomUUID())
|
.customerId(UUID.randomUUID())
|
||||||
.subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue())
|
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||||
.paymentMethod(paymentMethod)
|
.paymentMethod(paymentMethod)
|
||||||
.desiredStartDate(desiredStartDate)
|
.desiredStartDate(desiredStartDate)
|
||||||
.startDate(desiredStartDate)
|
.startDate(desiredStartDate)
|
||||||
@@ -115,7 +115,7 @@ public class SubscriptionConverterTest {
|
|||||||
void shouldPreserveEmptyStrings() {
|
void shouldPreserveEmptyStrings() {
|
||||||
PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "");
|
PaymentMethodInfo paymentMethodInfo = new PaymentMethodInfo(PaymentType.CB.name(), "");
|
||||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||||
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
|
SubscriptionInfo subscriptionInfo = new SubscriptionInfo(DesiredSubscriptionDuration.THREE.getValue(), desiredStartDate);
|
||||||
|
|
||||||
Subscription domainResult = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo);
|
Subscription domainResult = SubscriptionConverter.toDomain(subscriptionInfo, paymentMethodInfo);
|
||||||
SubscriptionDTO dtoResult = SubscriptionConverter.toDTO(domainResult);
|
SubscriptionDTO dtoResult = SubscriptionConverter.toDTO(domainResult);
|
||||||
|
|||||||
+7
-7
@@ -1,7 +1,7 @@
|
|||||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
|
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.PaymentType;
|
||||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDurationDesired;
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.DesiredSubscriptionDuration;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ public class SubscriptionTest {
|
|||||||
void testSubscriptionBuilder() {
|
void testSubscriptionBuilder() {
|
||||||
UUID subscriptionId = UUID.randomUUID();
|
UUID subscriptionId = UUID.randomUUID();
|
||||||
UUID customerId = UUID.randomUUID();
|
UUID customerId = UUID.randomUUID();
|
||||||
Integer subscriptionDurationDesired = SubscriptionDurationDesired.THREE.getValue();
|
Integer desiredSubscriptionDuration = DesiredSubscriptionDuration.THREE.getValue();
|
||||||
String paymentType = PaymentType.CB.name();
|
String paymentType = PaymentType.CB.name();
|
||||||
Object details = new Object();
|
Object details = new Object();
|
||||||
details = "Maxime Lebreton";
|
details = "Maxime Lebreton";
|
||||||
@@ -34,7 +34,7 @@ public class SubscriptionTest {
|
|||||||
Subscription subscription = Subscription.builder()
|
Subscription subscription = Subscription.builder()
|
||||||
.subscriptionId(subscriptionId)
|
.subscriptionId(subscriptionId)
|
||||||
.customerId(customerId)
|
.customerId(customerId)
|
||||||
.subscriptionDurationDesired(subscriptionDurationDesired)
|
.desiredSubscriptionDuration(desiredSubscriptionDuration)
|
||||||
.paymentMethod(paymentMethod)
|
.paymentMethod(paymentMethod)
|
||||||
.desiredStartDate(desiredStartDate)
|
.desiredStartDate(desiredStartDate)
|
||||||
.startDate(startDate)
|
.startDate(startDate)
|
||||||
@@ -44,7 +44,7 @@ public class SubscriptionTest {
|
|||||||
|
|
||||||
assertEquals(subscriptionId, subscription.getSubscriptionId());
|
assertEquals(subscriptionId, subscription.getSubscriptionId());
|
||||||
assertEquals(customerId, subscription.getCustomerId());
|
assertEquals(customerId, subscription.getCustomerId());
|
||||||
assertEquals(subscriptionDurationDesired, subscription.getSubscriptionDurationDesired());
|
assertEquals(desiredSubscriptionDuration, subscription.getDesiredSubscriptionDuration());
|
||||||
assertEquals(paymentType, paymentMethod.getPaymentType());
|
assertEquals(paymentType, paymentMethod.getPaymentType());
|
||||||
assertEquals(details, paymentMethod.getDetails());
|
assertEquals(details, paymentMethod.getDetails());
|
||||||
assertEquals(desiredStartDate, subscription.getDesiredStartDate());
|
assertEquals(desiredStartDate, subscription.getDesiredStartDate());
|
||||||
@@ -92,12 +92,12 @@ public class SubscriptionTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("setEndDate should instantiate EndDate as the same value as the variable desiredStartDate + subscriptionDurationDesired")
|
@DisplayName("setEndDate should instantiate EndDate as the same value as the variable desiredStartDate + desiredSubscriptionDuration")
|
||||||
void testSetEndDate() {
|
void testSetEndDate() {
|
||||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||||
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
||||||
Subscription subscription = Subscription.builder()
|
Subscription subscription = Subscription.builder()
|
||||||
.subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue())
|
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||||
.desiredStartDate(desiredStartDate)
|
.desiredStartDate(desiredStartDate)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ public class SubscriptionTest {
|
|||||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||||
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
LocalDate estimateEndDate = LocalDate.of(2026, 9, 24);
|
||||||
Subscription subscription = Subscription.builder()
|
Subscription subscription = Subscription.builder()
|
||||||
.subscriptionDurationDesired(SubscriptionDurationDesired.THREE.getValue())
|
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||||
.desiredStartDate(desiredStartDate)
|
.desiredStartDate(desiredStartDate)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -23,7 +23,7 @@ public class NotValidSubscriptionExceptionTest {
|
|||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ValueSource(strings = {
|
@ValueSource(strings = {
|
||||||
"subscriptionDurationDesired will be 3, 6 or 12 by the ENUM file SubscriptionDurationDesired",
|
"desiredSubscriptionDuration will be 3, 6 or 12 by the ENUM file SubscriptionDurationDesired",
|
||||||
"desiredStartDate cannot be before today"
|
"desiredStartDate cannot be before today"
|
||||||
})
|
})
|
||||||
@DisplayName("Exception should handle different validation messages")
|
@DisplayName("Exception should handle different validation messages")
|
||||||
|
|||||||
+74
@@ -0,0 +1,74 @@
|
|||||||
|
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
@@ -0,0 +1,300 @@
|
|||||||
|
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
@@ -0,0 +1,273 @@
|
|||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
-8
@@ -1,6 +1,6 @@
|
|||||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
|
||||||
|
|
||||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDurationDesired;
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.DesiredSubscriptionDuration;
|
||||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
@@ -21,7 +21,7 @@ public class SubscriptionValidatorTest {
|
|||||||
@DisplayName("Should validate subscription with valid data")
|
@DisplayName("Should validate subscription with valid data")
|
||||||
void testValidateValidSubscription() {
|
void testValidateValidSubscription() {
|
||||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||||
SubscriptionInfo validSubscription = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
|
SubscriptionInfo validSubscription = new SubscriptionInfo(DesiredSubscriptionDuration.THREE.getValue(), desiredStartDate);
|
||||||
|
|
||||||
assertDoesNotThrow(() -> SubscriptionValidator.validate(validSubscription));
|
assertDoesNotThrow(() -> SubscriptionValidator.validate(validSubscription));
|
||||||
}
|
}
|
||||||
@@ -31,9 +31,9 @@ public class SubscriptionValidatorTest {
|
|||||||
class SubscriptionDurationDesiredValidationTests {
|
class SubscriptionDurationDesiredValidationTests {
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@EnumSource(value = SubscriptionDurationDesired.class)
|
@EnumSource(value = DesiredSubscriptionDuration.class)
|
||||||
void testValidateSubscriptionDurationDesired(SubscriptionDurationDesired subscriptionDurationDesired) {
|
void testValidateSubscriptionDurationDesired(DesiredSubscriptionDuration desiredSubscriptionDuration) {
|
||||||
Integer validSubscriptionDurationDesired = subscriptionDurationDesired.getValue();
|
Integer validSubscriptionDurationDesired = desiredSubscriptionDuration.getValue();
|
||||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||||
SubscriptionInfo validSubscription = new SubscriptionInfo(validSubscriptionDurationDesired, desiredStartDate);
|
SubscriptionInfo validSubscription = new SubscriptionInfo(validSubscriptionDurationDesired, desiredStartDate);
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ public class SubscriptionValidatorTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Should throw exception when subscriptionDurationDesired is not 3, 6 or 12")
|
@DisplayName("Should throw exception when desiredSubscriptionDuration is not 3, 6 or 12")
|
||||||
void testNotValidateSubscriptionDurationDesired() {
|
void testNotValidateSubscriptionDurationDesired() {
|
||||||
Integer notValidSubscriptionDurationDesired = 1 ;
|
Integer notValidSubscriptionDurationDesired = 1 ;
|
||||||
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||||
@@ -63,7 +63,7 @@ public class SubscriptionValidatorTest {
|
|||||||
@DisplayName("Should validate when desiredStartDate is before today")
|
@DisplayName("Should validate when desiredStartDate is before today")
|
||||||
void testValidateDesiredStartDate() {
|
void testValidateDesiredStartDate() {
|
||||||
LocalDate desiredStartDate = LocalDate.now();
|
LocalDate desiredStartDate = LocalDate.now();
|
||||||
SubscriptionInfo validSubscription = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
|
SubscriptionInfo validSubscription = new SubscriptionInfo(DesiredSubscriptionDuration.THREE.getValue(), desiredStartDate);
|
||||||
|
|
||||||
assertDoesNotThrow(() -> SubscriptionValidator.validate(validSubscription));
|
assertDoesNotThrow(() -> SubscriptionValidator.validate(validSubscription));
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ public class SubscriptionValidatorTest {
|
|||||||
@DisplayName("Should throw exception when desiredStartDate is before today")
|
@DisplayName("Should throw exception when desiredStartDate is before today")
|
||||||
void testNotValidateDesiredStartDate() {
|
void testNotValidateDesiredStartDate() {
|
||||||
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24);
|
LocalDate desiredStartDate = LocalDate.of(2026, 3, 24);
|
||||||
SubscriptionInfo validSubscription = new SubscriptionInfo(SubscriptionDurationDesired.THREE.getValue(), desiredStartDate);
|
SubscriptionInfo validSubscription = new SubscriptionInfo(DesiredSubscriptionDuration.THREE.getValue(), desiredStartDate);
|
||||||
|
|
||||||
NotValidSubscriptionException exception = assertThrows(
|
NotValidSubscriptionException exception = assertThrows(
|
||||||
NotValidSubscriptionException.class,
|
NotValidSubscriptionException.class,
|
||||||
|
|||||||
Reference in New Issue
Block a user