Skip to content

Commit 9f7c181

Browse files
authored
fix: added filtering for account/balance (#261)
* fix: added filtering for account/balance * fix: fixed tests
1 parent 25d72e3 commit 9f7c181

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

api/src/main/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImpl.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.openapitools.client.model.AccountBalanceResponse;
1515
import org.openapitools.client.model.AccountCoinsRequest;
1616
import org.openapitools.client.model.AccountCoinsResponse;
17+
import org.openapitools.client.model.Amount;
1718
import org.openapitools.client.model.Currency;
1819
import org.openapitools.client.model.CurrencyMetadata;
1920
import org.openapitools.client.model.PartialBlockIdentifier;
@@ -63,7 +64,7 @@ public AccountBalanceResponse getAccountBalance(AccountBalanceRequest accountBal
6364
hash = blockIdentifier.getHash();
6465
}
6566

66-
return findBalanceDataByAddressAndBlock(accountAddress, index, hash);
67+
return findBalanceDataByAddressAndBlock(accountAddress, index, hash, accountBalanceRequest.getCurrencies());
6768

6869
}
6970

@@ -92,7 +93,7 @@ public AccountCoinsResponse getAccountCoins(AccountCoinsRequest accountCoinsRequ
9293
}
9394

9495
private AccountBalanceResponse findBalanceDataByAddressAndBlock(String address, Long number,
95-
String hash) {
96+
String hash, List<Currency> currencies) {
9697

9798
return findBlockOrLast(number, hash)
9899
.map(blockDto -> {
@@ -105,7 +106,15 @@ private AccountBalanceResponse findBalanceDataByAddressAndBlock(String address,
105106
} else {
106107
balances = ledgerAccountService.findBalanceByAddressAndBlock(address, blockDto.getNumber());
107108
}
108-
return accountMapper.mapToAccountBalanceResponse(blockDto, balances);
109+
AccountBalanceResponse accountBalanceResponse = accountMapper.mapToAccountBalanceResponse(
110+
blockDto, balances);
111+
if (Objects.nonNull(currencies) && !currencies.isEmpty()) {
112+
validateCurrencies(currencies);
113+
List<Amount> accountBalanceResponseAmounts = accountBalanceResponse.getBalances();
114+
accountBalanceResponseAmounts.removeIf(b -> currencies.stream().noneMatch(c -> c.getSymbol().equals(b.getCurrency().getSymbol())));
115+
accountBalanceResponse.setBalances(accountBalanceResponseAmounts);
116+
}
117+
return accountBalanceResponse;
109118
})
110119
.orElseThrow(ExceptionFactory::blockNotFoundException);
111120
}

api/src/test/java/org/cardanofoundation/rosetta/api/account/service/AccountServiceImplTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.math.BigInteger;
44
import java.util.Collections;
5+
import java.util.List;
56
import java.util.Optional;
67
import jakarta.validation.constraints.NotNull;
78

@@ -38,6 +39,7 @@
3839
import static org.junit.jupiter.api.Assertions.assertEquals;
3940
import static org.junit.jupiter.api.Assertions.assertNotNull;
4041
import static org.junit.jupiter.api.Assertions.assertThrows;
42+
import static org.mockito.ArgumentMatchers.any;
4143
import static org.mockito.Mockito.verify;
4244
import static org.mockito.Mockito.verifyNoMoreInteractions;
4345
import static org.mockito.Mockito.when;
@@ -86,6 +88,7 @@ void getAccountBalanceNoStakeAddressPositiveTest() {
8688
verify(ledgerAccountService).findBalanceByAddressAndBlock(accountAddress, 1L);
8789
verify(accountBalanceRequest).getAccountIdentifier();
8890
verify(accountBalanceRequest).getBlockIdentifier();
91+
verify(accountBalanceRequest).getCurrencies();
8992
verifyNoMoreInteractions(ledgerAccountService);
9093
verifyNoMoreInteractions(accountBalanceRequest);
9194
verifyNoMoreInteractions(accountIdentifier);
@@ -116,6 +119,7 @@ void getAccountBalanceStakeAddressPositiveTest() {
116119
.findBalanceByStakeAddressAndBlock(accountAddress, 1L);
117120
verify(accountBalanceRequest).getAccountIdentifier();
118121
verify(accountBalanceRequest).getBlockIdentifier();
122+
verify(accountBalanceRequest).getCurrencies();
119123
verifyNoMoreInteractions(ledgerAccountService);
120124
verifyNoMoreInteractions(accountBalanceRequest);
121125
verifyNoMoreInteractions(accountIdentifier);
@@ -129,11 +133,32 @@ private static AddressBalance getMockedAddressBalance() {
129133
return mock;
130134
}
131135

136+
@Test
137+
void getFilteredAccountBalance() {
138+
String address = "addr_test1qz5t8wq55e09usmh07ymxry8atzwxwt2nwwzfngg6esffxvw2pfap6uqmkj3n6zmlrsgz397md2gt7yqs5p255uygaesx608y5";
139+
when(ledgerAccountService.findBalanceByAddressAndBlock(any(), any()))
140+
.thenReturn(List.of(AddressBalance.builder().address(address).unit("lovelace").number(10L).quantity(
141+
BigInteger.valueOf(10)).build(),
142+
AddressBalance.builder().address(address).unit("bd976e131cfc3956b806967b06530e48c20ed5498b46a5eb836b61c2").number(10L).quantity(
143+
BigInteger.valueOf(10)).build()));
144+
BlockIdentifierExtended block = getMockedBlockIdentifierExtended();
145+
when(ledgerBlockService.findLatestBlockIdentifier()).thenReturn(block);
146+
AccountBalanceRequest accountBalanceRequest = AccountBalanceRequest.builder()
147+
.accountIdentifier(AccountIdentifier.builder().address(address).build())
148+
.currencies(List.of(Currency.builder().symbol("ADA").build()))
149+
.build();
150+
AccountBalanceResponse accountBalanceResponse = accountService.getAccountBalance(
151+
accountBalanceRequest);
152+
153+
assertEquals(1, accountBalanceResponse.getBalances().size());
154+
}
155+
132156
@Test
133157
void getAccountBalanceNoStakeAddressNullBlockIdentifierPositiveTest() {
134158
// Shelly testnet address
135159
String accountAddress = "addr_test1vru64wlzn85v7fecg0mz33lh00wlggqtquvzzuhf6vusyes32jz9w";
136160
AccountBalanceRequest accountBalanceRequest = Mockito.mock(AccountBalanceRequest.class);
161+
137162
AccountIdentifier accountIdentifier = Mockito.mock(AccountIdentifier.class);
138163
when(accountBalanceRequest.getAccountIdentifier()).thenReturn(accountIdentifier);
139164
when(accountIdentifier.getAddress()).thenReturn(accountAddress);
@@ -158,6 +183,7 @@ void getAccountBalanceNoStakeAddressNullBlockIdentifierPositiveTest() {
158183
verify(ledgerAccountService).findBalanceByAddressAndBlock(accountAddress, 1L);
159184
verify(accountBalanceRequest).getAccountIdentifier();
160185
verify(accountBalanceRequest).getBlockIdentifier();
186+
verify(accountBalanceRequest).getCurrencies();
161187
verifyNoMoreInteractions(ledgerAccountService);
162188
verifyNoMoreInteractions(accountBalanceRequest);
163189
verifyNoMoreInteractions(accountIdentifier);
@@ -187,6 +213,7 @@ void getAccountBalanceStakeAddressWithEmptyBalancesThrowTest() {
187213
.findBalanceByStakeAddressAndBlock(accountAddress, 1L);
188214
verify(accountBalanceRequest).getAccountIdentifier();
189215
verify(accountBalanceRequest).getBlockIdentifier();
216+
verify(accountBalanceRequest).getCurrencies();
190217
verifyNoMoreInteractions(ledgerAccountService);
191218
verifyNoMoreInteractions(accountBalanceRequest);
192219
verifyNoMoreInteractions(accountIdentifier);
@@ -209,6 +236,7 @@ void getAccountBalanceStakeAddressWithBlockDtoNullThrowTest() {
209236
verify(ledgerBlockService).findBlockIdentifier(1L, HASH);
210237
verify(accountBalanceRequest).getAccountIdentifier();
211238
verify(accountBalanceRequest).getBlockIdentifier();
239+
verify(accountBalanceRequest).getCurrencies();
212240
verifyNoMoreInteractions(ledgerAccountService);
213241
verifyNoMoreInteractions(accountBalanceRequest);
214242
verifyNoMoreInteractions(accountIdentifier);
@@ -256,6 +284,7 @@ void getAccountBalanceWithStakeAddressAndNullBalanceThrowTest() {
256284
.findBalanceByStakeAddressAndBlock(accountAddress, 1L);
257285
verify(accountBalanceRequest).getAccountIdentifier();
258286
verify(accountBalanceRequest).getBlockIdentifier();
287+
verify(accountBalanceRequest).getCurrencies();
259288
verifyNoMoreInteractions(ledgerAccountService);
260289
verifyNoMoreInteractions(accountBalanceRequest);
261290
verifyNoMoreInteractions(accountIdentifier);

0 commit comments

Comments
 (0)