forked from pierront/mylibrary-template
converter
This commit is contained in:
+76
@@ -0,0 +1,76 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@DisplayName("AvisConverter Unit Tests")
|
||||||
|
class AvisConverterTest {
|
||||||
|
|
||||||
|
private final UUID clientId = UUID.randomUUID();
|
||||||
|
private final UUID livreId = UUID.randomUUID();
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDomain() method tests")
|
||||||
|
class ToDomainTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should convert AvisInfo to Avis domain object")
|
||||||
|
void shouldConvertAvisInfoToDomain() {
|
||||||
|
AvisInfo avisInfo = new AvisInfo(
|
||||||
|
clientId, livreId, 5, "Excellent livre !", LocalDate.of(2024, 1, 15)
|
||||||
|
);
|
||||||
|
|
||||||
|
Avis result = AvisConverter.toDomain(avisInfo);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(clientId, result.getClientId());
|
||||||
|
assertEquals(livreId, result.getLivreId());
|
||||||
|
assertEquals(5, result.getNote());
|
||||||
|
assertEquals("Excellent livre !", result.getCommentaire());
|
||||||
|
assertEquals(LocalDate.of(2024, 1, 15), result.getDateAchat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should have null ID after toDomain (set by repository)")
|
||||||
|
void shouldHaveNullIdAfterToDomain() {
|
||||||
|
AvisInfo avisInfo = new AvisInfo(clientId, livreId, 3, "Commentaire", LocalDate.now());
|
||||||
|
|
||||||
|
Avis result = AvisConverter.toDomain(avisInfo);
|
||||||
|
|
||||||
|
assertNull(result.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDTO() method tests")
|
||||||
|
class ToDTOTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should convert Avis domain object to AvisDTO")
|
||||||
|
void shouldConvertAvisToDTO() {
|
||||||
|
UUID avisId = UUID.randomUUID();
|
||||||
|
Avis avis = Avis.builder()
|
||||||
|
.id(avisId)
|
||||||
|
.clientId(clientId)
|
||||||
|
.livreId(livreId)
|
||||||
|
.note(5)
|
||||||
|
.commentaire("Excellent !")
|
||||||
|
.dateAchat(LocalDate.of(2024, 1, 15))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
AvisDTO result = AvisConverter.toDTO(avis);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(avisId, result.getAvisId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.exception.NotValidAvisException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.repository.AvisRepository;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.CustomerNotFoundException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository;
|
||||||
|
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.time.LocalDate;
|
||||||
|
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)
|
||||||
|
class AvisUseCaseTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private AvisRepository avisRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private CustomerRepository customerRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private AvisUseCase avisUseCase;
|
||||||
|
|
||||||
|
private UUID clientId;
|
||||||
|
private UUID livreId;
|
||||||
|
private Customer testCustomer;
|
||||||
|
private AvisInfo validAvisInfo;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
clientId = UUID.randomUUID();
|
||||||
|
livreId = UUID.randomUUID();
|
||||||
|
|
||||||
|
testCustomer = Customer.builder()
|
||||||
|
.id(clientId)
|
||||||
|
.firstName("Marie")
|
||||||
|
.lastName("Dupont")
|
||||||
|
.phoneNumber("0612345678")
|
||||||
|
.loyaltyPoints(100)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
validAvisInfo = new AvisInfo(clientId, livreId, 5, "Excellent livre !", LocalDate.of(2024, 1, 15));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("GererAvis tests")
|
||||||
|
class GererAvisTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should create avis when valid data is provided")
|
||||||
|
void testGererAvisWithValidData() throws NotValidAvisException, CustomerNotFoundException {
|
||||||
|
when(customerRepository.findById(clientId)).thenReturn(Optional.of(testCustomer));
|
||||||
|
|
||||||
|
UUID avisId = UUID.randomUUID();
|
||||||
|
Avis savedAvis = Avis.builder()
|
||||||
|
.id(avisId)
|
||||||
|
.clientId(clientId)
|
||||||
|
.livreId(livreId)
|
||||||
|
.note(5)
|
||||||
|
.commentaire("Excellent livre !")
|
||||||
|
.dateAchat(LocalDate.of(2024, 1, 15))
|
||||||
|
.build();
|
||||||
|
when(avisRepository.save(any(Avis.class))).thenReturn(savedAvis);
|
||||||
|
|
||||||
|
AvisDTO result = avisUseCase.gererAvis(validAvisInfo);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(avisId, result.getAvisId());
|
||||||
|
verify(avisRepository, times(1)).save(any(Avis.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when customer does not exist")
|
||||||
|
void testGererAvisWithUnknownCustomer() {
|
||||||
|
when(customerRepository.findById(clientId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(CustomerNotFoundException.class,
|
||||||
|
() -> avisUseCase.gererAvis(validAvisInfo));
|
||||||
|
|
||||||
|
verify(avisRepository, never()).save(any(Avis.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when note is invalid")
|
||||||
|
void testGererAvisWithInvalidNote() {
|
||||||
|
AvisInfo invalidAvis = new AvisInfo(clientId, livreId, 6, "Commentaire", LocalDate.now());
|
||||||
|
|
||||||
|
assertThrows(NotValidAvisException.class,
|
||||||
|
() -> avisUseCase.gererAvis(invalidAvis));
|
||||||
|
|
||||||
|
verify(avisRepository, never()).save(any(Avis.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when commentaire is blank")
|
||||||
|
void testGererAvisWithBlankCommentaire() {
|
||||||
|
AvisInfo invalidAvis = new AvisInfo(clientId, livreId, 5, "", LocalDate.now());
|
||||||
|
|
||||||
|
assertThrows(NotValidAvisException.class,
|
||||||
|
() -> avisUseCase.gererAvis(invalidAvis));
|
||||||
|
|
||||||
|
verify(avisRepository, never()).save(any(Avis.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when clientId is null")
|
||||||
|
void testGererAvisWithNullClientId() {
|
||||||
|
AvisInfo invalidAvis = new AvisInfo(null, livreId, 5, "Commentaire", LocalDate.now());
|
||||||
|
|
||||||
|
assertThrows(NotValidAvisException.class,
|
||||||
|
() -> avisUseCase.gererAvis(invalidAvis));
|
||||||
|
|
||||||
|
verify(avisRepository, never()).save(any(Avis.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("FindAvis tests")
|
||||||
|
class FindAvisTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return avis when ID exists")
|
||||||
|
void testFindAvisById() {
|
||||||
|
UUID avisId = UUID.randomUUID();
|
||||||
|
Avis avis = Avis.builder()
|
||||||
|
.id(avisId)
|
||||||
|
.clientId(clientId)
|
||||||
|
.livreId(livreId)
|
||||||
|
.note(5)
|
||||||
|
.commentaire("Excellent !")
|
||||||
|
.dateAchat(LocalDate.now())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(avisRepository.findById(avisId)).thenReturn(Optional.of(avis));
|
||||||
|
|
||||||
|
Optional<AvisDTO> result = avisUseCase.findAvisById(avisId);
|
||||||
|
|
||||||
|
assertTrue(result.isPresent());
|
||||||
|
assertEquals(avisId, result.get().getAvisId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty Optional when ID does not exist")
|
||||||
|
void testFindAvisByIdNotFound() {
|
||||||
|
UUID nonExistentId = UUID.randomUUID();
|
||||||
|
when(avisRepository.findById(nonExistentId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
Optional<AvisDTO> result = avisUseCase.findAvisById(nonExistentId);
|
||||||
|
|
||||||
|
assertTrue(result.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user