From 697171d429d3854b86b0c804db82b1a9ab865ad0 Mon Sep 17 00:00:00 2001 From: CJ Hare Date: Wed, 31 Oct 2018 11:25:02 +1000 Subject: [PATCH] Acceptance test - refactor JsonRpc to use conditionals (#204) --- .../CreateAccountAcceptanceTest.java | 4 +- .../RpcApisTogglesAcceptanceTest.java | 32 +++---- .../acceptance/dsl/AcceptanceTestBase.java | 24 +++-- .../tests/acceptance/dsl/account/Account.java | 12 +-- .../acceptance/dsl/account/Accounts.java | 2 +- .../acceptance/dsl/blockchain/Blockchain.java | 4 +- .../{ => account}/ExpectAccountBalance.java | 5 +- .../ExpectMinimumBlockNumber.java | 5 +- .../eth/ExpectEthAccountsException.java | 41 +++++++++ .../eth/ExpectEthGetWorkException.java | 41 +++++++++ .../eth/SanityCheckEthGetWorkValues.java | 35 +++++++ .../ExpectNetVersionConnectionException.java | 44 +++++++++ ...etVersionConnectionExceptionWithCause.java | 38 ++++++++ .../net/ExpectNetVersionIsNotBlank.java | 33 +++++++ .../condition/web3/ExpectWeb3Sha3Equals.java | 35 +++++++ .../tests/acceptance/dsl/jsonrpc/Eth.java | 40 ++++++++ .../acceptance/dsl/{ => jsonrpc}/JsonRpc.java | 3 +- .../tests/acceptance/dsl/jsonrpc/Net.java | 40 ++++++++ .../tests/acceptance/dsl/jsonrpc/Web3.java | 30 ++++++ .../acceptance/dsl/node/PantheonNode.java | 91 +++++-------------- .../dsl/transaction/Transactions.java | 2 + .../{ => account}/TransferTransaction.java | 3 +- .../{ => account}/TransferTransactionSet.java | 3 +- .../eth/EthAccountsTransaction.java} | 29 +++--- .../{ => eth}/EthBlockNumberTransaction.java | 4 +- .../{ => eth}/EthGetBalanceTransaction.java | 3 +- .../{ => eth}/EthGetWorkTransaction.java | 4 +- .../{ => eth}/EthTransactions.java | 6 +- .../dsl/transaction/net/NetTransactions.java | 20 ++++ .../net/NetVersionTransaction.java | 39 ++++++++ .../{ => web3}/Web3Sha3Transaction.java | 4 +- .../{ => web3}/Web3Transactions.java | 2 +- .../jsonrpc/EthGetWorkAcceptanceTest.java | 11 +-- .../jsonrpc/Web3Sha3AcceptanceTest.java | 12 +-- 34 files changed, 552 insertions(+), 149 deletions(-) rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/{ => account}/ExpectAccountBalance.java (88%) rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/{ => blockchain}/ExpectMinimumBlockNumber.java (83%) create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectEthAccountsException.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectEthGetWorkException.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/SanityCheckEthGetWorkValues.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionConnectionException.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionConnectionExceptionWithCause.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionIsNotBlank.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/web3/ExpectWeb3Sha3Equals.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Eth.java rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/{ => jsonrpc}/JsonRpc.java (90%) create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Web3.java rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/{ => account}/TransferTransaction.java (93%) rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/{ => account}/TransferTransactionSet.java (89%) rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/{node/Web3.java => transaction/eth/EthAccountsTransaction.java} (53%) rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/{ => eth}/EthBlockNumberTransaction.java (89%) rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/{ => eth}/EthGetBalanceTransaction.java (91%) rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/{ => eth}/EthGetWorkTransaction.java (89%) rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/{ => eth}/EthTransactions.java (86%) create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java create mode 100644 acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetVersionTransaction.java rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/{ => web3}/Web3Sha3Transaction.java (89%) rename acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/{ => web3}/Web3Transactions.java (91%) diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/CreateAccountAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/CreateAccountAcceptanceTest.java index 0b109dec2c..8cb08b39bf 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/CreateAccountAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/CreateAccountAcceptanceTest.java @@ -28,13 +28,11 @@ import org.junit.Test; public class CreateAccountAcceptanceTest extends AcceptanceTestBase { private PantheonNode minerNode; - private PantheonNode fullNode; @Before public void setUp() throws Exception { minerNode = cluster.create(pantheonMinerNode("node1")); - fullNode = cluster.create(pantheonNode("node2")); - cluster.start(minerNode, fullNode); + cluster.start(minerNode, cluster.create(pantheonNode("node2"))); } @Test diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/RpcApisTogglesAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/RpcApisTogglesAcceptanceTest.java index 80b07cd8d5..0e83832306 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/RpcApisTogglesAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/RpcApisTogglesAcceptanceTest.java @@ -12,8 +12,6 @@ */ package tech.pegasys.pantheon.tests.acceptance; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.catchThrowable; import static tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNodeConfig.pantheonNode; import static tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNodeConfig.pantheonRpcDisabledNode; import static tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNodeConfig.patheonNodeWithRpcApis; @@ -22,11 +20,10 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.RpcApis; import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode; +import org.java_websocket.exceptions.WebsocketNotConnectedException; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import org.web3j.protocol.Web3j; -import org.web3j.protocol.exceptions.ClientConnectionException; @Ignore public class RpcApisTogglesAcceptanceTest extends AcceptanceTestBase { @@ -45,37 +42,40 @@ public class RpcApisTogglesAcceptanceTest extends AcceptanceTestBase { @Test public void shouldSucceedConnectingToNodeWithJsonRpcEnabled() { - rpcEnabledNode.verifyJsonRpcEnabled(); + rpcEnabledNode.verify(net.netVersion()); } @Test public void shouldFailConnectingToNodeWithJsonRpcDisabled() { - rpcDisabledNode.verifyJsonRpcDisabled(); + final String expectedMessage = "Failed to connect to /127.0.0.1:8545"; + + rpcDisabledNode.verify(net.netVersionExceptional(expectedMessage)); } @Test public void shouldSucceedConnectingToNodeWithWsRpcEnabled() { - rpcEnabledNode.verifyWsRpcEnabled(); + rpcEnabledNode.useWebSocketsForJsonRpc(); + + // TODO previously this test was calling netVersion using the HTTP not WebSockets, now failing + rpcEnabledNode.verify(net.netVersion()); } @Test public void shouldFailConnectingToNodeWithWsRpcDisabled() { - rpcDisabledNode.verifyWsRpcDisabled(); + rpcDisabledNode.useWebSocketsForJsonRpc(); + + rpcDisabledNode.verify(net.netVersionExceptional(WebsocketNotConnectedException.class)); } @Test - public void shouldSucceedCallingMethodFromEnabledApiGroup() throws Exception { - final Web3j web3j = ethApiDisabledNode.web3j(); - - assertThat(web3j.netVersion().send().getError()).isNull(); + public void shouldSucceedCallingMethodFromEnabledApiGroup() { + ethApiDisabledNode.verify(net.netVersion()); } @Test public void shouldFailCallingMethodFromDisabledApiGroup() { - final Web3j web3j = ethApiDisabledNode.web3j(); + final String expectedMessage = "Invalid response received: 400"; - assertThat(catchThrowable(() -> web3j.ethAccounts().send())) - .isInstanceOf(ClientConnectionException.class) - .hasMessageContaining("Invalid response received: 400"); + ethApiDisabledNode.verify(eth.accountsExceptional(expectedMessage)); } } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java index b2c47dadaa..362f2b4391 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/AcceptanceTestBase.java @@ -14,10 +14,15 @@ package tech.pegasys.pantheon.tests.acceptance.dsl; import tech.pegasys.pantheon.tests.acceptance.dsl.account.Accounts; import tech.pegasys.pantheon.tests.acceptance.dsl.blockchain.Blockchain; +import tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc.Eth; +import tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc.JsonRpc; +import tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc.Net; +import tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc.Web3; import tech.pegasys.pantheon.tests.acceptance.dsl.node.Cluster; -import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.EthTransactions; import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transactions; -import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Web3Transactions; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetTransactions; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.web3.Web3Transactions; import org.junit.After; @@ -26,19 +31,22 @@ public class AcceptanceTestBase { protected final Accounts accounts; protected final Blockchain blockchain; protected final Cluster cluster; - protected final EthTransactions eth; protected final JsonRpc jsonRpc; protected final Transactions transactions; - protected final Web3Transactions web3; + protected final Web3 web3; + protected final Eth eth; + protected final Net net; protected AcceptanceTestBase() { - eth = new EthTransactions(); - accounts = new Accounts(eth); - blockchain = new Blockchain(eth); + final EthTransactions ethTransactions = new EthTransactions(); + accounts = new Accounts(ethTransactions); + blockchain = new Blockchain(ethTransactions); cluster = new Cluster(); + eth = new Eth(ethTransactions); jsonRpc = new JsonRpc(cluster); + net = new Net(new NetTransactions()); transactions = new Transactions(accounts); - web3 = new Web3Transactions(); + web3 = new Web3(new Web3Transactions()); } @After diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Account.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Account.java index 31496f5fc4..b06744d5c8 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Account.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Account.java @@ -17,8 +17,8 @@ import tech.pegasys.pantheon.crypto.SECP256K1.PrivateKey; import tech.pegasys.pantheon.ethereum.core.Address; import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; -import tech.pegasys.pantheon.tests.acceptance.dsl.condition.ExpectAccountBalance; -import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.EthTransactions; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.account.ExpectAccountBalance; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; import tech.pegasys.pantheon.util.bytes.Bytes32; import java.math.BigInteger; @@ -62,14 +62,6 @@ public class Account { return BigInteger.valueOf(nonce++); } - public void setNextNonce(final long nonce) { - this.nonce = nonce; - } - - public String getName() { - return name; - } - public Condition balanceEquals(final String expectedBalance, final Unit balanceUnit) { return new ExpectAccountBalance(eth, this, expectedBalance, balanceUnit); } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Accounts.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Accounts.java index ddb28abf07..ef39d9b0e8 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Accounts.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/account/Accounts.java @@ -12,7 +12,7 @@ */ package tech.pegasys.pantheon.tests.acceptance.dsl.account; -import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.EthTransactions; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; public class Accounts { diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/blockchain/Blockchain.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/blockchain/Blockchain.java index 7c0d7bbd08..a2d59509bd 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/blockchain/Blockchain.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/blockchain/Blockchain.java @@ -13,9 +13,9 @@ package tech.pegasys.pantheon.tests.acceptance.dsl.blockchain; import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; -import tech.pegasys.pantheon.tests.acceptance.dsl.condition.ExpectMinimumBlockNumber; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.blockchain.ExpectMinimumBlockNumber; import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; -import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.EthTransactions; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; public class Blockchain { diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/ExpectAccountBalance.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/account/ExpectAccountBalance.java similarity index 88% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/ExpectAccountBalance.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/account/ExpectAccountBalance.java index 38a3072e7c..0311b1ae97 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/ExpectAccountBalance.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/account/ExpectAccountBalance.java @@ -10,15 +10,16 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.condition.account; import static org.assertj.core.api.Assertions.assertThat; import static org.web3j.utils.Convert.toWei; import static tech.pegasys.pantheon.tests.acceptance.dsl.WaitUtils.waitFor; import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account; +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.EthTransactions; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; import org.web3j.utils.Convert.Unit; diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/ExpectMinimumBlockNumber.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/blockchain/ExpectMinimumBlockNumber.java similarity index 83% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/ExpectMinimumBlockNumber.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/blockchain/ExpectMinimumBlockNumber.java index 79580c8b8b..cde6da8b03 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/ExpectMinimumBlockNumber.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/blockchain/ExpectMinimumBlockNumber.java @@ -10,12 +10,13 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.condition.blockchain; 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.EthTransactions; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; import java.math.BigInteger; diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectEthAccountsException.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectEthAccountsException.java new file mode 100644 index 0000000000..fd24898d9f --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectEthAccountsException.java @@ -0,0 +1,41 @@ +/* + * Copyright 2018 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.eth; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; + +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.eth.EthAccountsTransaction; + +import org.web3j.protocol.exceptions.ClientConnectionException; + +public class ExpectEthAccountsException implements Condition { + + private final String expectedMessage; + private final EthAccountsTransaction transaction; + + public ExpectEthAccountsException( + final EthAccountsTransaction transaction, final String expectedMessage) { + this.expectedMessage = expectedMessage; + this.transaction = transaction; + } + + @Override + public void verify(final Node node) { + final Throwable thrown = catchThrowable(() -> node.execute(transaction)); + assertThat(thrown).isInstanceOf(ClientConnectionException.class); + assertThat(thrown.getMessage()).contains(expectedMessage); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectEthGetWorkException.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectEthGetWorkException.java new file mode 100644 index 0000000000..c877b2d85a --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/ExpectEthGetWorkException.java @@ -0,0 +1,41 @@ +/* + * Copyright 2018 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.eth; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; + +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.eth.EthGetWorkTransaction; + +import org.web3j.protocol.exceptions.ClientConnectionException; + +public class ExpectEthGetWorkException implements Condition { + + private final EthGetWorkTransaction transaction; + private final String expectedMessage; + + public ExpectEthGetWorkException( + final EthGetWorkTransaction transaction, final String expectedMessage) { + this.transaction = transaction; + this.expectedMessage = expectedMessage; + } + + @Override + public void verify(final Node node) { + final Throwable thrown = catchThrowable(() -> node.execute(transaction)); + assertThat(thrown).isInstanceOf(ClientConnectionException.class); + assertThat(thrown.getMessage()).contains(expectedMessage); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/SanityCheckEthGetWorkValues.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/SanityCheckEthGetWorkValues.java new file mode 100644 index 0000000000..7333a983f3 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/eth/SanityCheckEthGetWorkValues.java @@ -0,0 +1,35 @@ +/* + * Copyright 2018 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.eth; + +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.eth.EthGetWorkTransaction; + +public class SanityCheckEthGetWorkValues implements Condition { + + private final EthGetWorkTransaction transaction; + + public SanityCheckEthGetWorkValues(final EthGetWorkTransaction transaction) { + this.transaction = transaction; + } + + @Override + public void verify(final Node node) { + final String[] response = node.execute(transaction); + assertThat(response).hasSize(3); + assertThat(response).doesNotContainNull(); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionConnectionException.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionConnectionException.java new file mode 100644 index 0000000000..2d69789183 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionConnectionException.java @@ -0,0 +1,44 @@ +/* + * Copyright 2018 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 static org.assertj.core.api.Assertions.catchThrowable; + +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.NetVersionTransaction; + +import java.net.ConnectException; + +public class ExpectNetVersionConnectionException implements Condition { + + private final NetVersionTransaction transaction; + private final String expectedMessage; + + public ExpectNetVersionConnectionException( + final NetVersionTransaction transaction, final String expectedMessage) { + this.transaction = transaction; + this.expectedMessage = expectedMessage; + } + + @Override + public void verify(final Node node) { + final Throwable thrown = catchThrowable(() -> node.execute(transaction)); + assertThat(thrown).isInstanceOf(RuntimeException.class); + + final Throwable cause = thrown.getCause(); + assertThat(cause).isInstanceOf(ConnectException.class); + assertThat(cause.getMessage()).contains(expectedMessage); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionConnectionExceptionWithCause.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionConnectionExceptionWithCause.java new file mode 100644 index 0000000000..d47540bb6a --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionConnectionExceptionWithCause.java @@ -0,0 +1,38 @@ +/* + * Copyright 2018 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 static org.assertj.core.api.Assertions.catchThrowable; + +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.NetVersionTransaction; + +public class ExpectNetVersionConnectionExceptionWithCause implements Condition { + + private final NetVersionTransaction transaction; + private final Class cause; + + public ExpectNetVersionConnectionExceptionWithCause( + final NetVersionTransaction transaction, final Class cause) { + this.transaction = transaction; + this.cause = cause; + } + + @Override + public void verify(final Node node) { + final Throwable thrown = catchThrowable(() -> node.execute(transaction)); + assertThat(thrown).isInstanceOf(cause); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionIsNotBlank.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionIsNotBlank.java new file mode 100644 index 0000000000..e582d3ddc0 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/net/ExpectNetVersionIsNotBlank.java @@ -0,0 +1,33 @@ +/* + * Copyright 2018 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.NetVersionTransaction; + +public class ExpectNetVersionIsNotBlank implements Condition { + + private final NetVersionTransaction transaction; + + public ExpectNetVersionIsNotBlank(final NetVersionTransaction transaction) { + this.transaction = transaction; + } + + @Override + public void verify(final Node node) { + assertThat(node.execute(transaction)).isNotBlank(); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/web3/ExpectWeb3Sha3Equals.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/web3/ExpectWeb3Sha3Equals.java new file mode 100644 index 0000000000..2a88c3cf16 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/condition/web3/ExpectWeb3Sha3Equals.java @@ -0,0 +1,35 @@ +/* + * Copyright 2018 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.web3; + +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.web3.Web3Sha3Transaction; + +public class ExpectWeb3Sha3Equals implements Condition { + + private final Web3Sha3Transaction input; + private final String hash; + + public ExpectWeb3Sha3Equals(final Web3Sha3Transaction input, final String expectedHash) { + this.hash = expectedHash; + this.input = input; + } + + @Override + public void verify(final Node node) { + assertThat(node.execute(input)).isEqualTo(hash); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Eth.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Eth.java new file mode 100644 index 0000000000..82a24a26aa --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Eth.java @@ -0,0 +1,40 @@ +/* + * Copyright 2018 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.jsonrpc; + +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth.ExpectEthAccountsException; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth.ExpectEthGetWorkException; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.eth.SanityCheckEthGetWorkValues; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth.EthTransactions; + +public class Eth { + + private final EthTransactions transactions; + + public Eth(final EthTransactions transactions) { + this.transactions = transactions; + } + + public Condition getWork() { + return new SanityCheckEthGetWorkValues(transactions.getWork()); + } + + public Condition getWorkExceptional(final String expectedMessage) { + return new ExpectEthGetWorkException(transactions.getWork(), expectedMessage); + } + + public Condition accountsExceptional(final String expectedMessage) { + return new ExpectEthAccountsException(transactions.accounts(), expectedMessage); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/JsonRpc.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/JsonRpc.java similarity index 90% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/JsonRpc.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/JsonRpc.java index 6e4e7fe9c8..2fae04c636 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/JsonRpc.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/JsonRpc.java @@ -10,10 +10,11 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.jsonrpc; import static org.assertj.core.api.Assertions.assertThat; +import tech.pegasys.pantheon.tests.acceptance.dsl.WaitUtils; import tech.pegasys.pantheon.tests.acceptance.dsl.node.Cluster; import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode; 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 new file mode 100644 index 0000000000..8787599114 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Net.java @@ -0,0 +1,40 @@ +/* + * Copyright 2018 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.jsonrpc; + +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; +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; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetTransactions; + +public class Net { + + private final NetTransactions transactions; + + public Net(final NetTransactions transactions) { + this.transactions = transactions; + } + + public Condition netVersion() { + return new ExpectNetVersionIsNotBlank(transactions.netVersion()); + } + + public Condition netVersionExceptional(final String expectedMessage) { + return new ExpectNetVersionConnectionException(transactions.netVersion(), expectedMessage); + } + + public Condition netVersionExceptional(final Class cause) { + return new ExpectNetVersionConnectionExceptionWithCause(transactions.netVersion(), cause); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Web3.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Web3.java new file mode 100644 index 0000000000..8ef03daa8a --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/jsonrpc/Web3.java @@ -0,0 +1,30 @@ +/* + * Copyright 2018 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.jsonrpc; + +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; +import tech.pegasys.pantheon.tests.acceptance.dsl.condition.web3.ExpectWeb3Sha3Equals; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.web3.Web3Transactions; + +public class Web3 { + + private final Web3Transactions transactions; + + public Web3(final Web3Transactions transactions) { + this.transactions = transactions; + } + + public Condition sha3(final String input, final String expectedHash) { + return new ExpectWeb3Sha3Equals(transactions.sha3(input), expectedHash); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/PantheonNode.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/PantheonNode.java index ca946401b5..58089dfc48 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/PantheonNode.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/PantheonNode.java @@ -13,8 +13,6 @@ package tech.pegasys.pantheon.tests.acceptance.dsl.node; import static org.apache.logging.log4j.LogManager.getLogger; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.catchThrowable; import tech.pegasys.pantheon.controller.KeyPairUtil; import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair; @@ -27,7 +25,6 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.net.ConnectException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -40,7 +37,6 @@ import com.google.common.base.MoreObjects; import com.google.common.io.MoreFiles; import com.google.common.io.RecursiveDeleteOption; import org.apache.logging.log4j.Logger; -import org.java_websocket.exceptions.WebsocketNotConnectedException; import org.web3j.protocol.Web3j; import org.web3j.protocol.Web3jService; import org.web3j.protocol.http.HttpService; @@ -64,7 +60,6 @@ public class PantheonNode implements Node, AutoCloseable { private final Properties portsProperties = new Properties(); private List bootnodes = new ArrayList<>(); - private Web3 web3; private Web3j web3j; public PantheonNode( @@ -86,7 +81,7 @@ public class PantheonNode implements Node, AutoCloseable { LOG.info("Created PantheonNode {}", this.toString()); } - public String getName() { + String getName() { return name; } @@ -130,8 +125,7 @@ public class PantheonNode implements Node, AutoCloseable { @Deprecated public Web3j web3j() { if (!jsonRpcBaseUrl().isPresent()) { - throw new IllegalStateException( - "Can't create a web3j instance for a node with RPC disabled."); + return web3j(new HttpService("http://" + LOCALHOST + ":8545")); } if (web3j == null) { @@ -141,7 +135,16 @@ public class PantheonNode implements Node, AutoCloseable { return web3j; } - private Web3j web3j(final Web3jService web3jService) { + public void setWeb3j(final Web3jService web3jService) { + if (web3j != null) { + web3j.shutdown(); + } + + web3j = Web3j.build(web3jService, 2000, Async.defaultExecutorService()); + } + + @Deprecated + public Web3j web3j(final Web3jService web3jService) { if (web3j == null) { web3j = Web3j.build(web3jService, 2000, Async.defaultExecutorService()); } @@ -149,6 +152,19 @@ public class PantheonNode implements Node, AutoCloseable { return web3j; } + /** All future JSON-RPC calls are made via a web sockets connection. */ + public void useWebSocketsForJsonRpc() { + final String url = wsRpcBaseUrl().isPresent() ? wsRpcBaseUrl().get() : "ws://127.0.0.1:8546"; + + final WebSocketService webSocketService = new WebSocketService(url, true); + + if (web3j != null) { + web3j.shutdown(); + } + + web3j = Web3j.build(webSocketService, 2000, Async.defaultExecutorService()); + } + public int getPeerCount() { try { return web3j().netPeerCount().send().getQuantity().intValueExact(); @@ -171,53 +187,6 @@ public class PantheonNode implements Node, AutoCloseable { } } - public void verifyJsonRpcEnabled() { - if (!jsonRpcBaseUrl().isPresent()) { - throw new RuntimeException("JSON-RPC is not enabled in node configuration"); - } - - try { - assertThat(web3j().netVersion().send().getError()).isNull(); - } catch (final IOException e) { - throw new RuntimeException(e); - } - } - - public void verifyJsonRpcDisabled() { - if (jsonRpcBaseUrl().isPresent()) { - throw new RuntimeException("JSON-RPC is enabled in node configuration"); - } - - final HttpService web3jService = new HttpService("http://" + LOCALHOST + ":8545"); - assertThat(catchThrowable(() -> web3j(web3jService).netVersion().send())) - .isInstanceOf(ConnectException.class) - .hasMessage("Failed to connect to /127.0.0.1:8545"); - } - - public void verifyWsRpcEnabled() { - if (!wsRpcBaseUrl().isPresent()) { - throw new RuntimeException("WS-RPC is not enabled in node configuration"); - } - - try { - final WebSocketService webSocketService = new WebSocketService(wsRpcBaseUrl().get(), true); - assertThat(web3j(webSocketService).netVersion().send().getError()).isNull(); - } catch (final IOException e) { - throw new RuntimeException(e); - } - } - - public void verifyWsRpcDisabled() { - if (wsRpcBaseUrl().isPresent()) { - throw new RuntimeException("WS-RPC is enabled in node configuration"); - } - - final WebSocketService webSocketService = - new WebSocketService("ws://" + LOCALHOST + ":8546", true); - assertThat(catchThrowable(() -> web3j(webSocketService).netVersion().send())) - .isInstanceOf(WebsocketNotConnectedException.class); - } - Path homeDirectory() { return homeDirectory; } @@ -288,8 +257,6 @@ public class PantheonNode implements Node, AutoCloseable { web3j.shutdown(); web3j = null; } - - web3 = null; } @Override @@ -302,14 +269,6 @@ public class PantheonNode implements Node, AutoCloseable { } } - public Web3 web3() { - if (web3 == null) { - web3 = new Web3(web3j()); - } - - return web3; - } - @Override public T execute(final Transaction transaction) { return transaction.execute(web3j()); diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Transactions.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Transactions.java index af77ed219e..affc251c14 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Transactions.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Transactions.java @@ -14,6 +14,8 @@ package tech.pegasys.pantheon.tests.acceptance.dsl.transaction; import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account; import tech.pegasys.pantheon.tests.acceptance.dsl.account.Accounts; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.account.TransferTransaction; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.account.TransferTransactionSet; import java.util.ArrayList; import java.util.List; diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/TransferTransaction.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/account/TransferTransaction.java similarity index 93% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/TransferTransaction.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/account/TransferTransaction.java index 195ccfdc01..734495e3fd 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/TransferTransaction.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/account/TransferTransaction.java @@ -10,12 +10,13 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.account; import static org.web3j.utils.Numeric.toHexString; import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; import java.io.IOException; import java.math.BigInteger; diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/TransferTransactionSet.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/account/TransferTransactionSet.java similarity index 89% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/TransferTransactionSet.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/account/TransferTransactionSet.java index 58ea93a917..7c0665ce85 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/TransferTransactionSet.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/account/TransferTransactionSet.java @@ -10,9 +10,10 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.account; import tech.pegasys.pantheon.ethereum.core.Hash; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; import java.util.ArrayList; import java.util.List; diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/Web3.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthAccountsTransaction.java similarity index 53% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/Web3.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthAccountsTransaction.java index a14487161b..1aba152492 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/Web3.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthAccountsTransaction.java @@ -10,27 +10,30 @@ * 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.node; +package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth; import static org.assertj.core.api.Assertions.assertThat; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; + import java.io.IOException; +import java.util.List; import org.web3j.protocol.Web3j; -import org.web3j.protocol.core.methods.response.Web3Sha3; - -public class Web3 { +import org.web3j.protocol.core.methods.response.EthAccounts; - private final Web3j web3j; +public class EthAccountsTransaction implements Transaction> { - public Web3(final Web3j web3) { - this.web3j = web3; - } + EthAccountsTransaction() {} - public String web3Sha3(final String input) throws IOException { - final Web3Sha3 result = web3j.web3Sha3(input).send(); - assertThat(result).isNotNull(); - assertThat(result.hasError()).isFalse(); - return result.getResult(); + @Override + public List execute(final Web3j node) { + try { + final EthAccounts result = node.ethAccounts().send(); + assertThat(result).isNotNull(); + return result.getAccounts(); + } catch (final IOException e) { + throw new RuntimeException(e); + } } } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthBlockNumberTransaction.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthBlockNumberTransaction.java similarity index 89% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthBlockNumberTransaction.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthBlockNumberTransaction.java index fee3bf8a74..d9b6b1cdc1 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthBlockNumberTransaction.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthBlockNumberTransaction.java @@ -10,10 +10,12 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth; import static org.assertj.core.api.Assertions.assertThat; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; + import java.io.IOException; import java.math.BigInteger; diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthGetBalanceTransaction.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthGetBalanceTransaction.java similarity index 91% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthGetBalanceTransaction.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthGetBalanceTransaction.java index b65e992ff9..ef875bcf9a 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthGetBalanceTransaction.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthGetBalanceTransaction.java @@ -10,12 +10,13 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth; import static org.assertj.core.api.Assertions.assertThat; import static org.web3j.protocol.core.DefaultBlockParameterName.LATEST; import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; import java.io.IOException; import java.math.BigInteger; diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthGetWorkTransaction.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthGetWorkTransaction.java similarity index 89% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthGetWorkTransaction.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthGetWorkTransaction.java index 4cb97a4454..6a2140a038 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthGetWorkTransaction.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthGetWorkTransaction.java @@ -10,10 +10,12 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth; import static org.assertj.core.api.Assertions.assertThat; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; + import java.io.IOException; import org.web3j.protocol.Web3j; diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthTransactions.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthTransactions.java similarity index 86% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthTransactions.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthTransactions.java index d0087c8cdc..80dec048c0 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/EthTransactions.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eth/EthTransactions.java @@ -10,7 +10,7 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eth; import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account; @@ -27,4 +27,8 @@ public class EthTransactions { public EthGetBalanceTransaction getBalance(final Account account) { return new EthGetBalanceTransaction(account); } + + public EthAccountsTransaction accounts() { + return new EthAccountsTransaction(); + } } 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 new file mode 100644 index 0000000000..67a21d5816 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetTransactions.java @@ -0,0 +1,20 @@ +/* + * Copyright 2018 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; + +public class NetTransactions { + + public NetVersionTransaction netVersion() { + return new NetVersionTransaction(); + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetVersionTransaction.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetVersionTransaction.java new file mode 100644 index 0000000000..c5657570e7 --- /dev/null +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/net/NetVersionTransaction.java @@ -0,0 +1,39 @@ +/* + * Copyright 2018 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 static org.assertj.core.api.Assertions.assertThat; + +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; + +import java.io.IOException; + +import org.web3j.protocol.Web3j; +import org.web3j.protocol.core.methods.response.NetVersion; + +public class NetVersionTransaction implements Transaction { + + NetVersionTransaction() {} + + @Override + public String execute(final Web3j node) { + try { + final NetVersion result = node.netVersion().send(); + assertThat(result).isNotNull(); + assertThat(result.hasError()).isFalse(); + return result.getNetVersion(); + } catch (final IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Web3Sha3Transaction.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/web3/Web3Sha3Transaction.java similarity index 89% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Web3Sha3Transaction.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/web3/Web3Sha3Transaction.java index c1fedd80f3..ebf3ecbe25 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Web3Sha3Transaction.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/web3/Web3Sha3Transaction.java @@ -10,10 +10,12 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.web3; import static org.assertj.core.api.Assertions.assertThat; +import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction; + import java.io.IOException; import org.web3j.protocol.Web3j; diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Web3Transactions.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/web3/Web3Transactions.java similarity index 91% rename from acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Web3Transactions.java rename to acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/web3/Web3Transactions.java index 7c333e9a80..b9694cc257 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/Web3Transactions.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/web3/Web3Transactions.java @@ -10,7 +10,7 @@ * 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; +package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.web3; public class Web3Transactions { diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/EthGetWorkAcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/EthGetWorkAcceptanceTest.java index cdba399fbc..ad9c23c804 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/EthGetWorkAcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/EthGetWorkAcceptanceTest.java @@ -12,8 +12,6 @@ */ package tech.pegasys.pantheon.tests.acceptance.jsonrpc; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.catchThrowable; import static tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNodeConfig.pantheonMinerNode; import static tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNodeConfig.pantheonNode; @@ -22,7 +20,6 @@ import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode; import org.junit.Before; import org.junit.Test; -import org.web3j.protocol.exceptions.ClientConnectionException; public class EthGetWorkAcceptanceTest extends AcceptanceTestBase { @@ -38,15 +35,11 @@ public class EthGetWorkAcceptanceTest extends AcceptanceTestBase { @Test public void shouldReturnSuccessResponseWhenMining() { - final String[] response = minerNode.execute(eth.getWork()); - assertThat(response).hasSize(3); - assertThat(response).doesNotContainNull(); + minerNode.verify(eth.getWork()); } @Test public void shouldReturnErrorResponseWhenNotMining() { - final Throwable thrown = catchThrowable(() -> fullNode.execute(eth.getWork())); - assertThat(thrown).isInstanceOf(ClientConnectionException.class); - assertThat(thrown.getMessage()).contains("No mining work available yet"); + fullNode.verify(eth.getWorkExceptional("No mining work available yet")); } } diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/Web3Sha3AcceptanceTest.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/Web3Sha3AcceptanceTest.java index a4fb059d0f..3839ab9b80 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/Web3Sha3AcceptanceTest.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/jsonrpc/Web3Sha3AcceptanceTest.java @@ -12,14 +12,11 @@ */ package tech.pegasys.pantheon.tests.acceptance.jsonrpc; -import static org.assertj.core.api.Assertions.assertThat; import static tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNodeConfig.pantheonNode; import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase; import tech.pegasys.pantheon.tests.acceptance.dsl.node.PantheonNode; -import java.io.IOException; - import org.junit.Before; import org.junit.Test; @@ -34,12 +31,11 @@ public class Web3Sha3AcceptanceTest extends AcceptanceTestBase { } @Test - public void shouldReturnCorrectSha3() throws IOException { + public void shouldReturnCorrectSha3() { final String input = "0x68656c6c6f20776f726c64"; - final String sha3 = "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"; - - final String response = node.execute(web3.sha3(input)); + final String expectedHash = + "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"; - assertThat(response).isEqualTo(sha3); + node.verify(web3.sha3(input, expectedHash)); } }