Doc { protected Map<String, Object> toMap() { Map<String, Object> retval = new LinkedHashMap<String, Object>(); retval.put("_index", index); if(!Strings.isNullOrEmpty(type)) { retval.put("_type", type); } retval.put("_id", id); if(!fields.isEmpty()) { retval.put("fields", fields); } if(!Strings.isNullOrEmpty(routing)){ retval.put("_routing", routing); } if(source != null) { retval.put("_source", source); } return retval; } Doc(String index, String id);  Doc(String index, String type, String id); String getIndex(); String getType(); String getId(); Collection<String> getFields(); void addFields(Collection<String> fields); void addField(String field); void setRouting(String routing); String getRouting(); void setSource(Object source); Object getSource();  }
@Test public void testToMapWithOnlyRequiredParameters() { String index = "idx0"; String type = "typo"; String id = "00001_AE"; Map<String, Object> expectedMap = new HashMap<String, Object>(); expectedMap.put("_index", index); expectedMap.put("_type", type); expectedMap.put("_id", id); Doc doc = new Doc(index, type, id); Map<String, Object> actualMap = doc.toMap(); assertEquals(3, actualMap.size()); assertEquals(expectedMap, actualMap); }
@Test public void testConstructionWithNullType() { String index = "idx0"; String id = "00001_AE"; Map<String, Object> expectedMap = new HashMap<String, Object>(); expectedMap.put("_index", index); expectedMap.put("_id", id); Doc doc = new Doc(index, id); Map<String, Object> actualMap = doc.toMap(); assertEquals(2, actualMap.size()); assertEquals(expectedMap, actualMap); }