solution: integration tests
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package io.emeraldpay.dshackle.testing.trial
|
||||
|
||||
class Debugger {
|
||||
|
||||
public static enabled = true
|
||||
|
||||
static void showOut(String json) {
|
||||
if (!enabled) {
|
||||
return
|
||||
}
|
||||
println(">> $json")
|
||||
}
|
||||
|
||||
static void showIn(String json) {
|
||||
if (!enabled) {
|
||||
return
|
||||
}
|
||||
println("<< $json")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package io.emeraldpay.dshackle.testing.trial
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import org.apache.commons.codec.binary.Hex
|
||||
import org.apache.http.HttpHeaders
|
||||
import org.apache.http.client.HttpClient
|
||||
import org.apache.http.client.methods.CloseableHttpResponse
|
||||
import org.apache.http.client.methods.HttpPost
|
||||
import org.apache.http.entity.ContentType
|
||||
import org.apache.http.entity.StringEntity
|
||||
import org.apache.http.impl.client.CloseableHttpClient
|
||||
import org.apache.http.impl.client.HttpClientBuilder
|
||||
import org.apache.http.impl.client.HttpClients
|
||||
|
||||
class ProxyClient {
|
||||
|
||||
private int sequence = 0;
|
||||
|
||||
private String url
|
||||
private ObjectMapper objectMapper
|
||||
|
||||
ProxyClient(String url) {
|
||||
this.url = url
|
||||
this.objectMapper = new ObjectMapper()
|
||||
}
|
||||
|
||||
static ProxyClient forPrefix(String prefix) {
|
||||
return new ProxyClient("http://127.0.0.1:18080/$prefix")
|
||||
}
|
||||
|
||||
Map<String, Object> execute(String method, List<Object> params) {
|
||||
return execute(sequence++, method, params)
|
||||
}
|
||||
|
||||
Map<String, Object> execute(Object id, String method, List<Object> params) {
|
||||
Map jsonData = [
|
||||
id : id,
|
||||
jsonrpc: "2.0",
|
||||
method : method,
|
||||
params : params
|
||||
]
|
||||
String reqJson = objectMapper.writeValueAsString(jsonData)
|
||||
Debugger.showOut(reqJson)
|
||||
CloseableHttpClient httpClient = null
|
||||
try {
|
||||
httpClient = HttpClients.createDefault()
|
||||
def request = new HttpPost(url)
|
||||
request.setEntity(new StringEntity(reqJson, ContentType.APPLICATION_JSON))
|
||||
CloseableHttpResponse response = httpClient.execute(request)
|
||||
if (response.getStatusLine().statusCode != 200) {
|
||||
throw new IllegalStateException("Non-ok status: " + response.getStatusLine().statusCode)
|
||||
}
|
||||
String respJson = response.entity.content.text
|
||||
Debugger.showIn(respJson)
|
||||
return objectMapper.readerFor(Map).readValue(respJson)
|
||||
} finally {
|
||||
httpClient?.close()
|
||||
}
|
||||
|
||||
return [error: "NOT EXECUTED"]
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package io.emeraldpay.dshackle.testing.trial.proxy
|
||||
|
||||
import io.emeraldpay.dshackle.testing.trial.ProxyClient
|
||||
import spock.lang.Specification
|
||||
|
||||
class GivesErrorSpec extends Specification {
|
||||
|
||||
def client = ProxyClient.forPrefix("eth")
|
||||
|
||||
def "Gives error message"() {
|
||||
// issue #42
|
||||
when:
|
||||
def act = client.execute("eth_call", [[to: "0x542156d51D10Db5acCB99f9Db7e7C91B74E80a2c"]])
|
||||
then:
|
||||
act.result == null
|
||||
act.error != null
|
||||
with(act.error) {
|
||||
code == -32015
|
||||
message == "VM execution error."
|
||||
}
|
||||
}
|
||||
|
||||
def "Gives error data"() {
|
||||
// issue #42
|
||||
when:
|
||||
def act = client.execute("eth_call", [[to: "0x8ee2a5aca4f88cb8c757b8593d0734855dcc0eba"]])
|
||||
then:
|
||||
act.result == null
|
||||
act.error != null
|
||||
with(act.error) {
|
||||
code == -32015
|
||||
message == "VM execution error."
|
||||
data == "revert: SafeMath: division by zero"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package io.emeraldpay.dshackle.testing.trial.proxy
|
||||
|
||||
import io.emeraldpay.dshackle.testing.trial.ProxyClient
|
||||
import spock.lang.Specification
|
||||
|
||||
class HardcodedCallsSpec extends Specification {
|
||||
|
||||
def client = ProxyClient.forPrefix("eth")
|
||||
|
||||
def "get syncing"() {
|
||||
when:
|
||||
def act = client.execute("eth_syncing", [])
|
||||
then:
|
||||
act.result == false
|
||||
act.error == null
|
||||
}
|
||||
|
||||
def "get network version"() {
|
||||
when:
|
||||
def act = client.execute("net_version", [])
|
||||
then:
|
||||
act.result == 1
|
||||
act.error == null
|
||||
}
|
||||
|
||||
def "get peer count"() {
|
||||
when:
|
||||
def act = client.execute("net_peerCount", [])
|
||||
then:
|
||||
act.result == "0x2a"
|
||||
act.error == null
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package io.emeraldpay.dshackle.testing.trial.proxy
|
||||
|
||||
import io.emeraldpay.dshackle.testing.trial.ProxyClient
|
||||
import spock.lang.Specification
|
||||
|
||||
class StandardCallsSpec extends Specification {
|
||||
|
||||
def client = ProxyClient.forPrefix("eth")
|
||||
|
||||
def "get height"() {
|
||||
when:
|
||||
def act = client.execute("eth_blockNumber", [])
|
||||
then:
|
||||
act.result == "0x100001"
|
||||
act.error == null
|
||||
}
|
||||
|
||||
def "get block"() {
|
||||
when:
|
||||
def act = client.execute("eth_getBlockByNumber", ["0x100001", false])
|
||||
then:
|
||||
act.result != null
|
||||
with(act.result) {
|
||||
number == "0x100001"
|
||||
hash == "0x18c68d9ba58772a4409d65d61891b25db03a105a7769ae08ef2cff697921b446"
|
||||
transactions == [
|
||||
"0x146b8f4b6300c73bb7476359b9f1c5ee3f686a86b2aa673552cf0f9de9a42e77",
|
||||
"0xe589a39acea3091b584b650158d08b159aa07e97b8e8cddb8f81cb606e13382e"
|
||||
]
|
||||
}
|
||||
act.error == null
|
||||
}
|
||||
|
||||
def "get block with txes"() {
|
||||
when:
|
||||
def act = client.execute("eth_getBlockByNumber", ["0x100001", true])
|
||||
then:
|
||||
act.result != null
|
||||
with(act.result) {
|
||||
number == "0x100001"
|
||||
hash == "0x18c68d9ba58772a4409d65d61891b25db03a105a7769ae08ef2cff697921b446"
|
||||
transactions.size() == 2
|
||||
transactions[0] instanceof Map
|
||||
transactions[1] instanceof Map
|
||||
with(transactions.find { it.hash == "0x146b8f4b6300c73bb7476359b9f1c5ee3f686a86b2aa673552cf0f9de9a42e77" }) {
|
||||
from == "0x2a65aca4d5fc5b5c859090a6c34d164135398226"
|
||||
}
|
||||
}
|
||||
act.error == null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user