Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MINOR] CLI unit tests migrate to junit 5 #6281

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,30 @@
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.junit.jupiter.MockitoExtension;

/** Tests for {@link BlockExporter}. */
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public final class RlpBlockExporterTest {

@ClassRule public static final TemporaryFolder folder = new TemporaryFolder();
@TempDir public static Path folder;
private static Blockchain blockchain;
private static long chainHead;
private static ProtocolSchedule protocolSchedule;

@BeforeClass
@BeforeAll
public static void setupBlockchain() throws IOException {
final BesuController controller = createController();
final Path blocks = folder.newFile("1000.blocks").toPath();
final BesuController controller =
createController(Files.createTempDirectory(folder, "rlpBlockExporterTestData"));
final Path blocks = Files.createTempFile(folder, "1000", "blocks");
BlockTestUtil.write1000Blocks(blocks);
blockchain = importBlocks(controller, blocks);
chainHead = blockchain.getChainHeadBlockNumber();
Expand All @@ -83,8 +84,7 @@ private static Blockchain importBlocks(final BesuController controller, final Pa
return controller.getProtocolContext().getBlockchain();
}

private static BesuController createController() throws IOException {
final Path dataDir = folder.newFolder().toPath();
private static BesuController createController(final @TempDir Path dataDir) throws IOException {
return new BesuController.Builder()
.fromGenesisConfig(GenesisConfigFile.mainnet(), SyncMode.FAST)
.synchronizerConfiguration(SynchronizerConfiguration.builder().build())
Expand All @@ -105,13 +105,13 @@ private static BesuController createController() throws IOException {
}

@Test
public void exportBlocks_noBounds() throws IOException {
final File outputPath = folder.newFile();
public void exportBlocks_noBounds(final @TempDir Path outputDir) throws IOException {
final Path outputPath = outputDir.resolve("output");
final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);
exporter.exportBlocks(outputPath, Optional.empty(), Optional.empty());
exporter.exportBlocks(outputPath.toFile(), Optional.empty(), Optional.empty());

// Iterate over blocks and check that they match expectations
final RawBlockIterator blockIterator = getBlockIterator(outputPath.toPath());
final RawBlockIterator blockIterator = getBlockIterator(outputPath);
long currentBlockNumber = 0;
while (blockIterator.hasNext()) {
final Block actual = blockIterator.next();
Expand All @@ -125,15 +125,15 @@ public void exportBlocks_noBounds() throws IOException {
}

@Test
public void exportBlocks_withLowerBound() throws IOException {
final File outputPath = folder.newFile();
public void exportBlocks_withLowerBound(final @TempDir Path outputDir) throws IOException {
final Path outputPath = outputDir.resolve("output");
final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);

final long lowerBound = 990;
exporter.exportBlocks(outputPath, Optional.of(lowerBound), Optional.empty());
exporter.exportBlocks(outputPath.toFile(), Optional.of(lowerBound), Optional.empty());

// Iterate over blocks and check that they match expectations
final RawBlockIterator blockIterator = getBlockIterator(outputPath.toPath());
final RawBlockIterator blockIterator = getBlockIterator(outputPath);
long currentBlockNumber = lowerBound;
while (blockIterator.hasNext()) {
final Block actual = blockIterator.next();
Expand All @@ -147,15 +147,15 @@ public void exportBlocks_withLowerBound() throws IOException {
}

@Test
public void exportBlocks_withUpperBound() throws IOException {
final File outputPath = folder.newFile();
public void exportBlocks_withUpperBound(final @TempDir Path outputDir) throws IOException {
final Path outputPath = outputDir.resolve("output");
final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);

final long upperBound = 10;
exporter.exportBlocks(outputPath, Optional.empty(), Optional.of(upperBound));
exporter.exportBlocks(outputPath.toFile(), Optional.empty(), Optional.of(upperBound));

// Iterate over blocks and check that they match expectations
final RawBlockIterator blockIterator = getBlockIterator(outputPath.toPath());
final RawBlockIterator blockIterator = getBlockIterator(outputPath);
long currentBlockNumber = 0;
while (blockIterator.hasNext()) {
final Block actual = blockIterator.next();
Expand All @@ -169,16 +169,17 @@ public void exportBlocks_withUpperBound() throws IOException {
}

@Test
public void exportBlocks_withUpperAndLowerBounds() throws IOException {
final File outputPath = folder.newFile();
public void exportBlocks_withUpperAndLowerBounds(final @TempDir Path outputDir)
throws IOException {
final Path outputPath = outputDir.resolve("output");
final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);

final long lowerBound = 5;
final long upperBound = 10;
exporter.exportBlocks(outputPath, Optional.of(lowerBound), Optional.of(upperBound));
exporter.exportBlocks(outputPath.toFile(), Optional.of(lowerBound), Optional.of(upperBound));

// Iterate over blocks and check that they match expectations
final RawBlockIterator blockIterator = getBlockIterator(outputPath.toPath());
final RawBlockIterator blockIterator = getBlockIterator(outputPath);
long currentBlockNumber = lowerBound;
while (blockIterator.hasNext()) {
final Block actual = blockIterator.next();
Expand All @@ -192,16 +193,17 @@ public void exportBlocks_withUpperAndLowerBounds() throws IOException {
}

@Test
public void exportBlocks_withRangeBeyondChainHead() throws IOException {
final File outputPath = folder.newFile();
public void exportBlocks_withRangeBeyondChainHead(final @TempDir Path outputDir)
throws IOException {
final Path outputPath = outputDir.resolve("output");
final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);

final long lowerBound = chainHead - 10;
final long upperBound = chainHead + 10;
exporter.exportBlocks(outputPath, Optional.of(lowerBound), Optional.of(upperBound));
exporter.exportBlocks(outputPath.toFile(), Optional.of(lowerBound), Optional.of(upperBound));

// Iterate over blocks and check that they match expectations
final RawBlockIterator blockIterator = getBlockIterator(outputPath.toPath());
final RawBlockIterator blockIterator = getBlockIterator(outputPath);
long currentBlockNumber = lowerBound;
while (blockIterator.hasNext()) {
final Block actual = blockIterator.next();
Expand All @@ -215,8 +217,7 @@ public void exportBlocks_withRangeBeyondChainHead() throws IOException {
}

@Test
public void exportBlocks_negativeStartNumber() throws IOException {
final File outputPath = folder.newFile();
public void exportBlocks_negativeStartNumber(final @TempDir File outputPath) throws IOException {
final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);

assertThatThrownBy(() -> exporter.exportBlocks(outputPath, Optional.of(-1L), Optional.empty()))
Expand All @@ -225,8 +226,7 @@ public void exportBlocks_negativeStartNumber() throws IOException {
}

@Test
public void exportBlocks_negativeEndNumber() throws IOException {
final File outputPath = folder.newFile();
public void exportBlocks_negativeEndNumber(final @TempDir File outputPath) throws IOException {
final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);

assertThatThrownBy(() -> exporter.exportBlocks(outputPath, Optional.empty(), Optional.of(-1L)))
Expand All @@ -235,8 +235,7 @@ public void exportBlocks_negativeEndNumber() throws IOException {
}

@Test
public void exportBlocks_outOfOrderBounds() throws IOException {
final File outputPath = folder.newFile();
public void exportBlocks_outOfOrderBounds(final @TempDir File outputPath) throws IOException {
final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);

assertThatThrownBy(() -> exporter.exportBlocks(outputPath, Optional.of(10L), Optional.of(2L)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,39 @@
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.io.Resources;
import org.apache.tuweni.bytes.Bytes;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public abstract class JsonBlockImporterTest {

@Rule public final TemporaryFolder folder = new TemporaryFolder();
@TempDir public Path dataDir;

protected final String consensusEngine;
protected final GenesisConfigFile genesisConfigFile;
protected final boolean isEthash;
protected String consensusEngine;
protected GenesisConfigFile genesisConfigFile;
protected boolean isEthash;

protected JsonBlockImporterTest(final String consensusEngine) throws IOException {
protected void setup(final String consensusEngine) throws IOException {
this.consensusEngine = consensusEngine;
final String genesisData = getFileContents("genesis.json");
this.genesisConfigFile = GenesisConfigFile.fromConfig(genesisData);
this.isEthash = genesisConfigFile.getConfigOptions().isEthHash();
}

public static class SingletonTests extends JsonBlockImporterTest {
public SingletonTests() throws IOException {
super("unsupported");

@BeforeEach
public void setup() throws IOException {
super.setup("unsupported");
}

@Test
Expand All @@ -97,21 +98,23 @@ public void importChain_unsupportedConsensusAlgorithm() throws IOException {
}
}

@RunWith(Parameterized.class)
public static class ParameterizedTests extends JsonBlockImporterTest {

public ParameterizedTests(final String consensusEngine) throws IOException {
super(consensusEngine);
@Override
public void setup(final String consensusEngine) throws IOException {
super.setup(consensusEngine);
}

@Parameters(name = "Name: {0}")
public static Collection<Object[]> getParameters() {
final Object[][] params = {{"ethash"}, {"clique"}};
return Arrays.asList(params);
public static Stream<Arguments> getParameters() {
return Stream.of(Arguments.of("ethash"), Arguments.of("clique"));
}

@Test
public void importChain_validJson_withBlockNumbers() throws IOException {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("getParameters")
public void importChain_validJson_withBlockNumbers(final String consensusEngine)
throws IOException {
setup(consensusEngine);

final BesuController controller = createController();
final JsonBlockImporter importer = new JsonBlockImporter(controller);

Expand Down Expand Up @@ -201,8 +204,12 @@ public void importChain_validJson_withBlockNumbers() throws IOException {
assertThat(tx.getNonce()).isEqualTo(1L);
}

@Test
public void importChain_validJson_noBlockIdentifiers() throws IOException {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("getParameters")
public void importChain_validJson_noBlockIdentifiers(final String consensusEngine)
throws IOException {
setup(consensusEngine);

final BesuController controller = createController();
final JsonBlockImporter importer = new JsonBlockImporter(controller);

Expand Down Expand Up @@ -292,8 +299,12 @@ public void importChain_validJson_noBlockIdentifiers() throws IOException {
assertThat(tx.getNonce()).isEqualTo(1L);
}

@Test
public void importChain_validJson_withParentHashes() throws IOException {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("getParameters")
public void importChain_validJson_withParentHashes(final String consensusEngine)
throws IOException {
setup(consensusEngine);

final BesuController controller = createController();
final JsonBlockImporter importer = new JsonBlockImporter(controller);

Expand Down Expand Up @@ -343,8 +354,11 @@ public void importChain_validJson_withParentHashes() throws IOException {
assertThat(tx.getNonce()).isEqualTo(2L);
}

@Test
public void importChain_invalidParent() throws IOException {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("getParameters")
public void importChain_invalidParent(final String consensusEngine) throws IOException {
setup(consensusEngine);

final BesuController controller = createController();
final JsonBlockImporter importer = new JsonBlockImporter(controller);

Expand All @@ -355,8 +369,11 @@ public void importChain_invalidParent() throws IOException {
.hasMessageStartingWith("Unable to locate block parent at 2456");
}

@Test
public void importChain_invalidTransaction() throws IOException {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("getParameters")
public void importChain_invalidTransaction(final String consensusEngine) throws IOException {
setup(consensusEngine);

final BesuController controller = createController();
final JsonBlockImporter importer = new JsonBlockImporter(controller);

Expand All @@ -368,8 +385,11 @@ public void importChain_invalidTransaction() throws IOException {
"Unable to create block. 1 transaction(s) were found to be invalid.");
}

@Test
public void importChain_specialFields() throws IOException {
@ParameterizedTest(name = "{index}: {0}")
@MethodSource("getParameters")
public void importChain_specialFields(final String consensusEngine) throws IOException {
setup(consensusEngine);

final BesuController controller = createController();
final JsonBlockImporter importer = new JsonBlockImporter(controller);

Expand Down Expand Up @@ -414,7 +434,6 @@ protected BesuController createController() throws IOException {

protected BesuController createController(final GenesisConfigFile genesisConfigFile)
throws IOException {
final Path dataDir = folder.newFolder().toPath();
return new BesuController.Builder()
.fromGenesisConfig(genesisConfigFile, SyncMode.FAST)
.synchronizerConfiguration(SynchronizerConfiguration.builder().build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

import java.math.BigInteger;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
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 EthNetworkConfigTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import org.hyperledger.besu.cli.converter.exception.FractionConversionException;
import org.hyperledger.besu.util.number.Fraction;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
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 FractionConverterTest {

private final FractionConverter fractionConverter = new FractionConverter();
Expand Down
Loading
Loading