Skip to content

Commit

Permalink
1. Enabled EthCall integration test.
Browse files Browse the repository at this point in the history
2. Fixed issue with http connections not persisting.
3. Fixed serialisation issue with DefaultBlockParameterName.
4. Added RequestTest for unit testing requests.
5. Bumped version.
  • Loading branch information
conor10 committed Sep 16, 2016
1 parent afaee57 commit cc0f572
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -25,15 +25,15 @@ Add the following dependency to your project:
<repositories>
<repository>
<id>oss.jfrog.org</id>
<name>Repository from Bintray</name>
<value>Repository from Bintray</value>
<url>http://dl.bintray.com/web3j/maven</url>
</repository>
</repositories>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>0.1.1</version>
<version>0.1.2</version>
</dependency>
```

Expand All @@ -43,7 +43,7 @@ Add the following dependency to your project:
repositories {
maven {url "http://dl.bintray.com/web3j/maven"}
}
compile ("org.web3j:core:0.1.1")
compile ("org.web3j:core:0.1.2")
```

Start up an Ethereum client if you don't already have one running, such as [Geth](https://github.com/ethereum/go-ethereum/wiki/geth):
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'jacoco'
apply plugin: 'maven-publish'

group 'org.web3j'
version '0.1.1'
version '0.1.2'

sourceCompatibility = 1.8

Expand Down
Expand Up @@ -6,6 +6,8 @@
import org.junit.Ignore;
import org.junit.Test;

import org.web3j.methods.response.EthCall;
import org.web3j.methods.response.EthSendTransaction;
import org.web3j.protocol.http.HttpService;
import org.web3j.methods.response.*;
import org.web3j.protocol.Web3j;
Expand Down Expand Up @@ -202,13 +204,13 @@ public void testEthSendRawTransaction() throws Exception {

}

@Ignore // TODO: Complete
@Test
public void testEthCall() throws Exception {
EthCall ethCall = web3j.ethCall(config.ethCall(),
DefaultBlockParameter.valueOf(config.validBlock())).send();
assertThat(ethCall.getValue(), is(""));
DefaultBlockParameter.valueOf("latest")).send();

assertThat(DefaultBlockParameterName.LATEST.getValue(), is("latest"));
assertThat(ethCall.getValue(), is("0x"));
}

@Test
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/web3j/protocol/http/HttpService.java
Expand Up @@ -23,7 +23,9 @@
*/
public class HttpService implements Web3jService {

private CloseableHttpClient httpclient = HttpClients.createDefault();
private CloseableHttpClient httpClient =
HttpClients.custom().setConnectionManagerShared(true).build();

private ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();

private final String url;
Expand All @@ -38,7 +40,7 @@ public HttpService(String url) {

public HttpService(String url, CloseableHttpClient httpClient) {
this.url = url;
this.httpclient = httpClient;
this.httpClient = httpClient;
}

@Override
Expand All @@ -53,9 +55,9 @@ public <T extends Response> T send(

ResponseHandler<T> responseHandler = getResponseHandler(responseType);
try {
return httpclient.execute(httpPost, responseHandler);
return httpClient.execute(httpPost, responseHandler);
} finally {
httpclient.close();
httpClient.close();
}
}

Expand Down
@@ -1,5 +1,7 @@
package org.web3j.protocol.jsonrpc20;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter
*/
Expand All @@ -14,6 +16,7 @@ public enum DefaultBlockParameterName implements DefaultBlockParameter {
this.name = name;
}

@JsonValue
@Override
public String getValue() {
return name;
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/org/web3j/protocol/jsonrpc20/RequestTest.java
@@ -0,0 +1,64 @@
package org.web3j.protocol.jsonrpc20;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import org.web3j.methods.request.EthCall;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;


public class RequestTest {

private CloseableHttpClient closeableHttpClient;
private HttpService httpService;
private Web3j web3j;

@Before
public void setUp() {
closeableHttpClient = mock(CloseableHttpClient.class);
httpService = new HttpService("", closeableHttpClient);
web3j = Web3j.build(httpService);
}

@Test
public void testEthCall() throws Exception {
web3j.ethCall(new EthCall("0x52b93c80364dc2dd4444c146d73b9836bbbb2b3f", "0x0"),
DefaultBlockParameter.valueOf("latest")).send();

verifyResult("{\"jsonRpc\":\"2.0\",\"method\":\"eth_call\",\"params\":[{\"toAddress\":\"0x52b93c80364dc2dd4444c146d73b9836bbbb2b3f\",\"data\":\"0x0\"},\"latest\"],\"id\":1}");
}

private void verifyResult(String expected) throws Exception {
ArgumentCaptor<HttpPost> httpPostArgumentCaptor = ArgumentCaptor.forClass(HttpPost.class);
verify(closeableHttpClient).execute(httpPostArgumentCaptor.capture(), any(ResponseHandler.class));

String result = readResult(httpPostArgumentCaptor.getValue().getEntity().getContent());
assertThat(result, is(expected));
}

private static String readResult(InputStream inputStream) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}
}

0 comments on commit cc0f572

Please sign in to comment.