FieldQueryMapEncoder implements QueryMapEncoder { @Override public Map<String, Object> encode(Object object) throws EncodeException { try { ObjectParamMetadata metadata = getMetadata(object.getClass()); Map<String, Object> fieldNameToValue = new HashMap<String, Object>(); for (Field field : metadata.objectFields) { Object value = field.get(object); if (value != null && value != object) { Param alias = field.getAnnotation(Param.class); String name = alias != null ? alias.value() : field.getName(); fieldNameToValue.put(name, value); } } return fieldNameToValue; } catch (IllegalAccessException e) { throw new EncodeException("Failure encoding object into query map", e); } }  @Override Map<String, Object> encode(Object object);  }
@Test public void testDefaultEncoder_normalClassWithValues() { final Map<String, Object> expected = new HashMap<>(); expected.put("foo", "fooz"); expected.put("bar", "barz"); final NormalObject normalObject = new NormalObject("fooz", "barz"); final Map<String, Object> encodedMap = encoder.encode(normalObject); assertEquals("Unexpected encoded query map", expected, encodedMap); }
@Test public void testDefaultEncoder_normalClassWithOutValues() { final NormalObject normalObject = new NormalObject(null, null); final Map<String, Object> encodedMap = encoder.encode(normalObject); assertTrue("Non-empty map generated from null getter: " + encodedMap, encodedMap.isEmpty()); }
@Test public void testDefaultEncoder_withOverriddenParamName() { HashSet<Object> expectedNames = new HashSet<>(); expectedNames.add("fooAlias"); expectedNames.add("bar"); final NormalObjectWithOverriddenParamName normalObject = new NormalObjectWithOverriddenParamName("fooz", "barz"); final Map<String, Object> encodedMap = encoder.encode(normalObject); assertEquals("@Param ignored", expectedNames, encodedMap.keySet()); }