Skip to content

Commit

Permalink
[DE-626] Allocations tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-nedza committed Jan 4, 2024
1 parent c101520 commit b958c5a
Show file tree
Hide file tree
Showing 9 changed files with 692 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.maxio.advancedbilling.models.ComponentKind;
import com.maxio.advancedbilling.models.CreateAllocation;
import com.maxio.advancedbilling.models.CreditType;
import com.maxio.advancedbilling.models.PaymentForAllocation;
import com.maxio.advancedbilling.models.ProductFamily;
import com.maxio.advancedbilling.models.Subscription;
import com.maxio.advancedbilling.models.SubscriptionComponent;
import com.maxio.advancedbilling.models.Usage;
import com.maxio.advancedbilling.utils.matchers.AllocationPreviousQuantityGetter;
import com.maxio.advancedbilling.utils.matchers.AllocationQuantityGetter;
import com.maxio.advancedbilling.utils.matchers.SubscriptionComponentAllocatedQuantityGetter;
import com.maxio.advancedbilling.utils.matchers.UsageQuantityGetter;

Expand All @@ -34,33 +37,93 @@ static void assertUsage(Usage usage, Component component, Subscription subscript
}

static void assertAllocation(CreateAllocation createAllocation, Allocation responseAllocation,
Component component, Subscription subscription,
int quantity, String memo) {
//assertThat(responseAllocation.gettAllocationId()).isNotNull();
Component component, Subscription subscription) {
assertAllocation(createAllocation, responseAllocation, component, subscription, "0.0");
}

static void assertAllocation(CreateAllocation createAllocation, Allocation responseAllocation,
Component component, Subscription subscription, String previousAllocation) {
assertGenericAllocationFields(createAllocation, responseAllocation, component, subscription, previousAllocation);
assertThat(responseAllocation.getAccrueCharge()).isTrue();
assertThat(responseAllocation.getPayment()).isNull();
assertThat(responseAllocation.getUpgradeCharge()).isEqualTo(CreditType.PRORATED);
assertThat(responseAllocation.getDowngradeCredit()).isEqualTo(CreditType.NONE);
assertProrationScheme(responseAllocation.getProrationUpgradeScheme(), CreditType.PRORATED);
assertProrationScheme(responseAllocation.getProrationDowngradeScheme(), CreditType.NONE);
}

static void assertPrepaidComponentAllocation(CreateAllocation createAllocation, Allocation responseAllocation,
Component component, Subscription subscription) {

assertGenericAllocationFields(createAllocation, responseAllocation, component, subscription, "0.0");
assertThat(responseAllocation.getAccrueCharge()).isFalse();
assertThat(responseAllocation.getUpgradeCharge()).isEqualTo(CreditType.FULL);
assertThat(responseAllocation.getDowngradeCredit()).isEqualTo(CreditType.NONE);
assertProrationScheme(responseAllocation.getProrationUpgradeScheme(), CreditType.FULL);
assertProrationScheme(responseAllocation.getProrationDowngradeScheme(), CreditType.NONE);

assertThat(responseAllocation.getPayment()).isNotNull();
PaymentForAllocation allocationPayment = responseAllocation.getPayment().match(v -> v);
assertThat(allocationPayment.getId()).isNotNull();
assertThat((double) allocationPayment.getAmountInCents())
.isEqualTo(
createAllocation.getQuantity() *
Double.parseDouble(component.getUnitPrice()) *
100.0
);
assertThat(allocationPayment.getSuccess()).isTrue();
assertThat(allocationPayment.getMemo()).isEqualTo("Payment for: Prorated component allocation changes.");
}

private static void assertGenericAllocationFields(CreateAllocation createAllocation, Allocation responseAllocation,
Component component, Subscription subscription, String previousAllocation) {
assertThat(responseAllocation.getAllocationId()).isNotNull();
assertThat(responseAllocation.getComponentId()).isEqualTo(component.getId());
assertThat(responseAllocation.getSubscriptionId()).isEqualTo(subscription.getId());

// fractional will change here to string
assertThat(responseAllocation.getQuantity()).isEqualTo(component.getDefaultPricePointId());
assertThat(responseAllocation.getPreviousQuantity()).isEqualTo(component.getDefaultPricePointId());

assertThat(responseAllocation.getMemo()).isEqualTo(createAllocation.getMemo());
assertThat(responseAllocation.getTimestamp()).isNotNull();
assertThat(responseAllocation.getProrationUpgradeScheme()).isEqualTo("prorate-delay-capture");
assertThat(responseAllocation.getProrationDowngradeScheme()).isEqualTo("no-prorate");


assertThat(responseAllocation.getPricePointId()).isEqualTo(component.getDefaultPricePointId());
assertThat(responseAllocation.getPricePointName()).isEqualTo(component.getDefaultPricePointName());
assertThat(responseAllocation.getPricePointHandle()).isNotNull();
assertThat(responseAllocation.getPreviousPricePointId()).isEqualTo(component.getDefaultPricePointId());

assertThat(responseAllocation.getAccrueCharge()).isTrue();
assertThat(responseAllocation.getUpgradeCharge()).isEqualTo(CreditType.PRORATED);
assertThat(responseAllocation.getDowngradeCredit()).isEqualTo(CreditType.NONE);
//assertThat(responseAllocation.getCreatedAt()).isNotNull();
// maybe test with true? here or in
//assertThat(responseAllocation.getInitiateDunning()).isEqualTo(createAllocation.getInitiateDunning());
assertThat(responseAllocation.getPayment()).isNull();
assertThat(responseAllocation.getCreatedAt()).isNotNull();
if (createAllocation.getInitiateDunning() == null) {
assertThat(responseAllocation.getInitiateDunning()).isFalse();
} else {
assertThat(responseAllocation.getInitiateDunning()).isEqualTo(createAllocation.getInitiateDunning());
}

if (component.getAllowFractionalQuantities()) {
assertThat(responseAllocation.getQuantity()
.match(new AllocationQuantityGetter<String>()))
.isEqualTo(String.valueOf(createAllocation.getQuantity()));
assertThat(responseAllocation.getPreviousQuantity()
.match(new AllocationPreviousQuantityGetter<String>())).isEqualTo(previousAllocation);

} else {
assertThat(responseAllocation.getQuantity()
.match(new AllocationQuantityGetter<Integer>()))
.isEqualTo((int)createAllocation.getQuantity());
assertThat(responseAllocation.getPreviousQuantity()
.match(new AllocationPreviousQuantityGetter<Integer>()))
.isEqualTo((int)Double.parseDouble(previousAllocation));
}
}



private static void assertProrationScheme(String scheme, CreditType creditType) {
String expectedScheme = null;
switch (creditType) {
case FULL -> expectedScheme = "full-price-attempt-capture";
case PRORATED -> expectedScheme = "prorate-delay-capture";
case NONE -> expectedScheme = "no-prorate";
}
assertThat(scheme).isEqualTo(expectedScheme);
}

static void assertSubscriptionComponentWithSubscriptionObject(SubscriptionComponent subscriptionComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
import com.maxio.advancedbilling.models.Component;
import com.maxio.advancedbilling.models.CreateAllocation;
import com.maxio.advancedbilling.models.CreateAllocationRequest;
import com.maxio.advancedbilling.models.CreateUsage;
import com.maxio.advancedbilling.models.CreateUsageRequest;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.Product;
import com.maxio.advancedbilling.models.ProductFamily;
import com.maxio.advancedbilling.models.Subscription;
import com.maxio.advancedbilling.models.Usage;
import com.maxio.advancedbilling.models.containers.CreateUsageComponentId;
import com.maxio.advancedbilling.utils.TestSetup;
import com.maxio.advancedbilling.utils.TestTeardown;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -24,7 +20,7 @@
import java.io.IOException;

import static com.maxio.advancedbilling.controllers.subscriptioncomponents.SubscriptionComponentsAssertions.assertAllocation;
import static com.maxio.advancedbilling.controllers.subscriptioncomponents.SubscriptionComponentsAssertions.assertUsage;
import static com.maxio.advancedbilling.controllers.subscriptioncomponents.SubscriptionComponentsAssertions.assertPrepaidComponentAllocation;
import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound;
import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertThatErrorListResponse;
import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized;
Expand All @@ -38,15 +34,18 @@ public class SubscriptionComponentsControllerAllocateComponentTest {
private static ProductFamily productFamily;
private static Component quantityBasedComponent;
private static Component prepaidComponent;
private static Component onOffComponent;
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);
quantityBasedComponent = TEST_SETUP.createQuantityBasedComponent(productFamily.getId());
quantityBasedComponent = TEST_SETUP.createQuantityBasedComponent(productFamily.getId(),
b -> b.allowFractionalQuantities(true));
prepaidComponent = TEST_SETUP.createPrepaidComponent(productFamily, 1.0);
onOffComponent = TEST_SETUP.createOnOffComponent(productFamily.getId());

customer = TEST_SETUP.createCustomer();
subscription = TEST_SETUP.createSubscription(customer, product);
Expand All @@ -59,10 +58,9 @@ static void teardown() throws IOException, ApiException {

@Test
void shouldAllocateQuantityBasedComponent() throws IOException, ApiException {
int quantity = 5;
// given
CreateAllocation createAllocation = new CreateAllocation.Builder()
.quantity(5)
.quantity(5.4)
.memo("Recoding component purchase")
.build();

Expand All @@ -72,66 +70,93 @@ void shouldAllocateQuantityBasedComponent() throws IOException, ApiException {
new CreateAllocationRequest(createAllocation)).getAllocation();

// then
assertAllocation(usage, meteredComponent, subscription, 10, "created usage");
assertAllocation(createAllocation, allocationResponse, quantityBasedComponent, subscription);
}

@Test
void shouldCreatePrepaidComponentUsage() throws IOException, ApiException {
void shouldAllocateOnOffComponent() throws IOException, ApiException {
// given
Usage usage = SUBSCRIPTION_COMPONENTS_CONTROLLER.createUsage(subscription.getId(),
CreateUsageComponentId.fromNumber(prepaidComponent.getId()),
new CreateUsageRequest(
new CreateUsage.Builder()
.quantity(3.0)
.memo("created usage")
.build()
)).getUsage();
CreateAllocation createAllocation1 = new CreateAllocation.Builder()
.quantity(1)
.memo("Toggling component on.")
.build();
CreateAllocation createAllocation2 = new CreateAllocation.Builder()
.quantity(0)
.memo("Toggling component off.")
.build();

// when-then
Allocation allocationResponse1 = SUBSCRIPTION_COMPONENTS_CONTROLLER
.allocateComponent(subscription.getId(), onOffComponent.getId(),
new CreateAllocationRequest(createAllocation1)).getAllocation();
assertAllocation(createAllocation1, allocationResponse1, onOffComponent, subscription);

Allocation allocationResponse2 = SUBSCRIPTION_COMPONENTS_CONTROLLER
.allocateComponent(subscription.getId(), onOffComponent.getId(),
new CreateAllocationRequest(createAllocation2)).getAllocation();
assertAllocation(createAllocation2, allocationResponse2, onOffComponent, subscription, "1");
}

@Test
void shouldAllocatePrepaidComponent() throws IOException, ApiException {
// given
CreateAllocation createAllocation = new CreateAllocation.Builder()
.quantity(1)
.memo("Recoding component purchase")
.build();

// when
Allocation allocationResponse = SUBSCRIPTION_COMPONENTS_CONTROLLER
.allocateComponent(subscription.getId(), prepaidComponent.getId(),
new CreateAllocationRequest(createAllocation)).getAllocation();

// then
assertUsage(usage, prepaidComponent, subscription, 3, "created usage");
assertPrepaidComponentAllocation(createAllocation, allocationResponse, prepaidComponent, subscription);
}

@Test
void shouldNotCreateUsageForArchivedQuantityBasedComponent() throws IOException, ApiException {
void shouldNotAllocateComponentToTheSameQuantity() throws IOException, ApiException {
// given
Component quantityBasedComponent = TEST_SETUP.createQuantityBasedComponent(productFamily.getId());
TestClient.createClient().getComponentsController().archiveComponent(
productFamily.getId(),
String.valueOf(quantityBasedComponent.getId())
);
Component quantityBasedComponent2 = TEST_SETUP.createQuantityBasedComponent(productFamily.getId());

// when - then
assertThatErrorListResponse(() -> SUBSCRIPTION_COMPONENTS_CONTROLLER.createUsage(
subscription.getId(), CreateUsageComponentId.fromNumber(quantityBasedComponent.getId()),
new CreateUsageRequest(
new CreateUsage.Builder().quantity(2.0).build()
))
// when
CreateAllocation createAllocation = new CreateAllocation.Builder()
.quantity(1)
.build();
SUBSCRIPTION_COMPONENTS_CONTROLLER
.allocateComponent(subscription.getId(), quantityBasedComponent2.getId(),
new CreateAllocationRequest(createAllocation));

// then
assertThatErrorListResponse(() -> SUBSCRIPTION_COMPONENTS_CONTROLLER
.allocateComponent(subscription.getId(), quantityBasedComponent2.getId(),
new CreateAllocationRequest(createAllocation)).getAllocation()
)
.hasErrorCode(422)
.hasUnprocessableEntityMessage()
.isUnprocessableEntity()
.hasErrors(
"Usages cannot be added to archived components.",
"Usages can only be added to metered or prepaid usage components."
"One or more allocation changes are required."
);
}

@Test
void shouldNotCreateUsageWhenSubscriptionDoesNotExist() {
assertNotFound(() -> SUBSCRIPTION_COMPONENTS_CONTROLLER.createUsage(123,
CreateUsageComponentId.fromNumber(meteredComponent.getId()), null));
void shouldNotAllocateComponentWhenSubscriptionDoesNotExist() {
assertNotFound(() -> SUBSCRIPTION_COMPONENTS_CONTROLLER
.allocateComponent(123, quantityBasedComponent.getId(), null)
);
}

@Test
void shouldNotCreateUsageWhenComponentDoesNotExist() {
assertNotFound(() -> SUBSCRIPTION_COMPONENTS_CONTROLLER.createUsage(subscription.getId(),
CreateUsageComponentId.fromNumber(123), null));
void shouldNotAllocateNonExistentComponent() {
assertNotFound(() -> SUBSCRIPTION_COMPONENTS_CONTROLLER
.allocateComponent(subscription.getId(), 123, null)
);
}

@Test
void shouldNotCreateUsageWhenProvidingInvalidCredentials() {
void shouldNotAllocateComponentWhenProvidingInvalidCredentials() {
// when-then
assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getSubscriptionComponentsController()
.createUsage(subscription.getId(), CreateUsageComponentId.fromNumber(meteredComponent.getId()), null));
.allocateComponent(subscription.getId(), quantityBasedComponent.getId(), null));
}

}
Loading

0 comments on commit b958c5a

Please sign in to comment.