problem: Proxy fails to serialize JSON

reason: expects full JSON response from upstream, which is replaces with result field only
This commit is contained in:
Igor Artamonov
2020-06-29 22:44:44 -04:00
parent 04958784ec
commit c2636cd81e
5 changed files with 200 additions and 60 deletions

View File

@@ -84,41 +84,18 @@ class WriteRpcJsonSpec extends Specification {
def "Convert basic to JSON"() {
setup:
def call = new ProxyCall(ProxyCall.RpcType.SINGLE)
call.ids[1] = "aaa"
call.ids[1] = 105
def data = [
BlockchainOuterClass.NativeCallReplyItem.newBuilder()
.setId(1)
.setSucceed(true)
.setPayload(ByteString.copyFrom('{"jsonrpc": "2.0", "id": 1, "result": "0x98dbb1"}', 'UTF-8'))
.setPayload(ByteString.copyFrom('"0x98dbb1"', 'UTF-8'))
.build()
]
when:
def act = Flux.fromIterable(data)
.transform(writer.toJsons(call))
.collectList()
.block(Duration.ofSeconds(1))
def act = writer.toJson(call, data[0])
then:
act == ['{"jsonrpc":"2.0","id":"aaa","result":"0x98dbb1"}']
}
def "Convert error to JSON"() {
setup:
def call = new ProxyCall(ProxyCall.RpcType.SINGLE)
call.ids[1] = 1
def data = [
BlockchainOuterClass.NativeCallReplyItem.newBuilder()
.setId(1)
.setSucceed(true)
.setPayload(ByteString.copyFrom('{"jsonrpc": "2.0", "id": 1, "error": {"code": -32001, "message": "oops"}}', 'UTF-8'))
.build()
]
when:
def act = Flux.fromIterable(data)
.transform(writer.toJsons(call))
.collectList()
.block(Duration.ofSeconds(1))
then:
act == ['{"jsonrpc":"2.0","id":1,"error":{"code":-32001,"message":"oops"}}']
act == '{"jsonrpc":"2.0","id":105,"result":"0x98dbb1"}'
}
def "Convert gRPC error to JSON"() {
@@ -133,12 +110,26 @@ class WriteRpcJsonSpec extends Specification {
.build()
]
when:
def act = Flux.fromIterable(data)
.transform(writer.toJsons(call))
.collectList()
.block(Duration.ofSeconds(1))
def act = writer.toJson(call, data[0])
then:
act == ['{"jsonrpc":"2.0","id":1,"error":{"code":-32002,"message":"Internal Error"}}']
act == '{"jsonrpc":"2.0","id":1,"error":{"code":-32002,"message":"Internal Error"}}'
}
def "Convert basic to JSON with string id"() {
setup:
def call = new ProxyCall(ProxyCall.RpcType.SINGLE)
call.ids[1] = "aaa"
def data = [
BlockchainOuterClass.NativeCallReplyItem.newBuilder()
.setId(1)
.setSucceed(true)
.setPayload(ByteString.copyFrom('"0x98dbb1"', 'UTF-8'))
.build()
]
when:
def act = writer.toJson(call, data[0])
then:
act == '{"jsonrpc":"2.0","id":"aaa","result":"0x98dbb1"}'
}
def "Convert few items to JSON"() {
@@ -151,17 +142,17 @@ class WriteRpcJsonSpec extends Specification {
BlockchainOuterClass.NativeCallReplyItem.newBuilder()
.setId(1)
.setSucceed(true)
.setPayload(ByteString.copyFrom('{"jsonrpc": "2.0", "id": 1, "result": "0x98dbb1"}', 'UTF-8'))
.setPayload(ByteString.copyFrom('"0x98dbb1"', 'UTF-8'))
.build(),
BlockchainOuterClass.NativeCallReplyItem.newBuilder()
.setId(2)
.setSucceed(true)
.setPayload(ByteString.copyFrom('{"jsonrpc": "2.0", "id": 2, "error": {"code": -32001, "message": "oops"}}', 'UTF-8'))
.setSucceed(false)
.setErrorMessage("oops")
.build(),
BlockchainOuterClass.NativeCallReplyItem.newBuilder()
.setId(3)
.setSucceed(true)
.setPayload(ByteString.copyFrom('{"jsonrpc": "2.0", "id": 3, "result": {"hash": "0x2484f459dc"}}', 'UTF-8'))
.setPayload(ByteString.copyFrom('{"hash": "0x2484f459dc"}', 'UTF-8'))
.build(),
]
when:
@@ -170,10 +161,9 @@ class WriteRpcJsonSpec extends Specification {
.collectList()
.block(Duration.ofSeconds(1))
then:
act == [
'{"jsonrpc":"2.0","id":10,"result":"0x98dbb1"}',
'{"jsonrpc":"2.0","id":11,"error":{"code":-32001,"message":"oops"}}',
'{"jsonrpc":"2.0","id":15,"result":{"hash":"0x2484f459dc"}}'
]
act.size() == 3
act[0] == '{"jsonrpc":"2.0","id":10,"result":"0x98dbb1"}'
act[1] == '{"jsonrpc":"2.0","id":11,"error":{"code":-32002,"message":"oops"}}'
act[2] == '{"jsonrpc":"2.0","id":15,"result":{"hash": "0x2484f459dc"}}'
}
}

View File

@@ -15,10 +15,14 @@
*/
package io.emeraldpay.dshackle.upstream.rpcclient
import com.fasterxml.jackson.databind.ObjectMapper
import io.emeraldpay.dshackle.Global
import spock.lang.Specification
class JsonRpcResponseSpec extends Specification {
ObjectMapper objectMapper = Global.objectMapper
def "Same responses are equal"() {
setup:
def resp1 = new JsonRpcResponse("\"hello\"".bytes, null)
@@ -56,4 +60,78 @@ class JsonRpcResponseSpec extends Specification {
then:
act.isNull()
}
def "Serialize int id and null result"() {
setup:
def json = new JsonRpcResponse("null".bytes, null, new JsonRpcResponse.IntId(1))
when:
def act = objectMapper.writeValueAsString(json)
then:
act == '{"jsonrpc":"2.0","id":1,"result":null}'
}
def "Serialize int id and string result"() {
setup:
def json = new JsonRpcResponse('"Hello World"'.bytes, null, new JsonRpcResponse.IntId(10))
when:
def act = objectMapper.writeValueAsString(json)
then:
act == '{"jsonrpc":"2.0","id":10,"result":"Hello World"}'
}
def "Serialize int id and object result"() {
setup:
def json = new JsonRpcResponse('{"foo": "Hello World", "bar": 1}'.bytes, null, new JsonRpcResponse.IntId(101))
when:
def act = objectMapper.writeValueAsString(json)
then:
act == '{"jsonrpc":"2.0","id":101,"result":{"foo": "Hello World", "bar": 1}}'
}
def "Serialize int id and error"() {
setup:
def json = new JsonRpcResponse(null, new JsonRpcResponse.ResponseError(-32041, "Oooops"), new JsonRpcResponse.IntId(101))
when:
def act = objectMapper.writeValueAsString(json)
then:
act == '{"jsonrpc":"2.0","id":101,"error":{"code":-32041,"message":"Oooops"}}'
}
def "Serialize string id and null result"() {
setup:
def json = new JsonRpcResponse("null".bytes, null, new JsonRpcResponse.StringId("asf01t1gg"))
when:
def act = objectMapper.writeValueAsString(json)
then:
act == '{"jsonrpc":"2.0","id":"asf01t1gg","result":null}'
}
def "Serialize string id and string result"() {
setup:
def json = new JsonRpcResponse('"Hello World"'.bytes, null, new JsonRpcResponse.StringId("10"))
when:
def act = objectMapper.writeValueAsString(json)
then:
act == '{"jsonrpc":"2.0","id":"10","result":"Hello World"}'
}
def "Serialize string id and object result"() {
setup:
def json = new JsonRpcResponse('{"foo": "Hello World", "bar": 1}'.bytes, null, new JsonRpcResponse.StringId("g8gk19g"))
when:
def act = objectMapper.writeValueAsString(json)
then:
act == '{"jsonrpc":"2.0","id":"g8gk19g","result":{"foo": "Hello World", "bar": 1}}'
}
def "Serialize string id and error"() {
setup:
def json = new JsonRpcResponse(null,
new JsonRpcResponse.ResponseError(-32041, "Oooops"),
new JsonRpcResponse.StringId("9kbo29gkaasf"))
when:
def act = objectMapper.writeValueAsString(json)
then:
act == '{"jsonrpc":"2.0","id":"9kbo29gkaasf","error":{"code":-32041,"message":"Oooops"}}'
}
}