forked from pierront/mylibrary-template
J'avais oublié des customer dans book
This commit is contained in:
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
4
.idea/vcs.xml
generated
Normal file
4
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings" defaultProject="true" />
|
||||||
|
</project>
|
@@ -1,6 +1,5 @@
|
|||||||
package fr.iut_fbleau.but3.dev62.mylibrary.book.usecase;
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.usecase;
|
||||||
|
|
||||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerInfo;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
@@ -21,7 +20,7 @@ import static org.mockito.Mockito.*;
|
|||||||
public class BookUseCaseTest {
|
public class BookUseCaseTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private BookRepository customerRepository;
|
private BookRepository bookRepository;
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private BookUseCase bookUseCase;
|
private BookUseCase bookUseCase;
|
||||||
@@ -47,7 +46,7 @@ public class BookUseCaseTest {
|
|||||||
.language("Francais")
|
.language("Francais")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
validBookInfo = new CustomerInfo("LivreRandom", "John Doe", "RandomPublisher", date, 12.5, 50, cat, "Je suis un livre qui est composé de mots.", "Francais");
|
validBookInfo = new BookInfo("LivreRandom", "John Doe", "RandomPublisher", date, 12.5, 50, cat, "Je suis un livre qui est composé de mots.", "Francais");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -57,7 +56,7 @@ public class BookUseCaseTest {
|
|||||||
public class RegisterBookTests {
|
public class RegisterBookTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Should register customer when valid data is provided")
|
@DisplayName("Should register book when valid data is provided")
|
||||||
void testRegisterBookWithValidData() throws NotValidBookException {
|
void testRegisterBookWithValidData() throws NotValidBookException {
|
||||||
when(BookRepository.save(any(Book.class))).thenReturn(testBook);
|
when(BookRepository.save(any(Book.class))).thenReturn(testBook);
|
||||||
|
|
||||||
@@ -69,7 +68,7 @@ public class BookUseCaseTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Should throw exception when customer data is not valid")
|
@DisplayName("Should throw exception when book data is not valid")
|
||||||
void testRegisterBookWithInvalidData() {
|
void testRegisterBookWithInvalidData() {
|
||||||
BookInfo invalidBookInfo = new BookInfo("", "", "");
|
BookInfo invalidBookInfo = new BookInfo("", "", "");
|
||||||
|
|
||||||
@@ -86,7 +85,7 @@ public class BookUseCaseTest {
|
|||||||
|
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
@DisplayName("Find customer tests")
|
@DisplayName("Find book tests")
|
||||||
class FindBookTests {
|
class FindBookTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@@ -0,0 +1,116 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.features.subscription;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository;
|
||||||
|
import io.cucumber.datatable.DataTable;
|
||||||
|
import io.cucumber.java.en.And;
|
||||||
|
import io.cucumber.java.en.Given;
|
||||||
|
import io.cucumber.java.en.Then;
|
||||||
|
import io.cucumber.java.en.When;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
|
public class SubscriptionSteps {
|
||||||
|
|
||||||
|
private final CustomerRepository customerRepository = new CustomerRepository();
|
||||||
|
|
||||||
|
private final Map<String, UUID> customerPhoneUUID = new HashMap<>();
|
||||||
|
|
||||||
|
@Given("the system has the following customers:")
|
||||||
|
public void theSystemHasTheFollowingCustomers(DataTable dataTable) {
|
||||||
|
int size = customerRepository.findAll().size();
|
||||||
|
|
||||||
|
if (size > 0) {
|
||||||
|
customerRepository.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Map<String, String>> customers = dataTable.asMaps(String.class, String.class);
|
||||||
|
|
||||||
|
for (Map<String, String> customer : customers) {
|
||||||
|
String numeroTelephone = customer.get("numeroTelephone");
|
||||||
|
Customer newCustomer = Customer.builder()
|
||||||
|
.firstName(customer.get("prenom"))
|
||||||
|
.lastName(customer.get("nom"))
|
||||||
|
.phoneNumber(numeroTelephone)
|
||||||
|
.loyaltyPoints(Integer.parseInt(customer.get("pointsFidelite")))
|
||||||
|
.build();
|
||||||
|
Customer save = customerRepository.save(newCustomer);
|
||||||
|
customerPhoneUUID.put(numeroTelephone, save.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(customers.size(), customerRepository.findAll().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@When("I create a new subscription with CB:")
|
||||||
|
public void iCreateANewSubscriptionWithCB(DataTable dataTable) {
|
||||||
|
List<Map<String, String>> rows = dataTable.asMaps(String.class, String.class);
|
||||||
|
|
||||||
|
Map<String, String> customerInfo = rows.getFirst();
|
||||||
|
|
||||||
|
SubscriptionInfo newSubscription = new subscriptionInfo(
|
||||||
|
subscriptionInfo.get("customerId"),
|
||||||
|
subscriptionInfo.get("duration"),
|
||||||
|
subscriptionInfo.get("paymentMethod"),
|
||||||
|
subscriptionInfo.get("debutDate")
|
||||||
|
);
|
||||||
|
|
||||||
|
subscriptionRegistration = subscriptionUseCase.registerSubscription(newSubscription);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Then("a new subscription is created")
|
||||||
|
public void aNewSubscriptionIsCreated() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@When("I create a new subscription with Paypal:")
|
||||||
|
public void iCreateANewSubscriptionWithPaypal(DataTable dataTable) {
|
||||||
|
List<Map<String, String>> rows = dataTable.asMaps(String.class, String.class);
|
||||||
|
|
||||||
|
Map<String, String> customerInfo = rows.getFirst();
|
||||||
|
|
||||||
|
SubscriptionInfo newSubscription = new subscriptionInfo(
|
||||||
|
subscriptionInfo.get("customerId"),
|
||||||
|
subscriptionInfo.get("duration"),
|
||||||
|
subscriptionInfo.get("paymentMethod"),
|
||||||
|
subscriptionInfo.get("debutDate")
|
||||||
|
);
|
||||||
|
|
||||||
|
subscriptionRegistration = subscriptionUseCase.registerSubscription(newSubscription);
|
||||||
|
}
|
||||||
|
|
||||||
|
@When("I try to create a new subscription with the following information:")
|
||||||
|
public void iTryToCreateANewSubscriptionWithTheFollowingInformation(DataTable dataTable) {
|
||||||
|
List<Map<String, String>> rows = dataTable.asMaps(String.class, String.class);
|
||||||
|
|
||||||
|
Map<String, String> customerInfo = rows.getFirst();
|
||||||
|
|
||||||
|
SubscriptionInfo newSubscription = new subscriptionInfo(
|
||||||
|
subscriptionInfo.get("customerId"),
|
||||||
|
subscriptionInfo.get("duration"),
|
||||||
|
subscriptionInfo.get("paymentMethod"),
|
||||||
|
subscriptionInfo.get("debutDate")
|
||||||
|
);
|
||||||
|
|
||||||
|
notValidSubscriptionDurationException = assertThrows(NotValidSubscriptionDurationException.class, () -> subscriptionUseCase.registerSubscription(newSubscription));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Then("the subsription duration creation fails")
|
||||||
|
public void theSubsriptionDurationCreationFails() assertNotNull(notValidSubscriptionDurationException);{
|
||||||
|
}
|
||||||
|
|
||||||
|
@And("I receive an error for validation subscription message containing {string}")
|
||||||
|
public void iReceiveAnErrorForValidationSubscriptionMessageContaining(String errorMessage) {
|
||||||
|
assertEquals(errorMessage, notValidSubscriptionDurationException.getMessage());
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,105 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.CustomerInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.converter.CustomerConverter;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Subscription;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.Flow;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
@DisplayName(SubscriptionConverter Unit Tests)
|
||||||
|
public class SubscriptionConverterTest {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDomain() method tests")
|
||||||
|
public class toDomainTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should convert SubscriptionInfo to Subscription domain object")
|
||||||
|
void shouldConvertSubscriptionInfoToDomain() {
|
||||||
|
// Given
|
||||||
|
SubscriptionInfo subscriptionInfo = new SubcriptionInfo("1", "12", "CB", "2025-06-08");
|
||||||
|
|
||||||
|
// When
|
||||||
|
Subscription result = SubscriptionConverter.toDomain(subscriptionInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(subscriptionInfo.customerId(), result.getCustomerId());
|
||||||
|
assertEquals(subscriptionInfo.duration(), result.getDuration());
|
||||||
|
assertEquals(subscriptionInfo.paymentMethod(), result.getPaymentMethod());
|
||||||
|
assertEquals(subscriptionInfo.debutDate(), result.getDebutDate());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("toDTO() method tests")
|
||||||
|
public class toDTOTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should convert Subscriber domain object to SubscriberDTO with all fields mapped correctly")
|
||||||
|
void shouldConvertSubscriptionToDTO() {
|
||||||
|
Subscription subscription = Subscription.builder()
|
||||||
|
.id(UUID.randomUUID())
|
||||||
|
.customerId(1)
|
||||||
|
.duration(12)
|
||||||
|
.paymentMethod("CB")
|
||||||
|
.debutDate("2025-06-08")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
SubscriptionDTO result = SubscriptionConverter.toDTO(customer);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(subscription.getId(), result.getId());
|
||||||
|
assertEquals(subscription.getCustomerId(), result.getCustomerId());
|
||||||
|
assertEquals(subscription.getDuration(), result.getDuration());
|
||||||
|
assertEquals(subscription.getPaymentMethod(), result.getPaymentMethod());
|
||||||
|
assertEquals(subscription.getDebutDate(), result.getDebutDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should handle null values properly when converting between objects")
|
||||||
|
void shouldHandleNullValuesGracefully() {
|
||||||
|
Subscription subscription = Subscription.builder()
|
||||||
|
.id(UUID.randomUUID())
|
||||||
|
.customerId(1)
|
||||||
|
.duration(null)
|
||||||
|
.paymentMethod("NullTest")
|
||||||
|
.debutDate("2025-06-08")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
SubscriptionDTO result = SubscriptionConverter.toDTO(subscription);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertNull(result.getDuration());
|
||||||
|
assertEquals("NullTest", result.getPaymentMethod());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should preserve empty string values during conversion")
|
||||||
|
void shouldPreserveEmptyStrings() {
|
||||||
|
SubscriptionInfo subscriptionInfo = new SubscriptionInfo("", "", "", "");
|
||||||
|
|
||||||
|
Subscription domainResult = SubscriptionConverter.toDomain(subscriptionInfo);
|
||||||
|
SubscriptionDTO dtoResult = SubscriptionConverter.toDTO(domainResult);
|
||||||
|
|
||||||
|
assertEquals("", dtoResult.getCustomerId());
|
||||||
|
assertEquals("", dtoResult.getDuration());
|
||||||
|
assertEquals("", dtoResult.getPaymentMethod());
|
||||||
|
assertEquals("", dtoResult.getDebutDate());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@@ -0,0 +1,49 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
|
public class SubscriptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Builder should create a valid Subscription instance")
|
||||||
|
void testSubscriptionBuilder() {
|
||||||
|
UUID id = UUID.randomUUID();
|
||||||
|
int customerId = 1;
|
||||||
|
int duration = 12;
|
||||||
|
String paymentMethod = "CB";
|
||||||
|
String debutDate = "2025-06-08";
|
||||||
|
|
||||||
|
Subscription subscription = Subscription.builder()
|
||||||
|
.id(id)
|
||||||
|
.customerId(customerId)
|
||||||
|
.duration(duration)
|
||||||
|
.paymentMethod(paymentMethod)
|
||||||
|
.debutDate(debutDate)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals(id, subscription.getId());
|
||||||
|
assertEquals(customerId, subscription.getCustomerId());
|
||||||
|
assertEquals(duration, subscription.getDuration());
|
||||||
|
assertEquals(paymentMethod, subscription.getPaymentMethod());
|
||||||
|
assertEquals(debutDate, subscription.getDebutDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("setRandomUUID should change the ID to a new random UUID")
|
||||||
|
void testSetRandomUUID() {
|
||||||
|
Subscription subscription = Subscription.builder().build();
|
||||||
|
UUID originalId = subscription.getId();
|
||||||
|
|
||||||
|
subscription.setRandomUUID();
|
||||||
|
|
||||||
|
assertNotNull(subscription.getId());
|
||||||
|
assertNotEquals(originalId, subscription.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,157 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository;
|
||||||
|
|
||||||
|
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 java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class SubscriptionRepositoryTest {
|
||||||
|
|
||||||
|
|
||||||
|
private SubscriptionRepository repository;
|
||||||
|
private Subscription Subscription1;
|
||||||
|
private Subscription Subscription2;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
repository = new SubscriptionRepository();
|
||||||
|
|
||||||
|
Subscription1 = Subscription.builder()
|
||||||
|
.customerId(1)
|
||||||
|
.duration(12)
|
||||||
|
.paymentMethod("CB")
|
||||||
|
.debutDate("2025-06-10")
|
||||||
|
.build();
|
||||||
|
Subscription1.setRandomUUID();
|
||||||
|
|
||||||
|
Subscription2 = Subscription.builder()
|
||||||
|
.customerId(2)
|
||||||
|
.duration(24)
|
||||||
|
.paymentMethod("Paypal")
|
||||||
|
.debutDate("2025-06-11")
|
||||||
|
.build();
|
||||||
|
Subscription2.setRandomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("New repository should be empty")
|
||||||
|
void testNewRepositoryIsEmpty() {
|
||||||
|
List<Subscription> subscriptions = repository.findAll();
|
||||||
|
|
||||||
|
assertTrue(subscriptions.isEmpty());
|
||||||
|
assertEquals(0, subsciptions.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Save operations")
|
||||||
|
class SaveOperations {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should add a new subsciption")
|
||||||
|
void testSaveNewSubscription() {
|
||||||
|
Subscription savedSubscription = repository.save(subscription1);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(subscription1.getId(), savedSubscription.getId());
|
||||||
|
assertEquals(subscription1.getDebutDate(), savedSubscription.getDebutDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save multiple Subscriptions should add all of them")
|
||||||
|
void testSaveMultipleSubscriptions() {
|
||||||
|
repository.save(subscription1);
|
||||||
|
repository.save(subscription2);
|
||||||
|
|
||||||
|
List<Subscription> subscriptions = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, subscriptions.size());
|
||||||
|
assertTrue(subscriptions.contains(subscription1));
|
||||||
|
assertTrue(subscriptions.contains(subscription2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find operations")
|
||||||
|
class FindOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpSubscriptions() {
|
||||||
|
repository.save(subscription1);
|
||||||
|
repository.save(subscription2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindAll should return all subscriptions")
|
||||||
|
void testFindAll() {
|
||||||
|
List<Subscription> subscriptions = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, subscriptions.size());
|
||||||
|
assertTrue(subscriptions.contains(subscription1));
|
||||||
|
assertTrue(subscriptions.contains(subscription2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindById should return subscriptions with matching ID")
|
||||||
|
void testFindById() {
|
||||||
|
Optional<Subsciption> foundSubscription = repository.findById(subscription1.getId());
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isPresent());
|
||||||
|
assertEquals(subscription1.getFirstName(), foundSubscription.get().getId());
|
||||||
|
assertEquals(subscription1.getLastName(), foundSubscription.get().getCustomerId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindById should return empty Optional when ID doesn't exist")
|
||||||
|
void testFindByIdNotFound() {
|
||||||
|
UUID nonExistentId = UUID.randomUUID();
|
||||||
|
|
||||||
|
Optional<Subscription> foundSubscription = repository.findById(nonExistentId);
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindByCustomerId should return customer with matching customer id")
|
||||||
|
void testFindByCustomerId() {
|
||||||
|
Optional<Subscription> foundSubscription = repository.findByCustomerId("1");
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isPresent());
|
||||||
|
assertEquals(subscription1.getId(), foundSubscription.get().getId());
|
||||||
|
assertEquals(subscription1.getDebutDate(), foundSubscription.get().getDebutDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindByCustomerId should return empty Optional when phone number doesn't exist")
|
||||||
|
void testFindByPhoneNumberNotFound() {
|
||||||
|
Optional<Subscription> foundSubscription = repository.findByCustomerId("0");
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsById should return true when ID exists")
|
||||||
|
void testExistsByIdExists() {
|
||||||
|
boolean exists = repository.existsById(subscription1.getId());
|
||||||
|
|
||||||
|
assertTrue(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsById should return false when ID doesn't exist")
|
||||||
|
void testExistsByIdNotExists() {
|
||||||
|
UUID nonExistentId = UUID.randomUUID();
|
||||||
|
|
||||||
|
boolean exists = repository.existsById(nonExistentId);
|
||||||
|
|
||||||
|
assertFalse(exists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,111 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.Subscription.SubscriptionDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.Subscription.SubscriptionInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.Subscription.entity.Subscription;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.Subscription.exception.SubscriptionNotFoundException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.Subscription.exception.IllegalSubscriptionPointException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.Subscription.exception.NotValidSubscriptionException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.Subscription.repository.SubscriptionRepository;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.Subscription.usecase.SubscriptionUseCase;
|
||||||
|
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.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 SubscribeUseCaseTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private SubscriptionRepository subscriptionRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private SubscriptionUseCase subscriptionUseCase;
|
||||||
|
|
||||||
|
private UUID subscriptionId;
|
||||||
|
private Subscription testSubscription;
|
||||||
|
private SubscriptionInfo validSubscriptionInfo;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
subscriptionId = UUID.randomUUID();
|
||||||
|
testSubscription = Subscription.builder()
|
||||||
|
.id(subscriptionId)
|
||||||
|
.CustomerId(1)
|
||||||
|
.duration(12)
|
||||||
|
.phaymentMethod("CB")
|
||||||
|
.debutDate("2025-06-10")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
validSubscriptionInfo = new SubscriptionInfo("1", "12", "CB", "2025-06-08");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Register subscription tests")
|
||||||
|
class RegisterSubscriptionTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should register subscription when valid data is provided")
|
||||||
|
void testRegisterSubscriptionWithValidData() throws NotValidSubscriptionException {
|
||||||
|
when(subscriptionRepository.save(any(Subscription.class))).thenReturn(testSubscription);
|
||||||
|
|
||||||
|
UUID registeredId = subscriptionUseCase.registerSubscription(validSubscriptionInfo);
|
||||||
|
|
||||||
|
assertNotNull(registeredId);
|
||||||
|
assertEquals(subscriptionId, registeredId);
|
||||||
|
verify(subscriptionRepository, times(1)).save(any(Subscription.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when subscription data is not valid")
|
||||||
|
void testRegisterSubscriptionWithInvalidData() {
|
||||||
|
SubscriptionInfo invalidSubscriptionInfo = new SubscriptionInfo("", "", "");
|
||||||
|
|
||||||
|
assertThrows(NotValidSubscriptionException.class,
|
||||||
|
() -> subscriptionUseCase.registerSubscription(invalidSubscriptionInfo));
|
||||||
|
|
||||||
|
verify(subscriptionRepository, never()).save(any(Subscription.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Find subscription tests")
|
||||||
|
class FindSubscriptionTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return subscription when phone number exists")
|
||||||
|
void testFindSubscriptionByCustomerId() {
|
||||||
|
when(subscriptionRepository.findByCustomerId(1)).thenReturn(Optional.of(testSubscription));
|
||||||
|
|
||||||
|
Optional<SubscriptionDTO> foundSubscription = subscriptionUseCase.findSubscriptionByCustomerId(1);
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isPresent());
|
||||||
|
assertEquals(testSubscription.getId(), foundSubscription.get().getId());
|
||||||
|
assertEquals(testSubscription.getDebutDate(), foundSubscription.get().getDebutDate());
|
||||||
|
verify(subscriptionRepository, times(1)).findByCustomerId(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should return empty Optional when phone number doesn't exist")
|
||||||
|
void testFindSubscriptionByPhoneNumberNotFound() {
|
||||||
|
when(subscriptionRepository.findByCustomerId(0)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
Optional<SubscriptionDTO> foundSubscription = subscriptionUseCase.findSubscriptionByCustomerId(0);
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isEmpty());
|
||||||
|
verify(subscriptionRepository, times(1)).findByCustomerId(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,121 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator.SubscriptionValidator;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class SubscriptionValidatorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should validate subscription with valid data")
|
||||||
|
void testValidateValidSubscription() {
|
||||||
|
SubscriptionInfo validSubscription = new SubscriptionInfo("1", "12", "CB", "2025-06-08");
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> SubscriptionValidator.validate(validSubscription));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("customer id validation tests")
|
||||||
|
class CustomerIdValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when customer id is blank")
|
||||||
|
void testValidateBlankCustomerId() {
|
||||||
|
SubscriptionInfo subscriptionWithBlankCustomerId = new SubscriptionInfo("", "12", "CB", "2025-06-08");
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(
|
||||||
|
NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscriptionWithBlankCustomerId)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.CUSTOMER_ID_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
@DisplayName("Should throw exception when customer id contains only whitespace")
|
||||||
|
void testValidateWhitespaceCustomerId(String whitespace) {
|
||||||
|
SubscriptionInfo subscriptionWithWhitespaceFirstName = new SubscriptionInfo(whitespace, "12", "CB", "2025-06-08");
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(
|
||||||
|
NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscriptionWithWhitespaceCustomerId)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.CUSTOMER_ID_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Duration validation tests")
|
||||||
|
class DurationValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when duration is blank")
|
||||||
|
void testValidateBlankDuration() {
|
||||||
|
SubscriptionInfo subscriptionWithBlankDuration = new SubscriptionInfo("1", "", "CB", "2025-06-08");
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(
|
||||||
|
NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscriptionWithBlankDuration)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.DURATION_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
@DisplayName("Should throw exception when last name contains only whitespace")
|
||||||
|
void testValidateWhitespaceDuration(String whitespace) {
|
||||||
|
SubscriptionInfo subscriptionWithWhitespaceDuration = new SubscriptionInfo("1", whitespace, "CB", "2025-06-08");
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(
|
||||||
|
NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscriptionWithWhitespaceDuration)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.DURATION_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Payment method validation tests")
|
||||||
|
class PaymentMethodValidationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Should throw exception when payment method is blank")
|
||||||
|
void testValidateBlankPaymentMethod() {
|
||||||
|
SubscriptionInfo subscriptionWithBlankPaymentMethod = new SubscriptionInfo("1", "12", "", "2025-06-08");
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(
|
||||||
|
NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscriptionWithBlankPaymentMethod)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.PAYMENT_METHOD_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {" ", " ", "\t", "\n"})
|
||||||
|
@DisplayName("Should throw exception when payment method contains only whitespace")
|
||||||
|
void testValidateWhitespacePhoneNumber(String whitespace) {
|
||||||
|
SubscriptionInfo subscriptionWithWhitespacePhoneNumber = new SubscriptionInfo("1", "12", whitespace, "2025-06-08");
|
||||||
|
|
||||||
|
NotValidSubscriptionException exception = assertThrows(
|
||||||
|
NotValidSubscriptionException.class,
|
||||||
|
() -> SubscriptionValidator.validate(subscriptionWithWhitespacePaymentMethod)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(SubscriptionValidator.PAYMENT_METHOD_CANNOT_BE_BLANK, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
28
src/test/resources/features/subscription.feature
Normal file
28
src/test/resources/features/subscription.feature
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# language: en
|
||||||
|
|
||||||
|
Feature: Manage customer subscription
|
||||||
|
Background:
|
||||||
|
Given the system has the following customers:
|
||||||
|
| customerId | firstName | lastNale | phoneNumer | loyaltyPoints |
|
||||||
|
| 1 | John | Doe | 0612345678 | 100 |
|
||||||
|
| 2 | Bob | Dupond | 0687654321 | 50 |
|
||||||
|
| 3 | Alice | Untel | 0698765432 | 0 |
|
||||||
|
|
||||||
|
Scenario: Create a new subscription
|
||||||
|
When I create a new subscription with CB:
|
||||||
|
| customerId | duration | paymentMethod | debutDate |
|
||||||
|
| 1 | 12 | CB | 2025-06-10 |
|
||||||
|
Then a new subscription is created
|
||||||
|
|
||||||
|
Scenario: Create a new subscription
|
||||||
|
When I create a new subscription with Paypal:
|
||||||
|
| customerId | duration | paymentMethod | debutDate |
|
||||||
|
| 2 | 24 | Paypal | 2025-06-11 |
|
||||||
|
Then a new subscription is created
|
||||||
|
|
||||||
|
Scenario: Attempt to create a subscription with invalid duration:
|
||||||
|
When I try to create a new subscription with the following information:
|
||||||
|
| customerId | duration | paymentMethod | debutDate |
|
||||||
|
| 3 | 0 | CB | 2025-06-12 |
|
||||||
|
Then the subsription duration creation fails
|
||||||
|
And I receive an error for validation subscription message containing "Duration must be positive"
|
Reference in New Issue
Block a user