Skip to content

Commit

Permalink
[MINOR] More cli tests to junit 5 (hyperledger#6283)
Browse files Browse the repository at this point in the history
* more CLI and services tests to junit5

---------

Signed-off-by: Sally MacFarlane <[email protected]>
Co-authored-by: Fabio Di Fabio <[email protected]>
Signed-off-by: jflo <[email protected]>
  • Loading branch information
2 people authored and jflo committed Dec 18, 2023
1 parent 53f9ea4 commit a625e69
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
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;
import org.slf4j.Logger;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.RunLast;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class CommandLineUtilsTest {
@SuppressWarnings("PrivateStaticFinalLoggers") // @Mocks are inited by JUnit
@Mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import java.util.HashMap;
import java.util.Map;

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

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class EnvironmentVariableDefaultProviderTest {

private final Map<String, String> environment = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,37 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import picocli.CommandLine;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Model.OptionSpec;
import picocli.CommandLine.ParameterException;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class TomlConfigFileDefaultProviderTest {
@Mock CommandLine mockCommandLine;

@Mock CommandSpec mockCommandSpec;

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

@Test
public void defaultValueForMatchingKey() throws IOException {
public void defaultValueForMatchingKey(final @TempDir Path temp) throws IOException {
when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec);
Map<String, OptionSpec> validOptionsMap = new HashMap<>();
validOptionsMap.put("--a-short-option", null);
validOptionsMap.put("--an-actual-long-option", null);
validOptionsMap.put("--a-longer-option", null);
when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap);

final File tempConfigFile = temp.newFile("config.toml");
final File tempConfigFile = temp.resolve("config.toml").toFile();
try (final BufferedWriter fileWriter =
Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8)) {

Expand Down Expand Up @@ -106,7 +104,7 @@ public void defaultValueForMatchingKey() throws IOException {
}

@Test
public void defaultValueForOptionMustMatchType() throws IOException {
public void defaultValueForOptionMustMatchType(final @TempDir Path temp) throws IOException {
when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec);
Map<String, OptionSpec> validOptionsMap = new HashMap<>();
validOptionsMap.put("--a-boolean-option", null);
Expand All @@ -124,7 +122,7 @@ public void defaultValueForOptionMustMatchType() throws IOException {

when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap);

final File tempConfigFile = temp.newFile("config.toml");
final File tempConfigFile = temp.resolve("config.toml").toFile();
try (final BufferedWriter fileWriter =
Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8)) {

Expand Down Expand Up @@ -238,9 +236,9 @@ public void configFileNotFoundMustThrow() {
}

@Test
public void invalidConfigMustThrow() throws IOException {
public void invalidConfigMustThrow(final @TempDir Path temp) throws IOException {

final File tempConfigFile = temp.newFile("config.toml");
final File tempConfigFile = Files.createTempFile("invalid", "toml").toFile();

final TomlConfigFileDefaultProvider providerUnderTest =
new TomlConfigFileDefaultProvider(mockCommandLine, tempConfigFile);
Expand All @@ -254,9 +252,9 @@ public void invalidConfigMustThrow() throws IOException {
}

@Test
public void invalidConfigContentMustThrow() throws IOException {
public void invalidConfigContentMustThrow(final @TempDir Path temp) throws IOException {

final File tempConfigFile = temp.newFile("config.toml");
final File tempConfigFile = temp.resolve("config.toml").toFile();
final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8);

fileWriter.write("an-invalid-syntax=======....");
Expand All @@ -276,13 +274,13 @@ public void invalidConfigContentMustThrow() throws IOException {
}

@Test
public void unknownOptionMustThrow() throws IOException {
public void unknownOptionMustThrow(final @TempDir Path temp) throws IOException {

when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec);
Map<String, OptionSpec> validOptionsMap = new HashMap<>();
when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap);

final File tempConfigFile = temp.newFile("config.toml");
final File tempConfigFile = temp.resolve("config.toml").toFile();
final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8);

fileWriter.write("invalid_option=true");
Expand All @@ -300,7 +298,7 @@ public void unknownOptionMustThrow() throws IOException {
}

@Test
public void tomlTableHeadingsMustBeIgnored() throws IOException {
public void tomlTableHeadingsMustBeIgnored(final @TempDir Path temp) throws IOException {

when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec);

Expand All @@ -310,7 +308,7 @@ public void tomlTableHeadingsMustBeIgnored() throws IOException {
validOptionsMap.put("--onemore-valid-option", null);
when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap);

final File tempConfigFile = temp.newFile("config.toml");
final File tempConfigFile = temp.resolve("config.toml").toFile();
final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8);

fileWriter.write("a-valid-option=123");
Expand Down Expand Up @@ -343,15 +341,16 @@ public void tomlTableHeadingsMustBeIgnored() throws IOException {
}

@Test
public void tomlTableHeadingsMustNotSkipValidationOfUnknownOptions() throws IOException {
public void tomlTableHeadingsMustNotSkipValidationOfUnknownOptions(final @TempDir Path temp)
throws IOException {

when(mockCommandLine.getCommandSpec()).thenReturn(mockCommandSpec);

Map<String, OptionSpec> validOptionsMap = new HashMap<>();
validOptionsMap.put("--a-valid-option", null);
when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap);

final File tempConfigFile = temp.newFile("config.toml");
final File tempConfigFile = temp.resolve("config.toml").toFile();
final BufferedWriter fileWriter = Files.newBufferedWriter(tempConfigFile.toPath(), UTF_8);

fileWriter.write("[ignoreme]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -28,18 +29,19 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
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.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import picocli.CommandLine;
import picocli.CommandLine.IDefaultValueProvider;
import picocli.CommandLine.IExecutionStrategy;
Expand All @@ -49,11 +51,11 @@
import picocli.CommandLine.ParseResult;
import picocli.CommandLine.RunLast;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class ConfigOptionSearchAndRunHandlerTest {

private static final String CONFIG_FILE_OPTION_NAME = "--config-file";
@Rule public final TemporaryFolder temp = new TemporaryFolder();
@TempDir public Path temp;

private LoggingLevelOption levelOption;
private final IExecutionStrategy resultHandler = new RunLast();
Expand All @@ -68,16 +70,18 @@ public class ConfigOptionSearchAndRunHandlerTest {
@Mock IGetter mockConfigOptionGetter;
@Mock BesuParameterExceptionHandler mockParameterExceptionHandler;

@Before
@BeforeEach
public void initMocks() {
when(mockCommandSpec.commandLine()).thenReturn(mockCommandLine);
when(mockParseResult.commandSpec()).thenReturn(mockCommandSpec);
lenient().when(mockCommandSpec.commandLine()).thenReturn(mockCommandLine);
lenient().when(mockParseResult.commandSpec()).thenReturn(mockCommandSpec);
final List<String> originalArgs = new ArrayList<>();
originalArgs.add(CONFIG_FILE_OPTION_NAME);
when(mockParseResult.originalArgs()).thenReturn(originalArgs);
when(mockParseResult.matchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(mockConfigOptionSpec);
when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(true);
when(mockConfigOptionSpec.getter()).thenReturn(mockConfigOptionGetter);
lenient().when(mockParseResult.originalArgs()).thenReturn(originalArgs);
lenient()
.when(mockParseResult.matchedOption(CONFIG_FILE_OPTION_NAME))
.thenReturn(mockConfigOptionSpec);
lenient().when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(true);
lenient().when(mockConfigOptionSpec.getter()).thenReturn(mockConfigOptionGetter);
levelOption = new LoggingLevelOption();
levelOption.setLogLevel("INFO");
configParsingHandler =
Expand All @@ -87,7 +91,7 @@ public void initMocks() {

@Test
public void handleWithCommandLineOption() throws Exception {
when(mockConfigOptionGetter.get()).thenReturn(temp.newFile());
when(mockConfigOptionGetter.get()).thenReturn(Files.createTempFile("tmp", "txt").toFile());
final List<Object> result = configParsingHandler.handle(mockParseResult);
verify(mockCommandLine).setDefaultValueProvider(any(IDefaultValueProvider.class));
verify(mockCommandLine).setExecutionStrategy(eq(resultHandler));
Expand All @@ -105,7 +109,9 @@ public void handleWithEnvironmentVariable() throws IOException {
new ConfigOptionSearchAndRunHandler(
resultHandler,
mockParameterExceptionHandler,
singletonMap("BESU_CONFIG_FILE", temp.newFile().getAbsolutePath()));
singletonMap(
"BESU_CONFIG_FILE",
Files.createFile(temp.resolve("tmp")).toFile().getAbsolutePath()));

when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(false);

Expand Down Expand Up @@ -160,7 +166,7 @@ public void handleThrowsErrorWithWithEnvironmentVariableAndCommandLineSpecified(
new ConfigOptionSearchAndRunHandler(
resultHandler,
mockParameterExceptionHandler,
singletonMap("BESU_CONFIG_FILE", temp.newFile().getAbsolutePath()));
singletonMap("BESU_CONFIG_FILE", temp.resolve("tmp").toFile().getAbsolutePath()));

when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -76,15 +77,15 @@

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
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.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

@SuppressWarnings("unchecked")
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class BesuEventsImplTest {

private static final Supplier<SignatureAlgorithm> SIGNATURE_ALGORITHM =
Expand Down Expand Up @@ -113,7 +114,7 @@ public class BesuEventsImplTest {
private MutableBlockchain blockchain;
private final BlockDataGenerator gen = new BlockDataGenerator();

@Before
@BeforeEach
public void setUp() {
blockchain =
DefaultBlockchain.createMutable(
Expand All @@ -128,18 +129,22 @@ public void setUp() {
when(mockEthContext.getEthMessages()).thenReturn(mockEthMessages);
when(mockEthContext.getEthPeers()).thenReturn(mockEthPeers);
when(mockEthContext.getScheduler()).thenReturn(mockEthScheduler);
when(mockEthPeers.streamAvailablePeers()).thenAnswer(z -> Stream.empty());
lenient().when(mockEthPeers.streamAvailablePeers()).thenAnswer(z -> Stream.empty());
when(mockProtocolContext.getBlockchain()).thenReturn(blockchain);
when(mockProtocolContext.getWorldStateArchive()).thenReturn(mockWorldStateArchive);
when(mockProtocolSchedule.getByBlockHeader(any())).thenReturn(mockProtocolSpec);
when(mockProtocolSpec.getTransactionValidatorFactory())
lenient().when(mockProtocolContext.getWorldStateArchive()).thenReturn(mockWorldStateArchive);
lenient().when(mockProtocolSchedule.getByBlockHeader(any())).thenReturn(mockProtocolSpec);
lenient()
.when(mockProtocolSpec.getTransactionValidatorFactory())
.thenReturn(mockTransactionValidatorFactory);
when(mockProtocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L));
when(mockTransactionValidatorFactory.get().validate(any(), any(Optional.class), any()))
lenient().when(mockProtocolSpec.getFeeMarket()).thenReturn(FeeMarket.london(0L));
lenient()
.when(mockTransactionValidatorFactory.get().validate(any(), any(Optional.class), any()))
.thenReturn(ValidationResult.valid());
when(mockTransactionValidatorFactory.get().validateForSender(any(), any(), any()))
lenient()
.when(mockTransactionValidatorFactory.get().validateForSender(any(), any(), any()))
.thenReturn(ValidationResult.valid());
when(mockWorldStateArchive.getMutable(any(), anyBoolean()))
lenient()
.when(mockWorldStateArchive.getMutable(any(), anyBoolean()))
.thenReturn(Optional.of(mockWorldState));

blockBroadcaster = new BlockBroadcaster(mockEthContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

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;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.UnmatchedArgumentException;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class PicoCLIOptionsImplTest {

@Command
Expand All @@ -46,7 +46,7 @@ static final class MixinOptions {
private CommandLine commandLine;
private PicoCLIOptionsImpl serviceImpl;

@Before
@BeforeEach
public void setUp() {
command = new SimpleCommand();
mixin = new MixinOptions();
Expand Down

0 comments on commit a625e69

Please sign in to comment.