diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsAllServicesAsActive.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsAllServicesAsActive.java new file mode 100644 index 0000000000..52f68c8a00 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesReturnsAllServicesAsActive.java @@ -0,0 +1,55 @@ +/* + * Copyright 2019 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package tech.pegasys.pantheon.tests.acceptance.dsl.condition.net; + +import static org.assertj.core.api.Assertions.assertThat; + +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; +import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetServicesTransaction; +import tech.pegasys.pantheon.util.NetworkUtility; + +import java.util.Map; +import java.util.regex.Pattern; + +public class ExpectNetServicesReturnsAllServicesAsActive implements Condition { + + private final NetServicesTransaction transaction; + private final Pattern PATTERN = + Pattern.compile("^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"); + + public ExpectNetServicesReturnsAllServicesAsActive(final NetServicesTransaction transaction) { + this.transaction = transaction; + } + + @Override + public void verify(final Node node) { + final Map> result = node.execute(transaction); + + assertThat(validateHost(result.get("p2p").get("host"))).isTrue(); + final int p2pPort = Integer.valueOf(result.get("p2p").get("port")); + assertThat(NetworkUtility.isValidPort(p2pPort)).isTrue(); + + assertThat(validateHost(result.get("ws").get("host"))).isTrue(); + final int wsPort = Integer.valueOf(result.get("ws").get("port")); + assertThat(NetworkUtility.isValidPort(wsPort) || wsPort == 0).isTrue(); + + assertThat(validateHost(result.get("jsonrpc").get("host"))).isTrue(); + final int jsonRpcPort = Integer.valueOf(result.get("jsonrpc").get("port")); + assertThat(NetworkUtility.isValidPort(jsonRpcPort) || jsonRpcPort == 0).isTrue(); + } + + private boolean validateHost(final String ip) { + return PATTERN.matcher(ip).matches(); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesValidPort.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesValidPort.java deleted file mode 100644 index 9827929a8f..0000000000 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetServicesValidPort.java +++ /dev/null @@ -1,25 +0,0 @@ -package tech.pegasys.pantheon.tests.acceptance.dsl.condition.net; - -import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; -import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; -import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetServicesTransaction; -import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetVersionTransaction; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ExpectNetServicesValidPort implements Condition { - - private final NetServicesTransaction transaction; - - public ExpectNetServicesValidPort(final NetServicesTransaction transaction) { - this.transaction = transaction; - } - - @Override - public void verify(Node node) { - - final Boolean result = node.execute(); - - assertThat(result).isTrue(); - } -} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java index 9aaab52746..f75f9e6ca9 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java @@ -15,6 +15,7 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.AwaitNetPeerCount; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.AwaitNetPeerCountException; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.ExpectNetServicesReturnsAllServicesAsActive; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.ExpectNetVersionConnectionException; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.ExpectNetVersionConnectionExceptionWithCause; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.net.ExpectNetVersionIsNotBlank; @@ -32,9 +33,8 @@ public Net(final NetTransactions transactions) { this.transactions = transactions; } - public Condition netServices() { - - return transactions.netServices(); + public Condition netServicesAllActive() { + return new ExpectNetServicesReturnsAllServicesAsActive(transactions.netServices()); } public Condition netVersion() { diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetServicesTransaction.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetServicesTransaction.java index 16919c96e1..aeaa5428c3 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetServicesTransaction.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetServicesTransaction.java @@ -1,28 +1,40 @@ +/* + * Copyright 2019 ConsenSys AG. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net; -import org.web3j.protocol.core.Request; -import org.web3j.protocol.core.methods.response.NetVersion; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.JsonRequestFactories; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.NetServicesJsonRpcRequestFactory; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; import java.util.Map; +import org.web3j.protocol.core.Request; + public class NetServicesTransaction implements Transaction>> { - NetServicesTransaction() {} + NetServicesTransaction() {} - @Override - public Map> execute( - final JsonRequestFactories requestFactories) { - NetServicesJsonRpcRequestFactory.NetServicesResponse netServicesResponse = null; - try { - NetServicesJsonRpcRequestFactory netServicesJsonRpcRequestFactory = requestFactories.netServices(); - Request request = - netServicesJsonRpcRequestFactory.netServices(); - netServicesResponse = request.send(); - } catch (final Exception ignored) { - } - return netServicesResponse.getResult(); + @Override + public Map> execute(final JsonRequestFactories requestFactories) { + NetServicesJsonRpcRequestFactory.NetServicesResponse netServicesResponse = null; + try { + NetServicesJsonRpcRequestFactory netServicesJsonRpcRequestFactory = + requestFactories.netServices(); + Request request = + netServicesJsonRpcRequestFactory.netServices(); + netServicesResponse = request.send(); + } catch (final Exception ignored) { } + return netServicesResponse.getResult(); + } } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java index 7bf87b4170..535f9d5679 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java @@ -21,8 +21,8 @@ public NetVersionTransaction netVersion() { public NetServicesTransaction netServices() { return new NetServicesTransaction(); } + public NetPeerCountTransaction peerCount() { return new NetPeerCountTransaction(); } - } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/NetServicesAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/NetServicesAcceptanceTest.java index efffb70851..abe0814f74 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/NetServicesAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/NetServicesAcceptanceTest.java @@ -12,17 +12,12 @@ */ package tech.pegasys.pantheon.tests.acceptance.jsonrpc; -import static org.assertj.core.api.Assertions.assertThat; - import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; import tech.pegasys.pantheon.tests.acceptance.dsl.node.cluster.Cluster; import tech.pegasys.pantheon.tests.acceptance.dsl.node.cluster.ClusterConfiguration; import tech.pegasys.pantheon.tests.acceptance.dsl.node.cluster.ClusterConfigurationBuilder; -import java.util.HashMap; -import java.util.Map; - import org.junit.Before; import org.junit.Test; @@ -46,23 +41,8 @@ public void setUp() throws Exception { @Test public void shouldIndicateNetServicesEnabled() { - final Map> result = netServices.addPeer(nodeA); - final Map> expectation = new HashMap<>(); - final Map constituentMap = - new HashMap() { - { - put("host", "127.0.0.1"); - put("port", "0"); - } - }; - expectation.put("jsonrpc", constituentMap); - expectation.put("ws", constituentMap); - expectation.put("p2p", constituentMap); - assertThat(expectation.get("jsonrpc").get("host")).isEqualTo(result.get("jsonrpc").get("host")); - assertThat(expectation.get("jsonrpc").get("port")).isEqualTo(result.get("jsonrpc").get("port")); - assertThat(expectation.get("ws").get("host")).isEqualTo(result.get("ws").get("host")); - assertThat(expectation.get("ws").get("port")).isEqualTo(result.get("ws").get("port")); - assertThat(expectation.get("p2p").get("host")).isEqualTo(result.get("p2p").get("host")); + nodeA.verify(net.netServicesAllActive()); + nodeB.verify(net.netServicesAllActive()); } @Test