Skip to content

Commit

Permalink
[DE-117] DE-570 - Java - test CRUD for Payment Profiles endpoints par…
Browse files Browse the repository at this point in the history
…t 1 (#81)
  • Loading branch information
patryk-grudzien-keen authored Jan 18, 2024
1 parent ba0d41e commit 0829970
Show file tree
Hide file tree
Showing 7 changed files with 622 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.maxio.advancedbilling.controllers.paymentprofiles;

import com.maxio.advancedbilling.AdvancedBillingClient;
import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.PaymentProfilesController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.CardType;
import com.maxio.advancedbilling.models.CreatePaymentProfile;
import com.maxio.advancedbilling.models.CreatePaymentProfileRequest;
import com.maxio.advancedbilling.models.CreatedPaymentProfile;
import com.maxio.advancedbilling.models.CurrentVault;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.containers.CreatePaymentProfileExpirationMonth;
import com.maxio.advancedbilling.models.containers.CreatePaymentProfileExpirationYear;
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;
import static org.junit.jupiter.api.Assertions.assertAll;

class PaymentProfilesControllerCreateTest {
private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final PaymentProfilesController PAYMENT_PROFILES_CONTROLLER = CLIENT.getPaymentProfilesController();

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 shouldCreatePaymentProfileForCustomer() throws IOException, ApiException {
// when
CreatedPaymentProfile paymentProfile = PAYMENT_PROFILES_CONTROLLER
.createPaymentProfile(new CreatePaymentProfileRequest(new CreatePaymentProfile.Builder()
.customerId(customer.getId())
.expirationMonth(CreatePaymentProfileExpirationMonth.fromNumber(12))
.expirationYear(CreatePaymentProfileExpirationYear.fromNumber(2030))
.fullNumber("4111111111111111")
.build())
)
.getPaymentProfile();

// then
assertAll(
() -> assertThat(paymentProfile.getId()).isNotNull(),
() -> assertThat(paymentProfile.getFirstName()).isEqualTo(customer.getFirstName()),
() -> assertThat(paymentProfile.getLastName()).isEqualTo(customer.getLastName()),
() -> assertThat(paymentProfile.getMaskedCardNumber()).isEqualTo("XXXX-XXXX-XXXX-1111"),
() -> assertThat(paymentProfile.getCardType()).isEqualTo(CardType.VISA),
() -> assertThat(paymentProfile.getExpirationMonth()).isEqualTo(12),
() -> assertThat(paymentProfile.getExpirationYear()).isEqualTo(2030),
() -> assertThat(paymentProfile.getCustomerId()).isEqualTo(customer.getId()),
() -> assertThat(paymentProfile.getCurrentVault()).isEqualTo(CurrentVault.BOGUS),
() -> assertThat(paymentProfile.getVaultToken()).isNotNull(),
() -> assertThat(paymentProfile.getCustomerVaultToken()).isNull(),
() -> assertThat(paymentProfile.getBillingAddress2()).isNull(),
() -> assertThat(paymentProfile.getPaymentType()).isEqualTo("credit_card"),
() -> assertThat(paymentProfile.getDisabled()).isFalse()
);
}

@Test
void shouldReturn404WhenCreatingPaymentProfileForNotExistentCustomer() {
// when - then
CommonAssertions.assertNotFound(
() -> PAYMENT_PROFILES_CONTROLLER.createPaymentProfile(
new CreatePaymentProfileRequest(new CreatePaymentProfile.Builder().customerId(123).build()))
);
}

@Test
void shouldReturn422WhenRequiredParamsAreNotProvidedForCreditCard() {
// when - then
CommonAssertions
.assertThatErrorListResponse(() -> PAYMENT_PROFILES_CONTROLLER
.createPaymentProfile(new CreatePaymentProfileRequest(new CreatePaymentProfile.Builder()
.customerId(customer.getId())
.build())
)
)
.isUnprocessableEntity()
.hasErrors(
"Credit card expiration month: cannot be blank.",
"Credit card expiration year: cannot be blank.",
"Credit card number: cannot be blank."
);
}

@Test
void shouldReturn422WhenChargifyTokenCannotBeFound() {
// when - then
CommonAssertions
.assertThatErrorListResponse(() -> PAYMENT_PROFILES_CONTROLLER
.createPaymentProfile(new CreatePaymentProfileRequest(new CreatePaymentProfile.Builder()
.customerId(customer.getId())
.chargifyToken("tok_gdnc4xc2ychfmv6spkh6sv3kaa")
.build())
)
)
.isUnprocessableEntity()
.hasErrors("Chargify token not found");
}

@Test
void shouldReturn401WhenProvidingInvalidCredentials() {
// when - then
CommonAssertions.assertUnauthorized(
() -> TestClient.createInvalidCredentialsClient().getPaymentProfilesController()
.createPaymentProfile(new CreatePaymentProfileRequest())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.maxio.advancedbilling.controllers.paymentprofiles;

import com.maxio.advancedbilling.AdvancedBillingClient;
import com.maxio.advancedbilling.TestClient;
import com.maxio.advancedbilling.controllers.PaymentProfilesController;
import com.maxio.advancedbilling.exceptions.ApiException;
import com.maxio.advancedbilling.models.BankAccountPaymentProfile;
import com.maxio.advancedbilling.models.BankAccountVault;
import com.maxio.advancedbilling.models.CardType;
import com.maxio.advancedbilling.models.CreatePaymentProfile;
import com.maxio.advancedbilling.models.CreatePaymentProfileRequest;
import com.maxio.advancedbilling.models.CreditCardPaymentProfile;
import com.maxio.advancedbilling.models.CurrentVault;
import com.maxio.advancedbilling.models.Customer;
import com.maxio.advancedbilling.models.ListPaymentProfilesInput;
import com.maxio.advancedbilling.models.PaymentType;
import com.maxio.advancedbilling.models.containers.CreatePaymentProfileExpirationMonth;
import com.maxio.advancedbilling.models.containers.CreatePaymentProfileExpirationYear;
import com.maxio.advancedbilling.utils.TestSetup;
import com.maxio.advancedbilling.utils.TestTeardown;
import com.maxio.advancedbilling.utils.assertions.CommonAssertions;
import com.maxio.advancedbilling.utils.matchers.ReadPaymentProfileResponsePaymentProfileGetter;
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.Random;

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

class PaymentProfilesControllerListTest {
private static final TestSetup TEST_SETUP = new TestSetup();
private static final AdvancedBillingClient CLIENT = TestClient.createClient();
private static final PaymentProfilesController PAYMENT_PROFILES_CONTROLLER = CLIENT.getPaymentProfilesController();

private static Customer customerWithCreditCardPaymentProfiles;
private static Customer customerWithBankAccountPaymentProfiles;

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

Random random = new Random();
for (int i = 0; i < 2; i++) {
// credit card payment profiles
PAYMENT_PROFILES_CONTROLLER.createPaymentProfile(
new CreatePaymentProfileRequest(
new CreatePaymentProfile.Builder()
.customerId(customerWithCreditCardPaymentProfiles.getId())
.expirationMonth(CreatePaymentProfileExpirationMonth.fromNumber(random.nextInt(1, 13)))
.expirationYear(CreatePaymentProfileExpirationYear.fromNumber(random.nextInt(2030, 2040)))
.fullNumber("4111111111111111")
.build()
)
);
}

// payment profile for ACH usage
PAYMENT_PROFILES_CONTROLLER.createPaymentProfile(new CreatePaymentProfileRequest(
new CreatePaymentProfile.Builder()
.customerId(customerWithBankAccountPaymentProfiles.getId())
.bankName("Best Bank")
.bankRoutingNumber("021000089")
.bankAccountNumber("111111111111")
.bankAccountType("checking")
.bankAccountHolderType("business")
.paymentType(PaymentType.BANK_ACCOUNT)
.build()
)
);
}

@AfterAll
static void teardown() throws IOException, ApiException {
TestTeardown testTeardown = new TestTeardown();
testTeardown.deleteCustomer(customerWithCreditCardPaymentProfiles);
testTeardown.deleteCustomer(customerWithBankAccountPaymentProfiles);
}

@Test
void shouldReturnListOfCreditCardPaymentProfilesForCustomer() throws IOException, ApiException {
// when - then
assertThat(PAYMENT_PROFILES_CONTROLLER.listPaymentProfiles(new ListPaymentProfilesInput.Builder()
.customerId(customerWithCreditCardPaymentProfiles.getId())
.build())
)
.hasSize(2)
.map(readPaymentProfileResponse -> readPaymentProfileResponse
.getPaymentProfile()
.match(new ReadPaymentProfileResponsePaymentProfileGetter<CreditCardPaymentProfile>())
)
.allSatisfy(creditCardPaymentProfile -> {
assertThat(creditCardPaymentProfile.getId()).isNotNull();
assertThat(creditCardPaymentProfile.getFirstName()).isEqualTo(customerWithCreditCardPaymentProfiles.getFirstName());
assertThat(creditCardPaymentProfile.getLastName()).isEqualTo(customerWithCreditCardPaymentProfiles.getLastName());
assertThat(creditCardPaymentProfile.getMaskedCardNumber()).isEqualTo("XXXX-XXXX-XXXX-1111");
assertThat(creditCardPaymentProfile.getCardType()).isEqualTo(CardType.VISA);
assertThat(creditCardPaymentProfile.getExpirationMonth()).isNotNull();
assertThat(creditCardPaymentProfile.getExpirationYear()).isNotNull();
assertThat(creditCardPaymentProfile.getCustomerId()).isEqualTo(customerWithCreditCardPaymentProfiles.getId());
assertThat(creditCardPaymentProfile.getCurrentVault()).isEqualTo(CurrentVault.BOGUS);
assertThat(creditCardPaymentProfile.getVaultToken()).isNotNull();
assertThat(creditCardPaymentProfile.getBillingAddress()).isNull();
assertThat(creditCardPaymentProfile.getBillingCity()).isNull();
assertThat(creditCardPaymentProfile.getBillingState()).isNull();
assertThat(creditCardPaymentProfile.getBillingZip()).isNull();
assertThat(creditCardPaymentProfile.getBillingCountry()).isNull();
assertThat(creditCardPaymentProfile.getCustomerVaultToken()).isNull();
assertThat(creditCardPaymentProfile.getBillingAddress2()).isNull();
assertThat(creditCardPaymentProfile.getSiteGatewaySettingId()).isNull();
assertThat(creditCardPaymentProfile.getPaymentType()).isEqualTo("credit_card");
assertThat(creditCardPaymentProfile.getDisabled()).isFalse();
assertThat(creditCardPaymentProfile.getGatewayHandle()).isNull();
});
}

@Test
void shouldReturnListOfBankAccountPaymentProfilesForCustomer() throws IOException, ApiException {
// when - then
assertThat(PAYMENT_PROFILES_CONTROLLER.listPaymentProfiles(new ListPaymentProfilesInput.Builder()
.customerId(customerWithBankAccountPaymentProfiles.getId())
.build())
)
.hasSize(1)
.map(readPaymentProfileResponse -> readPaymentProfileResponse
.getPaymentProfile()
.match(new ReadPaymentProfileResponsePaymentProfileGetter<BankAccountPaymentProfile>())
)
.singleElement()
.satisfies(bankAccountPaymentProfile -> {
assertThat(bankAccountPaymentProfile.getId()).isNotNull();
assertThat(bankAccountPaymentProfile.getFirstName()).isEqualTo(customerWithBankAccountPaymentProfiles.getFirstName());
assertThat(bankAccountPaymentProfile.getLastName()).isEqualTo(customerWithBankAccountPaymentProfiles.getLastName());
assertThat(bankAccountPaymentProfile.getMaskedBankRoutingNumber()).isEqualTo("XXXX0089");
assertThat(bankAccountPaymentProfile.getMaskedBankAccountNumber()).isEqualTo("XXXX1111");
assertThat(bankAccountPaymentProfile.getCustomerId()).isEqualTo(customerWithBankAccountPaymentProfiles.getId());
assertThat(bankAccountPaymentProfile.getCurrentVault()).isEqualTo(BankAccountVault.BOGUS);
assertThat(bankAccountPaymentProfile.getVaultToken()).isEqualTo("111111111111");
assertThat(bankAccountPaymentProfile.getCustomerVaultToken()).isNull();
assertThat(bankAccountPaymentProfile.getBankName()).isEqualTo("Best Bank");
assertThat(bankAccountPaymentProfile.getBankAccountType()).isEqualTo("checking");
assertThat(bankAccountPaymentProfile.getBankAccountHolderType()).isEqualTo("business");
assertThat(bankAccountPaymentProfile.getPaymentType()).isEqualTo("bank_account");
assertThat(bankAccountPaymentProfile.getVerified()).isFalse();
assertThat(bankAccountPaymentProfile.getGatewayHandle()).isNull();
});
}

@Test
void shouldReturnEmptyListForNotExistentCustomer() throws IOException, ApiException {
// when - then
assertThat(PAYMENT_PROFILES_CONTROLLER.listPaymentProfiles(new ListPaymentProfilesInput.Builder().customerId(123).build()))
.isEmpty();
}

@Test
void shouldReturn401WhenProvidingInvalidCredentials() {
// when - then
CommonAssertions.assertUnauthorized(
() -> TestClient.createInvalidCredentialsClient().getPaymentProfilesController()
.listPaymentProfiles(new ListPaymentProfilesInput())
);
}
}
Loading

0 comments on commit 0829970

Please sign in to comment.