Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues/243 - Refactor integration tests to use mocked node. #244

Merged
merged 9 commits into from
Feb 13, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
HELP.md
.gradle
assets/
build/
src/test/resources/deploy-accounts/nctl
!gradle/wrapper/gradle-wrapper.jar
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/com/casper/sdk/service/AbstractJsonRpcTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.jupiter.api.BeforeAll;

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

/**
* Abstract class for testing json rpc methods
Expand All @@ -23,16 +24,22 @@ public enum CasperNetwork {
MAIN_NET("65.109.54.159", 7777),
TEST_NET("85.114.132.133", 7777),
NCTL("127.0.0.1", 11101),
NCTL_SPECULATIVE("127.0.0.1", 25101);
NCTL_SPECULATIVE("127.0.0.1", 25101),
MOCK("localhost", 7777);

private final String ip;
private final int port;

public URI getUri() {
return URI.create("http://" + ip + ":" + port);
}
}

protected static CasperService casperServiceMainnet;
protected static CasperService casperServiceTestnet;
protected static CasperService casperServiceNctl;
protected static CasperService speculativeCasperServiceNctl;
protected static CasperService casperServiceMock;

@BeforeAll
public static void setUp() throws MalformedURLException {
Expand All @@ -44,5 +51,6 @@ public static void setUp() throws MalformedURLException {
CasperNetwork.NCTL.getPort());
speculativeCasperServiceNctl = CasperService.usingPeer(CasperNetwork.NCTL_SPECULATIVE.getIp(),
CasperNetwork.NCTL_SPECULATIVE.getPort());
casperServiceMock = CasperService.usingPeer(CasperNetwork.MOCK.ip, CasperNetwork.MOCK.port);
}
}
383 changes: 273 additions & 110 deletions src/test/java/com/casper/sdk/service/CasperServiceTests.java

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/test/java/com/casper/sdk/test/MockNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public void setDispatcher(final Dispatcher dispatcher) {
mockWebServer.setDispatcher(dispatcher);
}

}
public RcpResponseDispatcher withRcpResponseDispatcher() {
final RcpResponseDispatcher whenDispatcher = new RcpResponseDispatcher();
setDispatcher(whenDispatcher);
return whenDispatcher;
}
}
109 changes: 109 additions & 0 deletions src/test/java/com/casper/sdk/test/RcpResponseDispatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.casper.sdk.test;


import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
import okio.Buffer;
import org.hamcrest.CustomMatcher;
import org.hamcrest.Matcher;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
* @author [email protected]
*/
public class RcpResponseDispatcher extends Dispatcher {

private static class MethodMatcher extends CustomMatcher<String> {
private final String method;

MethodMatcher(final String method) {
super("MethodMatcher");
this.method = method;
}

@Override
public boolean matches(final Object item) {
return this.method.equals(((DocumentContext) item).read("$.method", String.class));
}
}

private static class JsonBodyMatcher extends CustomMatcher<String> {

private final String jsonPath;
private final String expeted;

JsonBodyMatcher(final String jsonPath, final String expected) {
super("JsonBodyMatcher");
this.jsonPath = jsonPath;
this.expeted = expected;
}

@Override
public boolean matches(final Object item) {
final String actual = ((DocumentContext) item).read(this.jsonPath, String.class);
return this.expeted.equals(actual);
}
}

private final List<Matcher<?>> matchers = new ArrayList<>();
private URL resource;

@NotNull
@Override
public MockResponse dispatch(final RecordedRequest recordedRequest) {

final String body = recordedRequest.getBody().readString(StandardCharsets.UTF_8);
final DocumentContext ctx = JsonPath.parse(body);

for (final Matcher<?> matcher : matchers) {
if (!matcher.matches(ctx)) {
return new MockResponse().setResponseCode(404).setBody(matcher.toString());
}
}

//noinspection ConstantConditions
try (final InputStream in = resource.openStream();
final Buffer buffer = new Buffer().readFrom(in)) {

return new MockResponse().setResponseCode(200)
.addHeader("Content-Type", "application/json")
.setBody(buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public RcpResponseDispatcher withMethod(final String method) {
return addMatcher(new MethodMatcher(method));
}

public RcpResponseDispatcher withBody(final String jsonPath, final String expected) {
return addMatcher(new JsonBodyMatcher(jsonPath, expected));
}

public RcpResponseDispatcher addMatcher(final Matcher<?> matcher) {
this.matchers.add(matcher);
return this;
}

public RcpResponseDispatcher thenDispatch(final URL resource) {
this.resource = resource;
return this;
}

public RcpResponseDispatcher clear() {
this.matchers.clear();
this.resource = null;
return this;
}
}
Loading
Loading