Skip to content

Commit

Permalink
[DE-117] DE-545 Tests for "Override Subscription"
Browse files Browse the repository at this point in the history
  • Loading branch information
patryk-grudzien-keen committed Dec 14, 2023
1 parent c76b6f8 commit c4d3198
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.maxio.advancedbilling.controllers.subscriptions;

import com.maxio.advancedbilling.AdvancedBillingClient;
import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.SubscriptionsController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.exceptions.SingleErrorResponseErrorException;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.OverrideSubscription;
import com.maxio.advancedbilling.models.OverrideSubscriptionRequest;
import com.maxio.advancedbilling.models.Product;
import com.maxio.advancedbilling.models.ProductFamily;
import com.maxio.advancedbilling.models.Subscription;
import com.maxio.advancedbilling.utils.TestSetup;
import com.maxio.advancedbilling.utils.TestTeardown;
import com.maxio.advancedbilling.utils.assertions.CommonAssertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.Random;

import static org.assertj.core.api.Assertions.assertThat;

class SubscriptionsControllerOverrideTest {

private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final SubscriptionsController SUBSCRIPTIONS_CONTROLLER = CLIENT.getSubscriptionsController();

private static ProductFamily productFamily;
private static Customer customer;
private static Subscription subscription;

@BeforeAll
static void setUp() throws IOException, ApiException {
productFamily = TEST_SETUP.createProductFamily();
Product product = TEST_SETUP.createProduct(productFamily);
customer = TEST_SETUP.createCustomer();
subscription = TEST_SETUP.createSubscription(customer, product, b -> b.nextBillingAt(ZonedDateTime.now().plusDays(7)));
}

@AfterAll
static void teardown() throws IOException, ApiException {
new TestTeardown().deleteCustomer(customer);
}

@Test
void shouldReturn404WhenSubscriptionNotExists() {
// when - then
CommonAssertions.assertNotFound(() -> SUBSCRIPTIONS_CONTROLLER
.overrideSubscription(123, new OverrideSubscriptionRequest())
);
}

@Test
void shouldReturn422WhenProvidedCurrentPeriodStartsAtIsAfterTheCurrentDatetime() {
// when - then
CommonAssertions.assertUnprocessableEntity(
SingleErrorResponseErrorException.class,
() -> SUBSCRIPTIONS_CONTROLLER.overrideSubscription(
subscription.getId(),
new OverrideSubscriptionRequest(new OverrideSubscription.Builder()
.currentPeriodStartsAt(ZonedDateTime.now().plusDays(1))
.build()
)
),
e -> assertThat(e.getError()).isEqualTo("Current period starts at: must be before current date and time")
);
}

@Test
void shouldReturn422WhenSubscriptionIsAlreadyBilled() throws IOException, ApiException {
// given
Product product = TEST_SETUP.createProduct(productFamily, b -> b.priceInCents(new Random().nextInt(1, 100000)));
Subscription subscription = TEST_SETUP.createSubscription(customer, product);

// when - then
CommonAssertions.assertUnprocessableEntity(
SingleErrorResponseErrorException.class,
() -> SUBSCRIPTIONS_CONTROLLER.overrideSubscription(
subscription.getId(),
new OverrideSubscriptionRequest(new OverrideSubscription.Builder()
.currentPeriodStartsAt(ZonedDateTime.now().minusDays(1))
.build()
)
),
e -> assertThat(e.getError()).isEqualTo("Current period starts at: can only be updated if subscription has no statements or invoices")
);
}

@Test
void shouldOverrideSubscription() throws IOException, ApiException {
// when
SUBSCRIPTIONS_CONTROLLER.overrideSubscription(
subscription.getId(),
new OverrideSubscriptionRequest(new OverrideSubscription.Builder()
.activatedAt(ZonedDateTime.parse("2020-01-01T00:00:00.000Z"))
.expiresAt(ZonedDateTime.parse("2020-12-31T23:59:59.000Z"))
.currentPeriodStartsAt(ZonedDateTime.parse("2020-01-05T00:00:00.000Z"))
.build()
)
);

// then
Subscription overriddenSubscription = SUBSCRIPTIONS_CONTROLLER
.readSubscription(subscription.getId(), null)
.getSubscription();
assertThat(overriddenSubscription.getActivatedAt()).isEqualTo("2020-01-01T00:00:00.000Z");
assertThat(overriddenSubscription.getExpiresAt()).isEqualTo("2020-12-31T23:59:59.000Z");
assertThat(overriddenSubscription.getCurrentPeriodStartedAt()).isEqualTo("2020-01-05T00:00:00.000Z");
}
}
30 changes: 18 additions & 12 deletions tests/src/test/java/com/maxio/advancedbilling/utils/TestSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,25 @@ public ProductFamily createProductFamily() throws IOException, ApiException {
}

public Product createProduct(ProductFamily productFamily) throws IOException, ApiException {
return createProduct(productFamily, b -> {
});
}

public Product createProduct(ProductFamily productFamily, Consumer<CreateOrUpdateProduct.Builder> customizer)
throws IOException, ApiException {
String productName = "My Super Product " + randomNumeric(5);
String handle = productName.toLowerCase().replace(" ", "-");

CreateOrUpdateProduct.Builder builder = new CreateOrUpdateProduct.Builder()
.name(productName)
.handle(handle)
.intervalUnit(IntervalUnit.MONTH)
.interval(1)
.requireCreditCard(false);
customizer.accept(builder);

return advancedBillingClient.getProductsController()
.createProduct(
productFamily.getId(),
new CreateOrUpdateProductRequest(
new CreateOrUpdateProduct.Builder()
.name(productName)
.handle(handle)
.intervalUnit(IntervalUnit.MONTH)
.interval(1)
.requireCreditCard(false)
.build()
))
.createProduct(productFamily.getId(), new CreateOrUpdateProductRequest(builder.build()))
.getProduct();
}

Expand Down Expand Up @@ -280,7 +285,8 @@ public Subscription createSubscription(Customer customer, Product product, Consu
.cvv("123")
.expirationMonth(PaymentProfileAttributesExpirationMonth.fromNumber(5))
.expirationYear(PaymentProfileAttributesExpirationYear.fromNumber(2050))
.build());
.build()
);
customizer.accept(builder);
return advancedBillingClient.getSubscriptionsController()
.createSubscription(new CreateSubscriptionRequest(builder.build()))
Expand Down

0 comments on commit c4d3198

Please sign in to comment.