création des test sur les validator

This commit is contained in:
2026-06-06 23:38:56 +02:00
committed by Marvin AUBERT
parent 76b60297c1
commit aa40ad0e3b
6 changed files with 281 additions and 0 deletions
+6
View File
@@ -19,6 +19,12 @@
}
},
"PasserCommande": {
"input": {
"clientId": "uuid",
@@ -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,88 @@
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.ComandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
import java.util.List;
import java.util.Objects;
public final class CommandeValidator {
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(ComandeInfo comandeInfo) {
validateModePaiement(comandeInfo);
validateListLigneCommande(comandeInfo);
}
public static void validate(AdresseInfo adresseInfo) {
validateStreet(adresseInfo);
validateCity(adresseInfo);
validateCodePostal(adresseInfo);
validateCountry(adresseInfo);
}
private static void validateModePaiement(ComandeInfo comandeInfo) {
if(comandeInfo.modePayement()!="CB"&& comandeInfo.modePayement()!="PAYPAL"&&comandeInfo.modePayement()!="POINTS_FIDELITE"){
throw new NotValidCommandeException(MODE_PAIEMENT_IS_NOT_VALIDE);
}
}
private static void validateListLigneCommande(ComandeInfo comandeInfo) {
if (comandeInfo.listeLigne() == null
|| comandeInfo.listeLigne().isEmpty()
|| comandeInfo.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,166 @@
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.ComandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.LigneCommande;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
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();
ComandeInfo commandeValid = new ComandeInfo(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;
ComandeInfo commandeValid = new ComandeInfo(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();
ComandeInfo commandeValid = new ComandeInfo(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());
}
}
}