Skip to content

Commit a8a17a4

Browse files
Merge pull request #140 from onflow/access-api-code-duplication
Address code duplication in Access API tests and error handling
2 parents 662759b + 36d0930 commit a8a17a4

File tree

19 files changed

+1185
-1440
lines changed

19 files changed

+1185
-1440
lines changed

java-example/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Below is a list of all Java code examples currently supported in this repo:
7070

7171
[Get collections by ID.](src/main/java/org/onflow/examples/java/getCollection/GetCollectionAccessAPIConnector.java)
7272

73+
- Get collection by id
74+
- Get full collection by id (returns all transactions in collection response)
75+
7376
#### Get Execution Data
7477

7578
[Get execution data by block ID.](src/main/java/org/onflow/examples/java/getExecutionData/GetExecutionDataAccessAPIConnector.java)

java-example/src/main/java/org/onflow/examples/java/getCollection/GetCollectionAccessAPIConnector.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.onflow.flow.sdk.FlowAccessApi.AccessApiCallResponse;
55
import org.onflow.flow.sdk.FlowCollection;
66
import org.onflow.flow.sdk.FlowId;
7+
import org.onflow.flow.sdk.FlowTransaction;
8+
9+
import java.util.List;
710

811
public class GetCollectionAccessAPIConnector {
912
private final FlowAccessApi accessAPI;
@@ -21,4 +24,14 @@ public FlowCollection getCollectionById(FlowId collectionId) {
2124
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
2225
}
2326
}
27+
28+
public List<FlowTransaction> getFullCollectionById(FlowId collectionId) {
29+
AccessApiCallResponse<List<FlowTransaction>> response = accessAPI.getFullCollectionById(collectionId);
30+
if (response instanceof AccessApiCallResponse.Success) {
31+
return ((AccessApiCallResponse.Success<List<FlowTransaction>>) response).getData();
32+
} else {
33+
AccessApiCallResponse.Error errorResponse = (AccessApiCallResponse.Error) response;
34+
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
35+
}
36+
}
2437
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.onflow.examples.java.getProtocolState;
2+
3+
import org.onflow.flow.sdk.FlowAccessApi;
4+
import org.onflow.flow.sdk.FlowId;
5+
import org.onflow.flow.sdk.FlowSnapshot;
6+
7+
public class GetProtocolStateAccessAPIConnector {
8+
private final FlowAccessApi accessAPI;
9+
10+
public GetProtocolStateAccessAPIConnector(FlowAccessApi accessAPI) {
11+
this.accessAPI = accessAPI;
12+
}
13+
14+
public FlowSnapshot getLatestProtocolStateSnapshot() {
15+
FlowAccessApi.AccessApiCallResponse<FlowSnapshot> response = accessAPI.getLatestProtocolStateSnapshot();
16+
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
17+
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowSnapshot>) response).getData();
18+
} else {
19+
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response;
20+
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
21+
}
22+
}
23+
24+
public FlowSnapshot getProtocolStateSnapshotByBlockId(FlowId blockId) {
25+
FlowAccessApi.AccessApiCallResponse<FlowSnapshot> response = accessAPI.getProtocolStateSnapshotByBlockId(blockId);
26+
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
27+
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowSnapshot>) response).getData();
28+
} else {
29+
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response;
30+
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
31+
}
32+
}
33+
34+
public FlowSnapshot getProtocolStateSnapshotByHeight(Long height) {
35+
FlowAccessApi.AccessApiCallResponse<FlowSnapshot> response = accessAPI.getProtocolStateSnapshotByHeight(height);
36+
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
37+
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowSnapshot>) response).getData();
38+
} else {
39+
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response;
40+
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
41+
}
42+
}
43+
}

java-example/src/test/java/org/onflow/examples/java/getCollection/GetCollectionAccessAPIConnectorTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.onflow.flow.sdk.crypto.Crypto;
1212
import org.onflow.flow.sdk.crypto.PublicKey;
1313

14+
import java.util.List;
15+
1416
import static org.junit.jupiter.api.Assertions.*;
1517

1618
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
@@ -51,4 +53,16 @@ public void canFetchCollectionById() {
5153
assertNotNull(collection, "Collection should not be null");
5254
assertEquals(collectionId, collection.getId(), "Collection ID should match the fetched collection ID");
5355
}
56+
57+
@Test
58+
public void canFetchFullCollectionById() {
59+
List<FlowTransaction> fullCollectionResponse = connector.getFullCollectionById(collectionId);
60+
61+
assertNotNull(fullCollectionResponse, "Collection transactions should not be null");
62+
assertFalse(fullCollectionResponse.isEmpty(), "Collection transactions should not be empty");
63+
64+
FlowTransaction firstTransaction = fullCollectionResponse.get(0);
65+
assertNotNull(firstTransaction.getId(), "Transaction ID should not be null");
66+
assertNotNull(firstTransaction.getScript(), "Transaction script should not be null");
67+
}
5468
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.onflow.examples.java.getProtocolState;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.onflow.flow.common.test.FlowEmulatorProjectTest;
6+
import org.onflow.flow.common.test.FlowTestClient;
7+
import org.onflow.flow.sdk.FlowAccessApi;
8+
import org.onflow.flow.sdk.FlowBlock;
9+
import org.onflow.flow.sdk.FlowSnapshot;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
14+
public class GetProtocolStateAccessAPIConnectorTest {
15+
@FlowTestClient
16+
private FlowAccessApi accessAPI;
17+
private FlowBlock block;
18+
19+
private GetProtocolStateAccessAPIConnector protocolStateConnector;
20+
21+
@BeforeEach
22+
public void setup() {
23+
protocolStateConnector = new GetProtocolStateAccessAPIConnector(accessAPI);
24+
}
25+
26+
@Test
27+
public void canGetLatestProtocolStateSnapshot() {
28+
FlowSnapshot latestSnapshot = protocolStateConnector.getLatestProtocolStateSnapshot();
29+
assertNotNull(latestSnapshot, "Latest snapshot should not be null");
30+
}
31+
32+
@Test
33+
public void canGetProtocolStateSnapshotByBlockId() {
34+
block = getLatestBlock();
35+
FlowSnapshot snapshot = protocolStateConnector.getProtocolStateSnapshotByBlockId(block.getId());
36+
assertNotNull(snapshot, "Snapshot should not be null");
37+
}
38+
39+
@Test
40+
public void canGetProtocolStateSnapshotByHeight() {
41+
block = getLatestBlock();
42+
FlowSnapshot snapshot = protocolStateConnector.getProtocolStateSnapshotByHeight(block.getHeight());
43+
assertNotNull(snapshot, "Snapshot should not be null");
44+
}
45+
46+
private FlowBlock getLatestBlock() {
47+
FlowAccessApi.AccessApiCallResponse<FlowBlock> response = accessAPI.getLatestBlock(true, false);
48+
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
49+
return ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) response).getData();
50+
} else {
51+
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response;
52+
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
53+
}
54+
}
55+
}

kotlin-example/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ Below is a list of all Kotlin code examples currently supported in this repo:
7171

7272
[Get collections by ID.](src/main/kotlin/org/onflow/examples/kotlin/getCollection/GetCollectionAccessAPIConnector.kt)
7373

74+
- Get collection by id
75+
- Get full collection by id (returns all transactions in collection response)
76+
7477
#### Get Execution Data
7578

7679
[Get execution data by block ID.](src/main/kotlin/org/onflow/examples/kotlin/getExecutionData/GetExecutionDataAccessAPIConnector.kt)

kotlin-example/src/main/kotlin/org/onflow/examples/kotlin/getCollection/GetCollectionAccessAPIConnector.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ class GetCollectionAccessAPIConnector(
1111
is AccessApiCallResponse.Success -> response.data
1212
is AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
1313
}
14+
15+
fun getFullCollectionById(collectionId: FlowId): List<FlowTransaction> =
16+
when (val response = accessAPI.getFullCollectionById(collectionId)) {
17+
is AccessApiCallResponse.Success -> response.data
18+
is AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
19+
}
1420
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.onflow.examples.kotlin.getProtocolState
2+
3+
import org.onflow.flow.sdk.FlowAccessApi
4+
import org.onflow.flow.sdk.FlowId
5+
import org.onflow.flow.sdk.FlowSnapshot
6+
7+
internal class GetProtocolStateAccessAPIConnector(
8+
private val accessAPI: FlowAccessApi
9+
) {
10+
fun getLatestProtocolStateSnapshot(): FlowSnapshot =
11+
when (val response = accessAPI.getLatestProtocolStateSnapshot()) {
12+
is FlowAccessApi.AccessApiCallResponse.Success -> response.data
13+
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
14+
}
15+
16+
fun getProtocolStateSnapshotByBlockId(blockId: FlowId): FlowSnapshot =
17+
when (val response = accessAPI.getProtocolStateSnapshotByBlockId(blockId)) {
18+
is FlowAccessApi.AccessApiCallResponse.Success -> response.data
19+
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
20+
}
21+
22+
fun getProtocolStateSnapshotByHeight(height: Long): FlowSnapshot =
23+
when (val response = accessAPI.getProtocolStateSnapshotByHeight(height)) {
24+
is FlowAccessApi.AccessApiCallResponse.Success -> response.data
25+
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
26+
}
27+
}

kotlin-example/src/test/kotlin/org/onflow/examples/kotlin/getCollection/GetCollectionAccessAPIConnectorTest.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import org.onflow.flow.common.test.FlowEmulatorProjectTest
88
import org.onflow.flow.common.test.FlowServiceAccountCredentials
99
import org.onflow.flow.common.test.FlowTestClient
1010
import org.onflow.flow.common.test.TestAccount
11-
import org.onflow.flow.sdk.FlowAccessApi
12-
import org.onflow.flow.sdk.FlowCollection
13-
import org.onflow.flow.sdk.FlowId
14-
import org.onflow.flow.sdk.SignatureAlgorithm
11+
import org.onflow.flow.sdk.*
1512
import org.onflow.flow.sdk.crypto.Crypto
1613

1714
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
@@ -53,4 +50,16 @@ class GetCollectionAccessAPIConnectorTest {
5350
assertNotNull(collection, "Collection should not be null")
5451
assertEquals(collectionId, collection.id, "Collection ID should match the fetched collection ID")
5552
}
53+
54+
@Test
55+
fun `Can fetch full collection by ID`() {
56+
val collectionTransactions: List<FlowTransaction> = connector.getFullCollectionById(collectionId)
57+
58+
assertNotNull(collectionTransactions, "Collection transactions should not be null")
59+
assertTrue(collectionTransactions.isNotEmpty(), "Collection transactions should not be empty")
60+
61+
val firstTransaction = collectionTransactions.first()
62+
assertNotNull(firstTransaction.id, "Transaction ID should not be null")
63+
assertNotNull(firstTransaction.script, "Transaction script should not be null")
64+
}
5665
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.onflow.flow.sdk.FlowAccessApi
99
import org.onflow.flow.sdk.FlowChainId
1010

1111
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
12-
internal class GetNetworkParametersAccessAPIConnectorTest {
12+
internal class GetNetworkParamsAccessAPIConnectorTest {
1313
@FlowTestClient
1414
lateinit var accessAPI: FlowAccessApi
1515

0 commit comments

Comments
 (0)