forked from pierront/mylibrary-template
Passer commande #4
+49
@@ -0,0 +1,49 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+106
-11
@@ -1,7 +1,5 @@
|
||||
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.entity.Commande;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
|
||||
@@ -42,22 +40,30 @@ public class TestCommandeUseCase {
|
||||
private CommandeInfo validCommandeInfo;
|
||||
private LigneCommandeInfo validLigneCommandeInfo;
|
||||
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
|
||||
void setUp() {
|
||||
|
||||
clientId = UUID.randomUUID();
|
||||
commandeId = UUID.randomUUID();
|
||||
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
|
||||
LigneCommandeInfo commande1 = new LigneCommandeInfo(12);
|
||||
LigneCommandeInfo commande2 = new LigneCommandeInfo(13);
|
||||
lignesCommande = new ArrayList<>();
|
||||
commande1 = new LigneCommandeInfo(12);
|
||||
commande2 = new LigneCommandeInfo(13);
|
||||
lignesCommande.add(commande1);
|
||||
lignesCommande.add(commande2);
|
||||
String rue = "rue du chien";
|
||||
String ville = "LKa Rochette";
|
||||
String codePostal = "7700";
|
||||
String pays = "France";
|
||||
String modePayement = ModePaiement.CB.name();
|
||||
rue = "rue du chien";
|
||||
ville = "LKa Rochette";
|
||||
codePostal = "7700";
|
||||
pays = "France";
|
||||
modePayement = ModePaiement.CB.name();
|
||||
|
||||
testCommande = Commande.builder()
|
||||
.clientId(clientId)
|
||||
@@ -121,7 +127,6 @@ public class TestCommandeUseCase {
|
||||
verify(commandeRepository, times(1)).findById(testCommande.getCommandeId());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when isbn doesn't exist")
|
||||
void testFindCommandByUUIDNotFound() {
|
||||
@@ -137,13 +142,103 @@ public class TestCommandeUseCase {
|
||||
|
||||
@Nested
|
||||
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
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user