ArgumentConvertor implements IArgumentConvertor { Object convertArgument(String arg, Type paramType) throws IllegalArgumentException { Object result = null; if (null == arg || "null".equals(arg)) { return result; } logger.debug("Try to convert {} : param = {} : {}", new Object[]{arg, paramType, paramType.getClass()}); try { if (ParameterizedType.class.isInstance(paramType)) { JavaType javaType = getJavaType(paramType); logger.debug("Try to convert '{}' to JavaType : '{}'", arg, paramType); result = getObjectMapper().readValue(arg, javaType); logger.debug("Conversion of '{}' to '{}' : OK", arg, paramType); } else if (Class.class.isInstance(paramType)) { Class cls = (Class) paramType; logger.debug("Try to convert '{}' to Class '{}'", arg, paramType); checkStringArgument(cls, arg); result = getObjectMapper().readValue(arg, cls); logger.debug("Conversion of '{}' to '{}' : OK", arg, paramType); } else { logger.warn("Conversion of '{}' to '{}' not yet supported", arg, paramType); } } catch (IOException ex) { logger.debug("Conversion of '{}' to '{}' failed", arg, paramType); throw new IllegalArgumentException(paramType.toString()); } return result; }  @Override Object convertJsonToJava(String jsonArg, Type paramType, Annotation[] parameterAnnotations);  }
@Test public void testConvertArgumentNull() throws IllegalArgumentException { System.out.println("convertArgument"); Object result = instance.convertArgument(null, Result.class); assertThat(result).isNull(); result = instance.convertArgument("null", Result.class); assertThat(result).isNull(); }
@Test public void testConvertArgument() throws IllegalArgumentException { System.out.println("convertArgument"); Iterator<String> args = Arrays.asList("\"toto\"", "5", "[\"a\",\"b\"]", "[[\"a\", \"b\"],[\"c\", \"d\"]]", "[\"c\",\"d\"]", "{\"a\":1, \"b\":2, \"c\":3}", "{\"integer\":5}").iterator(); Type col = new GenericType<Collection<String>>() { }.getType(); Type map = new GenericType<Map<String, Integer>>() { }.getType(); Type colArray = new GenericType<Collection<String[]>>() { }.getType(); Type array = new String[]{}.getClass(); for (final Type type : new Type[]{String.class, Integer.class, array, colArray, col, map, Result.class}) { String arg = args.next(); Object result = instance.convertArgument(arg, type); assertThat(result).is(new Condition<Object>("" + type) { @Override public boolean matches(Object t) { Class cls; if (type instanceof ParameterizedType) { cls = (Class) ((ParameterizedType) type).getRawType(); } else { cls = (Class) type; } return cls.isInstance(t); } }); } }
@Test public void testConvertArgumentUnsupportedTypes() { Type type = mock(WildcardType.class); Object result = instance.convertArgument("", type); assertThat(result).isNull(); type = mock(GenericArrayType.class); result = instance.convertArgument("", type); assertThat(result).isNull(); type = mock(TypeVariable.class); result = instance.convertArgument("", type); assertThat(result).isNull(); ArgumentCaptor<Type> types = ArgumentCaptor.forClass(Type.class); verify(logger, times(3)).warn(anyString(), anyString(), types.capture()); List<Type> results = types.getAllValues(); assertThat(results).hasSize(3); assertThat(results.get(0)).isInstanceOf(WildcardType.class); assertThat(results.get(1)).isInstanceOf(GenericArrayType.class); assertThat(results.get(2)).isInstanceOf(TypeVariable.class); }
@Test(expected = IllegalArgumentException.class) public void testConvertArgumentFail() throws IllegalArgumentException { System.out.println("convertArgumentFail"); instance.convertArgument("toto", String.class); }