/** Add history to the map */ public Map<String, Object> convert() { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(new LinkedHashMap<>()); List<Object> list = createEventsList(); map.put(fields.getHistory(), list); return map; }
/** * Convert from a relation to a map. * * @param relation the relation to convert * @return a map containing the relation's fields (and history is required) */ public Map<String, Object> convertRelation(Relation relation) { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(Maps.newHashMap()); convertFeatures(map, relation); if (outputHistory && documentHistory != null) { convertHistory(map, relation); } map.put(fields.getExternalId(), relation.getExternalId()); return map; }
/** * Convert from an entity to a map. * * @param entity the entity to convert * @return a map containing the entity's fields (and history is required) */ public Map<String, Object> convertEntity(Entity entity) { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(Maps.newHashMap()); convertFeatures(map, entity); if (outputHistory && documentHistory != null) { convertHistory(map, entity); } map.put(fields.getExternalId(), entity.getExternalId()); return map; }
/** * Convert from an event to a map. * * @param event the relation to convert * @return a map containing the relation's fields (and history is required) */ public Map<String, Object> convertEvent(Event event) { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(Maps.newHashMap()); convertFeatures(map, event); if (outputHistory && documentHistory != null) { convertHistory(map, event); } map.put(fields.getExternalId(), event.getExternalId()); return map; }
@Test public void testRemove() { Map<String, String> delegate = new HashMap<>(); delegate.put("key", "value"); Map<String, String> ignoreEmptyKeyMap = new IgnoreEmptyKeyMapDecorator<>(delegate); ignoreEmptyKeyMap.remove("key"); assertEquals("Size should now be 0", 0, ignoreEmptyKeyMap.size()); }
@Test public void testValues() { Map<String, String> delegate = new HashMap<>(); delegate.put("key", "value"); Map<String, String> ignoreEmptyKeyMap = new IgnoreEmptyKeyMapDecorator<>(delegate); Collection<String> keySet = ignoreEmptyKeyMap.values(); assertEquals("Should have 1 value", 1, keySet.size()); } }
@Test public void testKeySet() { Map<String, String> delegate = new HashMap<>(); delegate.put("key", "value"); Map<String, String> ignoreEmptyKeyMap = new IgnoreEmptyKeyMapDecorator<>(delegate); Set<String> keySet = ignoreEmptyKeyMap.keySet(); assertEquals("Should have 1 key", 1, keySet.size()); }
@Test public void testValueAddedIfKeyExists() { Map<String, String> delegate = new HashMap<>(); delegate.put("key", "value"); Map<String, String> ignoreEmptyKeyMap = new IgnoreEmptyKeyMapDecorator<>(delegate); ignoreEmptyKeyMap.put("key", "newValue"); assertEquals("key should be mapped to newValue", "newValue", ignoreEmptyKeyMap.get("key")); assertEquals("key should be mapped to newValue", "newValue", ignoreEmptyKeyMap.get("key")); }
private Map<String, Object> createEventMap(HistoryEvent historyEvent) { Map<String, Object> linkedHistoryMap = new IgnoreEmptyKeyMapDecorator<>(new LinkedHashMap<>()); if (historyEvent.getRecordable().getInternalId() != entityInternalId) { // Only save the internal id as a reference to entities which aren't this one. linkedHistoryMap.put( fields.getHistoryRecordable(), historyEvent.getRecordable().getInternalId()); } linkedHistoryMap.put(fields.getHistoryAction(), historyEvent.getAction()); linkedHistoryMap.put(fields.getHistoryType(), historyEvent.getEventType()); linkedHistoryMap.put(fields.getHistoryParameters(), historyEvent.getParameters()); linkedHistoryMap.put(fields.getHistoryReferrer(), historyEvent.getReferrer()); linkedHistoryMap.put(fields.getHistoryTimestamp(), historyEvent.getTimestamp()); return linkedHistoryMap; }
@Test public void testMapFunctions() { Map<String, String> delegate = new HashMap<>(); delegate.put("key", "value"); Map<String, String> ignoreEmptyKeyMap = new IgnoreEmptyKeyMapDecorator<>(delegate); assertTrue("should contain value", ignoreEmptyKeyMap.containsValue("value")); ignoreEmptyKeyMap.clear(); assertEquals("Size should now be 0", 0, ignoreEmptyKeyMap.size()); assertFalse("Should not contain key", ignoreEmptyKeyMap.containsKey("key")); }
@Test public void testValueNotAddedIfKeyIsNullOrEmpty() { Map<String, String> delegate = new HashMap<>(); Map<String, String> ignoreEmptyKeyMap = new IgnoreEmptyKeyMapDecorator<>(delegate); assertNull("key should not be mapped", ignoreEmptyKeyMap.put("", "newValue")); assertNull("key should not be mapped", ignoreEmptyKeyMap.put(null, "newValue")); }