Skip to content

Commit 94b84e5

Browse files
committed
ajust MirrorNodeClientImpl methods and tests for pages
1 parent ae5c1a6 commit 94b84e5

File tree

5 files changed

+114
-52
lines changed

5 files changed

+114
-52
lines changed

hedera-base/src/main/java/com/openelements/hedera/base/Nft.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
package com.openelements.hedera.base;
24

35
import com.hedera.hashgraph.sdk.AccountId;

hedera-base/src/main/java/com/openelements/hedera/base/NftRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public interface NftRepository {
3131
* @throws HederaException if the search fails
3232
*/
3333
@NonNull
34-
default List<Nft> findByOwner(@NonNull String ownerId) throws HederaException {
34+
default Page<Nft> findByOwner(@NonNull String ownerId) throws HederaException {
3535
Objects.requireNonNull(ownerId, "ownerId must not be null");
3636
return findByOwner(AccountId.fromString(ownerId));
3737
}
@@ -104,7 +104,7 @@ default Optional<Nft> findByTypeAndSerial(@NonNull String tokenId, long serialNu
104104
* @throws HederaException if the search fails
105105
*/
106106
@NonNull
107-
default List<Nft> findByOwnerAndType(@NonNull String ownerId, @NonNull String tokenId) throws HederaException {
107+
default Page<Nft> findByOwnerAndType(@NonNull String ownerId, @NonNull String tokenId) throws HederaException {
108108
Objects.requireNonNull(ownerId, "ownerId must not be null");
109109
Objects.requireNonNull(tokenId, "tokenId must not be null");
110110
return findByOwnerAndType(AccountId.fromString(ownerId), TokenId.fromString(tokenId));

hedera-base/src/main/java/com/openelements/hedera/base/mirrornode/MirrorNodeClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface MirrorNodeClient {
3232
* @throws HederaException if an error occurs
3333
*/
3434
@NonNull
35-
default List<Nft> queryNftsByAccount(@NonNull String accountId) throws HederaException {
35+
default Page<Nft> queryNftsByAccount(@NonNull String accountId) throws HederaException {
3636
Objects.requireNonNull(accountId, "accountId must not be null");
3737
return queryNftsByAccount(AccountId.fromString(accountId));
3838
}
@@ -58,7 +58,7 @@ Page<Nft> queryNftsByAccountAndTokenId(@NonNull AccountId accountId, @NonNull To
5858
* @throws HederaException if an error occurs
5959
*/
6060
@NonNull
61-
default List<Nft> queryNftsByAccountAndTokenId(@NonNull String accountId, @NonNull String tokenId)
61+
default Page<Nft> queryNftsByAccountAndTokenId(@NonNull String accountId, @NonNull String tokenId)
6262
throws HederaException {
6363
Objects.requireNonNull(accountId, "accountId must not be null");
6464
Objects.requireNonNull(tokenId, "tokenId must not be null");

hedera-spring/src/main/java/com/openelements/hedera/spring/implementation/MirrorNodeClientImpl.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,27 @@ public MirrorNodeClientImpl(final RestClient.Builder restClientBuilder) {
4949
@Override
5050
public Page<Nft> queryNftsByAccount(@NonNull final AccountId accountId) throws HederaException {
5151
Objects.requireNonNull(accountId, "newAccountId must not be null");
52-
final URI uri = URI.create(
53-
getUriPrefix()
54-
+ "/api/v1/accounts/" + accountId + "/nfts");
52+
final String path = "/api/v1/accounts/" + accountId + "/nfts";
5553

5654
final Function<JsonNode, List<Nft>> dataExtractionFunction = node -> getNfts(node);
57-
final Function<JsonNode, URI> nextUriExtractionFunction = node -> getNextUri(node);
55+
5856

59-
return new RestBasedPage<>(objectMapper, restClient,
60-
uri,
61-
dataExtractionFunction, nextUriExtractionFunction);
57+
return new RestBasedPage<>(objectMapper, restClient.mutate().clone(), path,dataExtractionFunction);
6258
}
6359

6460
@Override
65-
public Page<Nft> queryNftsByAccountAndTokenId(@NonNull final AccountId accountId, @NonNull final TokenId tokenId)
66-
throws HederaException {
67-
Objects.requireNonNull(accountId, "newAccountId must not be null");
68-
Objects.requireNonNull(tokenId, "tokenId must not be null");
69-
70-
final URI uri = URI.create(
71-
getUriPrefix()
72-
+ doGetCall ("/api/v1/tokens/" + tokenId + "/nfts", Map.of("account.id", accountId)));
73-
74-
final Function<JsonNode, List<Nft>> dataExtractionFunction = node -> getNfts(node);
75-
final Function<JsonNode, URI> nextUriExtractionFunction = node -> getNextUri(node);
61+
public Page<Nft> queryNftsByAccountAndTokenId(@NonNull final AccountId accountId, @NonNull final TokenId tokenId)
62+
throws HederaException {
63+
Objects.requireNonNull(accountId, "newAccountId must not be null");
64+
Objects.requireNonNull(tokenId, "tokenId must not be null");
7665

77-
return new RestBasedPage<>(objectMapper, restClient,
78-
uri,
79-
dataExtractionFunction, nextUriExtractionFunction);
80-
81-
82-
}
66+
final String path = "/api/v1/tokens/" + tokenId + "/nfts/" + accountId;
67+
68+
final Function<JsonNode, List<Nft>> dataExtractionFunction = node -> getNfts(node);
69+
70+
return new RestBasedPage<>(objectMapper, restClient.mutate().clone(), path, dataExtractionFunction);
71+
72+
}
8373

8474
@Override
8575
public Page<Nft> queryNftsByTokenId(@NonNull TokenId tokenId) throws HederaException {

hedera-spring/src/test/java/com/openelements/hedera/spring/test/NftRepositoryTests.java

Lines changed: 94 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,65 @@ void findByAccountId() throws Exception {
174174
hederaTestUtils.waitForMirrorNodeRecords();
175175

176176
//when
177-
final Page<Nft> slice = nftRepository.findByOwner(newOwner);
178-
final List<Nft> result = getAll(slice);
179-
180-
//then
181-
Assertions.assertNotNull(result);
182-
Assertions.assertEquals(2, result.size());
183-
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(0)));
184-
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(1)));
185-
}
186-
187-
@Test
188-
void findByAccountIdWIthZeroResult() throws Exception {
189-
//given
190-
final String name = "Tokemon cards";
191-
final String symbol = "TOK";
192-
final TokenId tokenId = nftClient.createNftType(name, symbol);
193-
final Account account = accountClient.createAccount();
194-
final AccountId newOwner = account.accountId();
195-
final PrivateKey newOwnerPrivateKey = account.privateKey();
196-
nftClient.associateNft(tokenId, newOwner, newOwnerPrivateKey);
197-
hederaTestUtils.waitForMirrorNodeRecords();
198-
199-
//when
200-
final Page<Nft> slice = nftRepository.findByOwner(newOwner);
177+
final Page<Nft> slice = nftRepository.findByOwner(newOwner);
178+
final List<Nft> result = getAll(slice);
179+
180+
// then
181+
Assertions.assertNotNull(result);
182+
Assertions.assertEquals(2, result.size());
183+
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(0)));
184+
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(1)));
185+
}
186+
187+
@Test
188+
void findByAccountIdForSomePages() throws Exception {
189+
// given
190+
final String name = "Tokemon cards";
191+
final String symbol = "TOK";
192+
193+
final AccountId adminAccountId = adminAccount.accountId();
194+
final PrivateKey adminAccountPrivateKey = adminAccount.privateKey();
195+
final Account account = accountClient.createAccount();
196+
final AccountId newOwner = account.accountId();
197+
final PrivateKey newOwnerPrivateKey = account.privateKey();
198+
final List<byte[]> metadata = IntStream.range(0, 40).mapToObj(i -> "metadata" + i)
199+
.map(s -> s.getBytes(StandardCharsets.UTF_8)).toList();
200+
final TokenId tokenId = nftClient.createNftType(name, symbol);
201+
final int batchSize = 10;
202+
for (int i = 0; i < metadata.size(); i += batchSize) {
203+
final int start = i;
204+
final int end = Math.min(i + batchSize, metadata.size());
205+
final List<Long> serial = nftClient.mintNfts(tokenId, metadata.subList(start, end).toArray(new byte[0][]));
206+
nftClient.transferNft(tokenId, serial.get(i), adminAccountId, adminAccountPrivateKey, newOwner);
207+
208+
}
209+
nftClient.associateNft(tokenId, newOwner, newOwnerPrivateKey);
210+
hederaTestUtils.waitForMirrorNodeRecords();
211+
212+
// when
213+
final Page<Nft> slice = nftRepository.findByOwner(newOwner);
214+
final List<Nft> result = getAll(slice);
215+
216+
// then
217+
Assertions.assertNotNull(result);
218+
Assertions.assertEquals(metadata.size(), result.size());
219+
220+
}
221+
222+
@Test
223+
void findByAccountIdWIthZeroResult() throws Exception {
224+
// given
225+
final String name = "Tokemon cards";
226+
final String symbol = "TOK";
227+
final TokenId tokenId = nftClient.createNftType(name, symbol);
228+
final Account account = accountClient.createAccount();
229+
final AccountId newOwner = account.accountId();
230+
final PrivateKey newOwnerPrivateKey = account.privateKey();
231+
nftClient.associateNft(tokenId, newOwner, newOwnerPrivateKey);
232+
hederaTestUtils.waitForMirrorNodeRecords();
233+
234+
// when
235+
final Page<Nft> slice = nftRepository.findByOwner(newOwner);
201236
final List<Nft> result = getAll(slice);
202237

203238
//then
@@ -235,6 +270,40 @@ void findByTokenIdAndAccountId() throws Exception {
235270
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(0)));
236271
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(1)));
237272
}
273+
274+
@Test
275+
void findByTokenIdAndAccountIdForSomePages() throws Exception {
276+
// given
277+
final String name = "Tokemon cards";
278+
final String symbol = "TOK";
279+
final List<byte[]> metadata = IntStream.range(0, 40).mapToObj(i -> "metadata" + i)
280+
.map(s -> s.getBytes(StandardCharsets.UTF_8)).toList();
281+
final TokenId tokenId = nftClient.createNftType(name, symbol);
282+
283+
final AccountId adminAccountId = adminAccount.accountId();
284+
final PrivateKey adminAccountPrivateKey = adminAccount.privateKey();
285+
final Account account = accountClient.createAccount();
286+
final AccountId newOwner = account.accountId();
287+
final PrivateKey newOwnerPrivateKey = account.privateKey();
288+
final int batchSize = 10;
289+
for (int i = 0; i < metadata.size(); i += batchSize) {
290+
final int start = i;
291+
final int end = Math.min(i + batchSize, metadata.size());
292+
final List<Long> serial = nftClient.mintNfts(tokenId, metadata.subList(start, end).toArray(new byte[0][]));
293+
nftClient.transferNft(tokenId, serial.get(i), adminAccountId, adminAccountPrivateKey, newOwner);
294+
295+
}
296+
nftClient.associateNft(tokenId, newOwner, newOwnerPrivateKey);
297+
hederaTestUtils.waitForMirrorNodeRecords();
298+
299+
// when
300+
final Page<Nft> slice = nftRepository.findByOwnerAndType(newOwner, tokenId);
301+
final List<Nft> result = getAll(slice);
302+
303+
// then
304+
Assertions.assertNotNull(result);
305+
Assertions.assertEquals(metadata.size(), result.size());
306+
}
238307

239308
@Test
240309
void findByTokenIdAndAccountIdWithZeroResult() throws Exception {
@@ -339,5 +408,6 @@ void findByTokenIdAndAccountIdAndSerialWithZeroResult() throws Exception {
339408
//then
340409
Assertions.assertNotNull(result);
341410
Assertions.assertFalse(result.isPresent());
411+
342412
}
343413
}

0 commit comments

Comments
 (0)