[MINOR] junit5 (#5356)

* migrate to junit5

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

---------

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
pull/5374/head
Sally MacFarlane 2 years ago committed by GitHub
parent f8a7d87ea3
commit 681f4a5116
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      besu/src/test/java/org/hyperledger/besu/BesuInfoTest.java
  2. 54
      besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java
  3. 28
      besu/src/test/java/org/hyperledger/besu/RunnerTest.java

@ -18,11 +18,11 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.util.Optional; import java.util.Optional;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public final class BesuInfoTest { public final class BesuInfoTest {
/** /**

@ -72,6 +72,7 @@ import org.hyperledger.besu.services.RpcEndpointServiceImpl;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collections; import java.util.Collections;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
@ -79,18 +80,17 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt64; import org.apache.tuweni.units.bigints.UInt64;
import org.ethereum.beacon.discovery.schema.NodeRecord; import org.ethereum.beacon.discovery.schema.NodeRecord;
import org.ethereum.beacon.discovery.schema.NodeRecordFactory; import org.ethereum.beacon.discovery.schema.NodeRecordFactory;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Rule; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.rules.TemporaryFolder; import org.junit.jupiter.api.io.TempDir;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public final class RunnerBuilderTest { public final class RunnerBuilderTest {
@Rule public TemporaryFolder dataDir = new TemporaryFolder(); @TempDir private Path dataDir;
@Mock BesuController besuController; @Mock BesuController besuController;
@Mock ProtocolSchedule protocolSchedule; @Mock ProtocolSchedule protocolSchedule;
@ -99,7 +99,7 @@ public final class RunnerBuilderTest {
@Mock Vertx vertx; @Mock Vertx vertx;
private NodeKey nodeKey; private NodeKey nodeKey;
@Before @BeforeEach
public void setup() { public void setup() {
final SubProtocolConfiguration subProtocolConfiguration = mock(SubProtocolConfiguration.class); final SubProtocolConfiguration subProtocolConfiguration = mock(SubProtocolConfiguration.class);
final EthProtocolManager ethProtocolManager = mock(EthProtocolManager.class); final EthProtocolManager ethProtocolManager = mock(EthProtocolManager.class);
@ -118,11 +118,7 @@ public final class RunnerBuilderTest {
when(ethProtocolManager.ethContext()).thenReturn(ethContext); when(ethProtocolManager.ethContext()).thenReturn(ethContext);
when(subProtocolConfiguration.getSubProtocols()) when(subProtocolConfiguration.getSubProtocols())
.thenReturn(Collections.singletonList(new IbftSubProtocol())); .thenReturn(Collections.singletonList(new IbftSubProtocol()));
final DefaultBlockchain blockchain = mock(DefaultBlockchain.class);
when(protocolContext.getBlockchain()).thenReturn(blockchain);
final Block block = mock(Block.class);
when(blockchain.getGenesisBlock()).thenReturn(block);
when(block.getHash()).thenReturn(Hash.ZERO);
when(protocolContext.getWorldStateArchive()).thenReturn(worldstateArchive); when(protocolContext.getWorldStateArchive()).thenReturn(worldstateArchive);
when(besuController.getProtocolManager()).thenReturn(ethProtocolManager); when(besuController.getProtocolManager()).thenReturn(ethProtocolManager);
when(besuController.getSubProtocolConfiguration()).thenReturn(subProtocolConfiguration); when(besuController.getSubProtocolConfiguration()).thenReturn(subProtocolConfiguration);
@ -144,6 +140,8 @@ public final class RunnerBuilderTest {
@Test @Test
public void enodeUrlShouldHaveAdvertisedHostWhenDiscoveryDisabled() { public void enodeUrlShouldHaveAdvertisedHostWhenDiscoveryDisabled() {
setupBlockchainAndBlock();
final String p2pAdvertisedHost = "172.0.0.1"; final String p2pAdvertisedHost = "172.0.0.1";
final int p2pListenPort = 30302; final int p2pListenPort = 30302;
@ -164,7 +162,7 @@ public final class RunnerBuilderTest {
.jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class)) .jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class))
.metricsConfiguration(mock(MetricsConfiguration.class)) .metricsConfiguration(mock(MetricsConfiguration.class))
.vertx(vertx) .vertx(vertx)
.dataDir(dataDir.getRoot().toPath()) .dataDir(dataDir.getRoot())
.storageProvider(mock(KeyValueStorageProvider.class)) .storageProvider(mock(KeyValueStorageProvider.class))
.rpcEndpointService(new RpcEndpointServiceImpl()) .rpcEndpointService(new RpcEndpointServiceImpl())
.build(); .build();
@ -208,7 +206,7 @@ public final class RunnerBuilderTest {
.jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class)) .jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class))
.metricsConfiguration(mock(MetricsConfiguration.class)) .metricsConfiguration(mock(MetricsConfiguration.class))
.vertx(Vertx.vertx()) .vertx(Vertx.vertx())
.dataDir(dataDir.getRoot().toPath()) .dataDir(dataDir.getRoot())
.storageProvider(storageProvider) .storageProvider(storageProvider)
.rpcEndpointService(new RpcEndpointServiceImpl()) .rpcEndpointService(new RpcEndpointServiceImpl())
.build(); .build();
@ -236,6 +234,8 @@ public final class RunnerBuilderTest {
@Test @Test
public void whenEngineApiAddedListensOnDefaultPort() { public void whenEngineApiAddedListensOnDefaultPort() {
setupBlockchainAndBlock();
final JsonRpcConfiguration jrpc = JsonRpcConfiguration.createDefault(); final JsonRpcConfiguration jrpc = JsonRpcConfiguration.createDefault();
jrpc.setEnabled(true); jrpc.setEnabled(true);
final JsonRpcConfiguration engine = JsonRpcConfiguration.createEngineDefault(); final JsonRpcConfiguration engine = JsonRpcConfiguration.createEngineDefault();
@ -264,7 +264,7 @@ public final class RunnerBuilderTest {
.jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class)) .jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class))
.metricsConfiguration(mock(MetricsConfiguration.class)) .metricsConfiguration(mock(MetricsConfiguration.class))
.vertx(Vertx.vertx()) .vertx(Vertx.vertx())
.dataDir(dataDir.getRoot().toPath()) .dataDir(dataDir.getRoot())
.storageProvider(mock(KeyValueStorageProvider.class)) .storageProvider(mock(KeyValueStorageProvider.class))
.rpcEndpointService(new RpcEndpointServiceImpl()) .rpcEndpointService(new RpcEndpointServiceImpl())
.besuPluginContext(mock(BesuPluginContextImpl.class)) .besuPluginContext(mock(BesuPluginContextImpl.class))
@ -276,6 +276,8 @@ public final class RunnerBuilderTest {
@Test @Test
public void whenEngineApiAddedWebSocketReadyOnSamePort() { public void whenEngineApiAddedWebSocketReadyOnSamePort() {
setupBlockchainAndBlock();
final WebSocketConfiguration wsRpc = WebSocketConfiguration.createDefault(); final WebSocketConfiguration wsRpc = WebSocketConfiguration.createDefault();
wsRpc.setEnabled(true); wsRpc.setEnabled(true);
final EthNetworkConfig mockMainnet = mock(EthNetworkConfig.class); final EthNetworkConfig mockMainnet = mock(EthNetworkConfig.class);
@ -304,7 +306,7 @@ public final class RunnerBuilderTest {
.graphQLConfiguration(mock(GraphQLConfiguration.class)) .graphQLConfiguration(mock(GraphQLConfiguration.class))
.metricsConfiguration(mock(MetricsConfiguration.class)) .metricsConfiguration(mock(MetricsConfiguration.class))
.vertx(Vertx.vertx()) .vertx(Vertx.vertx())
.dataDir(dataDir.getRoot().toPath()) .dataDir(dataDir.getRoot())
.storageProvider(mock(KeyValueStorageProvider.class)) .storageProvider(mock(KeyValueStorageProvider.class))
.rpcEndpointService(new RpcEndpointServiceImpl()) .rpcEndpointService(new RpcEndpointServiceImpl())
.besuPluginContext(mock(BesuPluginContextImpl.class)) .besuPluginContext(mock(BesuPluginContextImpl.class))
@ -315,6 +317,8 @@ public final class RunnerBuilderTest {
@Test @Test
public void whenEngineApiAddedEthSubscribeAvailable() { public void whenEngineApiAddedEthSubscribeAvailable() {
setupBlockchainAndBlock();
final WebSocketConfiguration wsRpc = WebSocketConfiguration.createDefault(); final WebSocketConfiguration wsRpc = WebSocketConfiguration.createDefault();
wsRpc.setEnabled(true); wsRpc.setEnabled(true);
final EthNetworkConfig mockMainnet = mock(EthNetworkConfig.class); final EthNetworkConfig mockMainnet = mock(EthNetworkConfig.class);
@ -343,7 +347,7 @@ public final class RunnerBuilderTest {
.graphQLConfiguration(mock(GraphQLConfiguration.class)) .graphQLConfiguration(mock(GraphQLConfiguration.class))
.metricsConfiguration(mock(MetricsConfiguration.class)) .metricsConfiguration(mock(MetricsConfiguration.class))
.vertx(Vertx.vertx()) .vertx(Vertx.vertx())
.dataDir(dataDir.getRoot().toPath()) .dataDir(dataDir.getRoot())
.storageProvider(mock(KeyValueStorageProvider.class)) .storageProvider(mock(KeyValueStorageProvider.class))
.rpcEndpointService(new RpcEndpointServiceImpl()) .rpcEndpointService(new RpcEndpointServiceImpl())
.besuPluginContext(mock(BesuPluginContextImpl.class)) .besuPluginContext(mock(BesuPluginContextImpl.class))
@ -357,6 +361,8 @@ public final class RunnerBuilderTest {
@Test @Test
public void noEngineApiNoServiceForMethods() { public void noEngineApiNoServiceForMethods() {
setupBlockchainAndBlock();
final JsonRpcConfiguration defaultRpcConfig = JsonRpcConfiguration.createDefault(); final JsonRpcConfiguration defaultRpcConfig = JsonRpcConfiguration.createDefault();
defaultRpcConfig.setEnabled(true); defaultRpcConfig.setEnabled(true);
final WebSocketConfiguration defaultWebSockConfig = WebSocketConfiguration.createDefault(); final WebSocketConfiguration defaultWebSockConfig = WebSocketConfiguration.createDefault();
@ -383,7 +389,7 @@ public final class RunnerBuilderTest {
.jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class)) .jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class))
.metricsConfiguration(mock(MetricsConfiguration.class)) .metricsConfiguration(mock(MetricsConfiguration.class))
.vertx(Vertx.vertx()) .vertx(Vertx.vertx())
.dataDir(dataDir.getRoot().toPath()) .dataDir(dataDir.getRoot())
.storageProvider(mock(KeyValueStorageProvider.class)) .storageProvider(mock(KeyValueStorageProvider.class))
.rpcEndpointService(new RpcEndpointServiceImpl()) .rpcEndpointService(new RpcEndpointServiceImpl())
.besuPluginContext(mock(BesuPluginContextImpl.class)) .besuPluginContext(mock(BesuPluginContextImpl.class))
@ -393,4 +399,12 @@ public final class RunnerBuilderTest {
assertThat(runner.getJsonRpcPort()).isPresent(); assertThat(runner.getJsonRpcPort()).isPresent();
assertThat(runner.getEngineJsonRpcPort()).isEmpty(); assertThat(runner.getEngineJsonRpcPort()).isEmpty();
} }
private void setupBlockchainAndBlock() {
final DefaultBlockchain blockchain = mock(DefaultBlockchain.class);
when(protocolContext.getBlockchain()).thenReturn(blockchain);
final Block block = mock(Block.class);
when(blockchain.getGenesisBlock()).thenReturn(block);
when(block.getHash()).thenReturn(Hash.ZERO);
}
} }

@ -73,6 +73,7 @@ import org.hyperledger.besu.testutil.TestClock;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.InetAddress; import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -96,32 +97,31 @@ import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
import org.apache.tuweni.units.bigints.UInt256; import org.apache.tuweni.units.bigints.UInt256;
import org.awaitility.Awaitility; import org.awaitility.Awaitility;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Rule; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.rules.TemporaryFolder; import org.junit.jupiter.api.io.TempDir;
import org.junit.runner.RunWith; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.MockitoJUnitRunner;
/** Tests for {@link Runner}. */ /** Tests for {@link Runner}. */
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public final class RunnerTest { public final class RunnerTest {
public static final BigInteger NETWORK_ID = BigInteger.valueOf(2929); public static final BigInteger NETWORK_ID = BigInteger.valueOf(2929);
private Vertx vertx; private Vertx vertx;
@Before @BeforeEach
public void initVertx() { public void initVertx() {
vertx = Vertx.vertx(); vertx = Vertx.vertx();
} }
@After @AfterEach
public void stopVertx() { public void stopVertx() {
vertx.close(); vertx.close();
} }
@Rule public final TemporaryFolder temp = new TemporaryFolder(); @TempDir private static Path temp;
@Test @Test
public void getFixedNodes() { public void getFixedNodes() {
@ -159,7 +159,7 @@ public final class RunnerTest {
private void syncFromGenesis(final SyncMode mode, final GenesisConfigFile genesisConfig) private void syncFromGenesis(final SyncMode mode, final GenesisConfigFile genesisConfig)
throws Exception { throws Exception {
final Path dataDirAhead = temp.newFolder().toPath(); final Path dataDirAhead = Files.createTempDirectory(temp, "db-ahead");
final Path dbAhead = dataDirAhead.resolve("database"); final Path dbAhead = dataDirAhead.resolve("database");
final int blockCount = 500; final int blockCount = 500;
final NodeKey aheadDbNodeKey = NodeKeyUtils.createFrom(KeyPairUtil.loadKeyPair(dbAhead)); final NodeKey aheadDbNodeKey = NodeKeyUtils.createFrom(KeyPairUtil.loadKeyPair(dbAhead));
@ -181,7 +181,7 @@ public final class RunnerTest {
blockCount, controllerAhead.getProtocolSchedule(), controllerAhead.getProtocolContext()); blockCount, controllerAhead.getProtocolSchedule(), controllerAhead.getProtocolContext());
final String listenHost = InetAddress.getLoopbackAddress().getHostAddress(); final String listenHost = InetAddress.getLoopbackAddress().getHostAddress();
final Path pidPath = temp.getRoot().toPath().resolve("pid"); final Path pidPath = dataDirAhead.resolve("pid");
final RunnerBuilder runnerBuilder = final RunnerBuilder runnerBuilder =
new RunnerBuilder() new RunnerBuilder()
.vertx(vertx) .vertx(vertx)
@ -220,7 +220,7 @@ public final class RunnerTest {
.fastSyncPivotDistance(5) .fastSyncPivotDistance(5)
.fastSyncMinimumPeerCount(1) .fastSyncMinimumPeerCount(1)
.build(); .build();
final Path dataDirBehind = temp.newFolder().toPath(); final Path dataDirBehind = Files.createTempDirectory(temp, "db-behind");
// Setup runner with no block data // Setup runner with no block data
final BesuController controllerBehind = final BesuController controllerBehind =

Loading…
Cancel
Save