Unit test for Adresse is done, unit test for clients is WIP 80%

This commit is contained in:
Olivier PARPAILLON
2025-07-10 16:58:34 +02:00
parent 5ef3f25372
commit 9fbfb957da
8 changed files with 443 additions and 13 deletions

View File

@@ -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);
}

View File

@@ -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"));
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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);
}