Search in sources :

Example 16 with AbstractByteBuf

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

the class JacksonSerializerTest method testListParameter.

@Test
public void testListParameter() throws NoSuchMethodException {
    SofaRequest request = buildSay3Request();
    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, "say3");
    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, "say3");
    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, "say3");
    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", ((List<DemoRequest>) newRequest.getMethodArgs()[0]).get(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) ArrayList(java.util.ArrayList) List(java.util.List) 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)

Example 17 with AbstractByteBuf

use of com.alipay.sofa.rpc.transport.AbstractByteBuf 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)

Example 18 with AbstractByteBuf

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

the class SofaHessianSerializerTest method encodeSofaResponse.

@Test
public void encodeSofaResponse() {
    AbstractByteBuf data;
    SofaResponse response = new SofaResponse();
    response.setErrorMsg("1233");
    Assert.assertTrue(response.isError());
    Assert.assertEquals("1233", response.getErrorMsg());
    data = serializer.encode(response, null);
    SofaResponse newResponse = (SofaResponse) serializer.decode(data, SofaResponse.class, null);
    Assert.assertTrue(newResponse.isError());
    SofaResponse newResponse2 = (SofaResponse) serializer.decode(data, SofaResponse.class, Collections.singletonMap(RemotingConstants.HEAD_GENERIC_TYPE, RemotingConstants.SERIALIZE_FACTORY_GENERIC));
    Assert.assertTrue(newResponse2.isError());
    response = new SofaResponse();
    response.setAppResponse("123");
    data = serializer.encode(response, null);
    newResponse = (SofaResponse) serializer.decode(data, SofaResponse.class, null);
    Assert.assertFalse(newResponse.isError());
    Assert.assertEquals(response.getAppResponse(), newResponse.getAppResponse());
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test)

Example 19 with AbstractByteBuf

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

the class SofaHessianSerializerTest method encode.

@Test
public void encode() {
    AbstractByteBuf data = serializer.encode("xxx", null);
    String dst = (String) serializer.decode(data, String.class, null);
    Assert.assertEquals("xxx", dst);
    boolean error = false;
    try {
        serializer.decode(data, "", 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, (Object) null, null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 20 with AbstractByteBuf

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

the class JacksonSerializerTest method testSofaRequest.

@Test
public void testSofaRequest() throws Exception {
    SofaRequest request = buildSayRequest();
    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);
    // parameters size error
    Map<String, String> errorHead = new HashMap<String, String>();
    errorHead.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0");
    errorHead.put(RemotingConstants.HEAD_METHOD_NAME, "say");
    errorHead.put(RemotingConstants.HEAD_TARGET_APP, "targetApp");
    try {
        SofaRequest say2Request = buildSay2Request();
        AbstractByteBuf say2Data = serializer.encode(say2Request, errorHead);
        serializer.decode(say2Data, new SofaRequest(), errorHead);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // success
    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));
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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