Skip to content

Commit

Permalink
Make acceptance more explicit around what is being tested
Browse files Browse the repository at this point in the history
Signed-off-by: Antony Denyer <[email protected]>
  • Loading branch information
antonydenyer committed Sep 17, 2021
1 parent f25dadc commit 35d619a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,24 @@
@AutoService(BesuPlugin.class)
public class TestRpcEndpointServicePlugin implements BesuPlugin {

static class Bean {
final String value;
private final AtomicReference<String> stringStorage = new AtomicReference<>("InitialValue");
private final AtomicReference<Object[]> arrayStorage = new AtomicReference<>();

Bean(final String value) {
this.value = value;
}

public String getValue() {
return value;
}
private String setValue(final PluginRpcRequest request) {
checkArgument(request.getParams().length == 1, "Only one parameter accepted");
return stringStorage.updateAndGet(x -> request.getParams()[0].toString());
}

private final AtomicReference<String> storage = new AtomicReference<>("InitialValue");

private String replaceValue(final PluginRpcRequest request) {
checkArgument(request.getParams().length == 1, "Only one parameter accepted");
return storage.getAndSet(request.getParams()[0].toString());
private String getValue(final PluginRpcRequest request) {
return stringStorage.get();
}

private String[] replaceValueArray(final PluginRpcRequest request) {
return new String[] {replaceValue(request)};
private Object[] replaceValueList(final PluginRpcRequest request) {
return arrayStorage.updateAndGet(x -> request.getParams());
}

private Bean replaceValueBean(final PluginRpcRequest request) {
return new Bean(replaceValue(request));
private String throwException(final PluginRpcRequest request) {
throw new RuntimeException("Kaboom");
}

@Override
Expand All @@ -61,12 +54,12 @@ public void register(final BesuContext context) {
.getService(RpcEndpointService.class)
.ifPresent(
rpcEndpointService -> {
rpcEndpointService.registerRPCEndpoint("unitTests", "getValue", this::getValue);
rpcEndpointService.registerRPCEndpoint("unitTests", "setValue", this::setValue);
rpcEndpointService.registerRPCEndpoint(
"unitTests", "replaceValue", this::replaceValue);
rpcEndpointService.registerRPCEndpoint(
"unitTests", "replaceValueArray", this::replaceValueArray);
"unitTests", "replaceValueList", this::replaceValueList);
rpcEndpointService.registerRPCEndpoint(
"unitTests", "replaceValueBean", this::replaceValueBean);
"unitTests", "throwException", this::throwException);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
Expand All @@ -48,42 +51,59 @@ public void setUp() throws Exception {
}

@Test
public void rpcWorking() throws IOException {
final String firstCall = "FirstCall";
final String secondCall = "SecondCall";
final String thirdCall = "ThirdCall";
public void canUseRpcToSetValue() throws IOException {
String setValue = "secondCall";

ObjectNode resultJson = callTestMethod("unitTests_replaceValue", firstCall);
ObjectNode resultJson = callTestMethod("unitTests_getValue", List.of());
assertThat(resultJson.get("result").asText()).isEqualTo("InitialValue");

resultJson = callTestMethod("unitTests_replaceValueArray", secondCall);
assertThat(resultJson.get("result").get(0).asText()).isEqualTo(firstCall);
resultJson = callTestMethod("unitTests_setValue", List.of(setValue));
assertThat(resultJson.get("result").asText()).isEqualTo(setValue);

resultJson = callTestMethod("unitTests_replaceValueBean", thirdCall);
assertThat(resultJson.get("result").get("value").asText()).isEqualTo(secondCall);
resultJson = callTestMethod("unitTests_getValue", List.of("ignored"));
assertThat(resultJson.get("result").asText()).isEqualTo(setValue);
}

@Test
public void throwsError() throws IOException {
ObjectNode resultJson = callTestMethod("unitTests_replaceValue", null);
public void canCheckArgumentInsideSetValue() throws IOException {
ObjectNode resultJson = callTestMethod("unitTests_setValue", List.of("one", "two"));
assertThat(resultJson.get("error").get("message").asText()).isEqualTo("Internal error");
}

private ObjectNode callTestMethod(final String method, final String value) throws IOException {
@Test
public void canThrowExceptions() throws IOException {
ObjectNode resultJson = callTestMethod("unitTests_throwException", List.of());
assertThat(resultJson.get("error").get("message").asText()).isEqualTo("Internal error");
}

@Test
public void mixedTypeArraysAreStringified() throws IOException {
ObjectNode resultJson = callTestMethod("unitTests_replaceValueList", List.of());
assertThat(resultJson.get("result")).isEmpty();

resultJson = callTestMethod("unitTests_replaceValueList", List.of("One", 2, true));
JsonNode result = resultJson.get("result");

assertThat(result.get(0).asText()).isEqualTo("One");
assertThat(result.get(1).asText()).isEqualTo("2");
assertThat(result.get(2).asText()).isEqualTo("true");
}

private ObjectNode callTestMethod(final String method, final List<Object> params)
throws IOException {
String format =
String.format(
"{\"jsonrpc\":\"2.0\",\"method\":\"%s\",\"params\":[%s],\"id\":42}",
method,
params.stream().map(value -> "\"" + value + "\"").collect(Collectors.joining(",")));

RequestBody body = RequestBody.create(format, JSON);

final String resultString =
client
.newCall(
new Request.Builder()
.post(
RequestBody.create(
"{\"jsonrpc\":\"2.0\",\"method\":\""
+ method
+ "\",\"params\":["
+ "\""
+ value
+ "\""
+ "],\"id\":33}",
JSON))
.post(body)
.url(
"http://"
+ node.getHostName()
Expand Down

0 comments on commit 35d619a

Please sign in to comment.