Skip to content

Commit

Permalink
[DE-582] CRUD for Subscription Invoice Account endpoints Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
patryk-grudzien-keen committed Mar 7, 2024
1 parent afea64d commit 1e8369e
Show file tree
Hide file tree
Showing 3 changed files with 541 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.maxio.advancedbilling.controllers.subscriptioninvoiceaccount;

import com.maxio.advancedbilling.AdvancedBillingClient;
import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.SubscriptionInvoiceAccountController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.CreatePrepayment;
import com.maxio.advancedbilling.models.CreatePrepaymentMethod;
import com.maxio.advancedbilling.models.CreatePrepaymentRequest;
import com.maxio.advancedbilling.models.CreatedPrepayment;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.Subscription;
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 org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.stream.Stream;

import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound;
import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized;
import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnprocessableEntity;
import static org.assertj.core.api.Assertions.assertThat;

class SubscriptionInvoiceAccountCreatePrepaymentTest {
private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final SubscriptionInvoiceAccountController SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER =
CLIENT.getSubscriptionInvoiceAccountController();

private static Customer customer;
private static Subscription subscription;

@BeforeAll
static void setUp() throws IOException, ApiException {
customer = TEST_SETUP.createCustomer();
subscription = TEST_SETUP.createSubscription(customer, TEST_SETUP.createProduct(
TEST_SETUP.createProductFamily(),
b -> b.priceInCents(1250)
)
);
}

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

@Test
void shouldCreatePrepaymentForSubscription() throws IOException, ApiException {
// when
CreatedPrepayment prepayment = SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER
.createPrepayment(
subscription.getId(),
new CreatePrepaymentRequest(new CreatePrepayment.Builder()
.amount(100.0)
.details("John Doe signup for $100")
.memo("Signup for $100")
.method(CreatePrepaymentMethod.CHECK)
.build()
)
)
.getPrepayment();

// then
assertThat(prepayment.getAdditionalProperties()).isEmpty();
assertThat(prepayment.getId()).isNotNull();
assertThat(prepayment.getSubscriptionId()).isEqualTo(subscription.getId());
assertThat(prepayment.getAmountInCents()).isEqualTo(10000L);
assertThat(prepayment.getMemo()).isEqualTo("Signup for $100");
assertThat(prepayment.getCreatedAt()).isBeforeOrEqualTo(ZonedDateTime.now());
assertThat(prepayment.getStartingBalanceInCents()).isEqualTo(0L);
assertThat(prepayment.getEndingBalanceInCents()).isEqualTo(-10000L);
}

@ParameterizedTest
@MethodSource("argsForShouldReturn422WhenRequestBodyIsIncorrect")
void shouldReturn422WhenRequestBodyIsIncorrect(CreatePrepaymentRequest request, String expectedErrors) {
// when - then
assertUnprocessableEntity(
ApiException.class,
() -> SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER.createPrepayment(subscription.getId(), request),
e -> JSONAssert.assertEquals(expectedErrors, e.getHttpContext().getResponse().getBody(), JSONCompareMode.STRICT)
);
}

private static Stream<Arguments> argsForShouldReturn422WhenRequestBodyIsIncorrect() {
return Stream.of(
Arguments.arguments(
new CreatePrepaymentRequest(),
"{\"errors\":{\"prepayment\":\"can't be blank\"}}"
),
Arguments.arguments(
new CreatePrepaymentRequest(new CreatePrepayment()),
"{\"errors\":[\"Method: can't be blank\",\"Memo: can't be blank\",\"Details: can't be blank\"]}"
)
);
}

@Test
void shouldReturn404ForNotExistentSubscription() {
// when - then
assertNotFound(() -> SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER
.createPrepayment(123, new CreatePrepaymentRequest(new CreatePrepayment()))
);
}

@Test
void shouldReturn401WhenProvidingInvalidCredentials() {
// when - then
assertUnauthorized(() -> TestClient.createInvalidCredentialsClient()
.getSubscriptionInvoiceAccountController()
.createPrepayment(123, new CreatePrepaymentRequest(new CreatePrepayment()))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.maxio.advancedbilling.controllers.subscriptioninvoiceaccount;

import com.maxio.advancedbilling.AdvancedBillingClient;
import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.SubscriptionInvoiceAccountController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.CreatePrepayment;
import com.maxio.advancedbilling.models.CreatePrepaymentMethod;
import com.maxio.advancedbilling.models.CreatePrepaymentRequest;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.ListPrepaymentsInput;
import com.maxio.advancedbilling.models.Prepayment;
import com.maxio.advancedbilling.models.PrepaymentMethod;
import com.maxio.advancedbilling.models.Product;
import com.maxio.advancedbilling.models.Subscription;
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 static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertNotFound;
import static com.maxio.advancedbilling.utils.assertions.CommonAssertions.assertUnauthorized;
import static org.assertj.core.api.Assertions.assertThat;

class SubscriptionInvoiceAccountListPrepaymentsTest {
private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final SubscriptionInvoiceAccountController SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER =
CLIENT.getSubscriptionInvoiceAccountController();

private static Product product;
private static Customer customer;

@BeforeAll
static void setUp() throws IOException, ApiException {
product = TEST_SETUP.createProduct(TEST_SETUP.createProductFamily());
customer = TEST_SETUP.createCustomer();
}

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

@Test
void shouldReturnListOfPrepayments() throws IOException, ApiException {
// given
Integer subscriptionId = TEST_SETUP.createSubscription(customer, product).getId();
createCheckPrepaymentForSignup(subscriptionId);
createCashPrepaymentForLicence(subscriptionId);

// when
List<Prepayment> prepayments = SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER
.listPrepayments(new ListPrepaymentsInput.Builder(subscriptionId).build())
.getPrepayments();

// then
assertThat(prepayments)
.hasSize(2)
.usingRecursiveFieldByFieldElementComparatorIgnoringFields("id", "createdAt")
.containsExactlyInAnyOrder(
new Prepayment.Builder()
.subscriptionId(subscriptionId)
.amountInCents(5000L)
.remainingAmountInCents(5000L)
.refundedAmountInCents(0L)
.details("John Doe licence for $50")
.external(true)
.memo("Licence for $50")
.paymentType(PrepaymentMethod.CASH)
.build(),
new Prepayment.Builder()
.subscriptionId(subscriptionId)
.amountInCents(1000L)
.remainingAmountInCents(1000L)
.refundedAmountInCents(0L)
.details("John Doe signup for $10")
.external(true)
.memo("Signup for $10")
.paymentType(PrepaymentMethod.CHECK)
.build()
);
}

@Test
void shouldReturnListOfPrepaymentsUsingPagination() throws IOException, ApiException {
// given
Integer subscriptionId = TEST_SETUP
.createSubscription(customer, TEST_SETUP.createProduct(TEST_SETUP.createProductFamily()))
.getId();
createCheckPrepaymentForSignup(subscriptionId);
createCashPrepaymentForLicence(subscriptionId);

// when - then
assertThat(getSinglePrepaymentOnPage(subscriptionId, 1)).hasSize(1);
assertThat(getSinglePrepaymentOnPage(subscriptionId, 2)).hasSize(1);
assertThat(getSinglePrepaymentOnPage(subscriptionId, 3)).isEmpty();
}

@Test
void shouldReturnEmptyResponseWhenNoPrepaymentsExist() throws IOException, ApiException {
// given
Subscription subscription = TEST_SETUP.createSubscription(customer, product);

// when - then
assertThat(SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER
.listPrepayments(new ListPrepaymentsInput.Builder(subscription.getId()).build())
.getPrepayments()
).isEmpty();
}

@Test
void shouldReturn404ForNotExistentSubscription() {
// when - then
assertNotFound(() -> SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER
.listPrepayments(new ListPrepaymentsInput.Builder(123).build())
);
}

@Test
void shouldReturn401WhenProvidingInvalidCredentials() {
// when - then
assertUnauthorized(() -> TestClient.createInvalidCredentialsClient()
.getSubscriptionInvoiceAccountController()
.listPrepayments(new ListPrepaymentsInput.Builder(123).build())
);
}

private static List<Prepayment> getSinglePrepaymentOnPage(int subscriptionId, int page) throws ApiException, IOException {
return SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER
.listPrepayments(new ListPrepaymentsInput.Builder()
.subscriptionId(subscriptionId)
.page(page)
.perPage(1)
.build()
)
.getPrepayments();
}

private static void createCheckPrepaymentForSignup(int subscriptionId) throws IOException, ApiException {
createPrepayment(subscriptionId, new CreatePrepayment.Builder()
.amount(10.0)
.details("John Doe signup for $10")
.memo("Signup for $10")
.method(CreatePrepaymentMethod.CHECK)
.build()
);
}

private static void createCashPrepaymentForLicence(int subscriptionId) throws IOException, ApiException {
createPrepayment(subscriptionId, new CreatePrepayment.Builder()
.amount(50.0)
.details("John Doe licence for $50")
.memo("Licence for $50")
.method(CreatePrepaymentMethod.CASH)
.build()
);
}

private static void createPrepayment(int subscriptionId, CreatePrepayment createPrepayment) throws IOException, ApiException {
// given
SUBSCRIPTION_INVOICE_ACCOUNT_CONTROLLER
.createPrepayment(
subscriptionId,
new CreatePrepaymentRequest(createPrepayment)
);
}
}
Loading

0 comments on commit 1e8369e

Please sign in to comment.