Skip to content

Commit

Permalink
Make testEnv implement Autoclosable Interface, replace occurrences (#…
Browse files Browse the repository at this point in the history
…2023)

Signed-off-by: steffenboe <[email protected]>
Co-authored-by: Ivan Ivanov <[email protected]>
  • Loading branch information
steffenboe and 0xivanov authored Oct 10, 2024
1 parent f5c4b31 commit edf21e0
Show file tree
Hide file tree
Showing 71 changed files with 11,815 additions and 11,815 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,60 +37,60 @@ public class AccountAllowanceIntegrationTest {
@Test
@DisplayName("Can spend hbar allowance")
void canSpendHbarAllowance() throws Throwable {
var testEnv = new IntegrationTestEnv(1);
try (var testEnv = new IntegrationTestEnv(1)) {

var aliceKey = PrivateKey.generateED25519();
var aliceId = new AccountCreateTransaction()
.setKey(aliceKey)
.setInitialBalance(new Hbar(10))
.execute(testEnv.client)
.getReceipt(testEnv.client)
.accountId;
var aliceKey = PrivateKey.generateED25519();
var aliceId = new AccountCreateTransaction()
.setKey(aliceKey)
.setInitialBalance(new Hbar(10))
.execute(testEnv.client)
.getReceipt(testEnv.client)
.accountId;

var bobKey = PrivateKey.generateED25519();
var bobId = new AccountCreateTransaction()
.setKey(bobKey)
.setInitialBalance(new Hbar(10))
.execute(testEnv.client)
.getReceipt(testEnv.client)
.accountId;
var bobKey = PrivateKey.generateED25519();
var bobId = new AccountCreateTransaction()
.setKey(bobKey)
.setInitialBalance(new Hbar(10))
.execute(testEnv.client)
.getReceipt(testEnv.client)
.accountId;

Objects.requireNonNull(aliceId);
Objects.requireNonNull(bobId);
Objects.requireNonNull(aliceId);
Objects.requireNonNull(bobId);

new AccountAllowanceApproveTransaction()
.approveHbarAllowance(bobId, aliceId, new Hbar(10))
.freezeWith(testEnv.client)
.sign(bobKey)
.execute(testEnv.client)
.getReceipt(testEnv.client);
new AccountAllowanceApproveTransaction()
.approveHbarAllowance(bobId, aliceId, new Hbar(10))
.freezeWith(testEnv.client)
.sign(bobKey)
.execute(testEnv.client)
.getReceipt(testEnv.client);

var transferRecord = new TransferTransaction()
.addHbarTransfer(testEnv.operatorId, new Hbar(5))
.addApprovedHbarTransfer(bobId, new Hbar(5).negated())
.setTransactionId(TransactionId.generate(aliceId))
.freezeWith(testEnv.client)
.sign(aliceKey)
.execute(testEnv.client)
.getRecord(testEnv.client);
var transferRecord = new TransferTransaction()
.addHbarTransfer(testEnv.operatorId, new Hbar(5))
.addApprovedHbarTransfer(bobId, new Hbar(5).negated())
.setTransactionId(TransactionId.generate(aliceId))
.freezeWith(testEnv.client)
.sign(aliceKey)
.execute(testEnv.client)
.getRecord(testEnv.client);

var transferFound = false;
for (var transfer : transferRecord.transfers) {
if (transfer.accountId.equals(testEnv.operatorId) && transfer.amount.equals(new Hbar(5))) {
transferFound = true;
break;
var transferFound = false;
for (var transfer : transferRecord.transfers) {
if (transfer.accountId.equals(testEnv.operatorId) && transfer.amount.equals(new Hbar(5))) {
transferFound = true;
break;
}
}
}
assertThat(transferFound).isTrue();
assertThat(transferFound).isTrue();

new AccountDeleteTransaction()
.setAccountId(bobId)
.setTransferAccountId(testEnv.operatorId)
.freezeWith(testEnv.client)
.sign(bobKey)
.execute(testEnv.client)
.getReceipt(testEnv.client);
new AccountDeleteTransaction()
.setAccountId(bobId)
.setTransferAccountId(testEnv.operatorId)
.freezeWith(testEnv.client)
.sign(bobKey)
.execute(testEnv.client)
.getReceipt(testEnv.client);

testEnv.close(aliceId, aliceKey);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,114 +149,114 @@ void cannotConnectToPreviewnetWhenNetworkNameIsNullAndCertificateVerificationIsE
@Test
@DisplayName("Can fetch balance for client operator")
void canFetchBalanceForClientOperator() throws Exception {
var testEnv = new IntegrationTestEnv(1);
try (IntegrationTestEnv testEnv = new IntegrationTestEnv(1)) {

var balance = new AccountBalanceQuery()
var balance = new AccountBalanceQuery()
.setAccountId(testEnv.operatorId)
.execute(testEnv.client);

assertThat(balance.hbars.toTinybars() > 0).isTrue();
assertThat(balance.hbars.toTinybars() > 0).isTrue();

testEnv.close();
}
}

@Test
@DisplayName("Can fetch cost for the query")
void getCostBalanceForClientOperator() throws Exception {
var testEnv = new IntegrationTestEnv(1);
try (IntegrationTestEnv testEnv = new IntegrationTestEnv(1)) {

var balance = new AccountBalanceQuery()
.setAccountId(testEnv.operatorId)
.setMaxQueryPayment(new Hbar(1));
var balance = new AccountBalanceQuery()
.setAccountId(testEnv.operatorId)
.setMaxQueryPayment(new Hbar(1));

var cost = balance.getCost(testEnv.client);
var cost = balance.getCost(testEnv.client);

var accBalance = balance.setQueryPayment(cost).execute(testEnv.client);
var accBalance = balance.setQueryPayment(cost).execute(testEnv.client);

assertThat(accBalance.hbars.toTinybars() > 0).isTrue();
assertThat(cost.toTinybars()).isEqualTo(0);
assertThat(accBalance.hbars.toTinybars() > 0).isTrue();
assertThat(cost.toTinybars()).isEqualTo(0);

testEnv.close();
}
}

@Test
@DisplayName("Can fetch cost for the query, big max set")
void getCostBigMaxBalanceForClientOperator() throws Exception {
var testEnv = new IntegrationTestEnv(1);
try (IntegrationTestEnv testEnv = new IntegrationTestEnv(1)) {

var balance = new AccountBalanceQuery()
.setAccountId(testEnv.operatorId)
.setMaxQueryPayment(new Hbar(1000000));
var balance = new AccountBalanceQuery()
.setAccountId(testEnv.operatorId)
.setMaxQueryPayment(new Hbar(1000000));

var cost = balance.getCost(testEnv.client);
var cost = balance.getCost(testEnv.client);

var accBalance = balance.setQueryPayment(cost).execute(testEnv.client);
var accBalance = balance.setQueryPayment(cost).execute(testEnv.client);

assertThat(accBalance.hbars.toTinybars() > 0).isTrue();
assertThat(accBalance.hbars.toTinybars() > 0).isTrue();

testEnv.close();
}
}

@Test
@DisplayName("Can fetch cost for the query, very small max set")
void getCostSmallMaxBalanceForClientOperator() throws Exception {
var testEnv = new IntegrationTestEnv(1);
try (IntegrationTestEnv testEnv = new IntegrationTestEnv(1)) {

var balance = new AccountBalanceQuery()
.setAccountId(testEnv.operatorId)
.setMaxQueryPayment(Hbar.fromTinybars(1));
var balance = new AccountBalanceQuery()
.setAccountId(testEnv.operatorId)
.setMaxQueryPayment(Hbar.fromTinybars(1));

var cost = balance.getCost(testEnv.client);
var cost = balance.getCost(testEnv.client);

var accBalance = balance.setQueryPayment(cost).execute(testEnv.client);
var accBalance = balance.setQueryPayment(cost).execute(testEnv.client);

assertThat(accBalance.hbars.toTinybars() > 0).isTrue();
assertThat(accBalance.hbars.toTinybars() > 0).isTrue();

testEnv.close();
}
}

@Test
@DisplayName("Cannot fetch balance for invalid account ID")
void canNotFetchBalanceForInvalidAccountId() throws Exception {
var testEnv = new IntegrationTestEnv(1);
try (IntegrationTestEnv testEnv = new IntegrationTestEnv(1)) {

assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
new AccountBalanceQuery()
.setAccountId(AccountId.fromString("1.0.3"))
.execute(testEnv.client);
}).withMessageContaining(Status.INVALID_ACCOUNT_ID.toString());
assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
new AccountBalanceQuery()
.setAccountId(AccountId.fromString("1.0.3"))
.execute(testEnv.client);
}).withMessageContaining(Status.INVALID_ACCOUNT_ID.toString());

testEnv.close();
}
}

@Test
@DisplayName("Can fetch token balances for client operator")
void canFetchTokenBalancesForClientOperator() throws Exception {
var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();

var response = new TokenCreateTransaction()
.setTokenName("ffff")
.setTokenSymbol("F")
.setInitialSupply(10000)
.setDecimals(50)
.setTreasuryAccountId(testEnv.operatorId)
.setAdminKey(testEnv.operatorKey)
.setSupplyKey(testEnv.operatorKey)
.setFreezeDefault(false)
.execute(testEnv.client);
try(var testEnv = new IntegrationTestEnv(1).useThrowawayAccount()){

var response = new TokenCreateTransaction()
.setTokenName("ffff")
.setTokenSymbol("F")
.setInitialSupply(10000)
.setDecimals(50)
.setTreasuryAccountId(testEnv.operatorId)
.setAdminKey(testEnv.operatorKey)
.setSupplyKey(testEnv.operatorKey)
.setFreezeDefault(false)
.execute(testEnv.client);

var tokenId = Objects.requireNonNull(response.getReceipt(testEnv.client).tokenId);
var tokenId = Objects.requireNonNull(response.getReceipt(testEnv.client).tokenId);

var query = new AccountBalanceQuery();
var balance = query
.setAccountId(testEnv.operatorId)
.execute(testEnv.client);
var query = new AccountBalanceQuery();
var balance = query
.setAccountId(testEnv.operatorId)
.execute(testEnv.client);

assertThat(balance.tokens.get(tokenId)).isEqualTo(10000);
assertThat(balance.tokenDecimals.get(tokenId)).isEqualTo(50);
assertThat(query.toString()).isNotEmpty();
assertThat(query.getPaymentTransactionId()).isNull();
assertThat(balance.tokens.get(tokenId)).isEqualTo(10000);
assertThat(balance.tokenDecimals.get(tokenId)).isEqualTo(50);
assertThat(query.toString()).isNotEmpty();
assertThat(query.getPaymentTransactionId()).isNull();

testEnv.close(tokenId);
}
}
}
Loading

0 comments on commit edf21e0

Please sign in to comment.