Implementation des méthode de delete ainsi que la modification des test pour qu'il fonctionne

This commit is contained in:
2026-06-14 13:26:07 +02:00
committed by Marvin AUBERT
parent 0d2a88f4a0
commit 8fc037e4b1
2 changed files with 113 additions and 66 deletions
@@ -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));
}
} }
@@ -67,7 +67,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<>();
@@ -229,79 +228,117 @@ 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);
}
@BeforeEach
@Test void setUpCustomers() {
@DisplayName("Delete should remove the specified customer") repository.save(commandeN1);
void testDelete() { repository.save(commandeN2);
repository.delete(commandeN1); repository.save(commandeN3);
}
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() @Test
.lignesCommande(lignesCommande) @DisplayName("Delete should remove the specified customer")
.rue("non") void testDelete() {
.ville("non") repository.delete(commandeN1);
.codePostal("Existent")
.pays("0000")
.modePaiement(ModePaiement.PAYPAL.name())
.build();
nonExistentCommande.setRandomUUIDCommande();
assertDoesNotThrow(() -> repository.delete(nonExistentCommande)); List<Commande> commande = repository.findAll();
assertEquals(3, repository.findAll().size()); 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);
@Test Commande nonExistentCommande = Commande.builder()
void TestDeleteAllCommandeByCustomerID(){ .lignesCommande(lignesCommande)
repository.deleteById(commandeN1.getClientId()); .rue("non")
.ville("non")
.codePostal("Existent")
.pays("0000")
.modePaiement(ModePaiement.PAYPAL.name())
.build();
nonExistentCommande.setRandomUUIDCommande();
assertDoesNotThrow(() -> repository.delete(nonExistentCommande));
assertEquals(3, repository.findAll().size());
}
List<Commande> commande = repository.findAll(); @Test
void TestDeleteAllCommandeByCustomerID() {
System.out.println(commandeN1.getClientId());
System.out.println(commandeN2.getClientId());
System.out.println(commandeN3.getClientId());
repository.deleteById(commandeN1.getClientId());
assertEquals(1, commande.size());
assertFalse(commande.contains(commandeN1)); List<Commande> commande = repository.findAll();
assertFalse(commande.contains(commandeN2));
assertTrue(commande.contains(commandeN3)); 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());
}
} }
@@ -309,9 +346,6 @@ public class CommandeRepositoryTest {
} }