DefaultResourceParameterResolver implements ResourceParameterResolver { @Override public Object getResource(final MethodParameter parameter, final Map<String, Object> parameters) { Object resource = null; try { Type genericParameterType = parameter.getGenericParameterType(); ParameterizedType parameterizedType = (ParameterizedType) genericParameterType; Class<?> clz = (Class<?>) parameterizedType.getActualTypeArguments()[0]; resource = clz.newInstance(); enhanceParameters(resource, parameters); BeanUtils.populate(resource, parameters); } catch (Exception e) { LOGGER.info(e.getMessage(), e); throw new IllegalArgumentException("Some problems with resolving parameters for filtering. Please, contact dev team for fixing this issue"); } return resource; }  @PostConstruct void init(); @Override Object getResource(final MethodParameter parameter, final Map<String, Object> parameters); static List<Field> getAllFields(final List<Field> fields, final Class<?> type);  }
@Test public void testGetResource() throws Exception { Map<String, Object> parameters = new HashMap<>(); Integer resident = 1; parameters.put("resident", resident); PersonResourceList expected = new PersonResourceList(); expected.setResident(resident); when(param.getGenericParameterType()).thenReturn(type); when(type.getActualTypeArguments()).thenReturn(types); Object actual = unit.getResource(param, parameters); verify(param).getGenericParameterType(); verify(type).getActualTypeArguments(); assertEquals(expected, actual); }
@Test public void testGetResourceWithMultipleFields() throws Exception { String id = "1"; String name = "Ivan"; Map<String, Object> parameters = new HashMap<>(); parameters.put("name", name); PersonResourceList expected = new PersonResourceList(); expected.setName(Arrays.asList(name)); when(param.getGenericParameterType()).thenReturn(type); when(type.getActualTypeArguments()).thenReturn(types); Object actual = unit.getResource(param, parameters); verify(param).getGenericParameterType(); verify(type).getActualTypeArguments(); assertEquals(expected, actual); }
@Test public void testGetResourceWithMultipleFieldsId() throws Exception { String id = "1"; Map<String, Object> parameters = new HashMap<>(); parameters.put("id", id); PersonResourceList expected = new PersonResourceList(); expected.setId(Arrays.asList(Long.valueOf(id))); when(param.getGenericParameterType()).thenReturn(type); when(type.getActualTypeArguments()).thenReturn(types); Object actual = unit.getResource(param, parameters); verify(param).getGenericParameterType(); verify(type).getActualTypeArguments(); assertEquals(expected, actual); }
@Test public void testGetResourceWithMultipleFieldsAndReadyParameters() throws Exception { String id = "1"; String name = "ivan"; List<String> names = Arrays.asList(name); Map<String, Object> parameters = new HashMap<>(); parameters.put("name", names); PersonResourceList expected = new PersonResourceList(); expected.setName(names); when(param.getGenericParameterType()).thenReturn(type); when(type.getActualTypeArguments()).thenReturn(types); Object actual = unit.getResource(param, parameters); verify(param).getGenericParameterType(); verify(type).getActualTypeArguments(); assertEquals(expected, actual); }