Skip to content

Commit

Permalink
update some tests from junit 4 to 5 (hyperledger#4973)
Browse files Browse the repository at this point in the history
Signed-off-by: Sally MacFarlane <[email protected]>

Signed-off-by: Sally MacFarlane <[email protected]>
  • Loading branch information
macfarla authored Jan 23, 2023
1 parent c4dbc58 commit 1abe277
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 80 deletions.
16 changes: 6 additions & 10 deletions besu/src/test/java/org/hyperledger/besu/PrivacyReorgTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@
import com.google.common.base.Suppliers;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

@SuppressWarnings("rawtypes")
public class PrivacyReorgTest {

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

private static final Supplier<SignatureAlgorithm> SIGNATURE_ALGORITHM =
Suppliers.memoize(SignatureAlgorithmFactory::getInstance);
Expand Down Expand Up @@ -139,7 +138,7 @@ public class PrivacyReorgTest {
private Enclave mockEnclave;
private Transaction privacyMarkerTransaction;

@Before
@BeforeEach
public void setUp() throws IOException {
mockEnclave = mock(Enclave.class);
final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput();
Expand All @@ -164,9 +163,6 @@ public void setUp() throws IOException {
.value(Wei.ZERO)
.signAndBuild(KEY_PAIR);

// Create Storage
final Path dataDir = folder.newFolder().toPath();

// Configure Privacy
EnclaveFactory enclaveFactory = mock(EnclaveFactory.class);
when(enclaveFactory.createVertxEnclave(any())).thenReturn(mockEnclave);
Expand Down Expand Up @@ -203,7 +199,7 @@ public void setUp() throws IOException {
.build())
.nodeKey(NodeKeyUtils.generate())
.metricsSystem(new NoOpMetricsSystem())
.dataDirectory(dataDir)
.dataDirectory(folder)
.clock(TestClock.fixed())
.privacyParameters(privacyParameters)
.transactionPoolConfiguration(TransactionPoolConfiguration.DEFAULT)
Expand Down
20 changes: 8 additions & 12 deletions besu/src/test/java/org/hyperledger/besu/PrivacyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,22 @@
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;

import io.vertx.core.Vertx;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class PrivacyTest {

private final Vertx vertx = Vertx.vertx();

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

@After
@AfterEach
public void cleanUp() {
vertx.close();
}
Expand All @@ -97,8 +94,7 @@ public void flexibleEnabledPrivacy() throws IOException, URISyntaxException {

private BesuController setUpControllerWithPrivacyEnabled(final boolean flexibleEnabled)
throws IOException, URISyntaxException {
final Path dataDir = folder.newFolder().toPath();
final Path dbDir = dataDir.resolve("database");
final Path dbDir = Files.createTempDirectory(dataDir, "database");
final PrivacyParameters privacyParameters =
new PrivacyParameters.Builder()
.setEnabled(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability;

import java.nio.file.Path;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand All @@ -33,27 +34,26 @@
import okhttp3.Request;
import okhttp3.Response;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;

public class GraphQLHttpServiceCorsTest {
@Rule public final TemporaryFolder folder = new TemporaryFolder();
@TempDir private static Path folder;

private final Vertx vertx = Vertx.vertx();
private final OkHttpClient client = new OkHttpClient();
private GraphQLHttpService graphQLHttpService;

@Before
@BeforeEach
public void before() {
final GraphQLConfiguration configuration = GraphQLConfiguration.createDefault();
configuration.setPort(0);
}

@After
@AfterEach
public void after() {
client.dispatcher().executorService().shutdown();
client.connectionPool().evictAll();
Expand Down Expand Up @@ -233,12 +233,7 @@ private GraphQLHttpService createGraphQLHttpServiceWithAllowedDomains(

final GraphQLHttpService graphQLHttpService =
new GraphQLHttpService(
vertx,
folder.newFolder().toPath(),
config,
graphQL,
graphQLContextMap,
Mockito.mock(EthScheduler.class));
vertx, folder, config, graphQL, graphQLContextMap, Mockito.mock(EthScheduler.class));
graphQLHttpService.start().join();

return graphQLHttpService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.nat.NatService;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Optional;

Expand All @@ -28,26 +29,25 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class JsonRpcHttpServiceCorsTest {
@Rule public final TemporaryFolder folder = new TemporaryFolder();
@TempDir private static Path folder;

private final Vertx vertx = Vertx.vertx();
private final OkHttpClient client = new OkHttpClient();
private JsonRpcHttpService jsonRpcHttpService;

@Before
@BeforeEach
public void before() {
final JsonRpcConfiguration configuration = JsonRpcConfiguration.createDefault();
configuration.setPort(0);
}

@After
@AfterEach
public void after() {
jsonRpcHttpService.stop().join();
}
Expand Down Expand Up @@ -210,7 +210,7 @@ private JsonRpcHttpService createJsonRpcHttpServiceWithAllowedDomains(
final JsonRpcHttpService jsonRpcHttpService =
new JsonRpcHttpService(
vertx,
folder.newFolder().toPath(),
folder,
config,
new NoOpMetricsSystem(),
natService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.hyperledger.besu.nat.NatService;

import java.math.BigInteger;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -73,19 +74,18 @@
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.junit.After;
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.AfterEach;
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;

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

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

private final Vertx vertx = Vertx.vertx();
private final OkHttpClient client = new OkHttpClient();
Expand All @@ -103,13 +103,13 @@ public class JsonRpcHttpServiceRpcApisTest {
private final JsonRpcTestHelper testHelper = new JsonRpcTestHelper();
private final NatService natService = new NatService(Optional.empty());

@Before
@BeforeEach
public void before() {
configuration = JsonRpcConfiguration.createDefault();
configuration.setPort(0);
}

@After
@AfterEach
public void after() {
service.stop().join();
}
Expand Down Expand Up @@ -223,14 +223,14 @@ private JsonRpcHttpService createJsonRpcHttpServiceWithRpcApis(final JsonRpcConf
mock(MetricsConfiguration.class),
natService,
new HashMap<>(),
folder.getRoot().toPath(),
folder,
mock(EthPeers.class),
vertx,
Optional.empty()));
final JsonRpcHttpService jsonRpcHttpService =
new JsonRpcHttpService(
vertx,
folder.newFolder().toPath(),
folder,
config,
new NoOpMetricsSystem(),
natService,
Expand Down Expand Up @@ -329,14 +329,14 @@ private JsonRpcHttpService createJsonRpcHttpService(
metricsConfiguration,
natService,
new HashMap<>(),
folder.getRoot().toPath(),
folder,
mock(EthPeers.class),
vertx,
Optional.empty()));
final JsonRpcHttpService jsonRpcHttpService =
new JsonRpcHttpService(
vertx,
folder.newFolder().toPath(),
folder,
jsonRpcConfiguration,
new NoOpMetricsSystem(),
natService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,11 @@
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class PrivacyPrecompiledContractTest {

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

private final String actual = "Test String";
private final Bytes privateTransactionLookupId = Bytes.wrap(actual.getBytes(UTF_8));
private MessageFrame messageFrame;
Expand Down Expand Up @@ -112,7 +108,7 @@ private PrivateTransactionProcessor mockPrivateTxProcessor(
return mockPrivateTransactionProcessor;
}

@Before
@BeforeEach
public void setUp() {
final MutableWorldState mutableWorldState = mock(MutableWorldState.class);
when(mutableWorldState.updater()).thenReturn(mock(WorldUpdater.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@

import com.google.common.collect.Lists;
import io.vertx.core.json.DecodeException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class StaticNodesParserTest {

// NOTE: The invalid_static_nodes file is identical to the valid, however one node's port is set
// to "A".

// First peer ion the valid_static_nodes file.
// First peer in the valid_static_nodes file.
private final List<EnodeURL> validFileItems =
Lists.newArrayList(
EnodeURLImpl.builder()
Expand Down Expand Up @@ -69,7 +68,7 @@ public class StaticNodesParserTest {
.discoveryAndListeningPorts(30306)
.build());

@Rule public TemporaryFolder testFolder = new TemporaryFolder();
@TempDir private static Path testFolder;

@Test
public void validFileLoadsWithExpectedEnodes() throws IOException, URISyntaxException {
Expand Down Expand Up @@ -174,13 +173,11 @@ public void fromPath_withNonListeningNodesThrowsException() {

@Test
public void nonJsonFileThrowsAnException() throws IOException {
final File tempFile = testFolder.newFile("file.txt");
tempFile.deleteOnExit();
Files.write(tempFile.toPath(), "This Is Not Json".getBytes(Charset.forName("UTF-8")));
final Path tempFile = Files.createTempFile(testFolder, "file", "txt");
Files.write(tempFile, "This Is Not Json".getBytes(Charset.forName("UTF-8")));

assertThatThrownBy(
() ->
StaticNodesParser.fromPath(tempFile.toPath(), EnodeDnsConfiguration.DEFAULT_CONFIG))
() -> StaticNodesParser.fromPath(tempFile, EnodeDnsConfiguration.DEFAULT_CONFIG))
.isInstanceOf(DecodeException.class);
}

Expand All @@ -195,11 +192,10 @@ public void anEmptyCacheIsCreatedIfTheFileDoesNotExist() throws IOException {

@Test
public void cacheIsCreatedIfFileExistsButIsEmpty() throws IOException {
final File tempFile = testFolder.newFile("file.txt");
tempFile.deleteOnExit();
final Path tempFile = Files.createTempFile(testFolder, "file", "txt");

final Set<EnodeURL> enodes =
StaticNodesParser.fromPath(tempFile.toPath(), EnodeDnsConfiguration.DEFAULT_CONFIG);
StaticNodesParser.fromPath(tempFile, EnodeDnsConfiguration.DEFAULT_CONFIG);
assertThat(enodes.size()).isZero();
}
}

0 comments on commit 1abe277

Please sign in to comment.