forked from pierront/mylibrary-template
correction de bug et upgrade du BDD
This commit is contained in:
@@ -28,6 +28,12 @@ public class SubscriptionRepository {
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public Optional<Subscription> findById(UUID id) {
|
||||
return this.subscriptions.stream()
|
||||
.filter(subscription -> subscription.getId().equals(id))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsById(UUID uuid) {
|
||||
return this.subscriptions.stream()
|
||||
.anyMatch(subscription -> subscription.getId().equals(uuid));
|
||||
|
@@ -17,19 +17,25 @@ public class SubscriptionValidator {
|
||||
|
||||
}
|
||||
|
||||
public static void validate(SubscriptionInfo newSubscription) throws fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException {
|
||||
public static void validate(SubscriptionInfo newSubscription) throws NotValidSubscriptionException {
|
||||
validateCustomerId(newSubscription);
|
||||
validateDuration(newSubscription);
|
||||
validatePaymentMethod(newSubscription);
|
||||
}
|
||||
|
||||
|
||||
private static void validateDuration(SubscriptionInfo newSubscription) throws fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException {
|
||||
if (newSubscription.duration() == null) {
|
||||
throw new fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException(DURATION_CANNOT_BE_NULL);
|
||||
private static void validateCustomerId(SubscriptionInfo newSubscription) throws NotValidSubscriptionException {
|
||||
if (newSubscription.customerId() == null) {
|
||||
throw new NotValidSubscriptionException(CUSTOMER_ID_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePaymentMethod(SubscriptionInfo newSubscription) throws fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException {
|
||||
private static void validateDuration(SubscriptionInfo newSubscription) throws NotValidSubscriptionException {
|
||||
if (newSubscription.duration() == null) {
|
||||
throw new NotValidSubscriptionException(DURATION_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validatePaymentMethod(SubscriptionInfo newSubscription) throws NotValidSubscriptionException {
|
||||
if (newSubscription.paymentMethod().isBlank()) {
|
||||
throw new NotValidSubscriptionException(PAYMENT_METHOD_CANNOT_BE_BLANK);
|
||||
}
|
||||
|
@@ -9,8 +9,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.exception.NotValidCustomerException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.usecase.CustomerUseCase;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionDTO;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.NotValidSubscriptionException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception.SubscriptionNotFoundException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository.SubscriptionRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase.SubscriptionUseCase;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
|
||||
@@ -25,6 +27,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class SubscriptionSteps {
|
||||
@@ -32,6 +35,8 @@ public class SubscriptionSteps {
|
||||
private final SubscriptionRepository subscriptionRepository = new SubscriptionRepository();
|
||||
private final SubscriptionUseCase subscriptionUseCase = new SubscriptionUseCase(subscriptionRepository);
|
||||
private NotValidSubscriptionException notValidSubscriptionException;
|
||||
private SubscriptionNotFoundException subscriptionNotFoundException;
|
||||
private static final List<Subscription> subscriptions = new ArrayList<>();
|
||||
|
||||
private final CustomerRepository customerRepository = new CustomerRepository();
|
||||
private UUID subscriptionRegistration;
|
||||
@@ -82,6 +87,8 @@ public class SubscriptionSteps {
|
||||
|
||||
@Then("a new subscription is created")
|
||||
public void aNewSubscriptionIsCreated() {
|
||||
assertNotNull(subscriptionRegistration);
|
||||
assertTrue(subscriptionRepository.existsById(subscriptionRegistration));
|
||||
|
||||
}
|
||||
|
||||
@@ -119,11 +126,48 @@ public class SubscriptionSteps {
|
||||
}
|
||||
|
||||
@Then("the subsription duration creation fails")
|
||||
public void theSubsriptionDurationCreationFails() {assertNotNull(notValidSubscriptionException);}
|
||||
public void theSubsriptionDurationCreationFails() {
|
||||
assertNotNull(notValidSubscriptionException);
|
||||
}
|
||||
|
||||
|
||||
@And("I receive an error for validation subscription message containing {string}")
|
||||
public void iReceiveAnErrorForValidationSubscriptionMessageContaining(String errorMessage) {
|
||||
assertEquals(errorMessage, notValidSubscriptionException.getMessage());
|
||||
}
|
||||
|
||||
@When("I try to get a subscription with the following customerId:")
|
||||
public void iTryToGetASubscriptionWithTheFollowingCustomerId(DataTable dataTable) {
|
||||
List<Map<String, String>> rows = dataTable.asMaps(String.class, String.class);
|
||||
Map<String, String> subscriptionData = rows.getFirst();
|
||||
|
||||
UUID customerId = UUID.fromString(subscriptionData.get("customerId"));
|
||||
try {
|
||||
subscriptionRegistration = subscriptionUseCase.findSubscriptionByCustomerId(customerId)
|
||||
.orElseThrow(() -> new SubscriptionNotFoundException(customerId))
|
||||
.getId();
|
||||
} catch (SubscriptionNotFoundException e) {
|
||||
subscriptionNotFoundException = e;
|
||||
}
|
||||
}
|
||||
|
||||
@Then("I receive the following subscription:")
|
||||
public void iReceiveTheFollowingSubscription(DataTable dataTable) {
|
||||
List<Map<String, String>> rows = dataTable.asMaps(String.class, String.class);
|
||||
Map<String, String> expectedSubscription = rows.getFirst();
|
||||
|
||||
SubscriptionDTO subscription = subscriptionUseCase.findSubscriptionByCustomerId(
|
||||
UUID.fromString(expectedSubscription.get("customerId"))
|
||||
).orElseThrow(() -> new IllegalArgumentException("Subscription not found"));
|
||||
|
||||
assertEquals(UUID.fromString(expectedSubscription.get("customerId")), subscription.getCustomerId());
|
||||
assertEquals(Integer.parseInt(expectedSubscription.get("duration")), subscription.getDuration());
|
||||
assertEquals(expectedSubscription.get("paymentMethod"), subscription.getPaymentMethod());
|
||||
|
||||
}
|
||||
|
||||
@Then("I receive an error for not found subscription")
|
||||
public void iReceiveAnErrorForNotFoundSubscription() {
|
||||
assertNotNull(subscriptionNotFoundException);
|
||||
}
|
||||
}
|
||||
|
@@ -32,7 +32,7 @@ public class SubscriptionRepositoryTest {
|
||||
Subscription1.setRandomUUID();
|
||||
|
||||
Subscription2 = Subscription.builder()
|
||||
.customerId(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"))
|
||||
.customerId(UUID.fromString("456e4567-e89b-12d3-a456-426614174000"))
|
||||
.duration(24)
|
||||
.paymentMethod("Paypal")
|
||||
.build();
|
||||
@@ -99,7 +99,7 @@ public class SubscriptionRepositoryTest {
|
||||
@Test
|
||||
@DisplayName("FindById should return subscriptions with matching ID")
|
||||
void testFindById() {
|
||||
Optional<Subscription> foundSubscription = repository.findByCustomerId(Subscription1.getId());
|
||||
Optional<Subscription> foundSubscription = repository.findById(Subscription1.getId());
|
||||
|
||||
assertTrue(foundSubscription.isPresent());
|
||||
assertEquals(Subscription1.getId(), foundSubscription.get().getId());
|
||||
|
@@ -27,13 +27,13 @@ public class SubscriptionValidatorTest {
|
||||
class CustomerIdValidationTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when customer id is blank")
|
||||
@DisplayName("Should throw exception when customer id is null")
|
||||
void testValidateBlankCustomerId() {
|
||||
SubscriptionInfo subscriptionWithBlankCustomerId = new SubscriptionInfo(null, 12, "CB");
|
||||
SubscriptionInfo subscriptionWithNullCustomerId = new SubscriptionInfo(null, 12, "CB");
|
||||
|
||||
NotValidSubscriptionException exception = assertThrows(
|
||||
NotValidSubscriptionException.class,
|
||||
() -> SubscriptionValidator.validate(subscriptionWithBlankCustomerId)
|
||||
() -> SubscriptionValidator.validate(subscriptionWithNullCustomerId)
|
||||
);
|
||||
|
||||
assertEquals(SubscriptionValidator.CUSTOMER_ID_CANNOT_BE_NULL,
|
||||
|
@@ -1,5 +1,7 @@
|
||||
# language: en
|
||||
|
||||
|
||||
|
||||
Feature: Manage customer subscription
|
||||
Background:
|
||||
Given the system has the following customers:
|
||||
@@ -25,4 +27,27 @@ Feature: Manage customer subscription
|
||||
| customerId | duration | paymentMethod |
|
||||
| 33333333-3333-3333-3333-333333333333 | 0 | CB |
|
||||
Then the subsription duration creation fails
|
||||
And I receive an error for validation subscription message containing "Duration must be positive"
|
||||
And I receive an error for validation subscription message containing "Duration must be positive"
|
||||
|
||||
Scenario: Attempt to get a subscription
|
||||
Given I create a new subscription with CB:
|
||||
| customerId | duration | paymentMethod |
|
||||
| 11111111-1111-1111-1111-111111111111 | 12 | CB |
|
||||
When I try to get a subscription with the following customerId:
|
||||
| customerId |
|
||||
| 11111111-1111-1111-1111-111111111111 |
|
||||
Then I receive the following subscription:
|
||||
| subscriptionId | customerId | duration | paymentMethod | debutDate |
|
||||
| 99999999-9999-9999-9999-999999999999 | 11111111-1111-1111-1111-111111111111 | 12 | CB | 2025-06-11 |
|
||||
|
||||
Scenario: Attempt to find a unexisting subscription
|
||||
When I try to get a subscription with the following customerId:
|
||||
| customerId |
|
||||
| 11111111-1111-1111-1111-111111111111 |
|
||||
Then I receive an error for not found subscription
|
||||
|
||||
Scenario: Attempt to find a subscription with a non-existing customerId
|
||||
When I try to get a subscription with the following customerId:
|
||||
| customerId |
|
||||
| 44444444-4444-4444-4444-444444444444 |
|
||||
Then I receive an error for not found subscription
|
Reference in New Issue
Block a user