Skip to content

Commit

Permalink
[DE-676] Proforma invoices tests 2
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-nedza committed Mar 1, 2024
1 parent 68b7d87 commit 48f4cfc
Show file tree
Hide file tree
Showing 6 changed files with 453 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.maxio.advancedbilling.controllers.proformainvoices;

import com.maxio.advancedbilling.AdvancedBillingClient;
import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.ProformaInvoicesController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.CreateSubscription;
import com.maxio.advancedbilling.models.CreateSubscriptionRequest;
import com.maxio.advancedbilling.models.Customer;
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.util.List;
import java.util.Map;

public class ProformaInvoicesControllerCreateSignupProformaTest {

private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final ProformaInvoicesController PROFORMA_INVOICES_CONTROLLER = CLIENT
.getProformaInvoicesController();

private static Customer customer;

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

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

@Test
void shouldCreateSignupProforma() throws IOException, ApiException {
// given-when
ProformaInvoicesCreator proformaInvoicesCreator = new ProformaInvoicesCreator();
ProformaInvoicesCreator.ProformaInvoiceWithComponents invoiceWithData =
proformaInvoicesCreator.createSignupProformaInvoice(customer);

// then
proformaInvoicesCreator.assertProformaInvoice(customer, invoiceWithData, true, true);
}

@Test
void shouldReturn422WhenCreatingProformaInvoiceWithInvalidData() {
// when - then
CommonAssertions
.assertThatErrorArrayMapResponse(() -> PROFORMA_INVOICES_CONTROLLER.createSignupProformaInvoice(
new CreateSubscriptionRequest(new CreateSubscription.Builder()
.productId(11)
.build()
)
)
)
.isUnprocessableEntity()
.hasErrorMap(Map.of("base", List.of("Couldn't find Product by 11")));
}

@Test
void shouldReturn401WhenProvidingInvalidCredentials() {
// when - then
CommonAssertions.assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getProformaInvoicesController()
.createSignupProformaInvoice(null)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ void shouldCreateProformaInvoice() throws IOException, ApiException {
proformaInvoicesCreator.createProformaInvoiceWithComponents(customer);

// then
proformaInvoicesCreator.assertProformaInvoice(customer, invoiceWithData, true);
proformaInvoicesCreator.assertProformaInvoice(customer, invoiceWithData,
true, false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.maxio.advancedbilling.controllers.proformainvoices;

import com.maxio.advancedbilling.AdvancedBillingClient;
import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.ProformaInvoicesController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.CreateSubscription;
import com.maxio.advancedbilling.models.CreateSubscriptionRequest;
import com.maxio.advancedbilling.models.Customer;
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.util.List;
import java.util.Map;

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

public class ProformaInvoicesControllerPreviewSignupProformaTest {

private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final ProformaInvoicesController PROFORMA_INVOICES_CONTROLLER = CLIENT
.getProformaInvoicesController();

private static Customer customer;

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

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

@Test
void shouldPreviewSignupProformaWithNextProforma() throws IOException, ApiException {
// given-when
ProformaInvoicesCreator proformaInvoicesCreator = new ProformaInvoicesCreator();
ProformaInvoicesCreator.SignupProformaPreviewWithComponents previewWithData =
proformaInvoicesCreator.previewSignupProformaInvoice(customer);

// then
proformaInvoicesCreator.assertProformaInvoice(customer,
new ProformaInvoicesCreator.ProformaInvoiceWithComponents(previewWithData.preview().getCurrentProformaInvoice(),
previewWithData.quantityBasedComponent(), previewWithData.meteredComponent()),
false, true);

assertThat(previewWithData.preview().getNextProformaInvoice()).usingRecursiveComparison()
.ignoringFields("subtotalAmount", "totalAmount", "uid", "lineItems", "deliveryDate", "dueAmount")
.isEqualTo(previewWithData.preview().getCurrentProformaInvoice());
}

@Test
void shouldReturn422WhenCreatingProformaInvoiceWithInvalidData() {
// when - then
CommonAssertions
.assertThatErrorArrayMapResponse(() -> PROFORMA_INVOICES_CONTROLLER.previewSignupProformaInvoice(
null,
new CreateSubscriptionRequest(new CreateSubscription.Builder()
.productId(11)
.build()
)
)
)
.isUnprocessableEntity()
.hasErrorMap(Map.of("base", List.of("Couldn't find Product by 11")));
}

@Test
void shouldReturn401WhenProvidingInvalidCredentials() {
// when - then
CommonAssertions.assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getProformaInvoicesController()
.previewSignupProformaInvoice(null, null)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ void shouldPreviewProformaInvoice() throws IOException, ApiException {
proformaInvoicesCreator.previewProformaInvoiceWithComponents(customer);

// then
proformaInvoicesCreator.assertProformaInvoice(customer, invoiceWithData, false);
proformaInvoicesCreator.assertProformaInvoice(customer, invoiceWithData,
false, false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.maxio.advancedbilling.controllers.proformainvoices;

import com.maxio.advancedbilling.AdvancedBillingClient;
import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.ProformaInvoicesController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.Component;
import com.maxio.advancedbilling.models.ListProformaInvoicesResponse;
import com.maxio.advancedbilling.models.Product;
import com.maxio.advancedbilling.models.ProductFamily;
import com.maxio.advancedbilling.models.SubscriptionGroupSignupResponse;
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 static org.assertj.core.api.Assertions.assertThat;

/**
* This class tests both `Create Consolidated Proforma Invoices` and `List Subscription Group Proforma Invoices`
* operations, as the Creation response is asynchronous and has no specific response, but it's needed to execute listing.
*/
public class ProformaInvoicesControllerSubscriptionGroupsTest {

private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final ProformaInvoicesController PROFORMA_INVOICES_CONTROLLER = CLIENT
.getProformaInvoicesController();

private static SubscriptionGroupSignupResponse groupSignup;

@BeforeAll
static void setUp() throws IOException, ApiException, InterruptedException {
ProductFamily productFamily = TEST_SETUP.createProductFamily();
Product product = TEST_SETUP.createProduct(productFamily, b -> b.priceInCents(1250));
Component meteredComponent = TEST_SETUP.createMeteredComponent(productFamily, 11.5);

groupSignup = TEST_SETUP.signupWithSubscriptionGroup(product, meteredComponent);
PROFORMA_INVOICES_CONTROLLER.createConsolidatedProformaInvoice(groupSignup.getUid());

Thread.sleep(5000);
}

@AfterAll
static void teardown() throws IOException, ApiException {
TestTeardown testTeardown = new TestTeardown();
testTeardown.deleteSubscriptionGroup(groupSignup);
}

@Test
void shouldListGroupProformas() throws IOException, ApiException {
// given-when
ListProformaInvoicesResponse listProformaInvoicesResponse = PROFORMA_INVOICES_CONTROLLER
.listSubscriptionGroupProformaInvoices(groupSignup.getUid());

// then
assertThat(listProformaInvoicesResponse.getProformaInvoices()).isNotEmpty();
}

@Test
void shouldReturn404WhenCreatingConsolidatedProformaInvoiceForNonGroup() {
// when - then
CommonAssertions.assertNotFound(
() -> PROFORMA_INVOICES_CONTROLLER.createConsolidatedProformaInvoice("123")
);
}

@Test
void shouldReturn401WhenProvidingInvalidCredentials() {
// when - then
CommonAssertions.assertUnauthorized(() -> TestClient.createInvalidCredentialsClient().getProformaInvoicesController()
.createConsolidatedProformaInvoice("123")
);
}

}
Loading

0 comments on commit 48f4cfc

Please sign in to comment.