Search in sources :

Example 1 with DemoRequest

use of com.alipay.sofa.rpc.codec.msgpack.model.DemoRequest 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)

Example 2 with DemoRequest

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

the class MsgPackSerializerTest method buildRequest.

private SofaRequest buildRequest() throws NoSuchMethodException {
    SofaRequest request = new SofaRequest();
    request.setInterfaceName(DemoService.class.getName());
    request.setMethodName("say");
    request.setMethod(DemoService.class.getMethod("say", DemoRequest.class));
    final DemoRequest demoRequest = new DemoRequest();
    demoRequest.setName("name");
    request.setMethodArgs(new Object[] { demoRequest });
    request.setMethodArgSigs(new String[] { DemoRequest.class.getCanonicalName() });
    request.setTargetServiceUniqueName(DemoService.class.getName() + ":1.0");
    request.setTargetAppName("targetApp");
    request.setSerializeType((byte) 11);
    request.setTimeout(1024);
    request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
    Map<String, String> map = new HashMap<String, String>();
    map.put("a", "xxx");
    map.put("b", "yyy");
    request.addRequestProp(RemotingConstants.RPC_TRACE_NAME, map);
    request.setSofaResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
        }
    });
    return request;
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) DemoService(com.alipay.sofa.rpc.codec.msgpack.model.DemoService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) DemoRequest(com.alipay.sofa.rpc.codec.msgpack.model.DemoRequest) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Aggregations

DemoRequest (com.alipay.sofa.rpc.codec.msgpack.model.DemoRequest)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)2 DemoService (com.alipay.sofa.rpc.codec.msgpack.model.DemoService)1 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)1 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)1 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)1 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1