History extends JSONObject implements Parcelable { public static History buildHistoryBetween(RapidFtrApplication rapidFtrApplication, BaseModel originalModel, BaseModel updatedModel) throws JSONException { History history = new History(); addChangesForOldValues(originalModel, updatedModel, history); addChangesForNewValues(originalModel, updatedModel, history); User currentUser = rapidFtrApplication.getCurrentUser(); String organisation = currentUser.getOrganisation(); String userName = currentUser.getUserName(); history.put(History.USER_NAME, userName); history.put(History.USER_ORGANISATION, organisation); history.put(History.DATETIME, RapidFtrDateTime.now().defaultFormat()); return history; } 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 shouldNotIncludeAddingEmptyValuesInHistory() throws JSONException { BaseModel originalModel = new BaseModel("{\"change1\":\"Foo Bar\",\"unique_identifier\":\"1\"}"); BaseModel changedModel = new BaseModel("{\"change1\":\"Foo Bar1\",\"addition\":\"\",\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); JSONObject changes = (JSONObject) history.get(History.CHANGES); assertFalse(changes.has("addition")); }
@Test public void shouldNotIncludeEmptyChangesInHistory() throws JSONException { BaseModel originalModel = new BaseModel("{\"change1\":\"Foo Bar\",\"addition\":\"\",\"unique_identifier\":\"1\"}"); BaseModel changedModel = new BaseModel("{\"change1\":\"Foo Bar1\",\"addition\":\"\",\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); JSONObject changes = (JSONObject) history.get(History.CHANGES); assertFalse(changes.has("addition")); }
@Test public void shouldNotIncludeUnecessaryFieldsInChanges() throws JSONException, ParseException { BaseModel originalModel = new BaseModel("{\"change1\":\"Foo Bar\"," + "\"deletion\":\"old stuff\"," + "\"change2\":\"Foo Bar\"," + "\"unique_identifier\":\"1\"," + "\"last_updated_at\":\"2013-12-12 11:11:11\"," + "\"last_synced_at\":\"2013-12-12 11:11:11\"," + "\"synced\":\"true\"}"); originalModel.addHistory(new History("{\"changes\":{\"change1\":{\"from\":\"Foo Bar\",\"to\":\"old stuff\"}}}")); BaseModel changedModel = new BaseModel("{\"change1\":\"Foo Bar1\"," + "\"addition\":\"new stuff\"," + "\"change2\":\"Foo Bar2\"," + "\"unique_identifier\":\"1\"," + "\"last_updated_at\":\"2014-01-01 00:00:00\"," + "\"last_synced_at\":\"2014-01-01 00:00:00\"," + "\"synced\":\"false\"}"); changedModel.addHistory(new History("{\"changes\":{\"something_else\":{\"from\":\"Foo Bar\",\"to\":\"old stuff\"}}}")); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); JSONObject changes = (JSONObject) history.get(History.CHANGES); assertFalse(changes.has(History.HISTORIES)); assertFalse(changes.has(Database.ChildTableColumn.synced.getColumnName())); assertFalse(changes.has(Database.ChildTableColumn.last_updated_at.getColumnName())); assertFalse(changes.has(Database.ChildTableColumn.last_synced_at.getColumnName())); assertTrue(changedModel.has(History.HISTORIES)); assertTrue(originalModel.has(History.HISTORIES)); }
@Test public void shouldCompareObjectsAndReturnHistory() throws JSONException { BaseModel originalModel = new BaseModel("{\"child_name\":\"Foo Bar\",\"unique_identifier\":\"1\"}"); BaseModel changedModel = new BaseModel("{\"child_name\":\"Foo Bar124\",\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); String expectedJSON = "{\"child_name\":{\"from\":\"Foo Bar\", \"to\":\"Foo Bar124\"}}"; JSONAssert.assertEquals(expectedJSON, history.getString(History.CHANGES), true); }
@Test public void shouldIncludeNewFieldsInHistory() throws JSONException { BaseModel originalModel = new BaseModel("{\"unique_identifier\":\"1\"}"); BaseModel changedModel = new BaseModel("{\"child_name\":\"Foo Bar\",\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); String expectedJSON = "{\"child_name\":{\"from\":\"\", \"to\":\"Foo Bar\"}}"; JSONAssert.assertEquals(expectedJSON, history.getString(History.CHANGES), true); }
@Test public void shouldIncludeDeletedFieldsInHistory() throws JSONException { BaseModel originalModel = new BaseModel("{\"child_name\":\"Foo Bar\",\"unique_identifier\":\"1\"}"); BaseModel changedModel = new BaseModel("{\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); String expectedJSON = "{\"child_name\":{\"from\":\"Foo Bar\", \"to\":\"\"}}"; JSONAssert.assertEquals(expectedJSON, history.getString(History.CHANGES), true); }
@Test public void shouldHandleComplexChanges() throws JSONException { BaseModel originalModel = new BaseModel("{\"change1\":\"Foo Bar\",\"deletion\":\"old stuff\",\"change2\":\"Foo Bar\",\"unique_identifier\":\"1\"}"); BaseModel changedModel = new BaseModel("{\"change1\":\"Foo Bar1\",\"addition\":\"new stuff\",\"change2\":\"Foo Bar2\",\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); String expectedJSON = "{\"change1\":{\"from\":\"Foo Bar\",\"to\":\"Foo Bar1\"}," + "\"change2\":{\"from\":\"Foo Bar\",\"to\":\"Foo Bar2\"}," + "\"deletion\":{\"from\":\"old stuff\",\"to\":\"\"}," + "\"addition\":{\"from\":\"\",\"to\":\"new stuff\"}" + "}"; JSONAssert.assertEquals(expectedJSON, history.getString(History.CHANGES), true); }
@Test public void shouldAddUserName() throws JSONException { RapidFtrApplication.getApplicationInstance().setCurrentUser(new User("user_name")); BaseModel originalModel = new BaseModel("{\"change1\":\"Foo Bar\",\"deletion\":\"old stuff\",\"change2\":\"Foo Bar\",\"unique_identifier\":\"1\"}"); BaseModel changedModel = new BaseModel("{\"change1\":\"Foo Bar1\",\"addition\":\"new stuff\",\"change2\":\"Foo Bar2\",\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); assertEquals("user_name", history.getString(History.USER_NAME)); }
@Test public void shouldAddUserOrganisationToHistory() throws JSONException { User user = new User("user_name"); user.setOrganisation("UNICEF"); RapidFtrApplication.getApplicationInstance().setCurrentUser(user); BaseModel originalModel = new BaseModel("{\"change1\":\"Foo Bar\",\"deletion\":\"old stuff\",\"change2\":\"Foo Bar\",\"unique_identifier\":\"1\"}"); BaseModel changedModel = new BaseModel("{\"change1\":\"Foo Bar1\",\"addition\":\"new stuff\",\"change2\":\"Foo Bar2\",\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); assertEquals("UNICEF", history.getString(History.USER_ORGANISATION)); }
@Test public void shouldAddDateTimeToHistory() throws JSONException, ParseException { BaseModel originalModel = new BaseModel("{\"change1\":\"Foo Bar\",\"deletion\":\"old stuff\",\"change2\":\"Foo Bar\",\"unique_identifier\":\"1\"}"); BaseModel changedModel = new BaseModel("{\"change1\":\"Foo Bar1\",\"addition\":\"new stuff\",\"change2\":\"Foo Bar2\",\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); Calendar updatedAt = RapidFtrDateTime.getDateTime(history.getString(History.DATETIME)); assert(Calendar.getInstance().getTimeInMillis() - updatedAt.getTimeInMillis() < 1000); }
@Test public void shouldNotIncludeRemovingEmptyValuesInHistory() throws JSONException { BaseModel originalModel = new BaseModel("{\"change1\":\"Foo Bar\",\"unique_identifier\":\"1\",\"deletionz\":\"\"}"); BaseModel changedModel = new BaseModel("{\"change1\":\"Foo Bar1\",\"unique_identifier\":\"1\"}"); History history = History.buildHistoryBetween(rapidFtrApplication, originalModel, changedModel); JSONObject changes = (JSONObject) history.get(History.CHANGES); assertFalse(changes.has("deletion")); }