forked from the-nft-company/flow-jvm-sdk
-
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.
Merge pull request #140 from onflow/access-api-code-duplication
Address code duplication in Access API tests and error handling
- Loading branch information
Showing
19 changed files
with
1,185 additions
and
1,440 deletions.
There are no files selected for viewing
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
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
43 changes: 43 additions & 0 deletions
43
...in/java/org/onflow/examples/java/getProtocolState/GetProtocolStateAccessAPIConnector.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,43 @@ | ||
package org.onflow.examples.java.getProtocolState; | ||
|
||
import org.onflow.flow.sdk.FlowAccessApi; | ||
import org.onflow.flow.sdk.FlowId; | ||
import org.onflow.flow.sdk.FlowSnapshot; | ||
|
||
public class GetProtocolStateAccessAPIConnector { | ||
private final FlowAccessApi accessAPI; | ||
|
||
public GetProtocolStateAccessAPIConnector(FlowAccessApi accessAPI) { | ||
this.accessAPI = accessAPI; | ||
} | ||
|
||
public FlowSnapshot getLatestProtocolStateSnapshot() { | ||
FlowAccessApi.AccessApiCallResponse<FlowSnapshot> response = accessAPI.getLatestProtocolStateSnapshot(); | ||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowSnapshot>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
|
||
public FlowSnapshot getProtocolStateSnapshotByBlockId(FlowId blockId) { | ||
FlowAccessApi.AccessApiCallResponse<FlowSnapshot> response = accessAPI.getProtocolStateSnapshotByBlockId(blockId); | ||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowSnapshot>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
|
||
public FlowSnapshot getProtocolStateSnapshotByHeight(Long height) { | ||
FlowAccessApi.AccessApiCallResponse<FlowSnapshot> response = accessAPI.getProtocolStateSnapshotByHeight(height); | ||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowSnapshot>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...ava/org/onflow/examples/java/getProtocolState/GetProtocolStateAccessAPIConnectorTest.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.getProtocolState; | ||
|
||
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 org.onflow.flow.sdk.FlowSnapshot; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json") | ||
public class GetProtocolStateAccessAPIConnectorTest { | ||
@FlowTestClient | ||
private FlowAccessApi accessAPI; | ||
private FlowBlock block; | ||
|
||
private GetProtocolStateAccessAPIConnector protocolStateConnector; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
protocolStateConnector = new GetProtocolStateAccessAPIConnector(accessAPI); | ||
} | ||
|
||
@Test | ||
public void canGetLatestProtocolStateSnapshot() { | ||
FlowSnapshot latestSnapshot = protocolStateConnector.getLatestProtocolStateSnapshot(); | ||
assertNotNull(latestSnapshot, "Latest snapshot should not be null"); | ||
} | ||
|
||
@Test | ||
public void canGetProtocolStateSnapshotByBlockId() { | ||
block = getLatestBlock(); | ||
FlowSnapshot snapshot = protocolStateConnector.getProtocolStateSnapshotByBlockId(block.getId()); | ||
assertNotNull(snapshot, "Snapshot should not be null"); | ||
} | ||
|
||
@Test | ||
public void canGetProtocolStateSnapshotByHeight() { | ||
block = getLatestBlock(); | ||
FlowSnapshot snapshot = protocolStateConnector.getProtocolStateSnapshotByHeight(block.getHeight()); | ||
assertNotNull(snapshot, "Snapshot should not be null"); | ||
} | ||
|
||
private FlowBlock getLatestBlock() { | ||
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getLatestBlock(true, false); | ||
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()); | ||
} | ||
} | ||
} |
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
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
27 changes: 27 additions & 0 deletions
27
.../kotlin/org/onflow/examples/kotlin/getProtocolState/GetProtocolStateAccessAPIConnector.kt
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,27 @@ | ||
package org.onflow.examples.kotlin.getProtocolState | ||
|
||
import org.onflow.flow.sdk.FlowAccessApi | ||
import org.onflow.flow.sdk.FlowId | ||
import org.onflow.flow.sdk.FlowSnapshot | ||
|
||
internal class GetProtocolStateAccessAPIConnector( | ||
private val accessAPI: FlowAccessApi | ||
) { | ||
fun getLatestProtocolStateSnapshot(): FlowSnapshot = | ||
when (val response = accessAPI.getLatestProtocolStateSnapshot()) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
|
||
fun getProtocolStateSnapshotByBlockId(blockId: FlowId): FlowSnapshot = | ||
when (val response = accessAPI.getProtocolStateSnapshotByBlockId(blockId)) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
|
||
fun getProtocolStateSnapshotByHeight(height: Long): FlowSnapshot = | ||
when (val response = accessAPI.getProtocolStateSnapshotByHeight(height)) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
...lin/org/onflow/examples/kotlin/getProtocolState/GetProtocolStateAccessAPIConnectorTest.kt
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,52 @@ | ||
package org.onflow.examples.kotlin.getProtocolState | ||
|
||
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 org.onflow.flow.sdk.FlowSnapshot | ||
|
||
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json") | ||
internal class GetProtocolStateAccessAPIConnectorTest { | ||
@FlowTestClient | ||
lateinit var accessAPI: FlowAccessApi | ||
lateinit var block: FlowBlock | ||
|
||
private lateinit var protocolStateConnector: GetProtocolStateAccessAPIConnector | ||
|
||
@BeforeEach | ||
fun setup() { | ||
protocolStateConnector = GetProtocolStateAccessAPIConnector(accessAPI) | ||
} | ||
|
||
@Test | ||
fun `Can get latest protocol state snapshot`() { | ||
val latestSnapshot: FlowSnapshot = protocolStateConnector.getLatestProtocolStateSnapshot() | ||
assertNotNull(latestSnapshot, "Latest snapshot should not be null") | ||
} | ||
|
||
@Test | ||
fun `Can get protocol state snapshot by blockId`() { | ||
block = when (val response = accessAPI.getLatestBlock()) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
|
||
val latestSnapshot: FlowSnapshot = protocolStateConnector.getProtocolStateSnapshotByBlockId(block.id) | ||
assertNotNull(latestSnapshot, ("Snapshot should not be null")) | ||
} | ||
|
||
@Test | ||
fun `Can get protocol state snapshot by height`() { | ||
block = when (val response = accessAPI.getLatestBlock()) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
|
||
val latestSnapshot: FlowSnapshot = protocolStateConnector.getProtocolStateSnapshotByHeight(block.height) | ||
assertNotNull(latestSnapshot, ("Snapshot should not be null")) | ||
} | ||
} |
Oops, something went wrong.