Compare commits

...

3 Commits

Author SHA1 Message Date
Maxime LEBRETON 0af1c20659 suite de l'impementation 2026-06-14 19:50:28 +02:00
Maxime LEBRETON 9c1fe4016f début test usecase 2026-06-14 19:42:48 +02:00
Maxime LEBRETON ae8daf5e2c refactor nom fichier 2026-06-14 19:42:04 +02:00
6 changed files with 220 additions and 80 deletions
@@ -15,7 +15,18 @@ public final class CommandeConverter {
.ville(adressInfo.ville()) .ville(adressInfo.ville())
.codePostal(adressInfo.codePostal()) .codePostal(adressInfo.codePostal())
.pays(adressInfo.pays()) .pays(adressInfo.pays())
.modePaiement(modePaiement) .modePaiement(commandeInfo.modePayement())
.build();
}
public static Commande toDomain(CommandeInfo commandeInfo, AdresseInfo adressInfo) {
return Commande.builder()
.lignesCommande(commandeInfo.listeLigne())
.rue(adressInfo.rue())
.ville(adressInfo.ville())
.codePostal(adressInfo.codePostal())
.pays(adressInfo.pays())
.modePaiement(commandeInfo.modePayement())
.build(); .build();
} }
@@ -30,4 +41,15 @@ public final class CommandeConverter {
.build(); .build();
} }
public static CommandeDTO toDTO(Commande commande) {
return CommandeDTO.builder()
.lignesCommande(commande.getLignesCommande())
.rue(commande.getRue())
.ville(commande.getVille())
.codePostal(commande.getCodePostal())
.pays(commande.getPays())
.modePaiement(commande.getModePaiement())
.build();
}
} }
@@ -1,15 +1,13 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository; package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
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.customer.entity.Customer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
public class ComandeRepository { public class CommandeRepository {
private final List<Commande> commande = new ArrayList<>(); private final List<Commande> commande = new ArrayList<>();
@@ -0,0 +1,41 @@
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.CommandeDTO;
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.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.CommandeRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.validator.CommandeValidator;
import java.util.Optional;
import java.util.UUID;
public class CommandeUseCase {
private final CommandeRepository commandeRepository;
public CommandeUseCase(CommandeRepository commandeRepository) {
this.commandeRepository = commandeRepository;
}
public UUID registerCommande(CommandeInfo validCommandeInfo, AdresseInfo validAdresseInfo){
CommandeValidator.validate(validCommandeInfo);
CommandeValidator.validate(validAdresseInfo);
Commande commandeToRegister = CommandeConverter.toDomain(validCommandeInfo,validAdresseInfo);
Commande commandeToRegistered = commandeRepository.save(commandeToRegister) ;
return commandeToRegistered.getCommandeId();
}
public Optional<CommandeDTO> findCommandByUUID(UUID uuid) {
Optional<Commande> optionalCommande = commandeRepository.findById(uuid);
return optionalCommande.map(CommandeConverter::toDTO);
}
}
@@ -17,14 +17,14 @@ import static org.junit.jupiter.api.Assertions.*;
public class CommandeRepositoryTest { public class CommandeRepositoryTest {
private ComandeRepository repository; private CommandeRepository repository;
private Commande commandeN1; private Commande commandeN1;
private Commande commandeN2; private Commande commandeN2;
private Commande commandeN3; private Commande commandeN3;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
repository = new ComandeRepository(); repository = new CommandeRepository();
UUID clientId = UUID.randomUUID(); UUID clientId = UUID.randomUUID();
List<LigneCommandeInfo> lignesCommande = new ArrayList<>(); List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
@@ -1,74 +0,0 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.CommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.ComandeRepository;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.usecase.CustomerUseCase;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class CommandeUseCase {
@Mock
private ComandeRepository comandeRepository;
@InjectMocks
private CommandeUseCase commandeUseCase;
private UUID commandeId;
private UUID clientId;
private Commande testCommande;
private CommandeInfo validCommandeInfo;
private LigneCommandeInfo validLigneCommandeInfo;
private AdresseInfo validAdresseInfo;
@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.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();
testCommande = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande)
.rue(rue)
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.modePaiement(modePayement)
.commandeId(commandeId)
.build();
validCommandeInfo = new CommandeInfo(lignesCommande,ModePaiement.CB.name());
validLigneCommandeInfo= new LigneCommandeInfo(12);
validAdresseInfo = new AdresseInfo("rue du chien","LKa Rochette","7700","France");
}
@Nested
}
@@ -0,0 +1,153 @@
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;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.CommandeRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class TestCommandeUseCase {
@Mock
private CommandeRepository commandeRepository;
@InjectMocks
private CommandeUseCase commandeUseCase;
private UUID commandeId;
private UUID clientId;
private Commande testCommande;
private CommandeInfo validCommandeInfo;
private LigneCommandeInfo validLigneCommandeInfo;
private AdresseInfo validAdresseInfo;
@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.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();
testCommande = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande)
.rue(rue)
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.modePaiement(modePayement)
.commandeId(commandeId)
.build();
validCommandeInfo = new CommandeInfo(lignesCommande,ModePaiement.CB.name());
validLigneCommandeInfo= new LigneCommandeInfo(12);
validAdresseInfo = new AdresseInfo("rue du chien","LKa Rochette","7700","France");
}
@Nested
class RegisterCommandeTest{
@Test
void testRegisterCommandeWhitValidData(){
when(commandeRepository.save(any(Commande.class))).thenReturn(testCommande);
UUID registeredIsbn = commandeUseCase.registerCommande(validCommandeInfo,validAdresseInfo);
assertNotNull(registeredIsbn);
assertEquals(commandeId, registeredIsbn);
verify(commandeRepository, times(1)).save(any(Commande.class));
}
@Test
void testRegisterCommandeWhitInvalidData(){
CommandeInfo invalidCommandeInfo = new CommandeInfo(new ArrayList<LigneCommandeInfo>(),"pas bon ");
AdresseInfo invalidAdressInfo = new AdresseInfo(""," "," ","");
assertThrows(NotValidCommandeException.class,
()->commandeUseCase.registerCommande(invalidCommandeInfo,invalidAdressInfo));
verify(commandeRepository, never()).save(any(Commande.class));
}
}
@Nested
class FindCommandeTests {
@Test
@DisplayName("Should return book when isbn exists")
void testFindCommandByUUID() {
when(commandeRepository.findById(testCommande.getCommandeId())).thenReturn(Optional.of(testCommande));
Optional<CommandeDTO> foundCommande = commandeUseCase.findCommandByUUID(testCommande.getCommandeId());
assertTrue(foundCommande.isPresent());
assertEquals(testCommande.getCommandeId(), foundCommande.get().getCommandeId());
verify(commandeRepository, times(1)).findById(testCommande.getCommandeId());
}
@Test
@DisplayName("Should return empty Optional when isbn doesn't exist")
void testFindCommandByUUIDNotFound() {
UUID uuid = UUID.randomUUID();
when(commandeRepository.findById(uuid)).thenReturn(Optional.empty());
Optional<CommandeDTO> foundCommande = commandeUseCase.findCommandByUUID(uuid);
assertTrue(foundCommande.isEmpty());
verify(commandeRepository, times(1)).findById(uuid);
}
}
@Nested
class UpdateCommandeTests {
}
@Nested
class DeleteCommandeTests {
}
}