MethodServices { public Method getMethodFromDataService(final Class dsClass, final MessageFromClient message, List<Object> arguments) throws NoSuchMethodException { logger.debug("Try to find method {} on class {}", message.getOperation(), dsClass); List<String> parameters = message.getParameters(); int nbparam = parameters.size() - getNumberOfNullEnderParameter(parameters); List<Method> candidates = getSortedCandidateMethods(message.getOperation(), dsClass.getMethods()); if (!candidates.isEmpty()) { while (nbparam <= parameters.size()) { for (Method method : candidates) { if (method.getParameterTypes().length == nbparam) { logger.debug("Process method {}", method.getName()); try { checkMethod(method, arguments, parameters, nbparam); logger.debug("Method {}.{} with good signature found.", dsClass, message.getOperation()); return method; } catch (JsonMarshallerException | JsonUnmarshallingException | IllegalArgumentException iae) { logger.debug("Method {}.{} not found. Some arguments didn't match. {}.", new Object[]{dsClass, message.getOperation(), iae.getMessage()}); } arguments.clear(); } } nbparam++; } } throw new NoSuchMethodException(dsClass.getName() + "." + message.getOperation()); }  Method getMethodFromDataService(final Class dsClass, final MessageFromClient message, List<Object> arguments); Method getNonProxiedMethod(Class cls, String methodName, Class<?>... parameterTypes);  }
@Test public void testGetMethodFromDataService() throws NoSuchMethodException, JsonUnmarshallingException, JsonMarshallerException { System.out.println("getMethodFromDataService"); Class dsClass = ClassAsDataService.class; MessageFromClient message = new MessageFromClient(); message.setOperation("methodWith2Arguments"); message.setParameters(Arrays.asList("5", "\"toto\"")); List<Object> arguments = new ArrayList<>(); Method expResult = dsClass.getMethod("methodWith2Arguments", new Class<?>[]{Integer.class, String.class}); when(argumentsServices.convertJsonToJava(eq("5"), any(Type.class), any(Annotation[].class))).thenReturn(5); when(argumentsServices.convertJsonToJava(eq("\"toto\""), any(Type.class), any(Annotation[].class))).thenReturn("toto"); Method result = instance.getMethodFromDataService(dsClass, message, arguments); assertThat(result).isEqualTo(expResult); }
@Test(expected = NoSuchMethodException.class) public void testGetMethodFromDataServiceNotFound() throws NoSuchMethodException, JsonUnmarshallingException, JsonMarshallerException { System.out.println("getMethodFromDataService"); Class dsClass = ClassAsDataService.class; MessageFromClient message = new MessageFromClient(); message.setOperation("methodWith2Arguments"); message.setParameters(Arrays.asList("\"toto\"", "5")); List<Object> arguments = new ArrayList<>(); when(argumentsServices.convertJsonToJava(anyString(), any(Type.class), any(Annotation[].class))).thenThrow(JsonUnmarshallingException.class); instance.getMethodFromDataService(dsClass, message, arguments); }
@Test public void testGetMethodFromDataServiceWithWithUnmarshaller() throws NoSuchMethodException, JsonUnmarshallingException, JsonMarshallerException { System.out.println("getMethodFromDataService"); Class dsClass = ClassAsDataService.class; MessageFromClient message = new MessageFromClient(); message.setOperation("methodWithUnmarshaller"); String json = "{\"language\":\"fr\",\"country\":\"FR\"}"; message.setParameters(Arrays.asList(json)); List<Object> arguments = new ArrayList<>(); Method expResult = dsClass.getMethod("methodWithUnmarshaller", new Class<?>[]{Locale.class}); when(argumentsServices.convertJsonToJava(eq(json), any(Type.class), any(Annotation[].class))).thenReturn(Locale.FRANCE); Method result = instance.getMethodFromDataService(dsClass, message, arguments); assertThat(result).isEqualTo(expResult); assertThat(arguments).hasSize(1); Locale l = (Locale) arguments.get(0); assertThat(l.getCountry()).isEqualTo("FR"); assertThat(l.getLanguage()).isEqualTo("fr"); }