Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DE-578] Subscription Components tests part 1 #65

Merged
merged 10 commits into from
Dec 14, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.maxio.advancedbilling.controllers.subscriptioncomponents;

import com.maxio.advancedbilling.models.Component;
import com.maxio.advancedbilling.models.ComponentKind;
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.SubscriptionComponentAllocatedQuantityGetter;
import com.maxio.advancedbilling.utils.matchers.UsageQuantityGetter;

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

public class SubscriptionComponentsAssertions {

static void assertUsage(Usage usage, Component component, Subscription subscription,
int quantity, String memo) {
assertThat(usage.getId()).isNotNull();
assertThat(usage.getMemo()).isEqualTo(memo);
assertThat(usage.getCreatedAt()).isNotNull();
assertThat(usage.getPricePointId()).isEqualTo(component.getDefaultPricePointId());
assertThat(usage.getQuantity().match(new UsageQuantityGetter<Integer>())).isEqualTo(quantity);
assertThat(usage.getComponentId()).isEqualTo(component.getId());
assertThat(usage.getComponentHandle()).isEqualTo(component.getHandle());
assertThat(usage.getSubscriptionId()).isEqualTo(subscription.getId());
if (component.getKind() == ComponentKind.PREPAID_USAGE_COMPONENT) {
assertThat(usage.getOverageQuantity()).isEqualTo(quantity);
} else {
assertThat(usage.getOverageQuantity()).isNull();
}
}

static void assertSubscriptionComponentWithSubscriptionObject(SubscriptionComponent subscriptionComponent,
Component expectedComponent, Subscription subscription,
ProductFamily productFamily) {
assertSubscriptionComponent(subscriptionComponent, expectedComponent, subscription, productFamily);

assertThat(subscriptionComponent.getSubscription()).isNotNull();
assertThat(subscriptionComponent.getSubscription().getState()).isEqualTo(subscription.getState());
assertThat(subscriptionComponent.getSubscription().getUpdatedAt()).isNotNull();
assertThat(subscriptionComponent.getSubscriptionId()).isEqualTo(subscription.getId());
}

static void assertSubscriptionComponent(SubscriptionComponent subscriptionComponent, Component expectedComponent,
Subscription subscription, ProductFamily productFamily) {
assertThat(subscriptionComponent).isNotNull();
assertThat(subscriptionComponent.getId()).isNotNull();
assertThat(subscriptionComponent.getName()).isEqualTo(expectedComponent.getName());
assertThat(subscriptionComponent.getKind()).isEqualTo(expectedComponent.getKind());
assertThat(subscriptionComponent.getCurrency()).isEqualTo(subscription.getCurrency());
assertThat(subscriptionComponent.getPricingScheme()).isNull();
assertThat(subscriptionComponent.getComponentId()).isEqualTo(expectedComponent.getId());
assertThat(subscriptionComponent.getComponentHandle()).isEqualTo(expectedComponent.getHandle());
assertThat(subscriptionComponent.getAllowFractionalQuantities()).isEqualTo(expectedComponent.getAllowFractionalQuantities());
assertThat(subscriptionComponent.getRecurring()).isEqualTo(expectedComponent.getRecurring());
assertThat(subscriptionComponent.getUpgradeCharge()).isEqualTo(expectedComponent.getUpgradeCharge());
assertThat(subscriptionComponent.getDowngradeCredit()).isEqualTo(expectedComponent.getDowngradeCredit());
assertThat(subscriptionComponent.getDescription()).isEqualTo(expectedComponent.getDescription());
assertThat(subscriptionComponent.getArchivedAt()).isNull();
assertThat(subscriptionComponent.getPricePointId()).isNull();
assertThat(subscriptionComponent.getPricePointHandle()).isNull();
assertThat(subscriptionComponent.getPricePointName()).isNull();
assertThat(subscriptionComponent.getPricePointType()).isNull();
assertThat(subscriptionComponent.getUseSiteExchangeRate()).isNull();
assertThat(subscriptionComponent.getProductFamilyId()).isEqualTo(productFamily.getId());
assertThat(subscriptionComponent.getProductFamilyHandle()).isEqualTo(productFamily.getHandle());
assertThat(subscriptionComponent.getDisplayOnHostedPage()).isFalse();
assertThat(subscriptionComponent.getCreatedAt()).isNotNull();
assertThat(subscriptionComponent.getUpdatedAt()).isNotNull();
if (subscriptionComponent.getKind().equals(ComponentKind.QUANTITY_BASED_COMPONENT)) {
assertThat(subscriptionComponent.getUnitBalance()).isNull();
if (subscriptionComponent.getAllowFractionalQuantities()) {
assertThat(subscriptionComponent.getAllocatedQuantity()
.match(new SubscriptionComponentAllocatedQuantityGetter<String>())).isEqualTo("0.0");
} else {
assertThat(subscriptionComponent.getAllocatedQuantity()
.match(new SubscriptionComponentAllocatedQuantityGetter<Integer>())).isEqualTo(0);
}
} else if (subscriptionComponent.getKind().equals(ComponentKind.METERED_COMPONENT)) {
assertThat(subscriptionComponent.getUnitBalance()).isZero();
assertThat(subscriptionComponent.getAllocatedQuantity()).isNull();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.maxio.advancedbilling.controllers.subscriptioncomponents;

import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.SubscriptionComponentsController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.Component;
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;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static com.maxio.advancedbilling.controllers.subscriptioncomponents.SubscriptionComponentsAssertions.assertUsage;
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;

public class SubscriptionComponentsControllerCreateUsageTest {

private static final TestSetup TEST_SETUP = new TestSetup();
private static final SubscriptionComponentsController SUBSCRIPTION_COMPONENTS_CONTROLLER =
TestClient.createClient().getSubscriptionComponentsController();

private static ProductFamily productFamily;
private static Component meteredComponent;
private static Component prepaidComponent;
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);
meteredComponent = TEST_SETUP.createMeteredComponent(productFamily, 1.0);
prepaidComponent = TEST_SETUP.createPrepaidComponent(productFamily, 1.0);

customer = TEST_SETUP.createCustomer();
subscription = TEST_SETUP.createSubscription(customer, product);
}

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

@Test
void shouldCreateMeteredComponentUsage() throws IOException, ApiException {
// given
Usage usage = SUBSCRIPTION_COMPONENTS_CONTROLLER.createUsage(subscription.getId(),
CreateUsageComponentId.fromNumber(meteredComponent.getId()),
new CreateUsageRequest(
new CreateUsage.Builder()
.quantity(10.0)
.memo("created usage")
.build()
)).getUsage();

// then
assertUsage(usage, meteredComponent, subscription, 10, "created usage");
}

@Test
void shouldCreatePrepaidComponentUsage() 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();

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

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

// when - then
assertThatErrorListResponse(() -> SUBSCRIPTION_COMPONENTS_CONTROLLER.createUsage(
subscription.getId(), CreateUsageComponentId.fromNumber(quantityBasedComponent.getId()),
new CreateUsageRequest(
new CreateUsage.Builder().quantity(2.0).build()
))
)
.hasErrorCode(422)
.hasUnprocessableEntityMessage()
.hasErrors(
"Usages cannot be added to archived components.",
"Usages can only be added to metered or prepaid usage components."
);
}

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

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

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.maxio.advancedbilling.controllers.subscriptioncomponents;

import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.SubscriptionComponentsController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.Component;
import com.maxio.advancedbilling.models.CreditType;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.ListSubscriptionComponentsForSiteInput;
import com.maxio.advancedbilling.models.ListSubscriptionComponentsInclude;
import com.maxio.advancedbilling.models.ListSubscriptionComponentsInput;
import com.maxio.advancedbilling.models.Product;
import com.maxio.advancedbilling.models.ProductFamily;
import com.maxio.advancedbilling.models.Subscription;
import com.maxio.advancedbilling.models.SubscriptionComponent;
import com.maxio.advancedbilling.utils.TestSetup;
import com.maxio.advancedbilling.utils.TestTeardown;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.maxio.advancedbilling.controllers.subscriptioncomponents.SubscriptionComponentsAssertions.assertSubscriptionComponentWithSubscriptionObject;
import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized;
import static org.assertj.core.api.Assertions.assertThat;


public class SubscriptionComponentsControllerListForSiteTest {

private static final TestSetup TEST_SETUP = new TestSetup();
private static final SubscriptionComponentsController SUBSCRIPTION_COMPONENTS_CONTROLLER =
TestClient.createClient().getSubscriptionComponentsController();

private static ProductFamily productFamily;
private static Component component1;
private static Component component2;
private static Component component3;
private static Customer customer;
private static Subscription subscription;

@BeforeAll
static void setup() throws IOException, ApiException {
new TestTeardown().deleteSubscriptions();
new TestTeardown().archiveComponents();

productFamily = TEST_SETUP.createProductFamily();
Product product = TEST_SETUP.createProduct(productFamily);
component1 = TEST_SETUP.createQuantityBasedComponent(productFamily.getId(),
builder -> builder
.allowFractionalQuantities(true)
.upgradeCharge(CreditType.NONE)
.downgradeCredit(CreditType.FULL)
.recurring(true)
);
component2 = TEST_SETUP.createQuantityBasedComponent(productFamily.getId(),
builder -> builder
.upgradeCharge(CreditType.PRORATED)
.downgradeCredit(CreditType.PRORATED)
.recurring(false)
);
component3 = TEST_SETUP.createMeteredComponent(productFamily, 1.0);

customer = TEST_SETUP.createCustomer();
subscription = TEST_SETUP.createSubscription(customer, product);

// looks like list components for site might have some caching which can be invalidated by calling this endpoint first.
SUBSCRIPTION_COMPONENTS_CONTROLLER
.listSubscriptionComponents(new ListSubscriptionComponentsInput()
.toBuilder()
.subscriptionId(subscription.getId())
.include(ListSubscriptionComponentsInclude.SUBSCRIPTION)
.build()
);
}

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

@Test
void shouldReturnAllSubscriptionComponents() throws IOException, ApiException {
// when
List<SubscriptionComponent> componentResponseList = SUBSCRIPTION_COMPONENTS_CONTROLLER
.listSubscriptionComponentsForSite(new ListSubscriptionComponentsForSiteInput()
.toBuilder()
.include(ListSubscriptionComponentsInclude.SUBSCRIPTION)
.build()
).getSubscriptionsComponents();

// then
assertThat(componentResponseList).isNotNull();
assertThat(componentResponseList.size()).isEqualTo(3);
Map<Integer,SubscriptionComponent> componentMap = componentResponseList
.stream().collect(Collectors.toMap(SubscriptionComponent::getComponentId, Function.identity()));

assertSubscriptionComponentWithSubscriptionObject(componentMap.get(component1.getId()), component1, subscription, productFamily);
assertSubscriptionComponentWithSubscriptionObject(componentMap.get(component2.getId()), component2, subscription, productFamily);
assertSubscriptionComponentWithSubscriptionObject(componentMap.get(component3.getId()), component3, subscription, productFamily);
}

@Test
void shouldNotReadSubscriptionComponentWhenProvidingInvalidCredentials() {
// when-then
assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getSubscriptionComponentsController()
.listSubscriptionComponentsForSite(new ListSubscriptionComponentsForSiteInput()));
}

}
Loading