ListFieldProcessor implements FieldProcessor { @Override public Object mapResourceToField(Resource resource, ValueMap valueMap, Field field, String propertyName) { if (valueMap == null) { return null; } Object value = valueMap.get(propertyName); if (value == null) { return null; } if (value.getClass().isArray()) { return Arrays.asList((Object[]) value); } else { return Arrays.asList(value); } }  @Override boolean accepts(Resource resource, Field field); @Override Object mapResourceToField(Resource resource, ValueMap valueMap, Field field, String propertyName);  }
@Test public void testMapResourceToField() { String[] resultArray = new String[] { "asd", "qwe" }; Mockito.when(valueMap.get(propertyName)).thenReturn(resultArray); Object result = processor.mapResourceToField(resource, valueMap, null, propertyName); Assert.assertTrue(result instanceof Collection); Collection<?> coll = (Collection<?>) result; Assert.assertEquals(2, coll.size()); Iterator<?> iterator = coll.iterator(); Assert.assertEquals("asd", iterator.next()); Assert.assertEquals("qwe", iterator.next()); }
@Test public void testMapResourceToField_emptyList() { String[] resultArray = new String[] {}; Mockito.when(valueMap.get(propertyName)).thenReturn(resultArray); Object result = processor.mapResourceToField(resource, valueMap, null, propertyName); Assert.assertTrue(result instanceof Collection); Collection<?> coll = (Collection<?>) result; Assert.assertEquals(0, coll.size()); }
@Test public void testMapResourceToField_notArray() { String resultProperty = "result"; Mockito.when(valueMap.get(propertyName)).thenReturn(resultProperty); Object result = processor.mapResourceToField(resource, valueMap, null, propertyName); Assert.assertTrue(result instanceof Collection); Collection<?> coll = (Collection<?>) result; Assert.assertEquals(1, coll.size()); Assert.assertEquals("result", coll.iterator().next()); }
@Test public void testMapResourceToField_nullValue() { String resultProperty = "result"; Mockito.when(valueMap.get(propertyName)).thenReturn(resultProperty); Object result = processor.mapResourceToField(resource, null, null, propertyName); Assert.assertNull(result); Mockito.when(valueMap.get(propertyName)).thenReturn(null); result = processor.mapResourceToField(resource, valueMap, null, propertyName); Assert.assertNull(result); }