|
| 1 | +package org.onflow.examples.java.getAccountBalance; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Assertions; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.onflow.flow.common.test.FlowEmulatorProjectTest; |
| 7 | +import org.onflow.flow.common.test.FlowServiceAccountCredentials; |
| 8 | +import org.onflow.flow.common.test.FlowTestClient; |
| 9 | +import org.onflow.flow.common.test.TestAccount; |
| 10 | +import org.onflow.flow.sdk.FlowAccessApi; |
| 11 | +import org.onflow.flow.sdk.FlowAddress; |
| 12 | +import org.onflow.flow.sdk.FlowBlock; |
| 13 | + |
| 14 | +@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json") |
| 15 | +public class GetAccountBalanceAccessAPIConnectorTest { |
| 16 | + |
| 17 | + @FlowTestClient |
| 18 | + private FlowAccessApi accessAPI; |
| 19 | + |
| 20 | + @FlowServiceAccountCredentials |
| 21 | + private TestAccount serviceAccount; |
| 22 | + |
| 23 | + private GetAccountBalanceAccessAPIConnector balanceAPIConnector; |
| 24 | + |
| 25 | + @BeforeEach |
| 26 | + public void setup() { |
| 27 | + balanceAPIConnector = new GetAccountBalanceAccessAPIConnector(accessAPI); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testCanFetchBalanceAtLatestBlock() { |
| 32 | + FlowAddress address = serviceAccount.getFlowAddress(); |
| 33 | + long balance = balanceAPIConnector.getBalanceAtLatestBlock(address); |
| 34 | + |
| 35 | + Assertions.assertTrue(balance >= 0, "Balance at the latest block should be non-negative"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testCanFetchBalanceAtSpecificBlockHeight() { |
| 40 | + FlowAddress address = serviceAccount.getFlowAddress(); |
| 41 | + |
| 42 | + FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true); |
| 43 | + |
| 44 | + if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) { |
| 45 | + FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData(); |
| 46 | + long blockHeight = latestBlock.getHeight(); |
| 47 | + long balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, blockHeight); |
| 48 | + |
| 49 | + Assertions.assertTrue(balanceAtHeight >= 0, "Balance at specific block height should be non-negative"); |
| 50 | + } else { |
| 51 | + FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) latestBlockResponse; |
| 52 | + Assertions.fail("Failed to fetch the latest block: " + errorResponse.getMessage()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testBalancesAtLatestBlockAndSpecificHeightShouldMatch() { |
| 58 | + FlowAddress address = serviceAccount.getFlowAddress(); |
| 59 | + |
| 60 | + long balanceAtLatest = balanceAPIConnector.getBalanceAtLatestBlock(address); |
| 61 | + FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true); |
| 62 | + |
| 63 | + if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) { |
| 64 | + FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData(); |
| 65 | + long blockHeight = latestBlock.getHeight(); |
| 66 | + |
| 67 | + // Fetch balance at the same block height |
| 68 | + long balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, blockHeight); |
| 69 | + |
| 70 | + // Ensure balances match |
| 71 | + Assertions.assertEquals(balanceAtLatest, balanceAtHeight, "Balance at latest block and specific block height should match"); |
| 72 | + } else { |
| 73 | + FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) latestBlockResponse; |
| 74 | + Assertions.fail("Failed to fetch the latest block: " + errorResponse.getMessage()); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments