Compare commits

..

6 Commits

20 changed files with 652 additions and 0 deletions
+12
View File
@@ -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,6 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
import java.util.List;
public record ComandeInfo(List<LigneCommandeInfo> listeLigne ,String modePayement) {
}
@@ -0,0 +1,19 @@
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;
}
@@ -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,34 @@
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.ComandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
public final class CommandeConverter {
public static Commande toDomain(ComandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
return Commande.builder()
.lignesCommande(commandeInfo.listeLigne())
.rue(adressInfo.rue())
.ville(adressInfo.ville())
.codePostal(adressInfo.codePostal())
.pays(adressInfo.pays())
.modePaiement(modePaiement)
.build();
}
public static CommandeDTO toDTO(ComandeInfo 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,26 @@
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;
public void setRandomUUID() {
this.clientId = UUID.randomUUID();
}
}
@@ -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,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,66 @@
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 java.util.UUID;
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);
ComandeInfo commandeInfo = new ComandeInfo(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);
ComandeInfo commandeInfo = new ComandeInfo(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,65 @@
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.customer.entity.Customer;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
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" ;
Commande commande = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande)
.rue(rue)
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.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());
}
@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());
}
}
@@ -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,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());
}
}
}