AsyncTaskAuditEventWriter implements AuditEventLogger.AuditEventWriter { @Override public void writeEvents(List<AuditEvent> auditEvents) { AuditEvent[] auditEventArray = auditEvents.toArray(new AuditEvent[0]); saveTask = new AuditEventSaveTask(file, isLocationEnabled, isTrackingChangesEnabled, isUserRequired, isTrackChangesReasonEnabled).execute(auditEventArray); } AsyncTaskAuditEventWriter(@NonNull File file, boolean isLocationEnabled, boolean isTrackingChangesEnabled, boolean isUserRequired, boolean isTrackChangesReasonEnabled); @Override void writeEvents(List<AuditEvent> auditEvents); @Override boolean isWriting();  }
@Test public void saveAuditWithLocation() throws Exception { AsyncTaskAuditEventWriter writer = new AsyncTaskAuditEventWriter(auditFile, true, false, false, false); writer.writeEvents(getSampleAuditEventsWithLocations()); String expectedAuditContent = FileUtils.readFileToString(auditFile); String expectedData = "event,node,start,end,latitude,longitude,accuracy\n" + "form start,,1548106927319,,,,\n" + "location tracking enabled,,548108908250,,,,\n" + "location permissions granted,,548108908255,,,,\n" + "location providers enabled,,548108908259,,,,\n" + "question,/data/q1,1548106927323,1548106930112,54.4112062,18.5896652,30.716999053955078\n" + "add repeat,/data/g1[1],1548106930118,1548106931611,54.4112062,18.5896652,30.716999053955078\n" + "question,/data/g1[1]/q2,1548106931612,1548106937122,54.4112062,18.5896652,30.716999053955078\n" + "add repeat,/data/g1[2],1548106937123,1548106938276,54.4112062,18.5896652,30.716999053955078\n" + "question,/data/g1[2]/q2,1548106938277,1548106948127,54.4112062,18.5896652,30.716999053955078\n" + "add repeat,/data/g1[3],1548106948128,1548106949446,54.4112062,18.5896652,30.716999053955078\n" + "end screen,,1548106949448,1548106953601,54.4112062,18.5896652,30.716999053955078\n" + "form save,,1548106953600,,54.4112062,18.5896652,30.716999053955078\n" + "form exit,,1548106953601,,54.4112062,18.5896652,30.716999053955078\n" + "form finalize,,1548106953601,,54.4112062,18.5896652,30.716999053955078\n"; assertEquals(expectedData, expectedAuditContent); }
@Test public void saveAuditWithLocationAndTrackingChanges() throws Exception { AsyncTaskAuditEventWriter writer = new AsyncTaskAuditEventWriter(auditFile, true, true, false, false); writer.writeEvents(getSampleAuditEventsWithLocationsAndTrackingChanges()); String expectedAuditContent = FileUtils.readFileToString(auditFile); String expectedData = "event,node,start,end,latitude,longitude,accuracy,old-value,new-value\n" + "form start,,1548106927319,,,,,,\n" + "location tracking enabled,,548108908250,,,,,,\n" + "location permissions granted,,548108908255,,,,,,\n" + "location providers enabled,,548108908259,,,,,,\n" + "question,/data/q1,1548106927323,1548106930112,54.4112062,18.5896652,30.716999053955078,Old value,New Value\n" + "add repeat,/data/g1[1],1548106930118,1548106931611,54.4112062,18.5896652,30.716999053955078,,\n" + "end screen,,1548106949448,1548106953601,54.4112062,18.5896652,30.716999053955078,,\n" + "form save,,1548106953600,,54.4112062,18.5896652,30.716999053955078,,\n" + "form exit,,1548106953601,,54.4112062,18.5896652,30.716999053955078,,\n" + "form finalize,,1548106953601,,54.4112062,18.5896652,30.716999053955078,,\n"; assertEquals(expectedData, expectedAuditContent); }
@Test public void saveAuditWithUser() throws Exception { AsyncTaskAuditEventWriter writer = new AsyncTaskAuditEventWriter(auditFile, false, false, true, false); writer.writeEvents(getSampleAuditEventsWithUser()); String expectedAuditContent = FileUtils.readFileToString(auditFile); String expectedData = "event,node,start,end,user\n" + "form start,,1548106927319,,User1\n" + "question,/data/q1,1548106927323,1548106930112,User1\n" + "add repeat,/data/g1[1],1548106930118,1548106931611,User1\n" + "question,/data/g1[1]/q2,1548106931612,1548106937122,User1\n" + "add repeat,/data/g1[2],1548106937123,1548106938276,User1\n" + "question,/data/g1[2]/q2,1548106938277,1548106948127,User1\n" + "add repeat,/data/g1[3],1548106948128,1548106949446,User1\n" + "end screen,,1548106949448,1548106953601,User1\n" + "form save,,1548106953600,,User1\n" + "form exit,,1548106953601,,User1\n" + "form finalize,,1548106953601,,User1\n"; assertEquals(expectedData, expectedAuditContent); }
@Test public void saveAuditWithChangeReason() throws Exception { AsyncTaskAuditEventWriter writer = new AsyncTaskAuditEventWriter(auditFile, false, false, false, true); writer.writeEvents(asList( new AuditEvent(1548108900606L, FORM_RESUME, null, null, null, null), new AuditEvent(1548108900606L, CHANGE_REASON, null, null, null, "A good reason") )); String auditContent = FileUtils.readFileToString(auditFile); String expectedData = "event,node,start,end,change-reason\n" + "form resume,,1548108900606,,\n" + "change reason,,1548108900606,,A good reason\n"; assertEquals(expectedData, auditContent); }
@Test public void whenChangeReasonHasCommaOrQuotes_escapesThem() throws Exception { AsyncTaskAuditEventWriter writer = new AsyncTaskAuditEventWriter(auditFile, false, false, false, true); writer.writeEvents(asList( new AuditEvent(1548108900606L, FORM_RESUME, null, null, null, null), new AuditEvent(1548108900606L, CHANGE_REASON, null, null, null, "A \"good\", reason") )); String auditContent = FileUtils.readFileToString(auditFile); String expectedData = "event,node,start,end,change-reason\n" + "form resume,,1548108900606,,\n" + "change reason,,1548108900606,,\"A \"\"good\"\", reason\"\n"; assertEquals(expectedData, auditContent); }
@Test public void whenUserHasCommaOrQuotes_escapesThem() throws Exception { AsyncTaskAuditEventWriter writer = new AsyncTaskAuditEventWriter(auditFile, false, false, true, false); List<AuditEvent> auditEvents = getSampleAuditEventsWithUser().subList(0, 1); auditEvents.get(0).setUser("User,\"1\""); writer.writeEvents(auditEvents); String expectedAuditContent = FileUtils.readFileToString(auditFile); String expectedData = "event,node,start,end,user\n" + "form start,,1548106927319,,\"User,\"\"1\"\"\"\n"; assertEquals(expectedData, expectedAuditContent); }
@Test public void whenAppUpdatedBetweenInstances_updatesHeader() throws Exception { AsyncTaskAuditEventWriter writer = new AsyncTaskAuditEventWriter(auditFile, false, false, false, false); writer.writeEvents(getSampleAuditEventsWithoutLocations()); String expectedAuditContent = FileUtils.readFileToString(auditFile); String expectedData = "event,node,start,end\n" + "form start,,1548106927319,\n" + "question,/data/q1,1548106927323,1548106930112\n" + "add repeat,/data/g1[1],1548106930118,1548106931611\n" + "question,/data/g1[1]/q2,1548106931612,1548106937122\n" + "add repeat,/data/g1[2],1548106937123,1548106938276\n" + "question,/data/g1[2]/q2,1548106938277,1548106948127\n" + "add repeat,/data/g1[3],1548106948128,1548106949446\n" + "end screen,,1548106949448,1548106953601\n" + "form save,,1548106953600,\n" + "form exit,,1548106953601,\n" + "form finalize,,1548106953601,\n"; assertEquals(expectedData, expectedAuditContent); writer = new AsyncTaskAuditEventWriter(auditFile, true, false, false, false); writer.writeEvents(getMoreSampleAuditEventsWithLocations()); expectedAuditContent = FileUtils.readFileToString(auditFile); String expectedData2 = "event,node,start,end,latitude,longitude,accuracy\n" + "form start,,1548106927319,\n" + "question,/data/q1,1548106927323,1548106930112\n" + "add repeat,/data/g1[1],1548106930118,1548106931611\n" + "question,/data/g1[1]/q2,1548106931612,1548106937122\n" + "add repeat,/data/g1[2],1548106937123,1548106938276\n" + "question,/data/g1[2]/q2,1548106938277,1548106948127\n" + "add repeat,/data/g1[3],1548106948128,1548106949446\n" + "end screen,,1548106949448,1548106953601\n" + "form save,,1548106953600,\n" + "form exit,,1548106953601,\n" + "form finalize,,1548106953601,\n" + "form resume,,1548108900606,,54.4112062,18.5896652,30.716999053955078\n" + "jump,,1548108906276,1548108908206,54.4112062,18.5896652,30.716999053955078\n" + "location tracking enabled,,548108908250,,,,\n" + "location permissions granted,,548108908255,,,,\n" + "location providers enabled,,548108908259,,,,\n" + "end screen,,1548108908285,1548108909730,54.4112062,18.5896652,30.716999053955078\n" + "form save,,1548108909730,,54.4112062,18.5896652,30.716999053955078\n" + "form exit,,1548108909730,,54.4112062,18.5896652,30.716999053955078\n" + "form finalize,,1548108909731,,54.4112062,18.5896652,30.716999053955078\n"; assertEquals(expectedData2, expectedAuditContent); writer = new AsyncTaskAuditEventWriter(auditFile, true, true, false, false); writer.writeEvents(getMoreSampleAuditEventsWithLocationsAndTrackingChanges()); expectedAuditContent = FileUtils.readFileToString(auditFile); String expectedData3 = "event,node,start,end,latitude,longitude,accuracy,old-value,new-value\n" + "form start,,1548106927319,\n" + "question,/data/q1,1548106927323,1548106930112\n" + "add repeat,/data/g1[1],1548106930118,1548106931611\n" + "question,/data/g1[1]/q2,1548106931612,1548106937122\n" + "add repeat,/data/g1[2],1548106937123,1548106938276\n" + "question,/data/g1[2]/q2,1548106938277,1548106948127\n" + "add repeat,/data/g1[3],1548106948128,1548106949446\n" + "end screen,,1548106949448,1548106953601\n" + "form save,,1548106953600,\n" + "form exit,,1548106953601,\n" + "form finalize,,1548106953601,\n" + "form resume,,1548108900606,,54.4112062,18.5896652,30.716999053955078\n" + "jump,,1548108906276,1548108908206,54.4112062,18.5896652,30.716999053955078\n" + "location tracking enabled,,548108908250,,,,\n" + "location permissions granted,,548108908255,,,,\n" + "location providers enabled,,548108908259,,,,\n" + "end screen,,1548108908285,1548108909730,54.4112062,18.5896652,30.716999053955078\n" + "form save,,1548108909730,,54.4112062,18.5896652,30.716999053955078\n" + "form exit,,1548108909730,,54.4112062,18.5896652,30.716999053955078\n" + "form finalize,,1548108909731,,54.4112062,18.5896652,30.716999053955078\n" + "form resume,,1548108900606,,54.4112062,18.5896652,30.716999053955078,,\n" + "question,,1548108900700,,54.4112062,18.5896652,30.716999053955078,Old value,New value\n" + "question,,1548108903100,,54.4112062,18.5896652,30.716999053955078,\"Old value, with comma\",New value\n" + "question,,1548108903101,,54.4112062,18.5896652,30.716999053955078,\"Old value \n with linebreak\",\"New value \n with linebreak and \"\"quotes\"\"\"\n" + "question,,1548108904200,,54.4112062,18.5896652,30.716999053955078,Old value,\"New value, with comma\"\n" + "form save,,1548108909730,,54.4112062,18.5896652,30.716999053955078,,\n" + "form exit,,1548108909730,,54.4112062,18.5896652,30.716999053955078,,\n" + "form finalize,,1548108909731,,54.4112062,18.5896652,30.716999053955078,,\n"; assertEquals(expectedData3, expectedAuditContent); writer = new AsyncTaskAuditEventWriter(auditFile, true, true, true, false); writer.writeEvents(getMoreSampleAuditEventsWithLocationsAndTrackingChangesAndUser()); expectedAuditContent = FileUtils.readFileToString(auditFile); String expectedData4 = "event,node,start,end,latitude,longitude,accuracy,old-value,new-value,user\n" + "form start,,1548106927319,\n" + "question,/data/q1,1548106927323,1548106930112\n" + "add repeat,/data/g1[1],1548106930118,1548106931611\n" + "question,/data/g1[1]/q2,1548106931612,1548106937122\n" + "add repeat,/data/g1[2],1548106937123,1548106938276\n" + "question,/data/g1[2]/q2,1548106938277,1548106948127\n" + "add repeat,/data/g1[3],1548106948128,1548106949446\n" + "end screen,,1548106949448,1548106953601\n" + "form save,,1548106953600,\n" + "form exit,,1548106953601,\n" + "form finalize,,1548106953601,\n" + "form resume,,1548108900606,,54.4112062,18.5896652,30.716999053955078\n" + "jump,,1548108906276,1548108908206,54.4112062,18.5896652,30.716999053955078\n" + "location tracking enabled,,548108908250,,,,\n" + "location permissions granted,,548108908255,,,,\n" + "location providers enabled,,548108908259,,,,\n" + "end screen,,1548108908285,1548108909730,54.4112062,18.5896652,30.716999053955078\n" + "form save,,1548108909730,,54.4112062,18.5896652,30.716999053955078\n" + "form exit,,1548108909730,,54.4112062,18.5896652,30.716999053955078\n" + "form finalize,,1548108909731,,54.4112062,18.5896652,30.716999053955078\n" + "form resume,,1548108900606,,54.4112062,18.5896652,30.716999053955078,,\n" + "question,,1548108900700,,54.4112062,18.5896652,30.716999053955078,Old value,New value\n" + "question,,1548108903100,,54.4112062,18.5896652,30.716999053955078,\"Old value, with comma\",New value\n" + "question,,1548108903101,,54.4112062,18.5896652,30.716999053955078,\"Old value \n with linebreak\",\"New value \n with linebreak and \"\"quotes\"\"\"\n" + "question,,1548108904200,,54.4112062,18.5896652,30.716999053955078,Old value,\"New value, with comma\"\n" + "form save,,1548108909730,,54.4112062,18.5896652,30.716999053955078,,\n" + "form exit,,1548108909730,,54.4112062,18.5896652,30.716999053955078,,\n" + "form finalize,,1548108909731,,54.4112062,18.5896652,30.716999053955078,,\n" + "form resume,,1548108900606,,54.4112062,18.5896652,30.716999053955078,,,User1\n" + "question,,1548108900700,,54.4112062,18.5896652,30.716999053955078,Old value,New value,User1\n" + "question,,1548108903100,,54.4112062,18.5896652,30.716999053955078,\"Old value, with comma\",New value,User1\n" + "question,,1548108903101,,54.4112062,18.5896652,30.716999053955078,\"Old value \n with linebreak\",\"New value \n with linebreak and \"\"quotes\"\"\",User1\n" + "question,,1548108904200,,54.4112062,18.5896652,30.716999053955078,Old value,\"New value, with comma\",User1\n" + "form save,,1548108909730,,54.4112062,18.5896652,30.716999053955078,,,User1\n" + "form exit,,1548108909730,,54.4112062,18.5896652,30.716999053955078,,,User1\n" + "form finalize,,1548108909731,,54.4112062,18.5896652,30.716999053955078,,,User1\n"; assertEquals(expectedData4, expectedAuditContent); }