Compare commits

...

3 Commits

11 changed files with 233 additions and 44 deletions
@@ -1,6 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande;
import java.util.List;
public record ComandeInfo(List<LigneCommandeInfo> listeLigne ,String modePayement) {
}
@@ -16,4 +16,10 @@ public class CommandeDTO {
private String codePostal; private String codePostal;
private String pays; private String pays;
private String modePaiement; 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) {
}
@@ -1,15 +1,14 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter; 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.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ComandeInfo; 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.CommandeDTO;
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.entity.Commande;
public final class CommandeConverter { public final class CommandeConverter {
public static Commande toDomain(ComandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) { public static Commande toDomain(CommandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
return Commande.builder() return Commande.builder()
.lignesCommande(commandeInfo.listeLigne()) .lignesCommande(commandeInfo.listeLigne())
.rue(adressInfo.rue()) .rue(adressInfo.rue())
@@ -20,7 +19,7 @@ public final class CommandeConverter {
.build(); .build();
} }
public static CommandeDTO toDTO(ComandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) { public static CommandeDTO toDTO(CommandeInfo commandeInfo, AdresseInfo adressInfo, String modePaiement) {
return CommandeDTO.builder() return CommandeDTO.builder()
.lignesCommande(commandeInfo.listeLigne()) .lignesCommande(commandeInfo.listeLigne())
.rue(adressInfo.rue()) .rue(adressInfo.rue())
@@ -15,6 +15,11 @@ public class ComandeRepository {
public List<Commande> findAll(){return commande;} public List<Commande> findAll(){return commande;}
public void deleteAll() {
commande.clear();
}
public Commande save(Commande newCommande){ public Commande save(Commande newCommande){
Optional<Commande> optionalCommandeWithSameIsbn = this.findById(newCommande.getCommandeId()); Optional<Commande> optionalCommandeWithSameIsbn = this.findById(newCommande.getCommandeId());
optionalCommandeWithSameIsbn.ifPresent(commande::remove); optionalCommandeWithSameIsbn.ifPresent(commande::remove);
@@ -33,4 +38,12 @@ public class ComandeRepository {
.anyMatch(customer -> customer.getCommandeId().equals(uuid)); .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));
}
} }
@@ -1,13 +1,12 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator; 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.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ComandeInfo; 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.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException; 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.NotValidCommandeException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException; import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public final class CommandeValidator { public final class CommandeValidator {
@@ -28,10 +27,10 @@ public final class CommandeValidator {
} }
} }
public static void validate(ComandeInfo comandeInfo) { public static void validate(CommandeInfo commandeInfo) {
validateModePaiement(comandeInfo); validateModePaiement(commandeInfo);
validateListLigneCommande(comandeInfo); validateListLigneCommande(commandeInfo);
} }
public static void validate(AdresseInfo adresseInfo) { public static void validate(AdresseInfo adresseInfo) {
@@ -43,16 +42,16 @@ public final class CommandeValidator {
private static void validateModePaiement(ComandeInfo comandeInfo) { private static void validateModePaiement(CommandeInfo commandeInfo) {
if(comandeInfo.modePayement()!="CB"&& comandeInfo.modePayement()!="PAYPAL"&&comandeInfo.modePayement()!="POINTS_FIDELITE"){ if(commandeInfo.modePayement()!="CB"&& commandeInfo.modePayement()!="PAYPAL"&& commandeInfo.modePayement()!="POINTS_FIDELITE"){
throw new NotValidCommandeException(MODE_PAIEMENT_IS_NOT_VALIDE); throw new NotValidCommandeException(MODE_PAIEMENT_IS_NOT_VALIDE);
} }
} }
private static void validateListLigneCommande(ComandeInfo comandeInfo) { private static void validateListLigneCommande(CommandeInfo commandeInfo) {
if (comandeInfo.listeLigne() == null if (commandeInfo.listeLigne() == null
|| comandeInfo.listeLigne().isEmpty() || commandeInfo.listeLigne().isEmpty()
|| comandeInfo.listeLigne().stream().anyMatch(Objects::isNull)){ || commandeInfo.listeLigne().stream().anyMatch(Objects::isNull)){
throw new NotValidCommandeException(LIST_LIGNE_COMANDE_IS_NOT_VALIDE); throw new NotValidCommandeException(LIST_LIGNE_COMANDE_IS_NOT_VALIDE);
} }
@@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -22,7 +21,7 @@ public class CommandeConverterTest {
LigneCommandeInfo ligne2 = new LigneCommandeInfo(14); LigneCommandeInfo ligne2 = new LigneCommandeInfo(14);
listLigne.add(ligne); listLigne.add(ligne);
listLigne.add(ligne2); listLigne.add(ligne2);
ComandeInfo commandeInfo = new ComandeInfo(listLigne,modePaiement); CommandeInfo commandeInfo = new CommandeInfo(listLigne,modePaiement);
AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France"); AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France");
@@ -46,7 +45,7 @@ public class CommandeConverterTest {
LigneCommandeInfo ligne2 = new LigneCommandeInfo(14); LigneCommandeInfo ligne2 = new LigneCommandeInfo(14);
listLigne.add(ligne); listLigne.add(ligne);
listLigne.add(ligne2); listLigne.add(ligne2);
ComandeInfo commandeInfo = new ComandeInfo(listLigne,modePaiement); CommandeInfo commandeInfo = new CommandeInfo(listLigne,modePaiement);
AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France"); AdresseInfo adressInfo = new AdresseInfo("rue du cheval","La Rochette","77000","France");
@@ -1,4 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
public class Test {
}
@@ -3,8 +3,6 @@ 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.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement; 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.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
@@ -15,8 +13,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CommandeRepositoryTest { public class CommandeRepositoryTest {
@@ -68,7 +65,6 @@ public class CommandeRepositoryTest {
.pays(pays) .pays(pays)
.modePaiement(modePayement) .modePaiement(modePayement)
.build(); .build();
commandeN2.setRandomUUID();
commandeN2.setRandomUUIDCommande(); commandeN2.setRandomUUIDCommande();
List<LigneCommandeInfo> lignesCommande3 = new ArrayList<>(); List<LigneCommandeInfo> lignesCommande3 = new ArrayList<>();
@@ -220,14 +216,8 @@ public class CommandeRepositoryTest {
UUID nonExistentId = UUID.randomUUID(); UUID nonExistentId = UUID.randomUUID();
boolean exists = repository.existsById(nonExistentId); boolean exists = repository.existsById(nonExistentId);
assertTrue(exists); assertFalse(exists);
} }
} }
@@ -236,6 +226,119 @@ public class CommandeRepositoryTest {
@Nested @Nested
@DisplayName("Delete operations") @DisplayName("Delete operations")
class DeleteOperations { 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
}
@@ -1,7 +1,7 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator; 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.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ComandeInfo; 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.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement; 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.NotValidAdressException;
@@ -47,7 +47,7 @@ public class CommandeValidatorTest {
List<LigneCommandeInfo> listeLigne = new ArrayList<>(); List<LigneCommandeInfo> listeLigne = new ArrayList<>();
listeLigne.add(ligneCommandeValide); listeLigne.add(ligneCommandeValide);
String modePaiementValide = modePaiement.toString(); String modePaiementValide = modePaiement.toString();
ComandeInfo commandeValid = new ComandeInfo(listeLigne, modePaiementValide); CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide);
assertDoesNotThrow(() -> CommandeValidator.validate(commandeValid)); assertDoesNotThrow(() -> CommandeValidator.validate(commandeValid));
} }
@@ -59,7 +59,7 @@ public class CommandeValidatorTest {
List<LigneCommandeInfo> listeLigne = new ArrayList<>(); List<LigneCommandeInfo> listeLigne = new ArrayList<>();
listeLigne.add(ligneCommandeValide); listeLigne.add(ligneCommandeValide);
String modePaiementValide = modePaiementError; String modePaiementValide = modePaiementError;
ComandeInfo commandeValid = new ComandeInfo(listeLigne, modePaiementValide); CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide);
NotValidCommandeException exception = assertThrows( NotValidCommandeException exception = assertThrows(
NotValidCommandeException.class, NotValidCommandeException.class,
@@ -72,7 +72,7 @@ public class CommandeValidatorTest {
@MethodSource("provideInvalidOrderLists") @MethodSource("provideInvalidOrderLists")
void testValidateNotValideListLigneCommande(List<LigneCommandeInfo> listeLigne) { void testValidateNotValideListLigneCommande(List<LigneCommandeInfo> listeLigne) {
String modePaiementValide = ModePaiement.CB.toString(); String modePaiementValide = ModePaiement.CB.toString();
ComandeInfo commandeValid = new ComandeInfo(listeLigne, modePaiementValide); CommandeInfo commandeValid = new CommandeInfo(listeLigne, modePaiementValide);
NotValidCommandeException exception = assertThrows( NotValidCommandeException exception = assertThrows(
NotValidCommandeException.class, NotValidCommandeException.class,
() -> CommandeValidator.validate(commandeValid) () -> CommandeValidator.validate(commandeValid)