Skip to content

Commit

Permalink
fix: added filtering for account/balance (#261)
Browse files Browse the repository at this point in the history
* fix: added filtering for account/balance

* fix: fixed tests
  • Loading branch information
Kammerlo authored Dec 17, 2024
1 parent 25d72e3 commit 9f7c181
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.openapitools.client.model.AccountBalanceResponse;
import org.openapitools.client.model.AccountCoinsRequest;
import org.openapitools.client.model.AccountCoinsResponse;
import org.openapitools.client.model.Amount;
import org.openapitools.client.model.Currency;
import org.openapitools.client.model.CurrencyMetadata;
import org.openapitools.client.model.PartialBlockIdentifier;
Expand Down Expand Up @@ -63,7 +64,7 @@ public AccountBalanceResponse getAccountBalance(AccountBalanceRequest accountBal
hash = blockIdentifier.getHash();
}

return findBalanceDataByAddressAndBlock(accountAddress, index, hash);
return findBalanceDataByAddressAndBlock(accountAddress, index, hash, accountBalanceRequest.getCurrencies());

}

Expand Down Expand Up @@ -92,7 +93,7 @@ public AccountCoinsResponse getAccountCoins(AccountCoinsRequest accountCoinsRequ
}

private AccountBalanceResponse findBalanceDataByAddressAndBlock(String address, Long number,
String hash) {
String hash, List<Currency> currencies) {

return findBlockOrLast(number, hash)
.map(blockDto -> {
Expand All @@ -105,7 +106,15 @@ private AccountBalanceResponse findBalanceDataByAddressAndBlock(String address,
} else {
balances = ledgerAccountService.findBalanceByAddressAndBlock(address, blockDto.getNumber());
}
return accountMapper.mapToAccountBalanceResponse(blockDto, balances);
AccountBalanceResponse accountBalanceResponse = accountMapper.mapToAccountBalanceResponse(
blockDto, balances);
if (Objects.nonNull(currencies) && !currencies.isEmpty()) {
validateCurrencies(currencies);
List<Amount> accountBalanceResponseAmounts = accountBalanceResponse.getBalances();
accountBalanceResponseAmounts.removeIf(b -> currencies.stream().noneMatch(c -> c.getSymbol().equals(b.getCurrency().getSymbol())));
accountBalanceResponse.setBalances(accountBalanceResponseAmounts);
}
return accountBalanceResponse;
})
.orElseThrow(ExceptionFactory::blockNotFoundException);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import jakarta.validation.constraints.NotNull;

Expand Down Expand Up @@ -38,6 +39,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -86,6 +88,7 @@ void getAccountBalanceNoStakeAddressPositiveTest() {
verify(ledgerAccountService).findBalanceByAddressAndBlock(accountAddress, 1L);
verify(accountBalanceRequest).getAccountIdentifier();
verify(accountBalanceRequest).getBlockIdentifier();
verify(accountBalanceRequest).getCurrencies();
verifyNoMoreInteractions(ledgerAccountService);
verifyNoMoreInteractions(accountBalanceRequest);
verifyNoMoreInteractions(accountIdentifier);
Expand Down Expand Up @@ -116,6 +119,7 @@ void getAccountBalanceStakeAddressPositiveTest() {
.findBalanceByStakeAddressAndBlock(accountAddress, 1L);
verify(accountBalanceRequest).getAccountIdentifier();
verify(accountBalanceRequest).getBlockIdentifier();
verify(accountBalanceRequest).getCurrencies();
verifyNoMoreInteractions(ledgerAccountService);
verifyNoMoreInteractions(accountBalanceRequest);
verifyNoMoreInteractions(accountIdentifier);
Expand All @@ -129,11 +133,32 @@ private static AddressBalance getMockedAddressBalance() {
return mock;
}

@Test
void getFilteredAccountBalance() {
String address = "addr_test1qz5t8wq55e09usmh07ymxry8atzwxwt2nwwzfngg6esffxvw2pfap6uqmkj3n6zmlrsgz397md2gt7yqs5p255uygaesx608y5";
when(ledgerAccountService.findBalanceByAddressAndBlock(any(), any()))
.thenReturn(List.of(AddressBalance.builder().address(address).unit("lovelace").number(10L).quantity(
BigInteger.valueOf(10)).build(),
AddressBalance.builder().address(address).unit("bd976e131cfc3956b806967b06530e48c20ed5498b46a5eb836b61c2").number(10L).quantity(
BigInteger.valueOf(10)).build()));
BlockIdentifierExtended block = getMockedBlockIdentifierExtended();
when(ledgerBlockService.findLatestBlockIdentifier()).thenReturn(block);
AccountBalanceRequest accountBalanceRequest = AccountBalanceRequest.builder()
.accountIdentifier(AccountIdentifier.builder().address(address).build())
.currencies(List.of(Currency.builder().symbol("ADA").build()))
.build();
AccountBalanceResponse accountBalanceResponse = accountService.getAccountBalance(
accountBalanceRequest);

assertEquals(1, accountBalanceResponse.getBalances().size());
}

@Test
void getAccountBalanceNoStakeAddressNullBlockIdentifierPositiveTest() {
// Shelly testnet address
String accountAddress = "addr_test1vru64wlzn85v7fecg0mz33lh00wlggqtquvzzuhf6vusyes32jz9w";
AccountBalanceRequest accountBalanceRequest = Mockito.mock(AccountBalanceRequest.class);

AccountIdentifier accountIdentifier = Mockito.mock(AccountIdentifier.class);
when(accountBalanceRequest.getAccountIdentifier()).thenReturn(accountIdentifier);
when(accountIdentifier.getAddress()).thenReturn(accountAddress);
Expand All @@ -158,6 +183,7 @@ void getAccountBalanceNoStakeAddressNullBlockIdentifierPositiveTest() {
verify(ledgerAccountService).findBalanceByAddressAndBlock(accountAddress, 1L);
verify(accountBalanceRequest).getAccountIdentifier();
verify(accountBalanceRequest).getBlockIdentifier();
verify(accountBalanceRequest).getCurrencies();
verifyNoMoreInteractions(ledgerAccountService);
verifyNoMoreInteractions(accountBalanceRequest);
verifyNoMoreInteractions(accountIdentifier);
Expand Down Expand Up @@ -187,6 +213,7 @@ void getAccountBalanceStakeAddressWithEmptyBalancesThrowTest() {
.findBalanceByStakeAddressAndBlock(accountAddress, 1L);
verify(accountBalanceRequest).getAccountIdentifier();
verify(accountBalanceRequest).getBlockIdentifier();
verify(accountBalanceRequest).getCurrencies();
verifyNoMoreInteractions(ledgerAccountService);
verifyNoMoreInteractions(accountBalanceRequest);
verifyNoMoreInteractions(accountIdentifier);
Expand All @@ -209,6 +236,7 @@ void getAccountBalanceStakeAddressWithBlockDtoNullThrowTest() {
verify(ledgerBlockService).findBlockIdentifier(1L, HASH);
verify(accountBalanceRequest).getAccountIdentifier();
verify(accountBalanceRequest).getBlockIdentifier();
verify(accountBalanceRequest).getCurrencies();
verifyNoMoreInteractions(ledgerAccountService);
verifyNoMoreInteractions(accountBalanceRequest);
verifyNoMoreInteractions(accountIdentifier);
Expand Down Expand Up @@ -256,6 +284,7 @@ void getAccountBalanceWithStakeAddressAndNullBalanceThrowTest() {
.findBalanceByStakeAddressAndBlock(accountAddress, 1L);
verify(accountBalanceRequest).getAccountIdentifier();
verify(accountBalanceRequest).getBlockIdentifier();
verify(accountBalanceRequest).getCurrencies();
verifyNoMoreInteractions(ledgerAccountService);
verifyNoMoreInteractions(accountBalanceRequest);
verifyNoMoreInteractions(accountIdentifier);
Expand Down

0 comments on commit 9f7c181

Please sign in to comment.