[MINOR] move Clique tests to junit5 (#4975)

* easy ones

* parameterized test

* refactor to avoid unnecessary stubbings

* migrate abstract class and subclasses to junit 5

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
pull/4993/head
Sally MacFarlane 2 years ago committed by GitHub
parent 844db62344
commit 1a39e38315
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      consensus/clique/build.gradle
  2. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueBlockChoiceTests.java
  3. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueBlockHashingTest.java
  4. 2
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueBlockInterfaceTest.java
  5. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java
  6. 2
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueExtraDataTest.java
  7. 2
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueProtocolScheduleTest.java
  8. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/NodeCanProduceNextBlockTest.java
  9. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java
  10. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockSchedulerTest.java
  11. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java
  12. 32
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMiningCoordinatorTest.java
  13. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueProposerSelectorTest.java
  14. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java
  15. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueExtraDataValidationRuleTest.java
  16. 44
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/VoteValidationRuleTest.java
  17. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignerMetricsTest.java
  18. 12
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersAtHashTest.java
  19. 12
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueGetSignersTest.java
  20. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/CliqueProposalsTest.java
  21. 12
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/DiscardTest.java
  22. 6
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/jsonrpc/methods/ProposeTest.java
  23. 2
      consensus/common/src/test/java/org/hyperledger/besu/consensus/common/jsonrpc/AbstractVoteProposerMethodTest.java
  24. 6
      consensus/ibft/src/test/java/org/hyperledger/besu/consensus/ibft/jsonrpc/methods/IbftGetPendingVotesTest.java
  25. 6
      consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetPendingVotesTest.java

@ -56,10 +56,10 @@ dependencies {
testImplementation project(path: ':ethereum:core', configuration: 'testSupportArtifacts')
testImplementation project(':testutil')
testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.mockito:mockito-junit-jupiter'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
}

@ -41,8 +41,8 @@ import java.util.stream.Collectors;
import java.util.stream.IntStream;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueBlockChoiceTests {
private List<KeyPair> keyPairs;
@ -57,7 +57,7 @@ public class CliqueBlockChoiceTests {
return new Block(header, new BlockBody(Lists.newArrayList(), Lists.newArrayList()));
}
@Before
@BeforeEach
public void setup() {
keyPairs =
IntStream.range(0, 8)

@ -27,8 +27,8 @@ import java.util.Arrays;
import java.util.List;
import org.apache.tuweni.bytes.Bytes;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueBlockHashingTest {
@ -43,7 +43,7 @@ public class CliqueBlockHashingTest {
private static final Hash KNOWN_BLOCK_HASH =
Hash.fromHexString("0x8b27a29300811af926039b90288d3d384dcb55931049c17c4f762e45c116776e");
@Before
@BeforeEach
public void setup() {
expectedHeader = createKnownHeaderFromCapturedData();
}

@ -33,7 +33,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class CliqueBlockInterfaceTest {

@ -33,8 +33,8 @@ import java.math.BigInteger;
import java.util.List;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueDifficultyCalculatorTest {
@ -46,7 +46,7 @@ public class CliqueDifficultyCalculatorTest {
private BlockHeaderTestFixture blockHeaderBuilder;
private final CliqueBlockInterface blockInterface = new CliqueBlockInterface();
@Before
@BeforeEach
public void setup() {
localAddr = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());

@ -37,7 +37,7 @@ import com.google.common.base.Suppliers;
import com.google.common.collect.Lists;
import org.apache.tuweni.bytes.Bytes;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class CliqueExtraDataTest {

@ -35,7 +35,7 @@ import org.hyperledger.besu.evm.internal.EvmConfiguration;
import java.time.Instant;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class CliqueProtocolScheduleTest {

@ -39,8 +39,8 @@ import org.hyperledger.besu.ethereum.core.Util;
import java.util.List;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class NodeCanProduceNextBlockTest {
@ -62,7 +62,7 @@ public class NodeCanProduceNextBlockTest {
return new Block(header, new BlockBody(Lists.newArrayList(), Lists.newArrayList()));
}
@Before
@BeforeEach
public void setup() {
localAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
validatorList.add(localAddress);

@ -63,8 +63,8 @@ import java.util.Optional;
import com.google.common.collect.Lists;
import org.apache.tuweni.bytes.Bytes;
import org.assertj.core.api.Java6Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueBlockCreatorTest {
@ -84,7 +84,7 @@ public class CliqueBlockCreatorTest {
private ValidatorProvider validatorProvider;
private VoteProvider voteProvider;
@Before
@BeforeEach
public void setup() {
protocolSchedule =
CliqueProtocolSchedule.create(

@ -33,8 +33,8 @@ import java.time.Clock;
import java.util.List;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueBlockSchedulerTest {
@ -45,7 +45,7 @@ public class CliqueBlockSchedulerTest {
private ValidatorProvider validatorProvider;
private BlockHeaderTestFixture blockHeaderBuilder;
@Before
@BeforeEach
public void setup() {
localAddr = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());

@ -52,8 +52,8 @@ import java.util.Random;
import com.google.common.collect.Lists;
import org.apache.tuweni.bytes.Bytes;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueMinerExecutorTest {
@ -69,7 +69,7 @@ public class CliqueMinerExecutorTest {
private final MetricsSystem metricsSystem = new NoOpMetricsSystem();
private final CliqueBlockInterface blockInterface = new CliqueBlockInterface();
@Before
@BeforeEach
public void setup() {
localAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
validatorList.add(localAddress);

@ -49,14 +49,14 @@ import java.util.Optional;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class CliqueMiningCoordinatorTest {
private static final Supplier<SignatureAlgorithm> SIGNATURE_ALGORITHM =
@ -81,18 +81,13 @@ public class CliqueMiningCoordinatorTest {
@Mock private SyncState syncState;
@Mock private ValidatorProvider validatorProvider;
@Before
@BeforeEach
public void setup() {
headerTestFixture.number(1);
Block genesisBlock = createEmptyBlock(0, Hash.ZERO, proposerKeys); // not normally signed but ok
blockChain = createInMemoryBlockchain(genesisBlock);
when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators);
final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface);
when(protocolContext.getConsensusContext(CliqueContext.class)).thenReturn(cliqueContext);
when(protocolContext.getBlockchain()).thenReturn(blockChain);
when(minerExecutor.startAsyncMining(any(), any(), any())).thenReturn(Optional.of(blockMiner));
when(syncState.isInSync()).thenReturn(true);
@ -101,6 +96,8 @@ public class CliqueMiningCoordinatorTest {
@Test
public void outOfTurnBlockImportedDoesNotInterruptInTurnMiningOperation() {
setupCliqueContextAndBlockchain();
// As the head of the blockChain is 0 (which effectively doesn't have a signer, all validators
// are able to propose.
@ -159,6 +156,8 @@ public class CliqueMiningCoordinatorTest {
@Test
public void outOfTurnBlockImportedInterruptsOutOfTurnMiningOperation() {
setupCliqueContextAndBlockchain();
blockChain.appendBlock(
createEmptyBlock(1, blockChain.getChainHeadHash(), validatorKeys), Collections.emptyList());
@ -190,6 +189,8 @@ public class CliqueMiningCoordinatorTest {
@Test
public void outOfTurnBlockImportedInterruptsNonRunningMiner() {
setupCliqueContextAndBlockchain();
blockChain.appendBlock(
createEmptyBlock(1, blockChain.getChainHeadHash(), proposerKeys), Collections.emptyList());
@ -251,4 +252,13 @@ public class CliqueMiningCoordinatorTest {
TestHelpers.createCliqueSignedBlockHeader(headerTestFixture, signer, validators);
return new Block(header, new BlockBody(Collections.emptyList(), Collections.emptyList()));
}
private void setupCliqueContextAndBlockchain() {
when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators);
final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface);
when(protocolContext.getConsensusContext(CliqueContext.class)).thenReturn(cliqueContext);
when(protocolContext.getBlockchain()).thenReturn(blockChain);
}
}

@ -27,8 +27,8 @@ import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueProposerSelectorTest {
@ -40,7 +40,7 @@ public class CliqueProposerSelectorTest {
AddressHelpers.ofValue(4));
private ValidatorProvider validatorProvider;
@Before
@BeforeEach
public void setup() {
validatorProvider = mock(ValidatorProvider.class);
when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList);

@ -36,8 +36,8 @@ import org.hyperledger.besu.ethereum.core.Util;
import java.util.List;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueDifficultyValidationRuleTest {
@ -47,7 +47,7 @@ public class CliqueDifficultyValidationRuleTest {
private BlockHeaderTestFixture blockHeaderBuilder;
private final CliqueBlockInterface blockInterface = new CliqueBlockInterface();
@Before
@BeforeEach
public void setup() {
final Address localAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
validatorList.add(localAddress);

@ -38,8 +38,8 @@ import java.util.List;
import com.google.common.collect.Lists;
import org.apache.tuweni.bytes.Bytes;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueExtraDataValidationRuleTest {
@ -50,7 +50,7 @@ public class CliqueExtraDataValidationRuleTest {
private final List<Address> validatorList = Lists.newArrayList();
private ProtocolContext cliqueProtocolContext;
@Before
@BeforeEach
public void setup() {
localAddr = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());

@ -20,39 +20,33 @@ import org.hyperledger.besu.consensus.clique.CliqueBlockInterface;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
@RunWith(Parameterized.class)
public class VoteValidationRuleTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[][] {
{CliqueBlockInterface.DROP_NONCE, true},
{CliqueBlockInterface.ADD_NONCE, true},
{0x01L, false},
{0xFFFFFFFFFFFFFFFEL, false}
});
static class CliqueVoteValidationArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext context) {
return Stream.of(
Arguments.of(CliqueBlockInterface.DROP_NONCE, true),
Arguments.of(CliqueBlockInterface.ADD_NONCE, true),
Arguments.of(0x01L, false),
Arguments.of(0xFFFFFFFFFFFFFFFEL, false));
}
}
@Parameter public long actualVote;
@Parameter(1)
public boolean expectedResult;
@Test
public void test() {
@ParameterizedTest
@ArgumentsSource(CliqueVoteValidationArgumentsProvider.class)
public void test(final long input, final boolean expectedResult) {
final VoteValidationRule uut = new VoteValidationRule();
final BlockHeaderTestFixture blockBuilder = new BlockHeaderTestFixture();
blockBuilder.nonce(actualVote);
blockBuilder.nonce(input);
final BlockHeader header = blockBuilder.buildHeader();

@ -41,8 +41,8 @@ import java.util.List;
import java.util.Optional;
import java.util.stream.LongStream;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueGetSignerMetricsTest {
@ -58,7 +58,7 @@ public class CliqueGetSignerMetricsTest {
private BlockchainQueries blockchainQueries;
private BlockInterface blockInterface;
@Before
@BeforeEach
public void setup() {
validatorProvider = mock(ValidatorProvider.class);
blockchainQueries = mock(BlockchainQueries.class);

@ -42,13 +42,13 @@ import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;
import org.assertj.core.api.AssertionsForClassTypes;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class CliqueGetSignersAtHashTest {
private CliqueGetSignersAtHash method;
@ -63,7 +63,7 @@ public class CliqueGetSignersAtHashTest {
public static final String BLOCK_HASH =
"0xe36a3edf0d8664002a72ef7c5f8e271485e7ce5c66455a07cb679d855818415f";
@Before
@BeforeEach
public void setup() {
method = new CliqueGetSignersAtHash(blockchainQueries, validatorProvider);

@ -38,13 +38,13 @@ import java.util.List;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class CliqueGetSignersTest {
private CliqueGetSigners method;
private BlockHeader blockHeader;
@ -55,7 +55,7 @@ public class CliqueGetSignersTest {
@Mock private ValidatorProvider validatorProvider;
@Mock private BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata;
@Before
@BeforeEach
public void setup() {
method = new CliqueGetSigners(blockchainQueries, validatorProvider);

@ -19,8 +19,8 @@ import static org.assertj.core.api.Java6Assertions.assertThat;
import org.hyperledger.besu.consensus.common.jsonrpc.AbstractVoteProposerMethod;
import org.hyperledger.besu.consensus.common.jsonrpc.AbstractVoteProposerMethodTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CliqueProposalsTest extends AbstractVoteProposerMethodTest {
@ -36,7 +36,7 @@ public class CliqueProposalsTest extends AbstractVoteProposerMethodTest {
return "clique_proposals";
}
@Before
@BeforeEach
public void setup() {
method = new CliqueProposals(getValidatorProvider());
}

@ -32,19 +32,19 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcRespon
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class DiscardTest {
private final String JSON_RPC_VERSION = "2.0";
private final String METHOD = "clique_discard";
private ValidatorProvider validatorProvider;
@Before
@BeforeEach
public void setup() {
final Blockchain blockchain = mock(Blockchain.class);
final EpochManager epochManager = mock(EpochManager.class);

@ -32,15 +32,15 @@ import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcRespon
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ProposeTest {
private final String JSON_RPC_VERSION = "2.0";
private final String METHOD = "clique_propose";
private ValidatorProvider validatorProvider;
@Before
@BeforeEach
public void setup() {
final Blockchain blockchain = mock(Blockchain.class);
final EpochManager epochManager = mock(EpochManager.class);

@ -33,7 +33,7 @@ import java.util.Optional;
import com.google.common.collect.ImmutableMap;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public abstract class AbstractVoteProposerMethodTest {

@ -19,8 +19,8 @@ import static org.assertj.core.api.Java6Assertions.assertThat;
import org.hyperledger.besu.consensus.common.jsonrpc.AbstractVoteProposerMethod;
import org.hyperledger.besu.consensus.common.jsonrpc.AbstractVoteProposerMethodTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class IbftGetPendingVotesTest extends AbstractVoteProposerMethodTest {
@ -36,7 +36,7 @@ public class IbftGetPendingVotesTest extends AbstractVoteProposerMethodTest {
return "ibft_getPendingVotes";
}
@Before
@BeforeEach
public void setup() {
method = new IbftGetPendingVotes(getValidatorProvider());
}

@ -19,8 +19,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.hyperledger.besu.consensus.common.jsonrpc.AbstractVoteProposerMethod;
import org.hyperledger.besu.consensus.common.jsonrpc.AbstractVoteProposerMethodTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class QbftGetPendingVotesTest extends AbstractVoteProposerMethodTest {
@ -36,7 +36,7 @@ public class QbftGetPendingVotesTest extends AbstractVoteProposerMethodTest {
return "qbft_getPendingVotes";
}
@Before
@BeforeEach
public void setup() {
method = new QbftGetPendingVotes(getValidatorProvider());
}

Loading…
Cancel
Save