Unit test for Adresse is done, unit test for clients is WIP 80%
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Client> findById(Long clientId);
|
||||
Client findById(Long clientId);
|
||||
List<Client> findByName(String name);
|
||||
void fullUpdate(Long id, Client client, Adresse adresseDetails);
|
||||
void updateLocation(Long idClient, Adresse adresseDetails);
|
||||
}
|
||||
|
||||
@@ -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<Client> 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<Client> findByName(String name) {
|
||||
if (name.isBlank()) {
|
||||
throw new IllegalArgumentException("name is null");
|
||||
}
|
||||
List<Client> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Client, Long> {
|
||||
List<Client> findByPrenomIgnoreCaseContainingOrNomIgnoreCaseContaining(String prenom, String nom);
|
||||
}
|
||||
|
||||
@@ -130,14 +130,14 @@ class DemoApplicationTests {
|
||||
@DisplayName("-- Test add location game to a client --")
|
||||
void testAddLocationGame() {
|
||||
// Find a client by his ID
|
||||
Optional<Client> client = clientServiceImpl.findById(1L);
|
||||
Client client = clientServiceImpl.findById(1L);
|
||||
// Find a Game by his ID
|
||||
Optional<Stock> 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);
|
||||
|
||||
114
src/test/java/fr/eni/demo/bll/AdresseServiceTest.java
Normal file
114
src/test/java/fr/eni/demo/bll/AdresseServiceTest.java
Normal file
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
229
src/test/java/fr/eni/demo/bll/ClientServiceTest.java
Normal file
229
src/test/java/fr/eni/demo/bll/ClientServiceTest.java
Normal file
@@ -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<Client> 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user