RequestHandlerImpl implements RequestHandler { @Override public MethodResult handleCall(MethodCall call) { Map<String, String> metaData = call.getMetaData(); String contextId = metaData.get("contextId"); if (contextId != null) { ContextHolder.get().setCurrentContextId(contextId); } Object service = retrieveOpenEngSBService(call); Method method = findMethod(service, call.getMethodName(), getArgTypes(call)); Object[] args = retrieveArguments(call, method); MethodResult methodResult = invokeMethod(service, method, args); methodResult.setMetaData(call.getMetaData()); return methodResult; }  @Override MethodResult handleCall(MethodCall call); void setUtilsService(OsgiUtilsService utilsService);  }
@Test public void testCallByServiceId_shouldCallService() throws Exception { TestInterface mockService = mockService(TestInterface.class, "testid"); when(mockService.test(anyInt())).thenReturn(21); Map<String, String> metaData = ImmutableMap.of("serviceId", "testid"); MethodCall c = new MethodCall("test", new Object[]{ 42 }, metaData); MethodResult result = requestHandler.handleCall(c); verify(mockService).test(42); assertThat(result.getClassName(), is(Integer.class.getName())); assertThat((Integer) result.getArg(), is(21)); }
@Test public void testCallByFilter_shouldCallService() throws Exception { Map<String, Object> propData = ImmutableMap.of("testprop", (Object) new String[]{ "bla", "bleh", }); TestInterface mockService = mockServiceWithProps(propData); when(mockService.test(anyInt())).thenReturn(21); Map<String, String> metaData = ImmutableMap.of("serviceFilter", "(testprop=bla)"); MethodCall c = new MethodCall("test", new Object[]{ 42 }, metaData); MethodResult result = requestHandler.handleCall(c); verify(mockService).test(42); assertThat(result.getClassName(), is(Integer.class.getName())); assertThat((Integer) result.getArg(), is(21)); }
@Test public void testCallByFilterAndServiceId_shouldCallService() throws Exception { Map<String, Object> propData = ImmutableMap.of("testprop", (Object) new String[]{ "bla", "bleh", }, Constants.SERVICE_PID, "xxx"); TestInterface mockService = mockServiceWithProps(propData); when(mockService.test(anyInt())).thenReturn(21); Map<String, String> metaData = ImmutableMap.of("serviceFilter", "(testprop=bla)", "serviceId", "xxx"); MethodCall c = new MethodCall("test", new Object[]{ 42 }, metaData); MethodResult result = requestHandler.handleCall(c); verify(mockService).test(42); assertThat(result.getClassName(), is(Integer.class.getName())); assertThat((Integer) result.getArg(), is(21)); }
@Test public void testCallByFilterAndServiceIdWithCustomMarshaller_shouldCallService() throws Exception { Map<String, Object> propData = ImmutableMap.of("testprop", (Object) new String[]{ "blub", "bleh", }, Constants.SERVICE_PID, "xxx"); TestInterface realObject = new TestInterface() { @Override public Integer test(@UseCustomJasonMarshaller(CustomMarshaller.class) Integer arg) { return arg; } }; registerServiceWithProps(realObject, propData); Map<String, String> metaData = ImmutableMap.of("serviceFilter", "(testprop=blub)", "serviceId", "xxx"); MethodCall c = new MethodCall("test", new Object[]{ 42 }, metaData); MethodResult result = requestHandler.handleCall(c); assertThat(result.getClassName(), is(Integer.class.getName())); assertThat((Integer) result.getArg(), is(43)); }
@Test public void testCallButBothAttrsNull_shouldThrowException() throws Exception { Map<String, Object> propData = ImmutableMap.of("testprop", (Object) new String[]{ "bla", "bleh", }); TestInterface mockService = mockServiceWithProps(propData); MethodCall c = new MethodCall("test", new Object[]{ 42 }); try { requestHandler.handleCall(c); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { verifyZeroInteractions(mockService); } }