Skip to content

Commit

Permalink
[DE-676] Proforma invoices tests 2 (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-nedza authored Mar 5, 2024
1 parent 8bf6795 commit ce4007e
Show file tree
Hide file tree
Showing 10 changed files with 797 additions and 371 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.maxio.advancedbilling.controllers.proformainvoices;

import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.Component;
import com.maxio.advancedbilling.models.CreateSubscription;
import com.maxio.advancedbilling.models.CreateSubscriptionRequest;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.ProformaInvoice;
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 extends ProformaInvoicesTestBase {

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
ProformaInvoiceWithComponents invoiceWithData = createSignupProformaInvoice(customer);

// then
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)
);
}

private ProformaInvoiceWithComponents createSignupProformaInvoice(Customer customer) throws IOException, ApiException {
Component meteredComponent = TEST_SETUP.createMeteredComponent(productFamily, 11.5);
Component quantityBasedComponent = TEST_SETUP.createQuantityBasedComponent(productFamily.getId());

ProformaInvoice proformaInvoice = CLIENT.getProformaInvoicesController().createSignupProformaInvoice(
new CreateSubscriptionRequest(
getCreateSubscription(customer, meteredComponent, quantityBasedComponent)
)
);

return new ProformaInvoiceWithComponents(proformaInvoice, quantityBasedComponent, meteredComponent);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
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.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;
Expand All @@ -14,12 +11,7 @@

import java.io.IOException;

public class ProformaInvoicesControllerCreateTest {

private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final ProformaInvoicesController PROFORMA_INVOICES_CONTROLLER = CLIENT
.getProformaInvoicesController();
public class ProformaInvoicesControllerCreateTest extends ProformaInvoicesTestBase {

private static Customer customer;

Expand All @@ -35,13 +27,11 @@ static void teardown() throws IOException, ApiException {

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

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

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

import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.Component;
import com.maxio.advancedbilling.models.CreateSignupProformaPreviewInclude;
import com.maxio.advancedbilling.models.CreateSubscription;
import com.maxio.advancedbilling.models.CreateSubscriptionRequest;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.SignupProformaPreview;
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 extends ProformaInvoicesTestBase {

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
SignupProformaPreviewWithComponents previewWithData = previewSignupProformaInvoice(customer);

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

assertThat(previewWithData.preview().getNextProformaInvoice()).usingRecursiveComparison()
.ignoringFields("createdAt", "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)
);
}

private SignupProformaPreviewWithComponents previewSignupProformaInvoice(Customer customer) throws IOException, ApiException {
Component meteredComponent = TEST_SETUP.createMeteredComponent(productFamily, 11.5);
Component quantityBasedComponent = TEST_SETUP.createQuantityBasedComponent(productFamily.getId());

SignupProformaPreview signupProformaPreview = CLIENT.getProformaInvoicesController().previewSignupProformaInvoice(
CreateSignupProformaPreviewInclude.NEXT_PROFORMA_INVOICE,
new CreateSubscriptionRequest(
getCreateSubscription(customer, meteredComponent, quantityBasedComponent)
)
).getProformaInvoicePreview();

return new SignupProformaPreviewWithComponents(signupProformaPreview, quantityBasedComponent, meteredComponent);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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.Customer;
import com.maxio.advancedbilling.utils.TestSetup;
import com.maxio.advancedbilling.models.Subscription;
import com.maxio.advancedbilling.utils.TestTeardown;
import com.maxio.advancedbilling.utils.assertions.CommonAssertions;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -14,12 +13,7 @@

import java.io.IOException;

public class ProformaInvoicesControllerPreviewTest {

private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final ProformaInvoicesController PROFORMA_INVOICES_CONTROLLER = CLIENT
.getProformaInvoicesController();
public class ProformaInvoicesControllerPreviewTest extends ProformaInvoicesTestBase {

private static Customer customer;

Expand All @@ -35,13 +29,11 @@ static void teardown() throws IOException, ApiException {

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

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

@Test
Expand All @@ -60,4 +52,15 @@ void shouldReturn401WhenProvidingInvalidCredentials() {
);
}

private ProformaInvoiceWithComponents previewProformaInvoiceWithComponents(Customer customer) throws IOException, ApiException {
Component meteredComponent = TEST_SETUP.createMeteredComponent(productFamily, 11.5);
Component quantityBasedComponent = TEST_SETUP.createQuantityBasedComponent(productFamily.getId());
Subscription subscription = setupSubscription(customer, quantityBasedComponent, meteredComponent);

return new ProformaInvoiceWithComponents(
CLIENT.getProformaInvoicesController().previewProformaInvoice(subscription.getId()),
quantityBasedComponent, meteredComponent
);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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.Customer;
import com.maxio.advancedbilling.models.ProformaInvoice;
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;
Expand All @@ -17,12 +15,7 @@

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

public class ProformaInvoicesControllerReadTest {

private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final ProformaInvoicesController PROFORMA_INVOICES_CONTROLLER = CLIENT
.getProformaInvoicesController();
public class ProformaInvoicesControllerReadTest extends ProformaInvoicesTestBase {

private static Customer customer;

Expand All @@ -39,8 +32,7 @@ static void teardown() throws IOException, ApiException {
@Test
void shouldReadProformaInvoice() throws IOException, ApiException {
// given
ProformaInvoice createdProformaInvoice = new ProformaInvoicesCreator()
.createProformaInvoiceWithComponents(customer).invoice();
ProformaInvoice createdProformaInvoice = createProformaInvoiceWithComponents(customer).invoice();

// when
ProformaInvoice proformaInvoice = PROFORMA_INVOICES_CONTROLLER.readProformaInvoice(createdProformaInvoice.getUid());
Expand Down
Loading

0 comments on commit ce4007e

Please sign in to comment.