From f6647889d2e43ba19269c302f347512bf628739a Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Mon, 16 Dec 2024 16:07:10 +0100 Subject: [PATCH 1/2] fix: added filtering for account/balance --- .../account/service/AccountServiceImpl.java | 15 ++++++++++--- .../service/AccountServiceImplTest.java | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImpl.java b/api/src/main/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImpl.java index 449e9378..02c2c3f1 100644 --- a/api/src/main/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImpl.java +++ b/api/src/main/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImpl.java @@ -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; @@ -63,7 +64,7 @@ public AccountBalanceResponse getAccountBalance(AccountBalanceRequest accountBal hash = blockIdentifier.getHash(); } - return findBalanceDataByAddressAndBlock(accountAddress, index, hash); + return findBalanceDataByAddressAndBlock(accountAddress, index, hash, accountBalanceRequest.getCurrencies()); } @@ -92,7 +93,7 @@ public AccountCoinsResponse getAccountCoins(AccountCoinsRequest accountCoinsRequ } private AccountBalanceResponse findBalanceDataByAddressAndBlock(String address, Long number, - String hash) { + String hash, List currencies) { return findBlockOrLast(number, hash) .map(blockDto -> { @@ -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)) { + validateCurrencies(currencies); + List accountBalanceResponseAmounts = accountBalanceResponse.getBalances(); + accountBalanceResponseAmounts.removeIf(b -> currencies.stream().noneMatch(c -> c.getSymbol().equals(b.getCurrency().getSymbol()))); + accountBalanceResponse.setBalances(accountBalanceResponseAmounts); + } + return accountBalanceResponse; }) .orElseThrow(ExceptionFactory::blockNotFoundException); } diff --git a/api/src/test/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImplTest.java b/api/src/test/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImplTest.java index e1bc071f..673c52cc 100644 --- a/api/src/test/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImplTest.java +++ b/api/src/test/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImplTest.java @@ -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; @@ -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; @@ -129,6 +131,26 @@ 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 From 68f67f5141fbf4d8cb06f86fe4f282c471fa6490 Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Mon, 16 Dec 2024 16:18:37 +0100 Subject: [PATCH 2/2] fix: fixed tests --- .../rosetta/api/account/service/AccountServiceImpl.java | 2 +- .../api/account/service/AccountServiceImplTest.java | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImpl.java b/api/src/main/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImpl.java index 02c2c3f1..b70a257f 100644 --- a/api/src/main/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImpl.java +++ b/api/src/main/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImpl.java @@ -108,7 +108,7 @@ private AccountBalanceResponse findBalanceDataByAddressAndBlock(String address, } AccountBalanceResponse accountBalanceResponse = accountMapper.mapToAccountBalanceResponse( blockDto, balances); - if (Objects.nonNull(currencies)) { + if (Objects.nonNull(currencies) && !currencies.isEmpty()) { validateCurrencies(currencies); List accountBalanceResponseAmounts = accountBalanceResponse.getBalances(); accountBalanceResponseAmounts.removeIf(b -> currencies.stream().noneMatch(c -> c.getSymbol().equals(b.getCurrency().getSymbol()))); diff --git a/api/src/test/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImplTest.java b/api/src/test/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImplTest.java index 673c52cc..019508e3 100644 --- a/api/src/test/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImplTest.java +++ b/api/src/test/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImplTest.java @@ -88,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); @@ -118,6 +119,7 @@ void getAccountBalanceStakeAddressPositiveTest() { .findBalanceByStakeAddressAndBlock(accountAddress, 1L); verify(accountBalanceRequest).getAccountIdentifier(); verify(accountBalanceRequest).getBlockIdentifier(); + verify(accountBalanceRequest).getCurrencies(); verifyNoMoreInteractions(ledgerAccountService); verifyNoMoreInteractions(accountBalanceRequest); verifyNoMoreInteractions(accountIdentifier); @@ -156,6 +158,7 @@ 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); @@ -180,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); @@ -209,6 +213,7 @@ void getAccountBalanceStakeAddressWithEmptyBalancesThrowTest() { .findBalanceByStakeAddressAndBlock(accountAddress, 1L); verify(accountBalanceRequest).getAccountIdentifier(); verify(accountBalanceRequest).getBlockIdentifier(); + verify(accountBalanceRequest).getCurrencies(); verifyNoMoreInteractions(ledgerAccountService); verifyNoMoreInteractions(accountBalanceRequest); verifyNoMoreInteractions(accountIdentifier); @@ -231,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); @@ -278,6 +284,7 @@ void getAccountBalanceWithStakeAddressAndNullBalanceThrowTest() { .findBalanceByStakeAddressAndBlock(accountAddress, 1L); verify(accountBalanceRequest).getAccountIdentifier(); verify(accountBalanceRequest).getBlockIdentifier(); + verify(accountBalanceRequest).getCurrencies(); verifyNoMoreInteractions(ledgerAccountService); verifyNoMoreInteractions(accountBalanceRequest); verifyNoMoreInteractions(accountIdentifier);