BeanQueryMapEncoder implements QueryMapEncoder { @Override public Map<String, Object> encode(Object object) throws EncodeException { try { ObjectParamMetadata metadata = getMetadata(object.getClass()); Map<String, Object> propertyNameToValue = new HashMap<String, Object>(); for (PropertyDescriptor pd : metadata.objectProperties) { Method method = pd.getReadMethod(); Object value = method.invoke(object); if (value != null && value != object) { Param alias = method.getAnnotation(Param.class); String name = alias != null ? alias.value() : pd.getName(); propertyNameToValue.put(name, value); } } return propertyNameToValue; } catch (IllegalAccessException | IntrospectionException | InvocationTargetException e) { throw new EncodeException("Failure encoding object into query map", e); } }  @Override Map<String, Object> encode(Object object);  }
@Test public void testDefaultEncoder_normalClassWithValues() { Map<String, Object> expected = new HashMap<>(); expected.put("foo", "fooz"); expected.put("bar", "barz"); expected.put("fooAppendBar", "foozbarz"); NormalObject normalObject = new NormalObject("fooz", "barz"); Map<String, Object> encodedMap = encoder.encode(normalObject); assertEquals("Unexpected encoded query map", expected, encodedMap); }
@Test public void testDefaultEncoder_normalClassWithOutValues() { NormalObject normalObject = new NormalObject(null, null); Map<String, Object> encodedMap = encoder.encode(normalObject); assertTrue("Non-empty map generated from null getter: " + encodedMap, encodedMap.isEmpty()); }
@Test public void testDefaultEncoder_haveSuperClass() { Map<String, Object> expected = new HashMap<>(); expected.put("page", 1); expected.put("size", 10); expected.put("query", "queryString"); SubClass subClass = new SubClass(); subClass.setPage(1); subClass.setSize(10); subClass.setQuery("queryString"); Map<String, Object> encodedMap = encoder.encode(subClass); assertEquals("Unexpected encoded query map", expected, encodedMap); }
@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()); }