forked from pierront/mylibrary-template
début test usecase
This commit is contained in:
-74
@@ -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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
+156
@@ -0,0 +1,156 @@
|
|||||||
|
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.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.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<BookDTO> foundCommande = commandeUseCase.findCommandByUUID(testCommande.getCommandeId());
|
||||||
|
|
||||||
|
assertTrue(foundCommande.isPresent());
|
||||||
|
assertEquals(testCommande.getCommandeId(), foundCommande.get().getIsbn());
|
||||||
|
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<BookDTO> foundCommande = commandeUseCase.findCommandByUUID(uuid);
|
||||||
|
|
||||||
|
assertTrue(foundCommande.isEmpty());
|
||||||
|
verify(commandeRepository, times(1)).findById(uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class UpdateCommandeTests {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class DeleteCommandeTests {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user