@Override public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException { server.handleRequest(input, output); } }
private ArrayNode callMethod(String methodName, Object[] args) throws Exception { jsonRpcServerAnnotatedMethod.handleRequest( messageWithMapParamsStream(methodName, args), byteArrayOutputStream ); JsonNode res = result(); ObjectMapper mapper = new ObjectMapper(); return (ArrayNode) mapper.readTree(res.asText()); }
@Test public void receiveJsonRpcNotification() throws Exception { EasyMock.expect(mockService.testMethod(param1)).andReturn(param1); EasyMock.replay(mockService); jsonRpcServer.handleRequest(messageWithListParamsStream(null, "testMethod", param1), byteArrayOutputStream); assertEquals(0, byteArrayOutputStream.size()); }
@Test public void callOverloadedMethodOneStringParam() throws Exception { EasyMock.expect(mockService.overloadedMethod(param2)).andReturn(param2); EasyMock.replay(mockService); jsonRpcServer.handleRequest(messageWithListParamsStream(1, "overloadedMethod", param2), byteArrayOutputStream); assertEquals(param2, decodeAnswer(byteArrayOutputStream).get(JsonRpcBasicServer.RESULT).textValue()); }
@Test public void callOverloadedMethodTwoStringParams() throws Exception { EasyMock.expect(mockService.overloadedMethod(param1, param2)).andReturn(param1 + param2); EasyMock.replay(mockService); jsonRpcServer.handleRequest(messageWithListParamsStream(1, "overloadedMethod", param1, param2), byteArrayOutputStream); assertEquals(param1 + param2, decodeAnswer(byteArrayOutputStream).get(JsonRpcBasicServer.RESULT).textValue()); }
@Test public void callOverloadedMethodNoParams() throws Exception { final String noParam = "noParam"; EasyMock.expect(mockService.overloadedMethod()).andReturn(noParam); EasyMock.replay(mockService); jsonRpcServer.handleRequest(messageWithListParamsStream(1, "overloadedMethod"), byteArrayOutputStream); assertEquals(noParam, decodeAnswer(byteArrayOutputStream).get(JsonRpcBasicServer.RESULT).textValue()); }
@Test public void callOverloadedMethodNoNamedParams() throws Exception { EasyMock.expect(mockService.overloadedMethod()).andReturn(param1); EasyMock.replay(mockService); jsonRpcServerAnnotatedParam.handleRequest(messageWithMapParamsStream("overloadedMethod"), byteArrayOutputStream); assertEquals(param1, result().textValue()); }
@Test public void callMethodWithCustomMethodNameTest() throws Exception { EasyMock.expect(mockService.customMethod()).andReturn(param1); EasyMock.replay(mockService); jsonRpcServerAnnotatedMethod.handleRequest(messageWithMapParamsStream("Test.custom"), byteArrayOutputStream); assertEquals(param1, result().textValue()); }
@Test public void callOverloadedMethodOneNamedIntParam() throws Exception { EasyMock.expect(mockService.overloadedMethod(intParam1)).andReturn(param2); EasyMock.replay(mockService); jsonRpcServerAnnotatedParam.handleRequest(messageWithMapParamsStream("overloadedMethod", param1, intParam1), byteArrayOutputStream); assertEquals(param2, result().textValue()); }
@Test public void callOverloadedMethodExtraParams() throws Exception { EasyMock.expect(mockService.overloadedMethod(param1, param2)).andReturn(param1 + param2); EasyMock.replay(mockService); jsonRpcServer.setAllowExtraParams(true); jsonRpcServer.handleRequest(messageWithListParamsStream(1, "overloadedMethod", param1, param2, param3), byteArrayOutputStream); assertEquals(param1 + param2, decodeAnswer(byteArrayOutputStream).get(JsonRpcBasicServer.RESULT).textValue()); }
@Test public void callOverloadedMethodOneNamedStringParam() throws Exception { EasyMock.expect(mockService.overloadedMethod(EasyMock.anyString())).andReturn(param3); EasyMock.replay(mockService); jsonRpcServerAnnotatedParam.handleRequest(messageWithMapParamsStream("overloadedMethod", param1, param2), byteArrayOutputStream); assertEquals(param3, result().textValue()); }
@Test public void idLongType() throws Exception { EasyMock.expect(mockService.testMethod(param1)).andReturn(param1); EasyMock.replay(mockService); jsonRpcServer.handleRequest(messageWithListParamsStream(longParam, "testMethod", param1), byteArrayOutputStream); assertTrue(decodeAnswer(byteArrayOutputStream).get(ID).isLong()); }
@SuppressWarnings("unchecked") @Test public void callConvertedParameterTransformerShouldBeCalledIfSet() throws Exception { final ConvertedParameterTransformer convertedParameterTransformer = EasyMock.niceMock(ConvertedParameterTransformer.class); EasyMock.expect(mockService.testMethod(param1)).andReturn(param1); jsonRpcServer.setConvertedParameterTransformer(convertedParameterTransformer); EasyMock.expect(convertedParameterTransformer.transformConvertedParameters(anyObject(), anyObject(Object[].class))).andReturn(new Object[]{param1}); EasyMock.replay(convertedParameterTransformer); jsonRpcServer.handleRequest(messageWithListParamsStream(1, "testMethod", param1), byteArrayOutputStream); EasyMock.verify(convertedParameterTransformer); }
@Test public void callMethodWithExtraParameterNamedAllowOn() throws Exception { EasyMock.expect(mockService.testMethod(EasyMock.anyObject(String.class))).andReturn("success"); EasyMock.replay(mockService); jsonRpcServerAnnotatedParam.setAllowExtraParams(true); jsonRpcServerAnnotatedParam.handleRequest(messageWithMapParamsStream("testMethod", param1, param2, param3, intParam1), byteArrayOutputStream); assertEquals("success", result().textValue()); }
@Test public void receiveEmptyLine() throws IOException { jsonRpcServer.handleRequest(emptyLineStream(), byteArrayOutputStream); assertEquals(JsonError.PARSE_ERROR.code, decodeAnswer(byteArrayOutputStream).get(JsonRpcBasicServer.ERROR).get(JsonRpcBasicServer.ERROR_CODE).intValue()); }
@Test public void callMethodWithoutRequiredParamAllowOn() throws Exception { EasyMock.expect(mockService.methodWithoutRequiredParam(EasyMock.eq(param3), EasyMock.anyString())).andReturn(param3); EasyMock.replay(mockService); jsonRpcServerAnnotatedParam.setAllowLessParams(true); jsonRpcServerAnnotatedParam.handleRequest(messageWithMapParamsStream("methodWithoutRequiredParam", param1, param3), byteArrayOutputStream); assertEquals(param3, result().textValue()); }
@Test public void callOverloadedMethodNamedExtraParams() throws Exception { EasyMock.expect(mockService.overloadedMethod(intParam1, intParam2)).andReturn((intParam1 + intParam2) + ""); EasyMock.replay(mockService); jsonRpcServerAnnotatedParam.handleRequest(messageWithMapParamsStream("overloadedMethod", param1, intParam1, param2, intParam2, param3, param4), byteArrayOutputStream); assertEquals(METHOD_PARAMS_INVALID.code, errorCode(error(byteArrayOutputStream)).intValue()); }
@Test public void callMethodWithTooFewParametersNamed() throws Exception { EasyMock.expect(mockService.testMethod(EasyMock.anyObject(String.class))).andReturn("success"); EasyMock.replay(mockService); jsonRpcServerAnnotatedParam.handleRequest(messageWithMapParamsStream("testMethod"), byteArrayOutputStream); assertEquals(METHOD_PARAMS_INVALID.code, errorCode(error(byteArrayOutputStream)).intValue()); }
@Test public void callMethodWithTooFewParametersNamedAllowOn() throws Exception { EasyMock.expect(mockService.testMethod(EasyMock.anyObject(String.class))).andReturn("success"); EasyMock.replay(mockService); jsonRpcServerAnnotatedParam.setAllowExtraParams(true); jsonRpcServerAnnotatedParam.handleRequest(messageWithMapParamsStream("testMethod"), byteArrayOutputStream); assertEquals(METHOD_PARAMS_INVALID.code, errorCode(error(byteArrayOutputStream)).intValue()); }
@Test public void exceptionWithoutAnnotatedServiceInterface() throws Exception { JsonRpcBasicServer jsonRpcServer = new JsonRpcBasicServer(mapper, new Service(), ServiceInterfaceWithoutAnnotation.class); jsonRpcServer.handleRequest(messageWithListParamsStream(1, "testMethod"), byteArrayOutputStream); JsonNode error = error(byteArrayOutputStream); assertNotNull(error); assertEquals(ErrorResolver.JsonError.ERROR_NOT_HANDLED.code, errorCode(error).intValue()); }