Compare commits

...

18 Commits

Author SHA1 Message Date
Maxime LEBRETON 73e066b593 refactorr d'un nom de fichier 2026-06-14 17:33:46 +02:00
Maxime LEBRETON 7e8a5522d8 Implementation des méthode de delete ainsi que la modification des test pour qu'il fonctionne 2026-06-14 13:26:07 +02:00
Maxime LEBRETON 69ce78a113 test delete 2026-06-14 13:15:04 +02:00
Maxime LEBRETON 0ad7e71030 implementation du code 2026-06-14 12:45:05 +02:00
Maxime LEBRETON 2ed99a901a suite test 2026-06-14 03:02:52 +02:00
Maxime LEBRETON 98d41ea409 debut des test sur la bd 2026-06-13 19:31:13 +02:00
Maxime LEBRETON d985e2fa8f methode point de fidelité convert 2026-06-13 14:23:58 +02:00
Maxime LEBRETON 2161a0cf01 Test point de fidelité convert 2026-06-13 14:23:45 +02:00
Maxime LEBRETON 4552585ef9 ajout commandID 2026-06-13 14:10:37 +02:00
Maxime LEBRETON ce27a686ef ajout test sur commandID 2026-06-13 13:54:40 +02:00
Maxime LEBRETON 2c14ee5e0b rajoute du Mode de payement a tester oublie 2026-06-11 21:25:29 +02:00
Maxime LEBRETON 213f7f64db test sur les exception 2026-06-10 19:44:55 +02:00
Maxime LEBRETON 8d33d16cb8 création des test sur les validator 2026-06-08 17:26:35 +02:00
Maxime LEBRETON 82164689cf création des test sur les UUID 2026-05-25 18:46:21 +02:00
Maxime LEBRETON 67b5b83943 création des test sur toDTO 2026-05-25 18:45:14 +02:00
Maxime LEBRETON e125a420a6 création des test sur les entité 2026-05-23 20:18:35 +02:00
Maxime LEBRETON 3bf4fcad07 premier test valider 2026-05-20 00:11:28 +02:00
Maxime LEBRETON 6e94316e4b création premier test 2026-05-19 16:49:05 +02:00
26 changed files with 1358 additions and 0 deletions
+12
View File
@@ -19,6 +19,12 @@
}
},
"PasserCommande": {
"input": {
"clientId": "uuid",
@@ -40,6 +46,12 @@
"pointsFideliteGagnes": "integer"
}
},
"GererAvis": {
"input": {
"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,25 @@
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,
}
@@ -0,0 +1,33 @@
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(modePaiement)
.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();
}
}
@@ -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();
}
}
@@ -0,0 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
public class NotValidAdressException extends RuntimeException {
public NotValidAdressException(String message) {
super(message);
}
}
@@ -0,0 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
public class NotValidCommandeException extends RuntimeException {
public NotValidCommandeException(String message) {
super(message);
}
}
@@ -0,0 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
public class NotValidLigneCommandeException extends RuntimeException {
public NotValidLigneCommandeException(String message) {
super(message);
}
}
@@ -0,0 +1,49 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public class ComandeRepository {
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));
}
}
@@ -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);
}
}
}
@@ -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());
}
}
@@ -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()));
}
}
@@ -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());
}
}
@@ -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());
}
}
}
@@ -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());
}
}
}
@@ -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());
}
}
}
@@ -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 ComandeRepository repository;
private Commande commandeN1;
private Commande commandeN2;
private Commande commandeN3;
@BeforeEach
void setUp() {
repository = new ComandeRepository();
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());
}
}
}
@@ -0,0 +1,74 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.ComandeRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.usecase.CustomerUseCase;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class CommandeUseCase {
@Mock
private ComandeRepository comandeRepository;
@InjectMocks
private CommandeUseCase commandeUseCase;
private UUID commandeId;
private UUID clientId;
private Commande testCommande;
private CommandeInfo validCommandeInfo;
private LigneCommandeInfo validLigneCommandeInfo;
private AdresseInfo validAdresseInfo;
@BeforeEach
void setUp() {
clientId = UUID.randomUUID();
commandeId = UUID.randomUUID();
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
LigneCommandeInfo commande1 = new LigneCommandeInfo(12);
LigneCommandeInfo commande2 = new LigneCommandeInfo(13);
lignesCommande.add(commande1);
lignesCommande.add(commande2);
String rue = "rue du chien";
String ville = "LKa Rochette";
String codePostal = "7700";
String pays = "France";
String modePayement = ModePaiement.CB.name();
testCommande = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande)
.rue(rue)
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.modePaiement(modePayement)
.commandeId(commandeId)
.build();
validCommandeInfo = new CommandeInfo(lignesCommande,ModePaiement.CB.name());
validLigneCommandeInfo= new LigneCommandeInfo(12);
validAdresseInfo = new AdresseInfo("rue du chien","LKa Rochette","7700","France");
}
@Nested
}
@@ -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());
}
}
}