Search in sources :

Example 1 with YarRequest

use of com.weibo.yar.YarRequest in project motan by weibocom.

the class YarProtocolUtilTest method testConvertYarRequest.

@Test
public void testConvertYarRequest() throws NoSuchMethodException, SecurityException {
    DefaultRequest request = new DefaultRequest();
    request.setRequestId(123);
    request.setMethodName("hello");
    request.setArguments(new Object[] { "param1" });
    request.setInterfaceName(YarMessageRouterTest.AnnoService.class.getName());
    request.setParamtersDesc(ReflectUtil.getMethodParamDesc(YarMessageRouterTest.AnnoService.class.getMethod("hello", String.class)));
    YarRequest yarRequest = YarProtocolUtil.convert(request, "JSON");
    assertNotNull(yarRequest);
    Request newRequest = YarProtocolUtil.convert(yarRequest, YarMessageRouterTest.AnnoService.class);
    assertNotNull(newRequest);
    assertEquals(request.toString(), newRequest.toString());
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) YarRequest(com.weibo.yar.YarRequest) YarRequest(com.weibo.yar.YarRequest) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 2 with YarRequest

use of com.weibo.yar.YarRequest in project motan by weibocom.

the class YarMessageRouterTest method testHandle.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandle() {
    response = new DefaultResponse();
    response.setValue("test");
    response.setProcessTime(1);
    Provider provider = new DefaultProvider(null, null, AnnoService.class);
    router.addProvider(provider);
    YarRequest yarRequest = new YarRequest(1, "JSON", "hello", new Object[] { "params" });
    yarRequest.setRequestPath(requestPath);
    YarResponse yarResponse = (YarResponse) router.handle(null, yarRequest);
    assertEquals(YarProtocolUtil.convert(response, "JSON"), yarResponse);
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) YarRequest(com.weibo.yar.YarRequest) YarResponse(com.weibo.yar.YarResponse) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) Provider(com.weibo.api.motan.rpc.Provider) Test(org.junit.Test)

Example 3 with YarRequest

use of com.weibo.yar.YarRequest in project motan by weibocom.

the class YarMessageHandlerWarpperTest method testHandle.

@Test
public void testHandle() throws Exception {
    YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
    final YarResponse yarResponse = YarProtocolUtil.buildDefaultErrorResponse("test err", "JSON");
    YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {

        @Override
        public Object handle(Channel channel, Object message) {
            AttachmentRequest request = (AttachmentRequest) message;
            verifyAttachments(request.getAttachments());
            return yarResponse;
        }
    });
    FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
    assertNotNull(httpResponse);
    assertNotNull(httpResponse.content());
    YarResponse retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertEquals(yarResponse, retYarResponse);
}
Also used : YarMessageRouter(com.weibo.api.motan.protocol.yar.YarMessageRouter) YarRequest(com.weibo.yar.YarRequest) Channel(com.weibo.api.motan.transport.Channel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) YarResponse(com.weibo.yar.YarResponse) AttachmentRequest(com.weibo.api.motan.protocol.yar.AttachmentRequest) Test(org.junit.Test)

Example 4 with YarRequest

use of com.weibo.yar.YarRequest in project motan by weibocom.

the class YarMessageHandlerWarpperTest method testAbnormal.

@Test
public void testAbnormal() throws Exception {
    final String errmsg = "rpc process error";
    YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {

        @Override
        public Object handle(Channel channel, Object message) {
            throw new RuntimeException(errmsg);
        }
    });
    // yar协议无法解析
    FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(null, uri));
    assertNotNull(httpResponse);
    assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
    YarResponse retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertNotNull(retYarResponse.getError());
    // yar协议可以正常解析,但后续处理异常
    YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
    httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
    assertNotNull(httpResponse);
    assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
    retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertEquals(errmsg, retYarResponse.getError());
}
Also used : YarMessageRouter(com.weibo.api.motan.protocol.yar.YarMessageRouter) YarRequest(com.weibo.yar.YarRequest) Channel(com.weibo.api.motan.transport.Channel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) YarResponse(com.weibo.yar.YarResponse) Test(org.junit.Test)

Example 5 with YarRequest

use of com.weibo.yar.YarRequest in project motan by weibocom.

the class YarProtocolUtilTest method verifyMethodParam.

private <T> void verifyMethodParam(Class<T> interfaceClazz, T service, Method method, Object[] params, Object expectResult) throws Exception {
    YarRequest yarRequest = new YarRequest();
    yarRequest.setId(123);
    yarRequest.setMethodName(method.getName());
    yarRequest.setPackagerName("JSON");
    yarRequest.setParameters(params);
    Request request = YarProtocolUtil.convert(yarRequest, interfaceClazz);
    assertNotNull(request);
    assertEquals(method.getName(), request.getMethodName());
    Object[] requestParams = request.getArguments();
    assertEquals(params.length, requestParams.length);
    Object result = method.invoke(service, requestParams);
    assertEquals(expectResult, result);
}
Also used : YarRequest(com.weibo.yar.YarRequest) YarRequest(com.weibo.yar.YarRequest) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Aggregations

YarRequest (com.weibo.yar.YarRequest)8 YarResponse (com.weibo.yar.YarResponse)5 Test (org.junit.Test)4 Request (com.weibo.api.motan.rpc.Request)3 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)3 AttachmentRequest (com.weibo.api.motan.protocol.yar.AttachmentRequest)2 YarMessageRouter (com.weibo.api.motan.protocol.yar.YarMessageRouter)2 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)2 Channel (com.weibo.api.motan.transport.Channel)2 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)1 DefaultProvider (com.weibo.api.motan.rpc.DefaultProvider)1 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)1 Provider (com.weibo.api.motan.rpc.Provider)1 Response (com.weibo.api.motan.rpc.Response)1 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 IOException (java.io.IOException)1