Compare commits

...

2 Commits

Author SHA1 Message Date
Patrick FELIX-VIMALARATNAM cffad9475a test passé 2026-06-14 21:43:17 +02:00
Patrick FELIX-VIMALARATNAM b0c54a6096 création des test manquants 2026-06-14 21:26:14 +02:00
6 changed files with 210 additions and 17 deletions
@@ -16,10 +16,8 @@ public class CommandeDTO {
private String codePostal; private String codePostal;
private String pays; private String pays;
private String modePaiement; private String modePaiement;
private UUID commandeId ; private UUID commandeId ;
private double montantTotal ; private double montantTotal ;
private Integer pointsFideliteGagnes ; private Integer pointsFideliteGagnes ;
} }
@@ -49,6 +49,10 @@ public final class CommandeConverter {
.codePostal(commande.getCodePostal()) .codePostal(commande.getCodePostal())
.pays(commande.getPays()) .pays(commande.getPays())
.modePaiement(commande.getModePaiement()) .modePaiement(commande.getModePaiement())
.commandeId(commande.getCommandeId())
.clientId(commande.getClientId())
.montantTotal(commande.getMontantTotal())
.pointsFideliteGagnes(commande.getPointsFideliteGagnes())
.build(); .build();
} }
@@ -0,0 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
import java.text.MessageFormat;
import java.util.UUID;
public class CommandeNotFoundException extends RuntimeException {
public static final String THE_COMMANDE_WITH_ID_DOES_NOT_EXIST_MESSAGE = "The commande with id {0} does not exist";
public CommandeNotFoundException(UUID uuid) {
super(MessageFormat.format(THE_COMMANDE_WITH_ID_DOES_NOT_EXIST_MESSAGE, uuid));
}
}
@@ -1,13 +1,12 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase; package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.book.converter.BookConverter;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeDTO; import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.converter.CommandeConverter; import fr.iut_fbleau.but3.dev62.mylibrary.commande.converter.CommandeConverter;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande; import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.CommandeNotFoundException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.CommandeRepository; import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.CommandeRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.validator.CommandeValidator; import fr.iut_fbleau.but3.dev62.mylibrary.commande.validator.CommandeValidator;
@@ -38,4 +37,37 @@ public class CommandeUseCase {
return optionalCommande.map(CommandeConverter::toDTO); return optionalCommande.map(CommandeConverter::toDTO);
} }
public CommandeDTO updateCommande(UUID uuid, CommandeInfo commandeInfo, AdresseInfo adresseInfo)
throws CommandeNotFoundException, NotValidCommandeException {
CommandeValidator.validate(commandeInfo);
CommandeValidator.validate(adresseInfo);
Commande commandeByUUID = getCommandeIfDoesNotExistThrowCommandeNotFoundException(
uuid);
Commande commande = Commande.builder()
.clientId(commandeByUUID.getClientId())
.lignesCommande(commandeInfo.listeLigne())
.rue(adresseInfo.rue())
.ville(adresseInfo.ville())
.codePostal(adresseInfo.codePostal())
.pays(adresseInfo.pays())
.modePaiement(commandeInfo.modePayement())
.commandeId(uuid)
.build();
Commande updatedCommande = commandeRepository.save(commande);
return CommandeConverter.toDTO(updatedCommande);
}
public void deleteCommande(UUID uuid) throws CommandeNotFoundException {
Commande commandeToDelete = getCommandeIfDoesNotExistThrowCommandeNotFoundException(uuid);
this.commandeRepository.delete(commandeToDelete);
}
private Commande getCommandeIfDoesNotExistThrowCommandeNotFoundException(UUID uuid)
throws CommandeNotFoundException {
Optional<Commande> optionalCommandeById = commandeRepository.findById(uuid);
if (optionalCommandeById.isEmpty()) {
throw new CommandeNotFoundException(uuid);
}
return optionalCommandeById.get();
}
} }
@@ -0,0 +1,50 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.CommandeNotFoundException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CommandeNotFoundEsceptionTest {
@Test
@DisplayName("Exception message should contain the UUID provided")
void testExceptionMessageContainsUUID() {
UUID uuid = UUID.randomUUID();
CommandeNotFoundException exception = new CommandeNotFoundException(uuid);
String expectedMessage = String.format("The commande with id %s does not exist", uuid);
assertEquals(expectedMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should use the correct constant message format")
void testExceptionUsesConstantMessageFormat() {
UUID uuid = UUID.randomUUID();
CommandeNotFoundException exception = new CommandeNotFoundException(uuid);
String expectedFormatWithPlaceholder = "The commande with id {0} does not exist";
assertEquals(CommandeNotFoundException.THE_COMMANDE_WITH_ID_DOES_NOT_EXIST_MESSAGE,
expectedFormatWithPlaceholder);
assertTrue(exception.getMessage().contains(uuid.toString()));
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
UUID uuid = UUID.randomUUID();
try {
throw new CommandeNotFoundException(uuid);
} catch (CommandeNotFoundException e) {
String expectedMessage = String.format("The commande with id %s does not exist", uuid);
assertEquals(expectedMessage, e.getMessage());
}
}
}
@@ -1,9 +1,8 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase; package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.*; import fr.iut_fbleau.but3.dev62.mylibrary.commande.*;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande; import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.CommandeNotFoundException;
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.repository.CommandeRepository; import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.CommandeRepository;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@@ -42,22 +41,30 @@ public class TestCommandeUseCase {
private CommandeInfo validCommandeInfo; private CommandeInfo validCommandeInfo;
private LigneCommandeInfo validLigneCommandeInfo; private LigneCommandeInfo validLigneCommandeInfo;
private AdresseInfo validAdresseInfo; private AdresseInfo validAdresseInfo;
private List<LigneCommandeInfo> lignesCommande;
private LigneCommandeInfo commande1;
private LigneCommandeInfo commande2;
private String rue;
private String ville;
private String codePostal;
private String pays;
private String modePayement;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
clientId = UUID.randomUUID(); clientId = UUID.randomUUID();
commandeId = UUID.randomUUID(); commandeId = UUID.randomUUID();
List<LigneCommandeInfo> lignesCommande = new ArrayList<>(); lignesCommande = new ArrayList<>();
LigneCommandeInfo commande1 = new LigneCommandeInfo(12); commande1 = new LigneCommandeInfo(12);
LigneCommandeInfo commande2 = new LigneCommandeInfo(13); commande2 = new LigneCommandeInfo(13);
lignesCommande.add(commande1); lignesCommande.add(commande1);
lignesCommande.add(commande2); lignesCommande.add(commande2);
String rue = "rue du chien"; rue = "rue du chien";
String ville = "LKa Rochette"; ville = "LKa Rochette";
String codePostal = "7700"; codePostal = "7700";
String pays = "France"; pays = "France";
String modePayement = ModePaiement.CB.name(); modePayement = ModePaiement.CB.name();
testCommande = Commande.builder() testCommande = Commande.builder()
.clientId(clientId) .clientId(clientId)
@@ -121,7 +128,6 @@ public class TestCommandeUseCase {
verify(commandeRepository, times(1)).findById(testCommande.getCommandeId()); verify(commandeRepository, times(1)).findById(testCommande.getCommandeId());
} }
@Test @Test
@DisplayName("Should return empty Optional when isbn doesn't exist") @DisplayName("Should return empty Optional when isbn doesn't exist")
void testFindCommandByUUIDNotFound() { void testFindCommandByUUIDNotFound() {
@@ -137,13 +143,103 @@ public class TestCommandeUseCase {
@Nested @Nested
class UpdateCommandeTests { class UpdateCommandeTests {
@Test
@DisplayName("Should update commande when valid data is provided")
void testUpdateCommandeWithValidData() throws CommandeNotFoundException, NotValidCommandeException {
when(commandeRepository.findById(commandeId)).thenReturn(Optional.of(testCommande));
Commande updatedCommande = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande)
.rue(rue)
.ville("Fontainebleau")
.codePostal("77300")
.pays(pays)
.modePaiement(modePayement)
.commandeId(commandeId)
.build();
when(commandeRepository.save(any(Commande.class))).thenReturn(updatedCommande);
List<LigneCommandeInfo> updatelignesCommande = new ArrayList<>();
LigneCommandeInfo updateLigneCommandeInfo= new LigneCommandeInfo(12);
updatelignesCommande.add(updateLigneCommandeInfo);
CommandeInfo updateInfo = new CommandeInfo(updatelignesCommande,ModePaiement.CB.name());
AdresseInfo updateAdresseInfo = new AdresseInfo("rue du chien","Fontainebleau","77300","France");
CommandeDTO result = commandeUseCase.updateCommande(commandeId, updateInfo, updateAdresseInfo);
assertNotNull(result);
assertEquals(commandeId, result.getCommandeId());
assertEquals("Fontainebleau", result.getVille());
assertEquals("77300", result.getCodePostal());
verify(commandeRepository, times(1)).findById(commandeId);
verify(commandeRepository, times(1)).save(any(Commande.class));
}
@Test
@DisplayName("Should throw exception when commande ID doesn't exist")
void testUpdateCommandeNotFound() {
UUID nonExistentId = UUID.randomUUID();
when(commandeRepository.findById(nonExistentId)).thenReturn(Optional.empty());
List<LigneCommandeInfo> updatelignesCommande = new ArrayList<>();
LigneCommandeInfo updateLigneCommandeInfo= new LigneCommandeInfo(12);
updatelignesCommande.add(updateLigneCommandeInfo);
CommandeInfo updateInfo = new CommandeInfo(updatelignesCommande,ModePaiement.CB.name());
AdresseInfo updateAdresseInfo = new AdresseInfo("rue du chien","Fontainebleau","77300","France");
assertThrows(CommandeNotFoundException.class,
() -> commandeUseCase.updateCommande(nonExistentId, updateInfo, updateAdresseInfo));
verify(commandeRepository, times(1)).findById(nonExistentId);
verify(commandeRepository, never()).save(any(Commande.class));
}
@Test
@DisplayName("Should throw exception when update data is not valid")
void testUpdateCommandeWithInvalidData() {
List<LigneCommandeInfo> updatelignesCommande = new ArrayList<>();
LigneCommandeInfo updateLigneCommandeInfo= new LigneCommandeInfo(12);
updatelignesCommande.add(updateLigneCommandeInfo);
CommandeInfo invalidUpdateInfo = new CommandeInfo(updatelignesCommande, "CARTE");
AdresseInfo updateAdresseInfo = new AdresseInfo("rue du chien","Fontainebleau","77300","France");
assertThrows(NotValidCommandeException.class,
() -> commandeUseCase.updateCommande(commandeId, invalidUpdateInfo, updateAdresseInfo));
verify(commandeRepository, never()).findById(any(UUID.class));
verify(commandeRepository, never()).save(any(Commande.class));
}
} }
@Nested @Nested
class DeleteCommandeTests { class DeleteCommandeTests {
@Test
@DisplayName("Should delete commande when ID exists")
void testDeleteCommande() throws CommandeNotFoundException {
when(commandeRepository.findById(commandeId)).thenReturn(Optional.of(testCommande));
doNothing().when(commandeRepository).delete(testCommande);
commandeUseCase.deleteCommande(commandeId);
verify(commandeRepository, times(1)).findById(commandeId);
verify(commandeRepository, times(1)).delete(testCommande);
}
@Test
@DisplayName("Should throw exception when commande ID doesn't exist")
void testDeleteCustomerNotFound() {
UUID nonExistentId = UUID.randomUUID();
when(commandeRepository.findById(nonExistentId)).thenReturn(Optional.empty());
assertThrows(CommandeNotFoundException.class,
() -> commandeUseCase.deleteCommande(nonExistentId));
verify(commandeRepository, times(1)).findById(nonExistentId);
verify(commandeRepository, never()).delete(any(Commande.class));
}
} }