From 1720afc3b4642e6b45447f08ab8ee0bda7fdd5b1 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Fri, 1 Nov 2024 11:11:51 +0200 Subject: [PATCH 1/2] chore: remove IllegalStateException Signed-off-by: Ivan Ivanov --- sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java b/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java index b8810ab6d..42c27806c 100644 --- a/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java +++ b/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java @@ -616,8 +616,7 @@ void setNodesFromNodeAccountIds(Client client) { @Nullable var nodeProxies = client.network.getNodeProxies(accountId); if (nodeProxies == null || nodeProxies.size() == 0) { - throw new IllegalStateException( - "Some node account IDs did not map to valid nodes in the client's network"); + continue; } var node = nodeProxies.get(random.nextInt(nodeProxies.size())); From f7e78d7fe746b9aed6d28119f26f1135e422ab0f Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Tue, 5 Nov 2024 17:08:42 +0200 Subject: [PATCH 2/2] test: add integration test Signed-off-by: Ivan Ivanov --- .../com/hedera/hashgraph/sdk/Executable.java | 8 ++- .../integration/ClientIntegrationTest.java | 53 +++++++++++++++---- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java b/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java index 42c27806c..4bb6a01fa 100644 --- a/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java +++ b/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java @@ -601,7 +601,7 @@ void setNodesFromNodeAccountIds(Client client) { // failure the system can retry with different proxy on each attempt if (nodeAccountIds.size() == 1) { var nodeProxies = client.network.getNodeProxies(nodeAccountIds.get(0)); - if (nodeProxies == null || nodeProxies.size() == 0) { + if (nodeProxies == null || nodeProxies.isEmpty()) { throw new IllegalStateException("Account ID did not map to valid node in the client's network"); } @@ -615,7 +615,7 @@ void setNodesFromNodeAccountIds(Client client) { for (var accountId : nodeAccountIds) { @Nullable var nodeProxies = client.network.getNodeProxies(accountId); - if (nodeProxies == null || nodeProxies.size() == 0) { + if (nodeProxies == null || nodeProxies.isEmpty()) { continue; } @@ -623,6 +623,10 @@ void setNodesFromNodeAccountIds(Client client) { nodes.add(Objects.requireNonNull(node)); } + if (nodes.isEmpty()) { + throw new IllegalStateException( + "All node account IDs did not map to valid nodes in the client's network"); + } } /** diff --git a/sdk/src/testIntegration/java/com/hedera/hashgraph/sdk/test/integration/ClientIntegrationTest.java b/sdk/src/testIntegration/java/com/hedera/hashgraph/sdk/test/integration/ClientIntegrationTest.java index f3ae4e580..5ec000e6b 100644 --- a/sdk/src/testIntegration/java/com/hedera/hashgraph/sdk/test/integration/ClientIntegrationTest.java +++ b/sdk/src/testIntegration/java/com/hedera/hashgraph/sdk/test/integration/ClientIntegrationTest.java @@ -20,6 +20,7 @@ package com.hedera.hashgraph.sdk.test.integration; import com.hedera.hashgraph.sdk.*; +import java.util.Collections; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.time.Duration; @@ -32,6 +33,39 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; public class ClientIntegrationTest { + + @Test + @DisplayName("fails when all the manually set nodes are not matching the address book") + void failsWhenNoNodesAreMatching() throws Exception { + var client = Client.forTestnet() + .setTransportSecurity(true); + + var nodes = new ArrayList(); + nodes.add(new AccountId(1000)); + nodes.add(new AccountId(1001)); + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> new AccountBalanceQuery() + .setNodeAccountIds(nodes) + .setAccountId(new AccountId(7)) + .execute(client)).withMessageContaining("All node account IDs did not map to valid nodes in the client's network"); + client.close(); + } + + @Test + @DisplayName("can skip invalid nodes") + void canSkipNodes() throws Exception { + var client = Client.forTestnet() + .setTransportSecurity(true); + + var nodes = new ArrayList<>(client.getNetwork().values().stream().toList()); + nodes.add(new AccountId(1000)); + new AccountBalanceQuery() + .setNodeAccountIds(nodes) + .setAccountId(new AccountId(7)) + .execute(client); + + client.close(); + } + @Test @DisplayName("setNetwork() functions correctly") void testReplaceNodes() throws Exception { @@ -39,7 +73,7 @@ void testReplaceNodes() throws Exception { network.put("0.testnet.hedera.com:50211", new AccountId(3)); network.put("1.testnet.hedera.com:50211", new AccountId(4)); - try(var testEnv = new IntegrationTestEnv(1)){ + try (var testEnv = new IntegrationTestEnv(1)) { testEnv.client .setMaxQueryPayment(new Hbar(2)) @@ -88,7 +122,7 @@ void transactionIdNetworkIsVerified() { @Test @DisplayName("`setMaxNodesPerTransaction()`") void testMaxNodesPerTransaction() throws Exception { - try(var testEnv = new IntegrationTestEnv(1)){ + try (var testEnv = new IntegrationTestEnv(1)) { testEnv.client.setMaxNodesPerTransaction(1); @@ -104,7 +138,7 @@ void testMaxNodesPerTransaction() throws Exception { @Test void ping() throws Exception { - try(var testEnv = new IntegrationTestEnv(1)){ + try (var testEnv = new IntegrationTestEnv(1)) { var network = testEnv.client.getNetwork(); var nodes = new ArrayList<>(network.values()); @@ -119,7 +153,7 @@ void ping() throws Exception { @Test void pingAll() throws Exception { - try(var testEnv = new IntegrationTestEnv()){ + try (var testEnv = new IntegrationTestEnv()) { testEnv.client.setMaxNodeAttempts(1); testEnv.client.pingAll(); @@ -140,7 +174,7 @@ void pingAll() throws Exception { @Test void pingAllBadNetwork() throws Exception { - try(var testEnv = new IntegrationTestEnv(3)){ + try (var testEnv = new IntegrationTestEnv(3)) { // Skip if using local node. // Note: this check should be removed once the local node is supporting multiple nodes. @@ -161,7 +195,6 @@ void pingAllBadNetwork() throws Exception { testEnv.client.setNetwork(network); - assertThatExceptionOfType(MaxAttemptsExceededException.class).isThrownBy(() -> { testEnv.client.pingAll(); }).withMessageContaining("exceeded maximum attempts"); @@ -177,12 +210,12 @@ void pingAllBadNetwork() throws Exception { assertThat(testEnv.client.getNetwork().values().size()).isEqualTo(1); } - + } @Test void pingAsync() throws Exception { - try(var testEnv = new IntegrationTestEnv(1)){ + try (var testEnv = new IntegrationTestEnv(1)) { var network = testEnv.client.getNetwork(); var nodes = new ArrayList<>(network.values()); @@ -197,7 +230,7 @@ void pingAsync() throws Exception { @Test void pingAllAsync() throws Exception { - try(var testEnv = new IntegrationTestEnv()){ + try (var testEnv = new IntegrationTestEnv()) { testEnv.client.setMaxNodeAttempts(1); testEnv.client.pingAllAsync().get(); @@ -212,7 +245,7 @@ void pingAllAsync() throws Exception { new AccountBalanceQuery() .setAccountId(node) .execute(testEnv.client); - + } } }