Search in sources :

Example 6 with AbstractByteBuf

use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.

the class SofaHessianSerializerTest method encodeSofaRequest.

@Test
public void encodeSofaRequest() throws Exception {
    SofaRequest request = buildRequest();
    AbstractByteBuf data = serializer.encode(request, null);
    SofaRequest newRequest = (SofaRequest) serializer.decode(data, SofaRequest.class, null);
    Assert.assertEquals(newRequest.getInterfaceName(), request.getInterfaceName());
    Assert.assertEquals(newRequest.getMethodName(), request.getMethodName());
    Assert.assertArrayEquals(newRequest.getMethodArgSigs(), request.getMethodArgSigs());
    Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length);
    Assert.assertEquals(newRequest.getTargetServiceUniqueName(), request.getTargetServiceUniqueName());
    Assert.assertEquals(newRequest.getTargetAppName(), request.getTargetAppName());
    request = buildRequest();
    GenericObject genericObject = new GenericObject(TestGenericBean.class.getCanonicalName());
    genericObject.putField("name", "Lilei");
    genericObject.putField("age", 123);
    // change to generic request
    request.setMethodArgSigs(new String[] { TestGenericBean.class.getCanonicalName() });
    request.setMethodArgs(new Object[] { genericObject });
    data = serializer.encode(request, Collections.singletonMap(RemotingConstants.HEAD_GENERIC_TYPE, RemotingConstants.SERIALIZE_FACTORY_GENERIC));
    newRequest = (SofaRequest) serializer.decode(data, SofaRequest.class, null);
    Assert.assertEquals(newRequest.getInterfaceName(), request.getInterfaceName());
    Assert.assertEquals(newRequest.getMethodName(), request.getMethodName());
    Assert.assertArrayEquals(newRequest.getMethodArgSigs(), request.getMethodArgSigs());
    Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length);
    Assert.assertEquals(TestGenericBean.class.getCanonicalName(), newRequest.getMethodArgs()[0].getClass().getCanonicalName());
    Assert.assertEquals(newRequest.getTargetServiceUniqueName(), request.getTargetServiceUniqueName());
    Assert.assertEquals(newRequest.getTargetAppName(), request.getTargetAppName());
    // error request has no target service name
    request = buildRequest();
    request.setTargetServiceUniqueName(null);
    data = serializer.encode(request, null);
    boolean error = false;
    try {
        serializer.decode(data, SofaRequest.class, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) GenericObject(com.alipay.hessian.generic.model.GenericObject) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 7 with AbstractByteBuf

use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.

the class JacksonSerializerTest method encodeAndDecode.

@Test
public void encodeAndDecode() {
    boolean error = false;
    try {
        serializer.encode(null, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    DemoRequest demoRequest = new DemoRequest();
    demoRequest.setName("a");
    AbstractByteBuf byteBuf = serializer.encode(demoRequest, null);
    DemoRequest req2 = (DemoRequest) serializer.decode(byteBuf, DemoRequest.class, null);
    Assert.assertEquals(demoRequest.getName(), req2.getName());
    AbstractByteBuf data = serializer.encode("xxx", null);
    String dst = (String) serializer.decode(data, String.class, null);
    Assert.assertEquals("xxx", dst);
    error = false;
    try {
        serializer.encode(new Date(), 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, "", null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) DemoRequest(com.alipay.sofa.rpc.codec.jackson.model.DemoRequest) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date) Test(org.junit.Test)

Example 8 with AbstractByteBuf

use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.

the class MsgPackSerializerTest 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());
    // null 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");
    newResponse = new SofaResponse();
    serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newResponse, head);
    Assert.assertFalse(newResponse.isError());
    Assert.assertNotNull(newResponse.getAppResponse());
    Assert.assertEquals(null, ((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.msgpack.model.DemoResponse) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 9 with AbstractByteBuf

use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.

the class MsgPackSerializerTest method testSofaRequest.

@Test
public void testSofaRequest() throws Exception {
    SofaRequest request = buildRequest();
    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);
    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");
    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.assertArrayEquals(newRequest.getMethodArgSigs(), request.getMethodArgSigs());
    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));
    // null request
    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");
    newRequest = new SofaRequest();
    serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newRequest, head);
    final Object[] methodArgs = newRequest.getMethodArgs();
    Assert.assertEquals(null, ((DemoRequest) methodArgs[0]).getName());
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 10 with AbstractByteBuf

use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.

the class MsgPackSerializerTest method encodeAndDecode.

@Test
public void encodeAndDecode() {
    boolean error = false;
    try {
        serializer.encode(null, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    DemoRequest demoRequest = new DemoRequest();
    demoRequest.setName("a");
    AbstractByteBuf byteBuf = serializer.encode(demoRequest, null);
    DemoRequest req2 = (DemoRequest) serializer.decode(byteBuf, DemoRequest.class, null);
    Assert.assertEquals(demoRequest.getName(), req2.getName());
    AbstractByteBuf data = serializer.encode("xxx", null);
    String dst = (String) serializer.decode(data, String.class, null);
    Assert.assertEquals("xxx", dst);
    error = false;
    Date object = new Date();
    try {
        AbstractByteBuf encode = serializer.encode(object, null);
        Assert.assertEquals(object, serializer.decode(encode, Date.class, null));
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(!error);
    error = false;
    try {
        serializer.encode(1, 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, "", null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) DemoRequest(com.alipay.sofa.rpc.codec.msgpack.model.DemoRequest) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Date(java.util.Date) Test(org.junit.Test)

Aggregations

AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)24 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)19 Test (org.junit.Test)18 HashMap (java.util.HashMap)14 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)8 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)8 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)7 Serializer (com.alipay.sofa.rpc.codec.Serializer)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Date (java.util.Date)4 DemoRequest (com.alipay.sofa.rpc.codec.jackson.model.DemoRequest)3 DefaultCustomSerializer (com.alipay.remoting.DefaultCustomSerializer)2 DeserializationException (com.alipay.remoting.exception.DeserializationException)2 SerializationException (com.alipay.remoting.exception.SerializationException)2 StringSerializer (com.alipay.sofa.rpc.codec.common.StringSerializer)2 DemoResponse (com.alipay.sofa.rpc.codec.jackson.model.DemoResponse)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 GenericObject (com.alipay.hessian.generic.model.GenericObject)1