forked from pierront/mylibrary-template
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3deb17a4c7 | |||
| 655fad8fb2 |
+62
@@ -0,0 +1,62 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.subscription;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.subscription.entity.Subscription;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class SubscriptionRepository {
|
||||||
|
|
||||||
|
private final List<Subscription> subscriptions = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<Subscription> findAll() {
|
||||||
|
|
||||||
|
return subscriptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAll() {
|
||||||
|
|
||||||
|
subscriptions.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Subscription save(Subscription newSubscription) {
|
||||||
|
Optional<Subscription> optionalSubscriptionWithSameSubscriptionId = this.findBySubscriptionId(newSubscription.getSubscriptionId());
|
||||||
|
optionalSubscriptionWithSameSubscriptionId.ifPresent(subscriptions::remove);
|
||||||
|
this.subscriptions.add(newSubscription);
|
||||||
|
return newSubscription;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Subscription> FindByCustomerId(UUID customerUUID) {
|
||||||
|
return this.subscriptions.stream()
|
||||||
|
.filter(subscription -> subscription.getCustomerId().equals(customerUUID))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Subscription> findBySubscriptionId(UUID subscriptionUUID) {
|
||||||
|
return this.subscriptions.stream()
|
||||||
|
.filter(subscription -> subscription.getSubscriptionId().equals(subscriptionUUID))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsByCustomerId(UUID customerUUID) {
|
||||||
|
return this.subscriptions.stream()
|
||||||
|
.anyMatch(review -> review.getCustomerId().equals(customerUUID));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existsBySubscriptionId(UUID subscriptionUUID) {
|
||||||
|
return this.subscriptions.stream()
|
||||||
|
.anyMatch(subscription -> subscription.getSubscriptionId().equals(subscriptionUUID));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteByCustomerId(UUID customerUUID) {
|
||||||
|
|
||||||
|
this.subscriptions.removeIf(subscription -> subscription.getCustomerId().equals(customerUUID));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(Subscription subscription) {
|
||||||
|
|
||||||
|
this.subscriptions.remove(subscription);
|
||||||
|
}
|
||||||
|
}
|
||||||
+301
@@ -0,0 +1,301 @@
|
|||||||
|
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;
|
||||||
|
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.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class SubscriptionRepositoryTest {
|
||||||
|
|
||||||
|
private SubscriptionRepository repository;
|
||||||
|
private PaymentMethod paymentMethod;
|
||||||
|
private Subscription subscription1;
|
||||||
|
private Subscription subscription2;
|
||||||
|
private Subscription subscription3;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
repository = new SubscriptionRepository();
|
||||||
|
|
||||||
|
paymentMethod = PaymentMethod.builder()
|
||||||
|
.paymentType(PaymentType.CB.name())
|
||||||
|
.details("Maxime Lebreton")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
LocalDate desiredStartDate1 = LocalDate.of(2026, 6, 24);
|
||||||
|
subscription1 = Subscription.builder()
|
||||||
|
.customerId(UUID.randomUUID())
|
||||||
|
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||||
|
.paymentMethod(paymentMethod)
|
||||||
|
.desiredStartDate(desiredStartDate1)
|
||||||
|
.monthlyAmount(12.03)
|
||||||
|
.build();
|
||||||
|
subscription1.setRandomSubscriptionUUID();
|
||||||
|
subscription1.setDateSubscription();
|
||||||
|
|
||||||
|
UUID customCustomerId = UUID.randomUUID();
|
||||||
|
|
||||||
|
LocalDate desiredStartDate2 = LocalDate.of(2026, 7, 24);
|
||||||
|
subscription2 = Subscription.builder()
|
||||||
|
.customerId(customCustomerId)
|
||||||
|
.desiredSubscriptionDuration(DesiredSubscriptionDuration.SIX.getValue())
|
||||||
|
.paymentMethod(paymentMethod)
|
||||||
|
.desiredStartDate(desiredStartDate2)
|
||||||
|
.monthlyAmount(20)
|
||||||
|
.build();
|
||||||
|
subscription2.setRandomSubscriptionUUID();
|
||||||
|
subscription2.setDateSubscription();
|
||||||
|
|
||||||
|
LocalDate desiredStartDate3 = LocalDate.of(2026, 8, 24);
|
||||||
|
subscription3 = Subscription.builder()
|
||||||
|
.customerId(customCustomerId)
|
||||||
|
.desiredSubscriptionDuration(DesiredSubscriptionDuration.TWELVE.getValue())
|
||||||
|
.paymentMethod(paymentMethod)
|
||||||
|
.desiredStartDate(desiredStartDate3)
|
||||||
|
.monthlyAmount(30)
|
||||||
|
.build();
|
||||||
|
subscription3.setRandomSubscriptionUUID();
|
||||||
|
subscription3.setDateSubscription();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("New subecription should be empty")
|
||||||
|
void testNewRepositoryIsEmpty() {
|
||||||
|
List<Subscription> subscriptions = repository.findAll();
|
||||||
|
|
||||||
|
assertTrue(subscriptions.isEmpty());
|
||||||
|
assertEquals(0, subscriptions.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Save operations")
|
||||||
|
class SaveOperations {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should add a new customer")
|
||||||
|
void testSaveNewSubscription() {
|
||||||
|
Subscription savedSubscription = repository.save(subscription1);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(subscription1.getSubscriptionId(), savedSubscription.getSubscriptionId());
|
||||||
|
assertEquals(subscription1.getMonthlyAmount(), savedSubscription.getMonthlyAmount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Save should update existing subscription with same ID")
|
||||||
|
void testSaveUpdatesExistingSubscription() {
|
||||||
|
repository.save(subscription1);
|
||||||
|
|
||||||
|
UUID subscriptionId = subscription1.getSubscriptionId();
|
||||||
|
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||||
|
Subscription updatedSubscription = Subscription.builder()
|
||||||
|
.subscriptionId(subscriptionId)
|
||||||
|
.customerId(UUID.randomUUID())
|
||||||
|
.desiredSubscriptionDuration(DesiredSubscriptionDuration.SIX.getValue())
|
||||||
|
.paymentMethod(paymentMethod)
|
||||||
|
.desiredStartDate(desiredStartDate)
|
||||||
|
.monthlyAmount(20)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Subscription savedCustomer = repository.save(updatedSubscription);
|
||||||
|
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
assertEquals(subscriptionId, savedCustomer.getSubscriptionId());
|
||||||
|
assertEquals(6, savedCustomer.getDesiredSubscriptionDuration());
|
||||||
|
assertEquals(20, savedCustomer.getMonthlyAmount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
repository.save(subscription3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindAll should return all subscriptions")
|
||||||
|
void testFindAll() {
|
||||||
|
List<Subscription> subscriptions = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(3, subscriptions.size());
|
||||||
|
assertTrue(subscriptions.contains(subscription1));
|
||||||
|
assertTrue(subscriptions.contains(subscription2));
|
||||||
|
assertTrue(subscriptions.contains(subscription3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindBySubscriptionId should return subscription with matching ID")
|
||||||
|
void testFindBySubscriptionId() {
|
||||||
|
Optional<Subscription> foundSubscription = repository.findBySubscriptionId(subscription1.getSubscriptionId());
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isPresent());
|
||||||
|
assertEquals(subscription1.getStartDate(), foundSubscription.get().getStartDate());
|
||||||
|
assertEquals(subscription1.getMonthlyAmount(), foundSubscription.get().getMonthlyAmount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindBySubscriptionId should return empty Optional when ID doesn't exist")
|
||||||
|
void testFindBySubscriptionIdNotFound() {
|
||||||
|
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||||
|
|
||||||
|
Optional<Subscription> foundSubscription = repository.findBySubscriptionId(nonExistentSubscriptionId);
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindByCustomerId should return subscription with matching ID")
|
||||||
|
void testFindByCustomerId() {
|
||||||
|
List<Subscription> foundSubscriptions = repository.FindByCustomerId(subscription2.getCustomerId());
|
||||||
|
|
||||||
|
assertFalse(foundSubscriptions.isEmpty());
|
||||||
|
assertTrue(foundSubscriptions.contains(subscription2));
|
||||||
|
assertTrue(foundSubscriptions.contains(subscription3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("FindByCustomerId should return empty Optional when ID doesn't exist")
|
||||||
|
void testFindByCustomerIdNotFound() {
|
||||||
|
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||||
|
|
||||||
|
List<Subscription> foundSubscription = repository.FindByCustomerId(nonExistentCustomerId);
|
||||||
|
|
||||||
|
assertTrue(foundSubscription.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsBySubscriptionId should return true when ID exists")
|
||||||
|
void testExistsBySubscriptionIdExists() {
|
||||||
|
boolean exists = repository.existsBySubscriptionId(subscription1.getSubscriptionId());
|
||||||
|
|
||||||
|
assertTrue(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsBySubscriptionId should return false when ID doesn't exist")
|
||||||
|
void testExistsBySubscriptionIdNotExists() {
|
||||||
|
UUID nonExistentSubscriptionId = UUID.randomUUID();
|
||||||
|
|
||||||
|
boolean exists = repository.existsBySubscriptionId(nonExistentSubscriptionId);
|
||||||
|
|
||||||
|
assertFalse(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsByCustomerId should return true when ID exists")
|
||||||
|
void testExistsByCustomerIdExists() {
|
||||||
|
boolean exists = repository.existsByCustomerId(subscription1.getCustomerId());
|
||||||
|
|
||||||
|
assertTrue(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ExistsByCustomerId should return false when ID doesn't exist")
|
||||||
|
void testExistsByCustomerIdNotExists() {
|
||||||
|
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||||
|
|
||||||
|
boolean exists = repository.existsByCustomerId(nonExistentCustomerId);
|
||||||
|
|
||||||
|
assertFalse(exists);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Delete operations")
|
||||||
|
class DeleteOperations {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUpSubscriptions() {
|
||||||
|
repository.save(subscription1);
|
||||||
|
repository.save(subscription2);
|
||||||
|
repository.save(subscription3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should remove the specified subscription")
|
||||||
|
void testDelete() {
|
||||||
|
repository.delete(subscription1);
|
||||||
|
|
||||||
|
List<Subscription> subscriptions = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, subscriptions.size());
|
||||||
|
assertFalse(subscriptions.contains(subscription1));
|
||||||
|
assertTrue(subscriptions.contains(subscription2));
|
||||||
|
assertTrue(subscriptions.contains(subscription3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("DeleteAll should remove all subscriptions of a customer")
|
||||||
|
void testDeleteAllSubscriptionOfACustomer() {
|
||||||
|
repository.deleteByCustomerId(subscription2.getCustomerId());
|
||||||
|
|
||||||
|
List<Subscription> subscriptions = repository.findAll();
|
||||||
|
|
||||||
|
assertEquals(1, subscriptions.size());
|
||||||
|
assertTrue(subscriptions.contains(subscription1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("DeleteAll should remove all subscriptions")
|
||||||
|
void testDeleteAll() {
|
||||||
|
repository.deleteAll();
|
||||||
|
|
||||||
|
List<Subscription> subscriptions = repository.findAll();
|
||||||
|
|
||||||
|
assertTrue(subscriptions.isEmpty());
|
||||||
|
assertEquals(0, subscriptions.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Delete should not throw exception when customer doesn't exist")
|
||||||
|
void testDeleteNonExistentCustomer() {
|
||||||
|
LocalDate desiredStartDate = LocalDate.of(2026, 6, 24);
|
||||||
|
Subscription nonExistentsubscription = Subscription.builder()
|
||||||
|
.customerId(UUID.randomUUID())
|
||||||
|
.desiredSubscriptionDuration(DesiredSubscriptionDuration.THREE.getValue())
|
||||||
|
.paymentMethod(paymentMethod)
|
||||||
|
.desiredStartDate(desiredStartDate)
|
||||||
|
.monthlyAmount(12.03)
|
||||||
|
.build();
|
||||||
|
nonExistentsubscription.setRandomSubscriptionUUID();
|
||||||
|
nonExistentsubscription.setDateSubscription();
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> repository.delete(nonExistentsubscription));
|
||||||
|
|
||||||
|
assertEquals(3, repository.findAll().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user