use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.
the class ProtostuffSerializerTest method encodeAndDecode.
@Test
public void encodeAndDecode() {
boolean error = false;
try {
serializer.encode(null, null);
} catch (Exception e) {
error = true;
}
Assert.assertTrue(error);
ExampleObj req = new ExampleObj();
req.setName("xxx");
AbstractByteBuf byteBuf = serializer.encode(req, null);
ExampleObj req2 = (ExampleObj) serializer.decode(byteBuf, ExampleObj.class, null);
Assert.assertEquals(req.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);
}
use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.
the class ProtobufSerializerTest method encodeAndDecode.
@Test
public void encodeAndDecode() {
boolean error = false;
try {
serializer.encode(null, null);
} catch (Exception e) {
error = true;
}
Assert.assertTrue(error);
EchoStrReq req = EchoStrReq.newBuilder().setS("xxxx").build();
AbstractByteBuf byteBuf = serializer.encode(req, null);
EchoStrReq req2 = (EchoStrReq) serializer.decode(byteBuf, EchoStrReq.class, null);
Assert.assertEquals(req.getS(), req2.getS());
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);
}
use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.
the class ProtobufSerializerTest 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, ProtoService.class.getCanonicalName() + ":1.0");
head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
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("xxxx", ((EchoStrReq) newRequest.getMethodArgs()[0]).getS());
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, ProtoService.class.getCanonicalName() + ":1.0");
head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
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);
Assert.assertEquals("", ((EchoStrReq) newRequest.getMethodArgs()[0]).getS());
}
use of com.alipay.sofa.rpc.transport.AbstractByteBuf in project sofa-rpc by sofastack.
the class ProtobufSerializerTest 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, ProtoService.class.getCanonicalName() + ":1.0");
head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
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();
response.setAppResponse(EchoStrRes.newBuilder().setS("result").build());
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", ((EchoStrRes) newResponse.getAppResponse()).getS());
// null response
head = new HashMap<String, String>();
head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtoService.class.getCanonicalName() + ":1.0");
head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
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("", ((EchoStrRes) newResponse.getAppResponse()).getS());
// error response
head = new HashMap<String, String>();
head.put(RemotingConstants.HEAD_TARGET_SERVICE, ProtoService.class.getCanonicalName() + ":1.0");
head.put(RemotingConstants.HEAD_METHOD_NAME, "echoStr");
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());
}
use of com.alipay.sofa.rpc.transport.AbstractByteBuf 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());
}
Aggregations