From 9fbfb957da730ed972a7544a4e09c71870b1116e Mon Sep 17 00:00:00 2001 From: Olivier PARPAILLON Date: Thu, 10 Jul 2025 16:58:34 +0200 Subject: [PATCH] Unit test for Adresse is done, unit test for clients is WIP 80% --- .../java/fr/eni/demo/bll/AdresseService.java | 4 +- .../fr/eni/demo/bll/AdresseServiceImpl.java | 44 +++- .../java/fr/eni/demo/bll/ClientService.java | 8 +- .../fr/eni/demo/bll/ClientServiceImpl.java | 50 +++- .../fr/eni/demo/dal/ClientRepository.java | 3 + .../fr/eni/demo/DemoApplicationTests.java | 4 +- .../fr/eni/demo/bll/AdresseServiceTest.java | 114 +++++++++ .../fr/eni/demo/bll/ClientServiceTest.java | 229 ++++++++++++++++++ 8 files changed, 443 insertions(+), 13 deletions(-) create mode 100644 src/test/java/fr/eni/demo/bll/AdresseServiceTest.java create mode 100644 src/test/java/fr/eni/demo/bll/ClientServiceTest.java diff --git a/src/main/java/fr/eni/demo/bll/AdresseService.java b/src/main/java/fr/eni/demo/bll/AdresseService.java index 4b5d69b..313dfef 100644 --- a/src/main/java/fr/eni/demo/bll/AdresseService.java +++ b/src/main/java/fr/eni/demo/bll/AdresseService.java @@ -3,5 +3,7 @@ package fr.eni.demo.bll; import fr.eni.demo.bo.Adresse; public interface AdresseService { - void add(Adresse adresse); + Adresse add(Adresse adresse); + Adresse findById(Long id); + Adresse findAdresseByClientId(Long clientId); } diff --git a/src/main/java/fr/eni/demo/bll/AdresseServiceImpl.java b/src/main/java/fr/eni/demo/bll/AdresseServiceImpl.java index ee0b24e..f87d260 100644 --- a/src/main/java/fr/eni/demo/bll/AdresseServiceImpl.java +++ b/src/main/java/fr/eni/demo/bll/AdresseServiceImpl.java @@ -1,21 +1,55 @@ package fr.eni.demo.bll; import fr.eni.demo.bo.Adresse; +import fr.eni.demo.bo.Client; import fr.eni.demo.dal.AdresseRepository; +import jakarta.persistence.EntityNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class AdresseServiceImpl implements AdresseService{ - private AdresseRepository adresseRepository; + private AdresseRepository adresseRepo; + @Autowired + private ClientService clientService; - public AdresseServiceImpl(AdresseRepository adresseRepository) { - this.adresseRepository = adresseRepository; + public AdresseServiceImpl(AdresseRepository adresseRepo) { + this.adresseRepo = adresseRepo; } @Override - public void add(Adresse adresse) { - adresseRepository.save(adresse); + public Adresse add(Adresse adresse) { + if (adresse == null) { + throw new IllegalArgumentException("Adresse is null"); + } + if (adresse.getCodePostal() == null || adresse.getCodePostal().isBlank()) { + throw new IllegalArgumentException("Code postal is null or empty"); + } + if (adresse.getRue() == null || adresse.getRue().isBlank()) { + throw new IllegalArgumentException("Rue is null or empty"); + } + if (adresse.getVille() == null || adresse.getVille().isBlank()) { + throw new IllegalArgumentException("Ville is null or empty"); + } + adresseRepo.save(adresse); + return adresse; + } + + @Override + public Adresse findAdresseByClientId(Long clientId) { + if (clientId == null) { + throw new IllegalArgumentException("clientId is null"); + } + System.out.println(clientId); + Client client = clientService.findById(clientId); + System.out.println(client); + return client.getAdresse(); + } + + @Override + public Adresse findById(Long id) { + return adresseRepo.findById(id) + .orElseThrow(() -> new EntityNotFoundException("Adresse non trouvée")); } } diff --git a/src/main/java/fr/eni/demo/bll/ClientService.java b/src/main/java/fr/eni/demo/bll/ClientService.java index e446d6f..b5aed26 100644 --- a/src/main/java/fr/eni/demo/bll/ClientService.java +++ b/src/main/java/fr/eni/demo/bll/ClientService.java @@ -1,10 +1,14 @@ package fr.eni.demo.bll; +import fr.eni.demo.bo.Adresse; import fr.eni.demo.bo.Client; -import java.util.Optional; +import java.util.List; public interface ClientService { void add(Client client); - Optional findById(Long clientId); + Client findById(Long clientId); + List findByName(String name); + void fullUpdate(Long id, Client client, Adresse adresseDetails); + void updateLocation(Long idClient, Adresse adresseDetails); } diff --git a/src/main/java/fr/eni/demo/bll/ClientServiceImpl.java b/src/main/java/fr/eni/demo/bll/ClientServiceImpl.java index f1592ac..88d6300 100644 --- a/src/main/java/fr/eni/demo/bll/ClientServiceImpl.java +++ b/src/main/java/fr/eni/demo/bll/ClientServiceImpl.java @@ -1,14 +1,18 @@ package fr.eni.demo.bll; +import fr.eni.demo.bo.Adresse; import fr.eni.demo.bo.Client; +import fr.eni.demo.bo.Location; import fr.eni.demo.dal.ClientRepository; +import jakarta.persistence.EntityNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; import java.util.Optional; @Service -public class ClientServiceImpl implements ClientService { +public class ClientServiceImpl implements ClientService { private ClientRepository clientRepository; @@ -18,11 +22,51 @@ public class ClientServiceImpl implements ClientService { @Override public void add(Client client) { + if (client == null) { + throw new IllegalArgumentException("Client is null"); + } + if (client.getAdresse() == null) { + throw new IllegalArgumentException("Adresse mandatory"); + } clientRepository.save(client); } @Override - public Optional findById(Long clientId) { - return clientRepository.findById(clientId); + public Client findById(Long clientId) { + return clientRepository.findById(clientId) + .orElseThrow(() -> new EntityNotFoundException("Client not found")); + } + + @Override + public List findByName(String name) { + if (name.isBlank()) { + throw new IllegalArgumentException("name is null"); + } + List clients = clientRepository.findByPrenomIgnoreCaseContainingOrNomIgnoreCaseContaining(name, name); + if (clients.isEmpty()) { + throw new EntityNotFoundException("Aucun client trouvé pour : " + name); + } + return clients; + } + + public void fullUpdate(Long id, Client clientDetails, Adresse adresseDetails) { + Client client = clientRepository.findById(id) + .orElseThrow(() -> new EntityNotFoundException("Client non trouvé avec l'id " + id)); + + client.setPrenom(clientDetails.getPrenom()); + client.setNom(clientDetails.getNom()); + client.setEmail(clientDetails.getEmail()); + if (adresseDetails != null) { + client.setAdresse(adresseDetails); + } + clientRepository.save(client); + } + + public void updateLocation(Long idClient, Adresse adresseDetails) { + Client client = clientRepository.findById(idClient) + .orElseThrow(() -> new EntityNotFoundException("Client non trouvé avec l'id " + idClient)); + + client.setAdresse(adresseDetails); + clientRepository.save(client); } } diff --git a/src/main/java/fr/eni/demo/dal/ClientRepository.java b/src/main/java/fr/eni/demo/dal/ClientRepository.java index 3d6d708..dd641c1 100644 --- a/src/main/java/fr/eni/demo/dal/ClientRepository.java +++ b/src/main/java/fr/eni/demo/dal/ClientRepository.java @@ -4,6 +4,9 @@ import fr.eni.demo.bo.Client; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface ClientRepository extends JpaRepository { + List findByPrenomIgnoreCaseContainingOrNomIgnoreCaseContaining(String prenom, String nom); } diff --git a/src/test/java/fr/eni/demo/DemoApplicationTests.java b/src/test/java/fr/eni/demo/DemoApplicationTests.java index a1e52f4..ef5f7a5 100644 --- a/src/test/java/fr/eni/demo/DemoApplicationTests.java +++ b/src/test/java/fr/eni/demo/DemoApplicationTests.java @@ -130,14 +130,14 @@ class DemoApplicationTests { @DisplayName("-- Test add location game to a client --") void testAddLocationGame() { // Find a client by his ID - Optional client = clientServiceImpl.findById(1L); + Client client = clientServiceImpl.findById(1L); // Find a Game by his ID Optional game = stockServiceImpl.findById(1L); // Create the Location line for this client and the game Location gameLocation = new Location(); gameLocation.setStartDate(Date.valueOf(LocalDate.of(2025, 7, 8))); - gameLocation.setClient(client.get()); + gameLocation.setClient(client); gameLocation.setStock(game.get()); locationServiceImpl.add(gameLocation); System.out.println(gameLocation); diff --git a/src/test/java/fr/eni/demo/bll/AdresseServiceTest.java b/src/test/java/fr/eni/demo/bll/AdresseServiceTest.java new file mode 100644 index 0000000..2440b9e --- /dev/null +++ b/src/test/java/fr/eni/demo/bll/AdresseServiceTest.java @@ -0,0 +1,114 @@ +package fr.eni.demo.bll; + +import fr.eni.demo.bo.Adresse; +import fr.eni.demo.bo.Client; +import fr.eni.demo.dal.AdresseRepository; +import fr.eni.demo.dal.ClientRepository; +import jakarta.persistence.EntityNotFoundException; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.bean.override.mockito.MockitoBean; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@SpringBootTest +public class AdresseServiceTest { + @Autowired + AdresseService adresseService; + @MockitoBean + AdresseRepository adresseRepo; + @MockitoBean + ClientRepository clientRepo; + + @Test + @DisplayName("-- Test add Adresse with service : SUCCESS --") + public void addAdresseSuccess() { + Adresse adresse = new Adresse(); + adresse.setCodePostal("75000"); + adresse.setRue("Rue de la paix"); + adresse.setVille("Paris"); + + assertNotNull(adresse); + adresseService.add(adresse); + System.out.println(adresse); + } + + @Test + @DisplayName("-- Test add Adresse with service : FAILED --") + void testAddAdresseRepoFail() { + Adresse adresse = new Adresse(); + adresse.setCodePostal("44000"); + adresse.setRue("Rue d'Orvault"); + adresse.setVille(null); // ville manquante volontairement + + assertThrows((IllegalArgumentException.class), () -> { + adresseService.add(adresse); + }); + System.out.println(adresse); + } + + @Test + @DisplayName("-- Test findById with service : SUCCESS --") + void testFindByIdSuccess() { + Adresse adresse = new Adresse(); + adresse.setId(1); + adresse.setCodePostal("75000"); + adresse.setRue("Rue de la paix"); + adresse.setVille("Paris"); + when(adresseRepo.findById(1L)).thenReturn(Optional.of(adresse)); + + Adresse found = adresseService.findById(Long.valueOf(adresse.getId())); + + assertNotNull(found); + assertEquals(adresse.getVille(), found.getVille()); + System.out.println(found); + } + + @Test + @DisplayName("-- Test findById with service : FAILED --") + void testFindByIdFail() { + Long fakeId = 999L; + assertThrows(EntityNotFoundException.class, () -> { + adresseService.findById(fakeId); + }); + } + + @Test + @DisplayName("-- Test findAdresseByClientId with service : SUCCESS --") + void testFindAdresseByClientIdSuccess() { + Adresse adresse = new Adresse(); + adresse.setId(1); + adresse.setRue("10 rue Victor Hugo"); + adresse.setCodePostal("75000"); + adresse.setVille("Paris"); + + Client client = new Client(); + client.setId(1); + client.setNom("Dupont"); + client.setPrenom("Jean"); + client.setEmail("jean.dupont@test.fr"); + client.setAdresse(adresse); + + when(clientRepo.findById(1L)).thenReturn(Optional.of(client)); + + Adresse result = adresseService.findAdresseByClientId(Long.valueOf(client.getId())); + System.out.println(result); + assertNotNull(result); + assertEquals("Paris", result.getVille()); + } + + @Test + @DisplayName("-- Test findAdresseByClientId with service : FAILED --") + void testFindAdresseByClientIdFail() { + Long fakeId = 999L; + assertThrows(EntityNotFoundException.class, () -> { + adresseService.findAdresseByClientId(fakeId); + }); + } +} diff --git a/src/test/java/fr/eni/demo/bll/ClientServiceTest.java b/src/test/java/fr/eni/demo/bll/ClientServiceTest.java new file mode 100644 index 0000000..1bff3ef --- /dev/null +++ b/src/test/java/fr/eni/demo/bll/ClientServiceTest.java @@ -0,0 +1,229 @@ +package fr.eni.demo.bll; + +import fr.eni.demo.bo.Adresse; +import fr.eni.demo.bo.Client; +import fr.eni.demo.dal.AdresseRepository; +import fr.eni.demo.dal.ClientRepository; +import jakarta.persistence.EntityNotFoundException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.bean.override.mockito.MockitoBean; + +import java.util.List; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@SpringBootTest +public class ClientServiceTest { + @Autowired + ClientService clientService; + @MockitoBean + ClientRepository clientRepo; + @MockitoBean + AdresseRepository adresseRepo; + + @Test + @DisplayName("-- Test add Client and Adresse with service : SUCCESS --") + void testAddClientSuccess() { + Client client = new Client(); + client.setPrenom("Test"); + client.setNom("Test"); + client.setEmail("test@test.test"); + + Adresse adresse = new Adresse(); + adresse.setId(1); + adresse.setRue("Rue du test"); + adresse.setCodePostal("12345"); + adresse.setVille("Ville du test"); + client.setAdresse(adresse); + + assertNotNull(client); + assertNotNull(adresse); + clientService.add(client); + } + + @Test + @DisplayName("-- Test add Client without Adresse with service : FAILED --") + void testAddClientFailed() { + Client client = new Client(); + client.setPrenom("Test"); + client.setNom("Test"); + client.setEmail("test@test.test"); + + assertNotNull(client); + assertNull(client.getAdresse()); + assertThrows((IllegalArgumentException.class), () -> { + clientService.add(client); + }); + System.out.println(client); + } + + @Test + @DisplayName("-- Test findById Client with service : SUCCESS --") + void testFindByIdSuccess() { + Client client = new Client(); + client.setId(1); + client.setPrenom("Test"); + client.setNom("Test"); + client.setEmail("test@test.test"); + + Adresse adresse = new Adresse(); + adresse.setId(1); + adresse.setRue("Rue du test"); + adresse.setCodePostal("12345"); + adresse.setVille("Ville du test"); + client.setAdresse(adresse); + when(clientRepo.findById(1L)).thenReturn(Optional.of(client)); + + Client found = clientService.findById(1L); + + assertNotNull(found); + assertEquals(found.getPrenom(), client.getPrenom()); + System.out.println(found); + } + + @Test + @DisplayName("-- Test findById Client with service : FAILED --") + void testFindByIdFailed() { + Long fakeId = 55L; + assertThrows((EntityNotFoundException.class), () -> { + clientService.findById(fakeId); + }); + } + + @Test + @DisplayName("-- Test findByName Client with service : SUCCESS --") + void testFindByNameSuccess() { + Client client = new Client(); + client.setId(1); + client.setPrenom("Jean"); + client.setNom("Paul"); + client.setEmail("jean-paul@test.test"); + + Client client2 = new Client(); + client2.setId(2); + client2.setPrenom("Paul"); + client2.setNom("Jeanne"); + client2.setEmail("lilian-jeanne@test.test"); + + Adresse adresse = new Adresse(); + adresse.setId(1); + adresse.setRue("Rue du test"); + adresse.setCodePostal("12345"); + adresse.setVille("Ville du test"); + client.setAdresse(adresse); + client2.setAdresse(adresse); + + when(clientRepo.findByPrenomIgnoreCaseContainingOrNomIgnoreCaseContaining("Paul", "Paul")) + .thenReturn(List.of(client, client2)); + List founds = clientService.findByName(client.getNom()); + + assertNotNull(founds); + assertThat(founds).hasSize(2); + assertEquals(founds.get(0).getPrenom(), client.getPrenom()); + assertEquals(founds.get(1).getNom(), client2.getNom()); + System.out.println(founds); + } + + @Test + @DisplayName("-- Test findByName Client with service : FAILED --") + void testFindByNameFailed() { + String fakeName = "Olivier"; + assertThrows((EntityNotFoundException.class), () -> { + clientService.findByName(fakeName); + }); + } + + @Test + @DisplayName("-- Test update Client and Adresse with service : SUCCESS --") + void testUpdateSuccess() { + Client client = new Client(); + client.setId(1); + client.setPrenom("Test"); + client.setNom("Test"); + client.setEmail("test@test.test"); + Adresse adresse = new Adresse(); + adresse.setId(1); + adresse.setRue("Rue du test"); + adresse.setCodePostal("12345"); + adresse.setVille("Ville du test"); + client.setAdresse(adresse); + + Client newClient = new Client(); + newClient.setPrenom("Jean"); + newClient.setNom("Paul"); + newClient.setEmail("paul-jean@test.fr"); + Adresse newAdresse = new Adresse(); + newAdresse.setRue("Rue de la paix"); + newAdresse.setVille("Paris"); + newAdresse.setCodePostal("75000"); + newClient.setAdresse(newAdresse); + + when(clientRepo.findById(1L)).thenReturn(Optional.of(client)); + when(clientRepo.save(any(Client.class))).thenAnswer(invocation -> invocation.getArgument(0)); + + assertNotNull(client); + assertNotNull(client.getAdresse()); + assertNotNull(newClient); + assertNotNull(newClient.getAdresse()); + clientService.fullUpdate(1L, newClient, newAdresse); + verify(clientRepo).findById(1L); + verify(clientRepo).save(client); + System.out.println(newClient); + } + + @Test + @DisplayName("-- Test update Client and Adresse with service : FAILED --") + void testUpdateFailed() { + Long id = 99L; + Client client = new Client(); + client.setPrenom("Jean"); + Adresse adresse = new Adresse(); + + when(clientRepo.findById(id)).thenReturn(Optional.empty()); + assertThrows(Exception.class, () -> { + clientService.fullUpdate(id, client, adresse); + }); + + verify(clientRepo).findById(id); + verify(clientRepo, never()).save(any()); + } + + @Test + @DisplayName("-- Test update only Adresse of Client with service : SUCCESS --") + void testUpdateOnlyAdresseSuccess() { + Client client = new Client(); + client.setId(1); + client.setPrenom("Test"); + client.setNom("Test"); + client.setEmail("test@test.test"); + Adresse adresse = new Adresse(); + adresse.setId(1); + adresse.setRue("Rue du test"); + adresse.setCodePostal("12345"); + adresse.setVille("Ville du test"); + client.setAdresse(adresse); + + Adresse newAdresse = new Adresse(); + newAdresse.setRue("Rue de la paix"); + newAdresse.setVille("Paris"); + newAdresse.setCodePostal("75000"); + client.setAdresse(newAdresse); + + when(clientRepo.findById(1L)).thenReturn(Optional.of(client)); + when(clientRepo.save(any(Client.class))).thenAnswer(invocation -> invocation.getArgument(0)); + + assertNotNull(client); + assertNotNull(client.getAdresse()); + clientService.updateLocation(1L, newAdresse); + verify(clientRepo).findById(1L); + verify(clientRepo).save(client); + System.out.println(client); + } +}