History extends JSONObject implements Parcelable { public static History buildCreationHistory(BaseModel model, User user) throws JSONException { History creationHistory = new History(); creationHistory.put(USER_NAME, user.getUserName()); creationHistory.put(USER_ORGANISATION, user.getOrganisation()); creationHistory.put(DATETIME, RapidFtrDateTime.now()); JSONObject createdChange = new JSONObject().put(CREATED, RapidFtrDateTime.now().toString()); JSONObject changes = new JSONObject(); changes.put(model.getClass().getSimpleName().toLowerCase(), createdChange); creationHistory.put(CHANGES, changes); return creationHistory; } History(String jsonSource);  History(); @Override int describeContents(); @Override void writeToParcel(Parcel parcel, int flags); static History buildHistoryBetween(RapidFtrApplication rapidFtrApplication, BaseModel originalModel, BaseModel updatedModel); void addChange(String key, JSONObject changes); static History buildCreationHistory(BaseModel model, User user); static final String HISTORIES; static final String USER_NAME; static final String USER_ORGANISATION; static final String DATETIME; static final String CHANGES; static final String FROM; static final String TO; static final String CREATED; }
@Test public void shouldBuildCreationHistory() throws JSONException { Enquiry enquiry = new Enquiry(); User user = new User("field_worker"); user.setOrganisation("UNICEF"); History creationHistory = History.buildCreationHistory(enquiry, user); assertEquals(creationHistory.get(History.USER_NAME), "field_worker"); assertEquals(creationHistory.get(History.USER_ORGANISATION), "UNICEF"); assert(creationHistory.has(History.DATETIME)); JSONObject changes = (JSONObject) creationHistory.get(History.CHANGES); JSONObject enquiryChange = (JSONObject) changes.get("enquiry"); assert(enquiryChange.has(History.CREATED)); }