Search in sources :

Example 1 with DemoResponse

use of com.alipay.sofa.rpc.codec.jackson.model.DemoResponse in project sofa-rpc by sofastack.

the class JacksonSerializerTest method testSofaResponse.

@Test
public void testSofaResponse() throws Exception {
    SofaResponse response = new SofaResponse();
    response.setAppResponse("1233");
    AbstractByteBuf data = serializer.encode(response, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaResponse.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, null, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, new SofaResponse(), null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // success response
    Map<String, String> head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "say");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    response = new SofaResponse();
    final DemoResponse response1 = new DemoResponse();
    response1.setWord("result");
    response.setAppResponse(response1);
    data = serializer.encode(response, null);
    SofaResponse newResponse = new SofaResponse();
    serializer.decode(data, newResponse, head);
    Assert.assertFalse(newResponse.isError());
    Assert.assertEquals(response.getAppResponse(), newResponse.getAppResponse());
    Assert.assertEquals("result", ((DemoResponse) newResponse.getAppResponse()).getWord());
    // error response
    head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "say");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    head.put(RemotingConstants.HEAD_RESPONSE_ERROR, "true");
    response = new SofaResponse();
    response.setErrorMsg("1233");
    data = serializer.encode(response, null);
    newResponse = new SofaResponse();
    serializer.decode(data, newResponse, head);
    Assert.assertTrue(newResponse.isError());
    Assert.assertEquals(response.getErrorMsg(), newResponse.getErrorMsg());
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) HashMap(java.util.HashMap) DemoResponse(com.alipay.sofa.rpc.codec.jackson.model.DemoResponse) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 2 with DemoResponse

use of com.alipay.sofa.rpc.codec.jackson.model.DemoResponse in project sofa-rpc by sofastack.

the class JacksonSerializerTest method testListResponse.

@Test
public void testListResponse() {
    // success response
    Map<String, String> head = new HashMap<String, String>();
    head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    head.put(RemotingConstants.HEAD_METHOD_NAME, "say3");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    SofaResponse response = new SofaResponse();
    final DemoResponse response1 = new DemoResponse();
    response1.setWord("result");
    List<DemoResponse> listResponse = new ArrayList<DemoResponse>();
    listResponse.add(response1);
    response.setAppResponse(listResponse);
    AbstractByteBuf data = serializer.encode(response, null);
    SofaResponse newResponse = new SofaResponse();
    serializer.decode(data, newResponse, head);
    Assert.assertFalse(newResponse.isError());
    Assert.assertEquals(response.getAppResponse(), newResponse.getAppResponse());
    Assert.assertEquals("result", ((List<DemoResponse>) newResponse.getAppResponse()).get(0).getWord());
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DemoResponse(com.alipay.sofa.rpc.codec.jackson.model.DemoResponse) ArrayList(java.util.ArrayList) List(java.util.List) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Aggregations

DemoResponse (com.alipay.sofa.rpc.codec.jackson.model.DemoResponse)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1