-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
799fa4d
commit fe68568
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...-example/src/main/java/org/onflow/examples/java/get_block/GetBlockAccessAPIConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.onflow.examples.java.get_block; | ||
|
||
import org.onflow.flow.sdk.*; | ||
|
||
public class GetBlockAccessAPIConnector { | ||
|
||
private final FlowAccessApi accessAPI; | ||
|
||
public GetBlockAccessAPIConnector(FlowAccessApi accessAPI) { | ||
this.accessAPI = accessAPI; | ||
} | ||
|
||
public FlowBlock getLatestSealedBlock() { | ||
boolean isSealed = true; | ||
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getLatestBlock(isSealed); | ||
|
||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
|
||
public FlowBlock getBlockByID(FlowId blockID) { | ||
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getBlockById(blockID); | ||
|
||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
|
||
public FlowBlock getBlockByHeight(long height) { | ||
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getBlockByHeight(height); | ||
|
||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...mple/src/test/java/org/onflow/examples/java/get_block/GetBlockAccessAPIConnectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.onflow.examples.java.get_block; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.onflow.flow.common.test.FlowEmulatorProjectTest; | ||
import org.onflow.flow.common.test.FlowTestClient; | ||
import org.onflow.flow.sdk.FlowAccessApi; | ||
import org.onflow.flow.sdk.FlowBlock; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json") | ||
public class GetBlockAccessAPIConnectorTest { | ||
|
||
@FlowTestClient | ||
private FlowAccessApi accessAPI; | ||
|
||
private GetBlockAccessAPIConnector blockAPIConnector; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
blockAPIConnector = new GetBlockAccessAPIConnector(accessAPI); | ||
} | ||
|
||
@Test | ||
public void testCanFetchLatestSealedBlock() { | ||
FlowBlock block = blockAPIConnector.getLatestSealedBlock(); | ||
|
||
Assertions.assertNotNull(block.getId(), "Block ID should not be null"); | ||
Assertions.assertTrue(block.getHeight() >= 0, "Block height should be non-negative"); | ||
Assertions.assertTrue(block.getTimestamp().isBefore(LocalDateTime.now()), "Block timestamp should be in the past"); | ||
} | ||
|
||
@Test | ||
public void testCanFetchBlockByID() { | ||
FlowBlock latestBlock = blockAPIConnector.getLatestSealedBlock(); | ||
FlowBlock blockByID = blockAPIConnector.getBlockByID(latestBlock.getId()); | ||
|
||
Assertions.assertEquals(latestBlock.getId(), blockByID.getId(), "Block IDs should match"); | ||
Assertions.assertEquals(latestBlock.getHeight(), blockByID.getHeight(), "Block heights should match"); | ||
Assertions.assertEquals(latestBlock.getTimestamp(), blockByID.getTimestamp(), "Block timestamps should match"); | ||
Assertions.assertEquals(latestBlock.getCollectionGuarantees().size(), blockByID.getCollectionGuarantees().size(), "Block should have the same number of collection guarantees"); | ||
} | ||
|
||
@Test | ||
public void testCanFetchBlockByHeight() { | ||
FlowBlock latestBlock = blockAPIConnector.getLatestSealedBlock(); | ||
FlowBlock blockByHeight = blockAPIConnector.getBlockByHeight(latestBlock.getHeight()); | ||
|
||
Assertions.assertEquals(latestBlock.getId(), blockByHeight.getId(), "Block ID fetched by height should match the latest block ID"); | ||
Assertions.assertEquals(latestBlock.getTimestamp(), blockByHeight.getTimestamp(), "Block timestamps should match for the latest block and the block fetched by height"); | ||
Assertions.assertEquals(latestBlock.getCollectionGuarantees().size(), blockByHeight.getCollectionGuarantees().size(), "Block should have the same number of collection guarantees"); | ||
} | ||
} |