use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.
the class JacksonSerializerTest method testJacksonFeature.
@Test
public void testJacksonFeature() throws UnsupportedEncodingException {
try {
JacksonSerializer serializer = new JacksonSerializer();
serializer.decode(new ByteArrayWrapperByteBuf("{\"a\":1}".getBytes("UTF-8")), DemoRequest.class, null);
Assert.fail();
} catch (SofaRpcException e) {
// ok
} catch (Throwable e) {
Assert.fail();
}
try {
JacksonSerializer serializer = new JacksonSerializer();
serializer.encode(new DemoRequest2(), null);
Assert.fail();
} catch (SofaRpcException e) {
// ok
} catch (Throwable e) {
Assert.fail();
}
System.setProperty("sofa.rpc.codec.jackson.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES", "false");
JacksonSerializer serializer = new JacksonSerializer();
serializer.decode(new ByteArrayWrapperByteBuf("{\"a\":1}".getBytes("UTF-8")), DemoRequest.class, null);
System.setProperty("sofa.rpc.codec.jackson.SerializationFeature.FAIL_ON_EMPTY_BEANS", "false");
serializer = new JacksonSerializer();
serializer.encode(new DemoRequest2(), null);
System.setProperty("sofa.rpc.codec.jackson.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES", "true");
System.setProperty("sofa.rpc.codec.jackson.SerializationFeature.FAIL_ON_EMPTY_BEANS", "true");
}
use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf 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());
}
use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf 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());
}
use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf in project sofa-rpc by sofastack.
the class SofaRequestTest method getRequestProp.
@Test
public void getRequestProp() throws Exception {
SofaRequest request = new SofaRequest();
request.setInterfaceName(Invoker.class.getName());
request.setMethodName("invoke");
request.setMethod(Invoker.class.getMethod("invoke", SofaRequest.class));
request.setMethodArgs(new Object[] { new SofaRequest() });
request.setMethodArgSigs(new String[] { SofaRequest.class.getCanonicalName() });
request.setTargetServiceUniqueName(Invoker.class.getName() + ":1.0");
request.setTargetAppName("targetApp");
request.setSerializeType((byte) 11);
request.setTimeout(1024);
request.setSerializeType((byte) 11);
request.setData(new ByteArrayWrapperByteBuf(new byte[] { 1, 2, 3 }));
request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
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) {
}
});
Assert.assertEquals(Invoker.class.getName(), request.getInterfaceName());
Assert.assertEquals("invoke", request.getMethodName());
Assert.assertEquals("invoke", request.getMethod().getName());
Assert.assertArrayEquals(new String[] { SofaRequest.class.getCanonicalName() }, request.getMethodArgSigs());
Assert.assertEquals(1, request.getMethodArgs().length);
Assert.assertEquals(Invoker.class.getName() + ":1.0", request.getTargetServiceUniqueName());
Assert.assertEquals("targetApp", request.getTargetAppName());
Assert.assertTrue(request.getSerializeType() == 11);
Assert.assertTrue(request.getTimeout() == 1024);
Assert.assertEquals(RpcConstants.INVOKER_TYPE_SYNC, request.getInvokeType());
Assert.assertTrue(request.getSerializeType() == 11);
Assert.assertTrue(request.getData().array().length == 3);
Assert.assertNotNull(request.getSofaResponseCallback());
Map<String, Object> map = request.getRequestProps();
Assert.assertTrue(map == null);
request.addRequestProp("1", "1");
map = request.getRequestProps();
Assert.assertTrue(map.size() == 1);
request.addRequestProp(null, "1");
Assert.assertTrue(map.size() == 1);
request.addRequestProp("1", null);
Assert.assertTrue(map.size() == 1);
request.addRequestProps(null);
Assert.assertTrue(map.size() == 1);
request.addRequestProps(new HashMap<String, Object>());
Assert.assertTrue(map.size() == 1);
request.removeRequestProp("1");
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("1", "1");
request.addRequestProps(hashMap);
hashMap.put("2", "2");
request.addRequestProps(hashMap);
Assert.assertTrue(map.size() == 2);
Assert.assertEquals("2", request.getRequestProp("2"));
request.removeRequestProp(null);
Assert.assertTrue(map.size() == 2);
request.removeRequestProp("2");
Assert.assertTrue(map.size() == 1);
Assert.assertNull(request.getRequestProp("2"));
Assert.assertFalse(request.isAsync());
request.setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
Assert.assertTrue(request.isAsync());
request.setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
Assert.assertTrue(request.isAsync());
request.setInvokeType(null);
Assert.assertFalse(request.isAsync());
}
use of com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf 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());
}
Aggregations