LocalDocument implements Document<Local> { private String fieldsToJson(Iterable<String> contentFields, Iterable<String> metadataFields) { HashMap<String, Object> map = new HashMap<String, Object>(); if(getID() != null) { map.put(ID_KEY, getID().getID()); } else { map.put(ID_KEY, null); } if(contentFields!=null) { HashMap<String, Object> cmap = new HashMap<String, Object>(); for(String s : contentFields) { cmap.put(s, getContentField(s)); } map.put(CONTENTS_KEY, cmap); } if(metadataFields!=null) { HashMap<String, Object> mmap = new HashMap<String, Object>(); for(String s : metadataFields) { mmap.put(s, getMetadataField(s)); } map.put(METADATA_KEY, mmap); } if(touchedAction) { map.put(ACTION_KEY, getAction()); } return SerializationUtils.toJson(map); } LocalDocument();  LocalDocument(String json);  LocalDocument(LocalDocument doc); @Override Action getAction(); @Override void setAction(Action action); final void markSynced(); boolean isSynced(); @Override boolean hasContentField(String fieldName); @SuppressWarnings("unchecked") @Override boolean hasMetadataField(String fieldName); @Override LocalDocumentID getID(); void setID(DocumentID<Local> id); @SuppressWarnings("unchecked") Map<String, Object> getContentMap(); @SuppressWarnings("unchecked") Map<String, Object> getMetadataMap(); @Override final Object putContentField(String fieldName, Object value); @SuppressWarnings("unchecked") void appendToContentField(String fieldName, Object value); @Override Object getContentField(String fieldName); String getContentFieldAsString(String fieldName); @SuppressWarnings("unchecked") List<String> getContentFieldAsStrings(String fieldName); @SuppressWarnings("unchecked") Map<String, Object> getContentFieldAsMap(String fieldName); long getContentFieldAsLong(String fieldName); long getContentFieldAsLong(String fieldName, long defaultValue); double getContentFieldAsDouble(String fieldName); double getContentFieldAsDouble(String fieldName, double defaultValue); T getContentFieldAsType(String fieldName, Class<? extends T> type); @Override Set<String> getContentFields(); @Override boolean hasErrors(); @SuppressWarnings({ "unchecked", "rawtypes" }) @Override void addError(String from, Throwable t); @SuppressWarnings("unchecked") @Override final void fromJson(String json); @Override void putAll(Document<?> d); @Override String toJson(); @Override boolean isEqual(Document<?> d); @Override String contentFieldsToJson(Iterable<String> contentFields); @Override String metadataFieldsToJson(Iterable<String> metadataFields); String modifiedFieldsToJson(); Map<String, Object> toMap(); @Override void clear(); @Override String toString(); @Override Status getStatus(); Set<String> getTouchedContent(); Set<String> getTouchedMetadata(); boolean isTouchedAction(); @Override Object removeContentField(String key); void setDocumentFileRepository(DocumentFileRepository documentFileRepository); List<String> getFileNames(); DocumentFile<Local> getFile(String fileName); List<DocumentFile<Local>> getFiles(); boolean saveFile(DocumentFile<Local> file); boolean deleteFile(String fileName); void discard(); boolean isDiscarded();  }
@SuppressWarnings("unchecked") @Test public void testFieldsToJSON() throws JsonException { List<String> s = Arrays.asList("blahonga"); String json = test.contentFieldsToJson(s); if ((SerializationUtils.fromJson(json)).size() != 2) { fail("Did not return correct number of fields. Expected '<contents key>' and '_id'"); } if (!(SerializationUtils.fromJson(json)).containsKey(Document.CONTENTS_KEY)) { fail("Did not have a field named CONTENTS_KEY"); } Map<String, Object> m = (Map<String, Object>) (SerializationUtils.fromJson(json)).get(Document.CONTENTS_KEY); if (m.size() != 1) { fail("Did not return 1 contents field, expected 'blahonga'"); } s.set(0, "name"); json = test.contentFieldsToJson(s); if ((SerializationUtils.fromJson(json)).size() != 2) { fail("Did not return correct number of fields. Expected '<contents key>' and '_id'"); } if (!(SerializationUtils.fromJson(json)).containsKey(Document.CONTENTS_KEY)) { fail("Did not have a field named <contents key>"); } m = (Map<String, Object>) (SerializationUtils.fromJson(json)).get(Document.CONTENTS_KEY); if (!m.containsKey("name")) { fail("Did not contain a contents field called 'name'"); } if (!m.get("name").equals("test")) { fail("Did not get correct content for field 'name'"); } List<String> s2 = Arrays.asList("name", "number", "blahonga"); json = test.contentFieldsToJson(s2); m = (Map<String, Object>) (SerializationUtils.fromJson(json)).get(Document.CONTENTS_KEY); if (m.size() != 3) { fail("Did not return correct number of fields. Expected 'test, 'number, 'blahonga' and '_id'"); } if (!m.containsKey("name")) { fail("Did not get the field 'name' returned"); } if (!Comparator.equals(m.get("number"), 1)) { fail("Did not get correct content for field 'number'"); } test.setAction(Action.DELETE); json = test.contentFieldsToJson(s2); if (!(SerializationUtils.fromJson(json)).get(Document.ACTION_KEY).equals(Action.DELETE.toString())) { fail("Did not get 'action' serialized " + (SerializationUtils.fromJson(json)).get(Document.ACTION_KEY)); } }