forked from pierront/mylibrary-template
Compare commits
2 Commits
3deb17a4c7
...
7c2eb19e13
| Author | SHA1 | Date | |
|---|---|---|---|
| 7c2eb19e13 | |||
| 49ddb77fb2 |
+21
@@ -0,0 +1,21 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SubscriptionNotFoundException extends RuntimeException {
|
||||
public static final String THE_SUBSCRIPTION_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE = "The subscriptions with the customer id {0} does not exists";
|
||||
public static final String THE_SUBSCRIPTION_WITH_SUBSCRIPTION_ID_DOES_NOT_EXIST_MESSAGE = "The subscription with subscription id {0} does not exists";
|
||||
|
||||
public SubscriptionNotFoundException(Optional<UUID> customerUUID, Optional<UUID> subscriptionUUID) {
|
||||
super(buildMessage(customerUUID, subscriptionUUID));
|
||||
}
|
||||
|
||||
private static String buildMessage(Optional<UUID> customerUUID, Optional<UUID> subscriptionUUID) {
|
||||
if (customerUUID.isPresent()) {
|
||||
return MessageFormat.format(THE_SUBSCRIPTION_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE, customerUUID.get());
|
||||
}
|
||||
return MessageFormat.format(THE_SUBSCRIPTION_WITH_SUBSCRIPTION_ID_DOES_NOT_EXIST_MESSAGE, subscriptionUUID.get());
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentMethodInfo;
|
||||
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.converter.SubscriptionConverter;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.PaymentMethod;
|
||||
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.validator.PaymentMethodValidator;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.validator.SubscriptionValidator;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class SubscriptionUseCase {
|
||||
|
||||
private final SubscriptionRepository subscriptionRepository;
|
||||
|
||||
public SubscriptionUseCase(SubscriptionRepository subscriptionRepository) {
|
||||
|
||||
this.subscriptionRepository = subscriptionRepository;
|
||||
}
|
||||
|
||||
private Map<String, Object> Output(Subscription subscription){
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
result.put("subscriptionId", subscription.getSubscriptionId());
|
||||
result.put("startDate", subscription.getStartDate());
|
||||
result.put("endDate", subscription.getEndDate());
|
||||
result.put("monthlyAmount", subscription.getMonthlyAmount());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> registerSubscription(SubscriptionInfo newSubscription, PaymentMethodInfo newPaymentMethod)
|
||||
throws NotValidSubscriptionException {
|
||||
SubscriptionValidator.validate(newSubscription);
|
||||
PaymentMethodValidator.validate(newPaymentMethod);
|
||||
Subscription subscriptionToRegister = SubscriptionConverter.toDomain(newSubscription, newPaymentMethod);
|
||||
Subscription subscriptionToRegistered = subscriptionRepository.save(subscriptionToRegister);
|
||||
return Output(subscriptionToRegistered);
|
||||
}
|
||||
|
||||
public List<SubscriptionDTO> findSubscriptionByCustomerId(UUID customerId) {
|
||||
List<Subscription> optionalSubscriptions = subscriptionRepository.FindByCustomerId(customerId);
|
||||
return optionalSubscriptions.stream()
|
||||
.map(SubscriptionConverter::toDTO)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public Optional<SubscriptionDTO> findSubscriptionBySubscriptionId(UUID subscriptionId) {
|
||||
Optional<Subscription> optionalSubscription = subscriptionRepository.findBySubscriptionId(subscriptionId);
|
||||
return optionalSubscription.map(SubscriptionConverter::toDTO);
|
||||
}
|
||||
|
||||
public SubscriptionDTO updateSubscription(UUID SubscriptionUUID, SubscriptionInfo subscriptionInfo, PaymentMethodInfo paymentMethodInfo)
|
||||
throws SubscriptionNotFoundException, NotValidSubscriptionException {
|
||||
SubscriptionValidator.validate(subscriptionInfo);
|
||||
PaymentMethodValidator.validate(paymentMethodInfo);
|
||||
Subscription subscriptionBySubscriptionUUID = getSubscriptionIfDoesNotExistThrowSubscriptionNotFoundException(
|
||||
SubscriptionUUID);
|
||||
PaymentMethod paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(paymentMethodInfo.paymentType())
|
||||
.details(paymentMethodInfo.details())
|
||||
.build();
|
||||
Subscription subscription = Subscription.builder()
|
||||
.subscriptionId(SubscriptionUUID)
|
||||
.customerId(subscriptionBySubscriptionUUID.getCustomerId())
|
||||
.desiredSubscriptionDuration(subscriptionInfo.desiredSubscriptionDuration())
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(subscriptionInfo.desiredStartDate())
|
||||
.monthlyAmount(subscriptionBySubscriptionUUID.getMonthlyAmount())
|
||||
.build();
|
||||
subscription.setDateSubscription();
|
||||
Subscription updatedSubscription = subscriptionRepository.save(subscription);
|
||||
return SubscriptionConverter.toDTO(updatedSubscription);
|
||||
}
|
||||
|
||||
public void deleteSubscription(UUID subscriptionUUID) throws SubscriptionNotFoundException {
|
||||
Subscription subscriptionToDelete = getSubscriptionIfDoesNotExistThrowSubscriptionNotFoundException(subscriptionUUID);
|
||||
this.subscriptionRepository.delete(subscriptionToDelete);
|
||||
}
|
||||
|
||||
public void deleteSubscriptionsOfACustomer(UUID customerUUID) throws SubscriptionNotFoundException {
|
||||
List<Subscription> subscriptionsToDelete = getSubscriptionByCustomerIdIfDoesNotExistThrowSubscriptionNotFoundException(customerUUID);
|
||||
for (Subscription subscription : subscriptionsToDelete) {
|
||||
subscriptionRepository.delete(subscription);
|
||||
}
|
||||
}
|
||||
|
||||
private Subscription getSubscriptionIfDoesNotExistThrowSubscriptionNotFoundException(UUID subscriptionUUID)
|
||||
throws SubscriptionNotFoundException {
|
||||
Optional<Subscription> optionalSubscriptionBySubscriptionId = subscriptionRepository.findBySubscriptionId(subscriptionUUID);
|
||||
if (optionalSubscriptionBySubscriptionId.isEmpty()) {
|
||||
throw new SubscriptionNotFoundException(Optional.empty(),Optional.of(subscriptionUUID));
|
||||
}
|
||||
return optionalSubscriptionBySubscriptionId.get();
|
||||
}
|
||||
|
||||
private List<Subscription> getSubscriptionByCustomerIdIfDoesNotExistThrowSubscriptionNotFoundException(UUID customerUUID)
|
||||
throws SubscriptionNotFoundException {
|
||||
List<Subscription> optionalSubscriptionBySubscriptionId = subscriptionRepository.FindByCustomerId(customerUUID);
|
||||
if (optionalSubscriptionBySubscriptionId.isEmpty()) {
|
||||
throw new SubscriptionNotFoundException(Optional.of(customerUUID),Optional.empty());
|
||||
}
|
||||
return optionalSubscriptionBySubscriptionId;
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.exception;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class SubscriptionNotFoundExceptionTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the UUID provided for customer")
|
||||
void testExceptionMessageContainsUUIDForCustomer() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||
|
||||
String expectedMessage = String.format("The subscriptions with the customer id %s does not exists", customerUUID);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception message should contain the UUID provided for subscription")
|
||||
void testExceptionMessageContainsUUIDForSubscription() {
|
||||
UUID subscriptionUUID = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(Optional.empty(), Optional.of(subscriptionUUID));
|
||||
|
||||
String expectedMessage = String.format("The subscription with subscription id %s does not exists", subscriptionUUID);
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format for customer")
|
||||
void testExceptionUsesConstantMessageCustomerFormat() {
|
||||
UUID customerUUID = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(Optional.of(customerUUID), Optional.empty());
|
||||
|
||||
String expectedFormatWithPlaceholder = "The subscriptions with the customer id {0} does not exists";
|
||||
assertEquals(SubscriptionNotFoundException.THE_SUBSCRIPTION_WITH_CUSTOMER_ID_DOES_NOT_EXIST_MESSAGE,
|
||||
expectedFormatWithPlaceholder);
|
||||
assertTrue(exception.getMessage().contains(customerUUID.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should use the correct constant message format for subscription")
|
||||
void testExceptionUsesConstantMessageSubscriptionFormat() {
|
||||
UUID subscriptionUUID = UUID.randomUUID();
|
||||
|
||||
SubscriptionNotFoundException exception = new SubscriptionNotFoundException(Optional.empty(), Optional.of(subscriptionUUID));
|
||||
|
||||
String expectedFormatWithPlaceholder = "The subscription with subscription id {0} does not exists";
|
||||
assertEquals(SubscriptionNotFoundException.THE_SUBSCRIPTION_WITH_SUBSCRIPTION_ID_DOES_NOT_EXIST_MESSAGE,
|
||||
expectedFormatWithPlaceholder);
|
||||
assertTrue(exception.getMessage().contains(subscriptionUUID.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Exception should be properly thrown and caught")
|
||||
void testExceptionCanBeThrownAndCaught() {
|
||||
UUID subscriptionUUID = UUID.randomUUID();
|
||||
|
||||
try {
|
||||
throw new SubscriptionNotFoundException(Optional.empty(), Optional.of(subscriptionUUID));
|
||||
} catch (SubscriptionNotFoundException e) {
|
||||
String expectedMessage = String.format("The subscription with subscription id %s does not exists", subscriptionUUID);
|
||||
assertEquals(expectedMessage, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
-1
@@ -2,7 +2,6 @@ package fr.iut_fbleau.but3.dev62.mylibrary.subscription.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.DesiredSubscriptionDuration;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.PaymentType;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.SubscriptionRepository;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.PaymentMethod;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
||||
+273
@@ -0,0 +1,273 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.subscription.usecase;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.exception.NotValidReviewException;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.*;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.PaymentMethod;
|
||||
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 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.List;
|
||||
import java.util.Map;
|
||||
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.*;
|
||||
import static org.mockito.Mockito.never;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class SubscriptionUseCaseTest {
|
||||
|
||||
@Mock
|
||||
private SubscriptionRepository subscriptionRepository;
|
||||
|
||||
@InjectMocks
|
||||
private SubscriptionUseCase subscriptionUseCase;
|
||||
|
||||
private UUID subscriptionId;
|
||||
private UUID customerId;
|
||||
private Integer desiredSubscriptionDuration;
|
||||
private PaymentMethod paymentMethod;
|
||||
private LocalDate desiredStartDate;
|
||||
private double monthlyAmount;
|
||||
private Subscription testSubscription;
|
||||
private SubscriptionInfo validSubscriptionInfo;
|
||||
private PaymentMethodInfo validPaymentMethodInfo;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
subscriptionId = UUID.randomUUID();
|
||||
customerId = UUID.randomUUID();
|
||||
desiredSubscriptionDuration = DesiredSubscriptionDuration.THREE.getValue();
|
||||
desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
paymentMethod = PaymentMethod.builder()
|
||||
.paymentType(PaymentType.CB.name())
|
||||
.details("Maxime Lebreton")
|
||||
.build();
|
||||
monthlyAmount = 12.99;
|
||||
|
||||
testSubscription = Subscription.builder()
|
||||
.subscriptionId(subscriptionId)
|
||||
.customerId(customerId)
|
||||
.desiredSubscriptionDuration(desiredSubscriptionDuration)
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.monthlyAmount(monthlyAmount)
|
||||
.build();
|
||||
|
||||
validPaymentMethodInfo = new PaymentMethodInfo(PaymentType.PAYPAL.name(),"");
|
||||
validSubscriptionInfo = new SubscriptionInfo(desiredSubscriptionDuration, desiredStartDate);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Register subscription tests")
|
||||
class RegisterSubscriptionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should register review when valid data is provided")
|
||||
void testRegisterSubscriptionithValidData() throws NotValidSubscriptionException {
|
||||
when(subscriptionRepository.save(any(Subscription.class))).thenReturn(testSubscription);
|
||||
|
||||
Map<String, Object> registeredSubscription = subscriptionUseCase.registerSubscription(validSubscriptionInfo, validPaymentMethodInfo);
|
||||
|
||||
assertNotNull(registeredSubscription);
|
||||
assertEquals(subscriptionId, (UUID) registeredSubscription.get("subscriptionId"));
|
||||
verify(subscriptionRepository, times(1)).save(any(Subscription.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when review data is not valid")
|
||||
void testRegisterSubscriptionWithInvalidData() {
|
||||
PaymentMethodInfo invalidPaymentMethodInfo = new PaymentMethodInfo("carte","");
|
||||
SubscriptionInfo invalidSubscriptionInfo = new SubscriptionInfo(0, desiredStartDate);
|
||||
|
||||
assertThrows(NotValidSubscriptionException.class,
|
||||
() -> subscriptionUseCase.registerSubscription(invalidSubscriptionInfo, invalidPaymentMethodInfo));
|
||||
|
||||
verify(subscriptionRepository, never()).save(any(Subscription.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Find subscription tests")
|
||||
class FindSubscriptionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return reviews when customer ID exists")
|
||||
void testFindSubscriptionByCustomerId() {
|
||||
when(subscriptionRepository.FindByCustomerId(customerId)).thenReturn(List.of(testSubscription));
|
||||
|
||||
List<SubscriptionDTO> foundSubscriptions = subscriptionUseCase.findSubscriptionByCustomerId(customerId);
|
||||
|
||||
assertFalse(foundSubscriptions.isEmpty());
|
||||
boolean allSameCustomer = foundSubscriptions.stream()
|
||||
.allMatch(review -> review.getCustomerId().equals(customerId));
|
||||
assertTrue(allSameCustomer);
|
||||
verify(subscriptionRepository, times(1)).FindByCustomerId(customerId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when customer ID doesn't exist")
|
||||
void testFindSubscriptionByCustomerIdNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
when(subscriptionRepository.FindByCustomerId(nonExistentCustomerId)).thenReturn(List.of());
|
||||
|
||||
List<SubscriptionDTO> foundSubscriptions = subscriptionUseCase.findSubscriptionByCustomerId(nonExistentCustomerId);
|
||||
|
||||
assertTrue(foundSubscriptions.isEmpty());
|
||||
verify(subscriptionRepository, times(1)).FindByCustomerId(nonExistentCustomerId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return subscription when Subscription ID exists")
|
||||
void testFindSubscriptionBySubscriptionId() {
|
||||
when(subscriptionRepository.findBySubscriptionId(subscriptionId)).thenReturn(Optional.of(testSubscription));
|
||||
|
||||
Optional<SubscriptionDTO> foundSubscription = subscriptionUseCase.findSubscriptionBySubscriptionId(subscriptionId);
|
||||
|
||||
assertTrue(foundSubscription.isPresent());
|
||||
assertEquals(testSubscription.getCustomerId(), foundSubscription.get().getCustomerId());
|
||||
assertEquals(testSubscription.getMonthlyAmount(), foundSubscription.get().getMonthlyAmount());
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(subscriptionId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return empty Optional when subscription ID doesn't exist")
|
||||
void testFindSubscriptionBySubscriptionIdNotFound() {
|
||||
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||
when(subscriptionRepository.findBySubscriptionId(nonExistentSubscriptionId)).thenReturn(Optional.empty());
|
||||
|
||||
Optional<SubscriptionDTO> foundSubscription = subscriptionUseCase.findSubscriptionBySubscriptionId(nonExistentSubscriptionId);
|
||||
|
||||
assertTrue(foundSubscription.isEmpty());
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(nonExistentSubscriptionId);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Update subscription tests")
|
||||
class UpdateSubscriptionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should update subscription when valid data is provided")
|
||||
void testUpdateSubscriptionWithValidData() throws SubscriptionNotFoundException, NotValidReviewException {
|
||||
when(subscriptionRepository.findBySubscriptionId(subscriptionId)).thenReturn(Optional.of(testSubscription));
|
||||
|
||||
LocalDate updateDdesiredStartDate = LocalDate.of(2026, 6, 24);
|
||||
Subscription updatedSubscription = Subscription.builder()
|
||||
.subscriptionId(subscriptionId)
|
||||
.customerId(customerId)
|
||||
.desiredSubscriptionDuration(desiredSubscriptionDuration)
|
||||
.paymentMethod(paymentMethod)
|
||||
.desiredStartDate(desiredStartDate)
|
||||
.monthlyAmount(monthlyAmount)
|
||||
.build();
|
||||
|
||||
when(subscriptionRepository.save(any(Subscription.class))).thenReturn(updatedSubscription);
|
||||
|
||||
SubscriptionInfo updateInfo = new SubscriptionInfo(DesiredSubscriptionDuration.SIX.getValue(), updateDdesiredStartDate);
|
||||
|
||||
SubscriptionDTO result = subscriptionUseCase.updateSubscription(subscriptionId, updateInfo, validPaymentMethodInfo);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(customerId, result.getCustomerId());
|
||||
assertEquals(monthlyAmount, result.getMonthlyAmount());
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(subscriptionId);
|
||||
verify(subscriptionRepository, times(1)).save(any(Subscription.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when subscription ID doesn't exist")
|
||||
void testUpdateSubscriptionNotFound() {
|
||||
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||
when(subscriptionRepository.findBySubscriptionId(nonExistentSubscriptionId)).thenReturn(Optional.empty());
|
||||
|
||||
SubscriptionInfo updateInfo = new SubscriptionInfo(desiredSubscriptionDuration, desiredStartDate);
|
||||
|
||||
assertThrows(SubscriptionNotFoundException.class,
|
||||
() -> subscriptionUseCase.updateSubscription(nonExistentSubscriptionId, updateInfo, validPaymentMethodInfo));
|
||||
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(nonExistentSubscriptionId);
|
||||
verify(subscriptionRepository, never()).save(any(Subscription.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when update data is not valid")
|
||||
void testUpdateSubscriptionWithInvalidData() {
|
||||
SubscriptionInfo invalidUpdateInfo = new SubscriptionInfo(0, desiredStartDate);
|
||||
|
||||
assertThrows(NotValidSubscriptionException.class,
|
||||
() -> subscriptionUseCase.updateSubscription(subscriptionId, invalidUpdateInfo, validPaymentMethodInfo));
|
||||
|
||||
verify(subscriptionRepository, never()).findBySubscriptionId(any(UUID.class));
|
||||
verify(subscriptionRepository, never()).save(any(Subscription.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Delete subscription tests")
|
||||
class DeleteSubscriptionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("Should delete subscriptions when customer ID exists")
|
||||
void testDeleteSubscriptionsOfACustomer() throws SubscriptionNotFoundException {
|
||||
when(subscriptionRepository.FindByCustomerId(customerId)).thenReturn(List.of(testSubscription));
|
||||
doNothing().when(subscriptionRepository).delete(testSubscription);
|
||||
|
||||
subscriptionUseCase.deleteSubscriptionsOfACustomer(customerId);
|
||||
|
||||
verify(subscriptionRepository, times(1)).FindByCustomerId(customerId);
|
||||
verify(subscriptionRepository, times(1)).delete(testSubscription);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when customer ID doesn't exist")
|
||||
void testDeleteSubscriptionsOfACustomerNotFound() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
when(subscriptionRepository.FindByCustomerId(nonExistentCustomerId)).thenReturn(List.of());
|
||||
|
||||
assertThrows(SubscriptionNotFoundException.class,
|
||||
() -> subscriptionUseCase.deleteSubscriptionsOfACustomer(nonExistentCustomerId));
|
||||
|
||||
verify(subscriptionRepository, times(1)).FindByCustomerId(nonExistentCustomerId);
|
||||
verify(subscriptionRepository, never()).delete(any(Subscription.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should delete subscription when subscription ID exist")
|
||||
void testDeleteSubscription() throws SubscriptionNotFoundException {
|
||||
when(subscriptionRepository.findBySubscriptionId(subscriptionId)).thenReturn(Optional.of(testSubscription));
|
||||
doNothing().when(subscriptionRepository).delete(testSubscription);
|
||||
|
||||
subscriptionUseCase.deleteSubscription(subscriptionId);
|
||||
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(subscriptionId);
|
||||
verify(subscriptionRepository, times(1)).delete(testSubscription);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw exception when subscription ID doesn't exist")
|
||||
void testDeleteSubscriptionNotFound() {
|
||||
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||
when(subscriptionRepository.findBySubscriptionId(nonExistentSubscriptionId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(SubscriptionNotFoundException.class,
|
||||
() -> subscriptionUseCase.deleteSubscription(nonExistentSubscriptionId));
|
||||
|
||||
verify(subscriptionRepository, times(1)).findBySubscriptionId(nonExistentSubscriptionId);
|
||||
verify(subscriptionRepository, never()).delete(any(Subscription.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user