Skip to content

Commit

Permalink
Acceptance test refactoring - DSL - Node interface (hyperledger#170)
Browse files Browse the repository at this point in the history
* Conditions to remove the getters from Node interface

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
  • Loading branch information
CjHare authored Oct 29, 2018
1 parent cb93cbd commit 8fa4572
Show file tree
Hide file tree
Showing 18 changed files with 339 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,32 @@
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.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 org.junit.After;

public class AcceptanceTestBase {

protected final Accounts accounts;
protected final Blockchain blockchain;
protected final Cluster cluster;
protected final Transactions transactions;
protected final EthTransactions eth;
protected final JsonRpc jsonRpc;
protected final Transactions transactions;
protected final Web3Transactions web3;

protected AcceptanceTestBase() {
accounts = new Accounts();
eth = new EthTransactions();
accounts = new Accounts(eth);
blockchain = new Blockchain(eth);
cluster = new Cluster();
transactions = new Transactions(accounts);
jsonRpc = new JsonRpc(cluster);
transactions = new Transactions(accounts);
web3 = new Web3Transactions();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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.util.bytes.Bytes32;

import java.math.BigInteger;
Expand All @@ -27,21 +28,25 @@

public class Account {

private final EthTransactions eth;
private final String name;
private final KeyPair keyPair;
private long nonce = 0;

private Account(final String name, final KeyPair keyPair) {
private Account(final EthTransactions eth, final String name, final KeyPair keyPair) {
this.name = name;
this.keyPair = keyPair;
this.eth = eth;
}

public static Account create(final String name) {
return new Account(name, KeyPair.generate());
public static Account create(final EthTransactions eth, final String name) {
return new Account(eth, name, KeyPair.generate());
}

public static Account fromPrivateKey(final String name, final String privateKey) {
return new Account(name, KeyPair.create(PrivateKey.create(Bytes32.fromHexString(privateKey))));
static Account fromPrivateKey(
final EthTransactions eth, final String name, final String privateKey) {
return new Account(
eth, name, KeyPair.create(PrivateKey.create(Bytes32.fromHexString(privateKey))));
}

public Credentials web3jCredentials() {
Expand All @@ -66,7 +71,7 @@ public String getName() {
}

public Condition balanceEquals(final String expectedBalance, final Unit balanceUnit) {
return new ExpectAccountBalance(this, expectedBalance, balanceUnit);
return new ExpectAccountBalance(eth, this, expectedBalance, balanceUnit);
}

public Condition balanceEquals(final int expectedBalance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.account;

import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.EthTransactions;

public class Accounts {

private final EthTransactions eth;
private final Account richBenefactorOne;
private final Account richBenefactorTwo;

public Accounts() {
public Accounts(final EthTransactions eth) {
this.eth = eth;
richBenefactorOne =
Account.fromPrivateKey(
eth,
"Rich Benefactor One",
"8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63");
richBenefactorTwo =
Account.fromPrivateKey(
eth,
"Rich Benefactor Two",
"c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3");
}
Expand All @@ -37,6 +43,6 @@ public Account getPrimaryBenefactor() {
}

public Account createAccount(final String accountName) {
return Account.create(accountName);
return Account.create(eth, accountName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.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.node.Node;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.EthTransactions;

public class Blockchain {

private final EthTransactions eth;

public Blockchain(final EthTransactions eth) {
this.eth = eth;
}

public Condition blockNumberMustBeLatest(final Node node) {
return new ExpectMinimumBlockNumber(eth, node.execute(eth.blockNumber()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,33 @@

import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.EthTransactions;

import org.web3j.utils.Convert.Unit;

public class ExpectAccountBalance implements Condition {

private final EthTransactions eth;
private final Account account;
private final String expectedBalance;
private final Unit balanceUnit;

public ExpectAccountBalance(
final Account account, final String expectedBalance, final Unit balanceUnit) {
final EthTransactions eth,
final Account account,
final String expectedBalance,
final Unit balanceUnit) {
this.expectedBalance = expectedBalance;
this.balanceUnit = balanceUnit;
this.account = account;
this.eth = eth;
}

@Override
public void verify(final Node node) {
waitFor(
() ->
assertThat(node.getAccountBalance(account))
assertThat(node.execute(eth.getBalance((account))))
.isEqualTo(toWei(expectedBalance, balanceUnit).toBigIntegerExact()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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;

import static org.assertj.core.api.Assertions.assertThat;

import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.EthTransactions;

import java.math.BigInteger;

public class ExpectMinimumBlockNumber implements Condition {

private final EthTransactions eth;
private final BigInteger blockNumber;

public ExpectMinimumBlockNumber(final EthTransactions eth, final BigInteger blockNumber) {
this.blockNumber = blockNumber;
this.eth = eth;
}

@Override
public void verify(final Node node) {
assertThat(node.execute(eth.blockNumber())).isGreaterThanOrEqualTo(blockNumber);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.node;

import tech.pegasys.pantheon.tests.acceptance.dsl.account.Account;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction;

import java.math.BigInteger;

public interface Node {

<T> T execute(Transaction<T> transaction);

BigInteger getAccountBalance(Account account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@
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 static org.web3j.protocol.core.DefaultBlockParameterName.LATEST;

import tech.pegasys.pantheon.controller.KeyPairUtil;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.core.MiningParameters;
import tech.pegasys.pantheon.ethereum.jsonrpc.JsonRpcConfiguration;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.WebSocketConfiguration;
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.transaction.Transaction;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.net.ConnectException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -45,7 +43,6 @@
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jService;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.websocket.WebSocketService;
import org.web3j.utils.Async;
Expand All @@ -67,7 +64,6 @@ public class PantheonNode implements Node, AutoCloseable {
private final Properties portsProperties = new Properties();

private List<String> bootnodes = new ArrayList<>();
private Eth eth;
private Web3 web3;
private Web3j web3j;

Expand Down Expand Up @@ -145,26 +141,14 @@ public Web3j web3j() {
return web3j;
}

@Deprecated
public Web3j web3j(final Web3jService web3jService) {
private Web3j web3j(final Web3jService web3jService) {
if (web3j == null) {
web3j = Web3j.build(web3jService, 2000, Async.defaultExecutorService());
}

return web3j;
}

@Override
public BigInteger getAccountBalance(final Account account) {
try {
final EthGetBalance balanceResponse =
web3j().ethGetBalance(account.getAddress(), LATEST).send();
return balanceResponse.getBalance();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

public int getPeerCount() {
try {
return web3j().netPeerCount().send().getQuantity().intValueExact();
Expand Down Expand Up @@ -305,7 +289,6 @@ void stop() {
web3j = null;
}

eth = null;
web3 = null;
}

Expand All @@ -327,16 +310,12 @@ public Web3 web3() {
return web3;
}

public Eth eth() {
if (eth == null) {
eth = new Eth(web3j());
}

return eth;
}

@Override
public <T> T execute(final Transaction<T> transaction) {
return transaction.execute(web3j());
}

public void verify(final Condition expected) {
expected.verify(this);
}
}
Loading

0 comments on commit 8fa4572

Please sign in to comment.