debut des test sur la bd

This commit is contained in:
2026-06-13 19:31:13 +02:00
parent d985e2fa8f
commit 98d41ea409
2 changed files with 220 additions and 1 deletions
@@ -0,0 +1,220 @@
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 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.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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
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.setRandomUUID();
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("1", 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(commandeN1);
List<Commande> commande = repository.findAll();
assertEquals(2, commande.size());
assertTrue(commande.contains(commandeN1));
assertTrue(commande.contains(commandeN1));
}
}
@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.getCommandeId());
assertEquals(commandeN1.getClientId(), foundCommande.getClientId());
}
}
@Nested
@DisplayName("Delete operations")
class DeleteOperations {
}
}
@@ -4,7 +4,6 @@ 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.ComandeInfo;
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.LigneCommande;
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;