Skip to content

Commit

Permalink
Add unit test for cli options. Change endpoint type to URL
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansaleem committed Oct 3, 2024
1 parent c3cf9fd commit 39af637
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 23 deletions.
1 change: 1 addition & 0 deletions gradle/dependency-management.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ dependencyManagement {
entry "dsl"
entry "eth"
entry "rlp"
entry "besu"
}

dependencySet(group: 'ch.qos.logback', version: '1.5.6') {
Expand Down
2 changes: 1 addition & 1 deletion sequencer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ dependencies {
testImplementation "${besuArtifactGroup}:besu-datatypes"
testImplementation "${besuArtifactGroup}.internal:core"
testImplementation "${besuArtifactGroup}.internal:rlp"
testImplementation "${besuArtifactGroup}.internal:core"
testImplementation "${besuArtifactGroup}:plugin-api"
testImplementation "${besuArtifactGroup}.internal:besu"
testImplementation "org.awaitility:awaitility"

// workaround for bug https://github.com/dnsjava/dnsjava/issues/329, remove when upgraded upstream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package net.consensys.linea.config;

import java.net.URI;
import java.net.URL;
import java.util.Optional;

import com.google.common.base.MoreObjects;
Expand Down Expand Up @@ -43,10 +43,10 @@ static class DependentOptions {
names = {REJECTED_TX_ENDPOINT},
hidden = true,
required = true, // required within the group
paramLabel = "<URI>",
paramLabel = "<URL>",
description =
"Endpoint URI for reporting rejected transactions. Specify a valid URI to enable reporting.")
URI rejectedTxEndpoint = null;
URL rejectedTxEndpoint = null;

@Option(
names = {LINEA_NODE_TYPE},
Expand Down Expand Up @@ -85,6 +85,7 @@ public static LineaRejectedTxReportingCliOptions fromConfig(
depOpts.lineaNodeType = config.lineaNodeType();
options.dependentOptions = depOpts;
}

return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

package net.consensys.linea.config;

import java.net.URI;
import java.net.URL;

import lombok.Builder;
import net.consensys.linea.plugins.LineaOptionsConfiguration;

/** The Linea RPC configuration. */
@Builder(toBuilder = true)
public record LineaRejectedTxReportingConfiguration(
URI rejectedTxEndpoint, LineaNodeType lineaNodeType) implements LineaOptionsConfiguration {}
URL rejectedTxEndpoint, LineaNodeType lineaNodeType) implements LineaOptionsConfiguration {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
Expand Down Expand Up @@ -222,17 +221,9 @@ private void scheduleRetry(final Path jsonFile, final Duration currentDelay) {
private boolean sendJsonRpcCall(final String jsonContent) {
final RequestBody body = RequestBody.create(jsonContent, JSON);
final Request request;
try {
request =
new Request.Builder()
.url(reportingConfiguration.rejectedTxEndpoint().toURL())
.post(body)
.build();
} catch (final MalformedURLException e) {
log.error(
"Invalid rejected-tx endpoint URL: {}", reportingConfiguration.rejectedTxEndpoint(), e);
return false;
}

request =
new Request.Builder().url(reportingConfiguration.rejectedTxEndpoint()).post(body).build();

try (final Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright Consensys Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.config;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import java.net.MalformedURLException;
import java.net.URI;

import org.hyperledger.besu.plugin.services.PicoCLIOptions;
import org.hyperledger.besu.services.PicoCLIOptionsImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

class LineaRejectedTxReportingCliOptionsTest {

@Command
static final class MockLineaBesuCommand {
@Option(names = "--mock-option")
String mockOption;
}

private MockLineaBesuCommand command;
private LineaRejectedTxReportingCliOptions lineaRejectedTxReportingCliOptions;
private CommandLine commandLine;
private PicoCLIOptions picoCliService;

@BeforeEach
public void setup() {
command = new MockLineaBesuCommand();
commandLine = new CommandLine(command);
picoCliService = new PicoCLIOptionsImpl(commandLine);

lineaRejectedTxReportingCliOptions = LineaRejectedTxReportingCliOptions.create();
picoCliService.addPicoCLIOptions("linea", lineaRejectedTxReportingCliOptions);
}

@Test
void emptyLineaRejectedTxReportingCliOptions() {
commandLine.parseArgs("--mock-option", "mockValue");

assertThat(command.mockOption).isEqualTo("mockValue");
assertThat(lineaRejectedTxReportingCliOptions.dependentOptions).isNull();
}

@ParameterizedTest
@EnumSource(LineaNodeType.class)
void lineaRejectedTxOptionBothOptionsRequired(final LineaNodeType lineaNodeType)
throws MalformedURLException {
commandLine.parseArgs(
"--plugin-linea-rejected-tx-endpoint",
"http://localhost:8080",
"--plugin-linea-node-type",
lineaNodeType.name());

assertThat(lineaRejectedTxReportingCliOptions.dependentOptions.rejectedTxEndpoint)
.isEqualTo(URI.create("http://localhost:8080").toURL());
assertThat(lineaRejectedTxReportingCliOptions.dependentOptions.lineaNodeType)
.isEqualTo(lineaNodeType);
}

@Test
void lineaRejectedTxReportingCliOptionsOnlyEndpointCauseException() {
assertThatExceptionOfType(CommandLine.ParameterException.class)
.isThrownBy(
() ->
commandLine.parseArgs(
"--plugin-linea-rejected-tx-endpoint", "http://localhost:8080"))
.withMessageContaining(
"Error: Missing required argument(s): --plugin-linea-node-type=<NODE_TYPE>");
}

@Test
void lineaRejectedTxReportingCliOptionsOnlyNodeTypeCauseException() {
assertThatExceptionOfType(CommandLine.ParameterException.class)
.isThrownBy(
() -> commandLine.parseArgs("--plugin-linea-node-type", LineaNodeType.SEQUENCER.name()))
.withMessageContaining(
"Error: Missing required argument(s): --plugin-linea-rejected-tx-endpoint=<URL>");
}

@Test
void lineaRejectedTxReportingInvalidNodeTypeCauseException() {
assertThatExceptionOfType(CommandLine.ParameterException.class)
.isThrownBy(
() ->
commandLine.parseArgs(
"--plugin-linea-rejected-tx-endpoint",
"http://localhost:8080",
"--plugin-linea-node-type",
"INVALID_NODE_TYPE"))
.withMessageContaining(
"Invalid value for option '--plugin-linea-node-type': expected one of [SEQUENCER, RPC, P2P] (case-sensitive) but was 'INVALID_NODE_TYPE'");
}

@ParameterizedTest
@ValueSource(strings = {"", "http://localhost:8080:8080", "invalid"})
void lineaRejectedTxReportingCliOptionsInvalidEndpointCauseException(final String endpoint) {
assertThatExceptionOfType(CommandLine.ParameterException.class)
.isThrownBy(
() ->
commandLine.parseArgs(
"--plugin-linea-rejected-tx-endpoint",
endpoint,
"--plugin-linea-node-type",
"SEQUENCER"))
.withMessageContaining(
"Invalid value for option '--plugin-linea-rejected-tx-endpoint': cannot convert '"
+ endpoint
+ "' to URL (java.net.MalformedURLException:");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void init(final WireMockRuntimeInfo wmInfo) throws IOException {

final LineaRejectedTxReportingConfiguration config =
LineaRejectedTxReportingConfiguration.builder()
.rejectedTxEndpoint(URI.create(wmInfo.getHttpBaseUrl()))
.rejectedTxEndpoint(URI.create(wmInfo.getHttpBaseUrl()).toURL())
.lineaNodeType(LineaNodeType.SEQUENCER)
.build();
jsonRpcManager = new JsonRpcManager(tempDataDir, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -63,12 +64,12 @@ class JsonRpcManagerTest {
@Mock private Transaction transaction;

@BeforeEach
void init(final WireMockRuntimeInfo wmInfo) {
void init(final WireMockRuntimeInfo wmInfo) throws MalformedURLException {
// mock stubbing
when(transaction.encoded()).thenReturn(randomEncodedBytes);
final LineaRejectedTxReportingConfiguration config =
LineaRejectedTxReportingConfiguration.builder()
.rejectedTxEndpoint(URI.create(wmInfo.getHttpBaseUrl()))
.rejectedTxEndpoint(URI.create(wmInfo.getHttpBaseUrl()).toURL())
.lineaNodeType(LineaNodeType.SEQUENCER)
.build();
jsonRpcManager = new JsonRpcManager(tempDataDir, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -121,7 +122,7 @@ public static void beforeAll() throws IOException {
}

@BeforeEach
public void initialize(final WireMockRuntimeInfo wmInfo) {
public void initialize(final WireMockRuntimeInfo wmInfo) throws MalformedURLException {
final var tracerConf =
LineaTracerConfiguration.builder()
.moduleLimitsFilePath(lineLimitsConfPath.toString())
Expand All @@ -133,7 +134,7 @@ public void initialize(final WireMockRuntimeInfo wmInfo) {

final var rejectedTxReportingConf =
LineaRejectedTxReportingConfiguration.builder()
.rejectedTxEndpoint(URI.create(wmInfo.getHttpBaseUrl()))
.rejectedTxEndpoint(URI.create(wmInfo.getHttpBaseUrl()).toURL())
.lineaNodeType(LineaNodeType.P2P)
.build();
jsonRpcManager = new JsonRpcManager(tempDataDir, rejectedTxReportingConf).start();
Expand Down

0 comments on commit 39af637

Please sign in to comment.