Skip to content

Commit

Permalink
[MINOR] CLI Subcommands migrate to junit 5 (hyperledger#6282)
Browse files Browse the repository at this point in the history
* updated cli and chainimport/export tests to junit 5

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

* subcommands and besucommand test to junit5

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

* removed duplicate line

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

* removed vintage junit dep

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

---------

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Co-authored-by: Fabio Di Fabio <fabio.difabio@consensys.net>
  • Loading branch information
2 people authored and jflo committed Dec 18, 2023
1 parent df7ae8b commit 3184cf7
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 181 deletions.
2 changes: 0 additions & 2 deletions besu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ dependencies {
testImplementation 'org.awaitility:awaitility'
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.mockito:mockito-junit-jupiter'
testImplementation 'org.testcontainers:testcontainers'
testImplementation 'tech.pegasys.discovery:discovery'

testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
annotationProcessor 'com.google.dagger:dagger-compiler'
}
57 changes: 25 additions & 32 deletions besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,18 @@
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.toml.Toml;
import org.apache.tuweni.toml.TomlParseResult;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import picocli.CommandLine;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class BesuCommandTest extends CommandTestAbstract {

private static final String ENCLAVE_URI = "http://1.2.3.4:5555";
Expand Down Expand Up @@ -198,7 +197,7 @@ public class BesuCommandTest extends CommandTestAbstract {
DEFAULT_API_CONFIGURATION = ImmutableApiConfiguration.builder().build();
}

@Before
@BeforeEach
public void setup() {
try {
// optimistically tear down a potential previous loaded trusted setup
Expand All @@ -210,7 +209,7 @@ public void setup() {
MergeConfigOptions.setMergeEnabled(false);
}

@After
@AfterEach
public void tearDown() {

MergeConfigOptions.setMergeEnabled(false);
Expand Down Expand Up @@ -366,10 +365,10 @@ public void callingWithConfigOptionButInvalidValueTomlFileShouldDisplayHelp() th
}

@Test
public void overrideDefaultValuesIfKeyIsPresentInConfigFile() throws IOException {
public void overrideDefaultValuesIfKeyIsPresentInConfigFile(final @TempDir File dataFolder)
throws IOException {
final URL configFile = this.getClass().getResource("/complete_config.toml");
final Path genesisFile = createFakeGenesisFile(GENESIS_VALID_JSON);
final File dataFolder = temp.newFolder();
final String updatedConfig =
Resources.toString(configFile, UTF_8)
.replace("/opt/besu/genesis.json", escapeTomlString(genesisFile.toString()))
Expand Down Expand Up @@ -3745,7 +3744,7 @@ public void colorCanBeEnabledOrDisabledExplicitly() {
});
}

@Ignore
@Disabled
public void pruningIsEnabledIfSyncModeIsFast() {
parseCommand("--sync-mode", "FAST");

Expand All @@ -3756,7 +3755,7 @@ public void pruningIsEnabledIfSyncModeIsFast() {
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Ignore
@Disabled
public void pruningIsDisabledIfSyncModeIsFull() {
parseCommand("--sync-mode", "FULL");

Expand All @@ -3778,7 +3777,7 @@ public void pruningEnabledExplicitly() {
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Ignore
@Disabled
public void pruningDisabledExplicitly() {
parseCommand("--pruning-enabled=false", "--sync-mode=FAST");

Expand Down Expand Up @@ -4373,14 +4372,11 @@ public void privacyWithPruningMustError() {
assertThat(commandOutput.toString(UTF_8)).isEmpty();
}

@Rule public TemporaryFolder testFolder = new TemporaryFolder();

@Test
public void errorIsRaisedIfStaticNodesAreNotAllowed() throws IOException {
final File staticNodesFile = testFolder.newFile("static-nodes.json");
staticNodesFile.deleteOnExit();
final File permissioningConfig = testFolder.newFile("permissioning");
permissioningConfig.deleteOnExit();
public void errorIsRaisedIfStaticNodesAreNotAllowed(final @TempDir Path testFolder)
throws IOException {
final Path staticNodesFile = testFolder.resolve("static-nodes.json");
final Path permissioningConfig = testFolder.resolve("permissioning.json");

final EnodeURL staticNodeURI =
EnodeURLImpl.builder()
Expand All @@ -4399,17 +4395,16 @@ public void errorIsRaisedIfStaticNodesAreNotAllowed() throws IOException {
.listeningPort(30304)
.build();

Files.write(staticNodesFile, ("[\"" + staticNodeURI.toString() + "\"]").getBytes(UTF_8));
Files.write(
staticNodesFile.toPath(), ("[\"" + staticNodeURI.toString() + "\"]").getBytes(UTF_8));
Files.write(
permissioningConfig.toPath(),
permissioningConfig,
("nodes-allowlist=[\"" + allowedNode.toString() + "\"]").getBytes(UTF_8));

parseCommand(
"--data-path=" + testFolder.getRoot().getPath(),
"--data-path=" + testFolder,
"--bootnodes",
"--permissions-nodes-config-file-enabled=true",
"--permissions-nodes-config-file=" + permissioningConfig.getPath());
"--permissions-nodes-config-file=" + permissioningConfig);
assertThat(commandErrorOutput.toString(UTF_8))
.contains(staticNodeURI.toString(), "not in nodes-allowlist");
}
Expand Down Expand Up @@ -4985,12 +4980,10 @@ public void pkiBlockCreationPasswordFileRequired() {
"File containing password to unlock keystore is required when PKI Block Creation is enabled");
}

@Rule public TemporaryFolder pkiTempFolder = new TemporaryFolder();

@Test
public void pkiBlockCreationFullConfig() throws Exception {
public void pkiBlockCreationFullConfig(final @TempDir Path pkiTempFolder) throws Exception {
// Create temp file with password
final File pwdFile = pkiTempFolder.newFile("pwd");
final File pwdFile = pkiTempFolder.resolve("pwd").toFile();
FileUtils.writeStringToFile(pwdFile, "foo", UTF_8);

parseCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,20 @@
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.awaitility.Awaitility;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.RunLast;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public abstract class CommandTestAbstract {
private static final Logger TEST_LOGGER = LoggerFactory.getLogger(CommandTestAbstract.class);
protected static final JsonObject VALID_GENESIS_QBFT_POST_LONDON =
Expand Down Expand Up @@ -156,17 +154,30 @@ public abstract class CommandTestAbstract {
protected static final RpcEndpointServiceImpl rpcEndpointServiceImpl =
new RpcEndpointServiceImpl();

@Mock protected RunnerBuilder mockRunnerBuilder;
@Mock(lenient = true)
protected RunnerBuilder mockRunnerBuilder;

@Mock protected Runner mockRunner;

@Mock protected BesuController.Builder mockControllerBuilderFactory;
@Mock(lenient = true)
protected BesuController.Builder mockControllerBuilderFactory;

@Mock(lenient = true)
protected BesuControllerBuilder mockControllerBuilder;

@Mock(lenient = true)
protected EthProtocolManager mockEthProtocolManager;

@Mock protected BesuControllerBuilder mockControllerBuilder;
@Mock protected EthProtocolManager mockEthProtocolManager;
@Mock protected ProtocolSchedule mockProtocolSchedule;
@Mock protected ProtocolContext mockProtocolContext;

@Mock(lenient = true)
protected ProtocolContext mockProtocolContext;

@Mock protected BlockBroadcaster mockBlockBroadcaster;
@Mock protected BesuController mockController;

@Mock(lenient = true)
protected BesuController mockController;

@Mock protected RlpBlockExporter rlpBlockExporter;
@Mock protected JsonBlockImporter jsonBlockImporter;
@Mock protected RlpBlockImporter rlpBlockImporter;
Expand All @@ -188,7 +199,8 @@ public abstract class CommandTestAbstract {
@Mock
protected Logger mockLogger;

@Mock protected BesuComponent mockBesuComponent;
@Mock(lenient = true)
protected BesuComponent mockBesuComponent;

@Mock protected PkiBlockCreationConfigurationProvider mockPkiBlockCreationConfigProvider;
@Mock protected PkiBlockCreationConfiguration mockPkiBlockCreationConfiguration;
Expand Down Expand Up @@ -218,9 +230,7 @@ public abstract class CommandTestAbstract {

@Captor protected ArgumentCaptor<EthstatsOptions> ethstatsOptionsArgumentCaptor;

@Rule public final TemporaryFolder temp = new TemporaryFolder();

@Before
@BeforeEach
public void initMocks() throws Exception {
// doReturn used because of generic BesuController
doReturn(mockControllerBuilder)
Expand Down Expand Up @@ -353,7 +363,7 @@ public void initMocks() throws Exception {
when(mockBesuComponent.getBesuCommandLogger()).thenReturn(mockLogger);
}

@Before
@BeforeEach
public void setUpStreams() {
// reset the global opentelemetry singleton
GlobalOpenTelemetry.resetForTest();
Expand All @@ -364,7 +374,7 @@ public void setUpStreams() {
}

// Display outputs for debug purpose
@After
@AfterEach
public void displayOutput() throws IOException {
TEST_LOGGER.info("Standard output {}", commandOutput.toString(UTF_8));
TEST_LOGGER.info("Standard error {}", commandErrorOutput.toString(UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

import org.hyperledger.besu.BesuInfo;

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;
import picocli.CommandLine.Model.CommandSpec;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class PasswordSubCommandTest extends CommandTestAbstract {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
import org.bouncycastle.asn1.sec.SECNamedCurves;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.BeforeAll;
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;
import picocli.CommandLine.Model.CommandSpec;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class PublicKeySubCommandTest extends CommandTestAbstract {

private static final String EXPECTED_PUBLIC_KEY_USAGE =
Expand Down Expand Up @@ -126,13 +126,13 @@ public class PublicKeySubCommandTest extends CommandTestAbstract {
private static final String ALGORITHM = SignatureAlgorithm.ALGORITHM;
private static ECDomainParameters curve;

@BeforeClass
@BeforeAll
public static void setUp() {
final X9ECParameters params = SECNamedCurves.getByName(CURVE_NAME);
curve = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());
}

@Before
@BeforeEach
public void before() {
SignatureAlgorithmFactory.resetInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import java.nio.file.Path;

import com.google.common.io.Resources;
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;
import picocli.CommandLine.Model.CommandSpec;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class ValidateConfigSubCommandTest extends CommandTestAbstract {

private static final String EXPECTED_PUBLIC_KEY_USAGE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.json.JsonObject;
import org.apache.tuweni.bytes.Bytes32;
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;
import picocli.CommandLine.Model.CommandSpec;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class OperatorSubCommandTest extends CommandTestAbstract {

private static final String EXPECTED_OPERATOR_USAGE =
Expand All @@ -80,7 +80,7 @@ public class OperatorSubCommandTest extends CommandTestAbstract {

private Path tmpOutputDirectoryPath;

@Before
@BeforeEach
public void init() throws IOException {
SignatureAlgorithmFactory.resetInstance();
tmpOutputDirectoryPath = createTempDirectory(format("output-%d", currentTimeMillis()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.List;
import java.util.function.Consumer;

import org.junit.Test;
import org.junit.jupiter.api.Test;

public abstract class AbstractCLIOptionsTest<D, T extends CLIOptions<D>>
extends CommandTestAbstract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import org.hyperledger.besu.cli.options.unstable.EthProtocolOptions;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;

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 EthProtocolOptionsTest
extends AbstractCLIOptionsTest<EthProtocolConfiguration, EthProtocolOptions> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import org.hyperledger.besu.cli.options.unstable.MetricsCLIOptions;
import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration;

import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class MetricsCLIOptionsTest
extends AbstractCLIOptionsTest<MetricsConfiguration.Builder, MetricsCLIOptions> {

Expand Down
Loading

0 comments on commit 3184cf7

Please sign in to comment.