Skip to content

Commit c1cef4c

Browse files
committed
tests fixed
1 parent e4621ab commit c1cef4c

File tree

4 files changed

+150
-125
lines changed

4 files changed

+150
-125
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.openelements.hedera.base;
2+
3+
import com.hedera.hashgraph.sdk.Client;
4+
import com.hedera.hashgraph.sdk.Hbar;
5+
import com.openelements.hedera.base.protocol.TransactionType;
6+
import java.time.Duration;
7+
import org.jspecify.annotations.NonNull;
8+
9+
public interface HederaContext {
10+
11+
@NonNull
12+
Account getOperatorAccount();
13+
14+
@NonNull
15+
Client getClient();
16+
17+
@NonNull
18+
Hbar getDefaultMaxTransactionFee();
19+
20+
@NonNull
21+
Duration getDefaultTransactionValidDuration();
22+
23+
void setDefaultMaxTransactionFee(@NonNull Hbar maxTransactionFee);
24+
25+
void setDefaultTransactionValidDuration(@NonNull Duration transactionValidDuration);
26+
27+
@NonNull
28+
Hbar getDefaultMaxTransactionFee(@NonNull TransactionType transactionType);
29+
30+
@NonNull
31+
Duration getDefaultTransactionValidDuration(@NonNull TransactionType transactionType);
32+
33+
void setDefaultMaxTransactionFee(@NonNull TransactionType transactionType, @NonNull Hbar maxTransactionFee);
34+
35+
void setDefaultTransactionValidDuration(@NonNull TransactionType transactionType,
36+
@NonNull Duration transactionValidDuration);
37+
}

hedera-base/src/main/java/com/openelements/hedera/base/implementation/ProtocolLayerClientImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public TopicDeleteResult executeTopicDeleteTransaction(@NonNull final TopicDelet
332332
final TransactionReceipt receipt = executeTransactionAndWaitOnReceipt(transaction);
333333
return new TopicDeleteResult(receipt.transactionId, receipt.status);
334334
} catch (final Exception e) {
335-
throw new HederaException("Failed to execute create topic transaction", e);
335+
throw new HederaException("Failed to execute delete topic transaction", e);
336336
}
337337
}
338338

@@ -348,7 +348,7 @@ public TopicSubmitMessageResult executeTopicMessageSubmitTransaction(
348348
final TransactionReceipt receipt = executeTransactionAndWaitOnReceipt(transaction);
349349
return new TopicSubmitMessageResult(receipt.transactionId, receipt.status);
350350
} catch (final Exception e) {
351-
throw new HederaException("Failed to execute create topic transaction", e);
351+
throw new HederaException("Failed to execute submit message transaction", e);
352352
}
353353
}
354354

@@ -368,7 +368,7 @@ public TokenCreateResult executeTokenCreateTransaction(@NonNull final TokenCreat
368368
final TransactionReceipt receipt = executeTransactionAndWaitOnReceipt(transaction);
369369
return new TokenCreateResult(receipt.transactionId, receipt.status, receipt.tokenId);
370370
} catch (final Exception e) {
371-
throw new HederaException("Failed to execute create topic transaction", e);
371+
throw new HederaException("Failed to execute create token transaction", e);
372372
}
373373
}
374374

@@ -385,7 +385,7 @@ public TokenAssociateResult executeTokenAssociateTransaction(@NonNull final Toke
385385
final TransactionReceipt receipt = executeTransactionAndWaitOnReceipt(transaction);
386386
return new TokenAssociateResult(receipt.transactionId, receipt.status);
387387
} catch (final Exception e) {
388-
throw new HederaException("Failed to execute create topic transaction", e);
388+
throw new HederaException("Failed to execute associate token transaction", e);
389389
}
390390
}
391391

@@ -407,7 +407,7 @@ public TokenBurnResult executeBurnTokenTransaction(@NonNull final TokenBurnReque
407407
final TransactionReceipt receipt = executeTransactionAndWaitOnReceipt(transaction);
408408
return new TokenBurnResult(receipt.transactionId, receipt.status);
409409
} catch (final Exception e) {
410-
throw new HederaException("Failed to execute create topic transaction", e);
410+
throw new HederaException("Failed to execute burn token transaction", e);
411411
}
412412
}
413413

@@ -429,7 +429,7 @@ public TokenMintResult executeMintTokenTransaction(@NonNull final TokenMintReque
429429
final TransactionReceipt receipt = executeTransactionAndWaitOnReceipt(transaction);
430430
return new TokenMintResult(receipt.transactionId, receipt.status, receipt.serials);
431431
} catch (final Exception e) {
432-
throw new HederaException("Failed to execute create topic transaction", e);
432+
throw new HederaException("Failed to execute mint token transaction", e);
433433
}
434434
}
435435

@@ -447,7 +447,7 @@ public TokenTransferResult executeTransferTransactionForNft(@NonNull final Token
447447
final TransactionReceipt receipt = executeTransactionAndWaitOnReceipt(transaction);
448448
return new TokenTransferResult(receipt.transactionId, receipt.status);
449449
} catch (final Exception e) {
450-
throw new HederaException("Failed to execute create topic transaction", e);
450+
throw new HederaException("Failed to execute transfer nft transaction", e);
451451
}
452452
}
453453

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,27 @@ public MirrorNodeClientImpl(final RestClient.Builder restClientBuilder) {
4343
Objects.requireNonNull(restClientBuilder, "restClientBuilder must not be null");
4444
objectMapper = new ObjectMapper();
4545
restClient = restClientBuilder.build();
46-
4746
}
4847

4948
@Override
5049
public Page<Nft> queryNftsByAccount(@NonNull final AccountId accountId) throws HederaException {
5150
Objects.requireNonNull(accountId, "newAccountId must not be null");
5251
final String path = "/api/v1/accounts/" + accountId + "/nfts";
53-
5452
final Function<JsonNode, List<Nft>> dataExtractionFunction = node -> getNfts(node);
55-
56-
57-
return new RestBasedPage<>(objectMapper, restClient.mutate().clone(), path,dataExtractionFunction);
53+
return new RestBasedPage<>(objectMapper, restClient.mutate().clone(), path, dataExtractionFunction);
5854
}
5955

6056
@Override
61-
public Page<Nft> queryNftsByAccountAndTokenId(@NonNull final AccountId accountId, @NonNull final TokenId tokenId)
62-
throws HederaException {
63-
Objects.requireNonNull(accountId, "accountId must not be null");
64-
Objects.requireNonNull(tokenId, "tokenId must not be null");
65-
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-
}
57+
public Page<Nft> queryNftsByAccountAndTokenId(@NonNull final AccountId accountId, @NonNull final TokenId tokenId) {
58+
Objects.requireNonNull(accountId, "accountId must not be null");
59+
Objects.requireNonNull(tokenId, "tokenId must not be null");
60+
final String path = "/api/v1/tokens/" + tokenId + "/nfts/?account.id=" + accountId;
61+
final Function<JsonNode, List<Nft>> dataExtractionFunction = node -> getNfts(node);
62+
return new RestBasedPage<>(objectMapper, restClient.mutate().clone(), path, dataExtractionFunction);
63+
}
7364

7465
@Override
75-
public Page<Nft> queryNftsByTokenId(@NonNull TokenId tokenId) throws HederaException {
66+
public Page<Nft> queryNftsByTokenId(@NonNull TokenId tokenId) {
7667
final String path = "/api/v1/tokens/" + tokenId + "/nfts";
7768
final Function<JsonNode, List<Nft>> dataExtractionFunction = node -> getNfts(node);
7869
return new RestBasedPage<>(objectMapper, restClient.mutate().clone(), path, dataExtractionFunction);

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

Lines changed: 97 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class NftRepositoryTests {
3535
private AccountClient accountClient;
3636

3737
@Autowired
38-
private Account adminAccount;
38+
private Account operatorAccount;
3939

4040
private <T> List<T> getAll(Page<T> page) {
4141
if (!page.isFirst()) {
@@ -163,8 +163,8 @@ void findByAccountId() throws Exception {
163163
final byte[] metadata2 = "https://example.com/metadata2".getBytes(StandardCharsets.UTF_8);
164164
final TokenId tokenId = nftClient.createNftType(name, symbol);
165165
final List<Long> serial = nftClient.mintNfts(tokenId, metadata1, metadata2);
166-
final AccountId adminAccountId = adminAccount.accountId();
167-
final PrivateKey adminAccountPrivateKey = adminAccount.privateKey();
166+
final AccountId adminAccountId = operatorAccount.accountId();
167+
final PrivateKey adminAccountPrivateKey = operatorAccount.privateKey();
168168
final Account account = accountClient.createAccount();
169169
final AccountId newOwner = account.accountId();
170170
final PrivateKey newOwnerPrivateKey = account.privateKey();
@@ -174,65 +174,64 @@ 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 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);
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 List<byte[]> metadata = IntStream.range(0, 40).mapToObj(i -> "metadata" + i)
194+
.map(s -> s.getBytes(StandardCharsets.UTF_8)).toList();
195+
final TokenId tokenId = nftClient.createNftType(name, symbol);
196+
197+
final Account newOwnerAccount = accountClient.createAccount();
198+
final PrivateKey newOwnerPrivateKey = newOwnerAccount.privateKey();
199+
final AccountId newOwnerId = newOwnerAccount.accountId();
200+
nftClient.associateNft(tokenId, newOwnerId, newOwnerPrivateKey);
201+
202+
final int transferBatchSize = 10;
203+
for (int i = 0; i < metadata.size(); i += transferBatchSize) {
204+
final int start = i;
205+
final int end = Math.min(i + transferBatchSize, metadata.size());
206+
final List<Long> serial = nftClient.mintNfts(tokenId, metadata.subList(start, end).toArray(new byte[0][]));
207+
nftClient.transferNfts(tokenId, serial, operatorAccount, newOwnerId);
208+
}
209+
hederaTestUtils.waitForMirrorNodeRecords();
210+
211+
// when
212+
final Page<Nft> slice = nftRepository.findByOwner(newOwnerId);
213+
final List<Nft> result = getAll(slice);
214+
215+
// then
216+
Assertions.assertNotNull(result);
217+
Assertions.assertEquals(metadata.size(), result.size());
218+
219+
}
220+
221+
@Test
222+
void findByAccountIdWIthZeroResult() throws Exception {
223+
// given
224+
final String name = "Tokemon cards";
225+
final String symbol = "TOK";
226+
final TokenId tokenId = nftClient.createNftType(name, symbol);
227+
final Account account = accountClient.createAccount();
228+
final AccountId newOwner = account.accountId();
229+
final PrivateKey newOwnerPrivateKey = account.privateKey();
230+
nftClient.associateNft(tokenId, newOwner, newOwnerPrivateKey);
231+
hederaTestUtils.waitForMirrorNodeRecords();
232+
233+
// when
234+
final Page<Nft> slice = nftRepository.findByOwner(newOwner);
236235
final List<Nft> result = getAll(slice);
237236

238237
//then
@@ -250,8 +249,8 @@ void findByTokenIdAndAccountId() throws Exception {
250249
final byte[] metadata2 = "https://example.com/metadata2".getBytes(StandardCharsets.UTF_8);
251250
final TokenId tokenId = nftClient.createNftType(name, symbol);
252251
final List<Long> serial = nftClient.mintNfts(tokenId, metadata1, metadata2);
253-
final AccountId adminAccountId = adminAccount.accountId();
254-
final PrivateKey adminAccountPrivateKey = adminAccount.privateKey();
252+
final AccountId adminAccountId = operatorAccount.accountId();
253+
final PrivateKey adminAccountPrivateKey = operatorAccount.privateKey();
255254
final Account account = accountClient.createAccount();
256255
final AccountId newOwner = account.accountId();
257256
final PrivateKey newOwnerPrivateKey = account.privateKey();
@@ -270,40 +269,38 @@ void findByTokenIdAndAccountId() throws Exception {
270269
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(0)));
271270
Assertions.assertTrue(result.stream().anyMatch(nft -> nft.serial() == serial.get(1)));
272271
}
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-
}
272+
273+
@Test
274+
void findByTokenIdAndAccountIdForSomePages() throws Exception {
275+
// given
276+
final String name = "Tokemon cards";
277+
final String symbol = "TOK";
278+
final List<byte[]> metadata = IntStream.range(0, 40).mapToObj(i -> "metadata" + i)
279+
.map(s -> s.getBytes(StandardCharsets.UTF_8)).toList();
280+
final TokenId tokenId = nftClient.createNftType(name, symbol);
281+
282+
final Account newOwnerAccount = accountClient.createAccount();
283+
final AccountId newOwnerId = newOwnerAccount.accountId();
284+
final PrivateKey newOwnerPrivateKey = newOwnerAccount.privateKey();
285+
nftClient.associateNft(tokenId, newOwnerId, newOwnerPrivateKey);
286+
287+
final int transferBatchSize = 10;
288+
for (int i = 0; i < metadata.size(); i += transferBatchSize) {
289+
final int start = i;
290+
final int end = Math.min(i + transferBatchSize, metadata.size());
291+
final List<Long> serial = nftClient.mintNfts(tokenId, metadata.subList(start, end).toArray(new byte[0][]));
292+
nftClient.transferNfts(tokenId, serial, operatorAccount, newOwnerId);
293+
}
294+
hederaTestUtils.waitForMirrorNodeRecords();
295+
296+
// when
297+
final Page<Nft> slice = nftRepository.findByOwnerAndType(newOwnerId, tokenId);
298+
final List<Nft> result = getAll(slice);
299+
300+
// then
301+
Assertions.assertNotNull(result);
302+
Assertions.assertEquals(metadata.size(), result.size());
303+
}
307304

308305
@Test
309306
void findByTokenIdAndAccountIdWithZeroResult() throws Exception {
@@ -372,8 +369,8 @@ void findByTokenIdAndAccountIdAndSerial() throws Exception {
372369
final byte[] metadata = "https://example.com/metadata1".getBytes(StandardCharsets.UTF_8);
373370
final TokenId tokenId = nftClient.createNftType(name, symbol);
374371
final long serial = nftClient.mintNft(tokenId, metadata);
375-
final AccountId adminAccountId = adminAccount.accountId();
376-
final PrivateKey adminAccountPrivateKey = adminAccount.privateKey();
372+
final AccountId adminAccountId = operatorAccount.accountId();
373+
final PrivateKey adminAccountPrivateKey = operatorAccount.privateKey();
377374
final Account account = accountClient.createAccount();
378375
final AccountId newOwner = account.accountId();
379376
final PrivateKey newOwnerPrivateKey = account.privateKey();

0 commit comments

Comments
 (0)