Skip to content

Commit

Permalink
Balance transaction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markusjevringsesame committed Apr 29, 2024
1 parent 96d0873 commit 3ad86e4
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public List<BalanceTransaction> list(QueryParameters query, String stripeAccount
add(Charge.class, balanceTransactions, query, null);
add(Refund.class, balanceTransactions, query, null);
}
balanceTransactions.sort(Comparator.comparing(BalanceTransaction::getCreated));
// Most recent first
balanceTransactions.sort(Comparator.comparing(BalanceTransaction::getCreated)
.reversed());
return balanceTransactions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static BalanceTransaction toBalanceTransaction(Refund refund, String str
balanceTransaction.setFee(0L);
balanceTransaction.setFeeDetails(Collections.emptyList());
balanceTransaction.setCurrency(refund.getCurrency());
balanceTransaction.setDescription(refund.getDescription());
balanceTransaction.setDescription(null);
balanceTransaction.setId(refund.getBalanceTransaction());
balanceTransaction.setObject("balance_transaction");
balanceTransaction.setStatus(refund.getStatus()
Expand All @@ -55,7 +55,7 @@ private static BalanceTransaction toBalanceTransaction(Charge charge, String str
balanceTransaction.setFee(0L);
balanceTransaction.setFeeDetails(Collections.emptyList());
balanceTransaction.setCurrency(charge.getCurrency());
balanceTransaction.setDescription(charge.getDescription());
balanceTransaction.setDescription(null);
balanceTransaction.setId(charge.getBalanceTransaction());
balanceTransaction.setObject("balance_transaction");
balanceTransaction.setStatus(charge.getStatus()
Expand All @@ -81,7 +81,7 @@ private static BalanceTransaction toBalanceTransaction(Payout payout, String str
balanceTransaction.setFee(0L);
balanceTransaction.setFeeDetails(Collections.emptyList());
balanceTransaction.setCurrency(payout.getCurrency());
balanceTransaction.setDescription(payout.getDescription());
balanceTransaction.setDescription(null);
balanceTransaction.setId(payout.getBalanceTransaction());
balanceTransaction.setObject("balance_transaction");
balanceTransaction.setStatus(payout.getStatus()
Expand All @@ -108,7 +108,7 @@ private static BalanceTransaction toBalanceTransaction(Transfer transfer, String
balanceTransaction.setFee(0L);
balanceTransaction.setFeeDetails(Collections.emptyList());
balanceTransaction.setCurrency(transfer.getCurrency());
balanceTransaction.setDescription(transfer.getDescription());
balanceTransaction.setDescription(null);
balanceTransaction.setId(transfer.getBalanceTransaction());
balanceTransaction.setObject("balance_transaction");
balanceTransaction.setStatus("available");
Expand Down Expand Up @@ -165,7 +165,7 @@ private static BalanceTransaction toBalanceTransaction(TransferReversal transfer
balanceTransaction.setDescription("REFUND FOR PAYMENT");
balanceTransaction.setId(transferReversal.getBalanceTransaction());
Refund source = new Refund();
source.setAmount(amount);
source.setAmount(-amount);
source.setBalanceTransaction(transferReversal.getBalanceTransaction());
source.setCreated(transferReversal.getCreated());
source.setCurrency(transferReversal.getCurrency());
Expand Down
122 changes: 122 additions & 0 deletions src/test/java/com/sesame/oss/stripemock/BalanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import com.stripe.exception.StripeException;
import com.stripe.model.Account;
import com.stripe.model.Balance;
import com.stripe.model.Charge;
import com.stripe.model.Transfer;
import com.stripe.net.RequestOptions;
import com.stripe.param.BalanceRetrieveParams;
import com.stripe.param.ChargeCreateParams;
import com.stripe.param.TransferCreateParams;
import org.junit.jupiter.api.Test;

import static com.sesame.oss.stripemock.AccountTest.defaultCreationParameters;
Expand Down Expand Up @@ -87,4 +91,122 @@ void shouldFetchEmptyBalanceForMainAccount() throws StripeException {

assertEquals("balance", balance.getObject());
}

@Test
void shouldAddAllAvailableBalancesForStripeAccount() throws StripeException {
Account createdAccount = Account.create(defaultCreationParameters("Stripe-mock test company name"));
Transfer.create(TransferCreateParams.builder()
.setAmount(10_00L)
.setCurrency("usd")
.putMetadata("integration_test", "true")
.setDestination(createdAccount.getId())
.build());

Transfer.create(TransferCreateParams.builder()
.setAmount(20_00L)
.setCurrency("usd")
.putMetadata("integration_test", "true")
.setDestination(createdAccount.getId())
.build());

Balance balance = Balance.retrieve(BalanceRetrieveParams.builder()
.build(),
RequestOptions.builder()
.setStripeAccount(createdAccount.getId())
.build());

assertEquals(1,
balance.getAvailable()
.size());
assertEquals(30_00L,
balance.getAvailable()
.getFirst()
.getAmount());

assertNull(balance.getConnectReserved());

assertEquals(1,
balance.getInstantAvailable()
.size());
assertEquals(0,
balance.getInstantAvailable()
.getFirst()
.getAmount());

assertEquals(1,
balance.getPending()
.size());
assertEquals(0,
balance.getPending()
.getFirst()
.getAmount());

assertEquals("balance", balance.getObject());


createdAccount.delete();
}

@Test
void shouldAddAllAvailableBalancesForMainAccount() throws StripeException {
Account createdAccount = Account.create(defaultCreationParameters("Stripe-mock test company name"));
Transfer.create(TransferCreateParams.builder()
.setAmount(10_00L)
.setCurrency("usd")
.putMetadata("integration_test", "true")
.setDestination(createdAccount.getId())
.build());

Transfer.create(TransferCreateParams.builder()
.setAmount(20_00L)
.setCurrency("usd")
.putMetadata("integration_test", "true")
.setDestination(createdAccount.getId())
.build());

Charge.create(ChargeCreateParams.builder()
.setAmount(40_00L)
.setCurrency("usd")
.putMetadata("integration_test", "true")
.build());

Balance balance = Balance.retrieve();

assertEquals(1,
balance.getAvailable()
.size());
assertEquals(10_00L,
balance.getAvailable()
.getFirst()
.getAmount());

assertEquals(1,
balance.getConnectReserved()
.size());
assertEquals(0,
balance.getConnectReserved()
.getFirst()
.getAmount());

assertEquals(1,
balance.getInstantAvailable()
.size());
assertEquals(0,
balance.getInstantAvailable()
.getFirst()
.getAmount());

assertEquals(1,
balance.getPending()
.size());
assertEquals(0,
balance.getPending()
.getFirst()
.getAmount());

assertEquals("balance", balance.getObject());


createdAccount.delete();
}
}
148 changes: 127 additions & 21 deletions src/test/java/com/sesame/oss/stripemock/BalanceTransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
import com.stripe.model.*;
import com.stripe.net.RequestOptions;
import com.stripe.param.BalanceTransactionListParams;
import com.stripe.param.PayoutCreateParams;
import com.stripe.param.TransferCreateParams;
import com.stripe.param.TransferReversalCollectionCreateParams;
import org.junit.jupiter.api.Test;

import java.util.List;

import static com.sesame.oss.stripemock.AccountTest.defaultCreationParameters;
import static org.junit.jupiter.api.Assertions.*;

public class BalanceTransactionTest extends AbstractStripeMockTest {


@Test
void shouldListWithExpansions() throws StripeException {
Account account = Account.create(defaultCreationParameters("Stripe-mock test company name"));
Transfer createdTransfer = //
Transfer rtr = //
Transfer.create(TransferCreateParams.builder()
.setAmount(9_000L)
.setAmount(10_00L)
.setCurrency("usd")
.putMetadata("integration_test", "true")
.setTransferGroup("my transfer group")
Expand All @@ -29,25 +31,129 @@ void shouldListWithExpansions() throws StripeException {
.setDestination(account.getId())
.build());

StripeMock.adjustTimeTo(StripeMock.getClock()
.instant()
.plusSeconds(10));

TransferReversal transferReversal = createdTransfer.getReversals()
.create(TransferReversalCollectionCreateParams.builder()
.build());

// todo: set up things where these expands actually work, and then assert that they have been correctly expanded

BalanceTransactionCollection balanceTransactions = BalanceTransaction.list(BalanceTransactionListParams.builder()
.setLimit(100L)
.addAllExpand(List.of(
"data.source.source_transfer",
"data.source.source_transfer_reversal",
"data.source.source_transfer_reversal.transfer"))
.build(),
RequestOptions.builder()
.setStripeAccount(account.getId())
.build());
// todo: when listing the transfer reversal, the amount should be negative in this case.
// Yes, it looks like the "direction", i.e. negative or positive, is in the context of the stripe account.
System.out.println("balanceTransactions = " + balanceTransactions.getData());

TransferReversal trr = rtr.getReversals()
.create(TransferReversalCollectionCreateParams.builder()
.build());
StripeMock.adjustTimeTo(StripeMock.getClock()
.instant()
.plusSeconds(10));

Transfer tr = Transfer.create(TransferCreateParams.builder()
.setAmount(40_00L)
.setCurrency("usd")
.putMetadata("integration_test", "true")
.setDestination(account.getId())
.build());

StripeMock.adjustTimeTo(StripeMock.getClock()
.instant()
.plusSeconds(10));

Payout po = Payout.create(PayoutCreateParams.builder()
.setAmount(30_00L)
.setCurrency("usd")
.putMetadata("integration_test", "true")
.build(),
RequestOptions.builder()
.setStripeAccount(account.getId())
.build());

BalanceTransactionCollection result = BalanceTransaction.list(BalanceTransactionListParams.builder()
.setLimit(100L)
.addAllExpand(List.of("data.source.source_transfer",
"data.source.source_transfer_reversal",
"data.source.source_transfer_reversal.transfer"))
.build(),
RequestOptions.builder()
.setStripeAccount(account.getId())
.build());
assertEquals("list", result.getObject());
assertFalse(result.getHasMore());
List<BalanceTransaction> balanceTransactions = result.getData();
assertEquals(4, balanceTransactions.size());
assertTrue(balanceTransactions.stream()
.map(BalanceTransaction::getStatus)
.allMatch(status -> status.equals("available")));

BalanceTransaction btTrr = balanceTransactions.get(2);
assertEquals("REFUND FOR PAYMENT", btTrr.getDescription());
assertEquals("refund", btTrr.getReportingCategory());
assertEquals("payment_refund", btTrr.getType());
assertEquals(-1000, btTrr.getAmount());
assertEquals(-1000, btTrr.getNet());
if (btTrr.getSourceObject() instanceof Refund refund) {
assertEquals(1000, refund.getAmount());
assertTrue(refund.getId()
.startsWith("pyr_"));
assertTrue(refund.getCharge()
.startsWith("py_"));
assertNull(refund.getChargeObject());
assertNull(refund.getBalanceTransactionObject()); // otherwise we'll get a cycle
assertEquals(1000,
refund.getSourceTransferReversalObject()
.getAmount());
assertNull(refund.getTransferReversalObject());
} else {
fail("Not a refund");
}


BalanceTransaction btRtr = balanceTransactions.get(3);
assertNull(btRtr.getDescription());
assertEquals("charge", btRtr.getReportingCategory());
assertEquals("payment", btRtr.getType());
assertEquals(1000, btRtr.getAmount());
assertEquals(1000, btRtr.getNet());
if (btRtr.getSourceObject() instanceof Charge charge) {
assertEquals(1000, charge.getAmount());
assertTrue(charge.getId()
.startsWith("py_"));
assertTrue(charge.getCaptured());
assertEquals("charge", charge.getObject());
assertEquals("my description",
charge.getSourceTransferObject()
.getDescription());
} else {
fail("Not a charge");
}

BalanceTransaction btTr = balanceTransactions.get(1);
assertNull(btTr.getDescription());
assertEquals("charge", btTr.getReportingCategory());
assertEquals("payment", btTr.getType());
assertEquals(4000, btTr.getAmount());
assertEquals(4000, btTr.getNet());
if (btTr.getSourceObject() instanceof Charge charge) {
assertEquals(4000, charge.getAmount());
assertTrue(charge.getId()
.startsWith("py_"));
assertTrue(charge.getCaptured());
assertEquals("charge", charge.getObject());
assertNull(charge.getSourceTransferObject()
.getDescription());
assertTrue(charge.getSourceTransferObject()
.getId()
.startsWith("tr_"));
} else {
fail("Not a charge");
}

BalanceTransaction btPo = balanceTransactions.get(0);
assertNull(btPo.getDescription());
assertEquals("payout", btPo.getReportingCategory());
assertEquals("payout", btPo.getType());
assertEquals(-3000, btPo.getAmount());
assertEquals(-3000, btPo.getNet());
if (btPo.getSourceObject() instanceof Payout payout) {
assertEquals(3000, payout.getAmount());
assertEquals("standard", payout.getMethod());
} else {
fail("Not a payout");
}
}
}
1 change: 1 addition & 0 deletions src/test/java/com/sesame/oss/stripemock/ChargeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void testCharge() throws StripeException {
Charge charge = Charge.create(ChargeCreateParams.builder()
.setAmount(10_00L)
.setCurrency("usd")
.putMetadata("integration_test", "true")
.build());
assertEquals(charge, Charge.retrieve(charge.getId()));

Expand Down
Loading

0 comments on commit 3ad86e4

Please sign in to comment.