BaseModel extends JSONObject implements Parcelable { public void addHistory(History history) throws JSONException { boolean meaningfulHistory = history.has(History.CHANGES) && history.get(History.CHANGES) != null; if(meaningfulHistory) { if(has(History.HISTORIES)) { accumulate(History.HISTORIES, history); } else { put(History.HISTORIES, new JSONArray(Lists.newArrayList(history))); } } } BaseModel(String content);  BaseModel();  BaseModel(String id, String owner, String content); String getUniqueId(); String getInternalId(); void setUniqueId(String id); String getCreatedAt(); String getName(); void setName(String childName); String getLastUpdatedAt(); Long lastUpdatedAtInMillis(); void setLastUpdatedAt(String lastUpdatedAt); void generateUniqueId(); void setCreatedBy(String owner); String getJsonString(); boolean isNew(); @Override JSONArray names(); @Override int describeContents(); @Override void writeToParcel(Parcel parcel, int flags); void removeFromJSONArray(String key, String value); JSONObject values(); String getCreatedBy(); @Override JSONObject put(String key, Object value); void addToJSONArray(String key, Object element); String getString(String key); String getString(String key, String defaultValue); void setOrganisation(String userOrg); void setSynced(boolean synced); boolean isSynced(); String getShortId(); List<BaseModel> getPotentialMatchingModels(PotentialMatchRepository potentialMatchRepo, ChildRepository childRepo, EnquiryRepository enquiryRepository); String getApiPath(); String getApiParameter(); @Override boolean equals(Object other); List<BaseModel> getConfirmedMatchingModels(PotentialMatchRepository potentialMatchRepository, ChildRepository childRepository, EnquiryRepository enquiryRepository); void addHistory(History history); void setLastSyncedAt(String lastSyncedAt); String getLastSyncedAt(); String getSyncLog(); void setSyncLog(String syncLog1); String getRecordedAudio(); void putRecordedAudio(String fileName); static final ObjectMapper JSON_MAPPER; static final String FIELD_INTERNAL_ID; static final String FIELD_REVISION_ID; static final String EMPTY_STRING; }
@Test public void shouldAddHistoriesToNewModel() throws JSONException { BaseModel model = new BaseModel(); History history = History.buildHistoryBetween(rapidFtrApplication, model, new BaseModel("{\"some_field\":\"Values\"}")); model.addHistory(history); JSONArray jsonArray = (JSONArray) model.get(History.HISTORIES); assertThat(jsonArray.length(), is(1)); JSONObject changes = (JSONObject) ((JSONObject) jsonArray.get(0)).get(History.CHANGES); assert(changes.has("some_field")); }
@Test public void shouldAppendHistoriesToModel() throws JSONException { BaseModel model = new BaseModel(); History history = History.buildHistoryBetween(rapidFtrApplication, model, new BaseModel("{\"some_field\":\"Values\"}")); model.addHistory(history); history = History.buildHistoryBetween(rapidFtrApplication, model, new BaseModel("{\"some_other_field\":\"Values\"}")); model.addHistory(history); JSONArray jsonArray = (JSONArray) model.get(History.HISTORIES); assertThat(jsonArray.length(), is(2)); JSONObject changes = (JSONObject) ((JSONObject) jsonArray.get(0)).get(History.CHANGES); assert(changes.has("some_field")); changes = (JSONObject) ((JSONObject) jsonArray.get(1)).get(History.CHANGES); assert(changes.has("some_other_field")); }
@Test public void shouldNotAddEmptyHistory() throws JSONException { BaseModel model = new BaseModel(); History history = History.buildHistoryBetween(rapidFtrApplication, model, model); model.addHistory(history); assertFalse(history.has(History.HISTORIES)); }