Search in sources :

Example 6 with DemoRequest

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

the class JacksonSerializerTest method testMoreParameters.

@Test
public void testMoreParameters() throws NoSuchMethodException {
    SofaRequest request = buildSay2Request();
    AbstractByteBuf data = serializer.encode(request, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaRequest.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    try {
        serializer.decode(data, new SofaRequest(), null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    error = false;
    // parameters size error
    Map<String, String> errorHead1 = new HashMap<String, String>();
    errorHead1.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    errorHead1.put(RemotingConstants.HEAD_METHOD_NAME, "say2");
    try {
        ObjectMapper mapper = new ObjectMapper();
        DemoRequest req = new DemoRequest();
        req.setName("123");
        serializer.decode(new ByteArrayWrapperByteBuf(mapper.writeValueAsBytes(req)), new SofaRequest(), errorHead1);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // parameters size error
    Map<String, String> errorHead2 = new HashMap<String, String>();
    errorHead2.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    errorHead2.put(RemotingConstants.HEAD_METHOD_NAME, "say2");
    try {
        ObjectMapper mapper = new ObjectMapper();
        DemoRequest req = new DemoRequest();
        req.setName("123");
        serializer.decode(new ByteArrayWrapperByteBuf(mapper.writeValueAsBytes(new Object[] { req, "123" })), new SofaRequest(), errorHead2);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    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, "say2");
    head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx");
    head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy");
    head.put("unkown", "yes");
    SofaRequest newRequest = new SofaRequest();
    serializer.decode(data, newRequest, head);
    Assert.assertEquals(newRequest.getInterfaceName(), request.getInterfaceName());
    Assert.assertEquals(newRequest.getMethodName(), request.getMethodName());
    Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length);
    Assert.assertEquals("name", ((DemoRequest) newRequest.getMethodArgs()[0]).getName());
    Assert.assertEquals(newRequest.getTargetServiceUniqueName(), request.getTargetServiceUniqueName());
    Assert.assertEquals(newRequest.getTargetAppName(), request.getTargetAppName());
    Assert.assertEquals(newRequest.getRequestProp(RemotingConstants.RPC_TRACE_NAME), request.getRequestProp(RemotingConstants.RPC_TRACE_NAME));
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) DemoRequest(com.alipay.sofa.rpc.codec.jackson.model.DemoRequest) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

DemoRequest (com.alipay.sofa.rpc.codec.jackson.model.DemoRequest)6 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)3 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)2 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)1 List (java.util.List)1