AuditEventCSVLine { public static String toCSVLine(AuditEvent auditEvent, boolean isTrackingLocationsEnabled, boolean isTrackingChangesEnabled, boolean isTrackingChangesReasonEnabled) { FormIndex formIndex = auditEvent.getFormIndex(); AuditEvent.AuditEventType auditEventType = auditEvent.getAuditEventType(); long start = auditEvent.getStart(); long end = auditEvent.getEnd(); String latitude = auditEvent.getLatitude(); String longitude = auditEvent.getLongitude(); String accuracy = auditEvent.getAccuracy(); String oldValue = auditEvent.getOldValue(); String newValue = auditEvent.getNewValue(); String user = auditEvent.getUser(); String changeReason = auditEvent.getChangeReason(); String node = formIndex == null || formIndex.getReference() == null ? "" : getXPathPath(formIndex); String string = String.format("%s,%s,%s,%s", auditEventType.getValue(), node, start, end != 0 ? end : ""); if (isTrackingLocationsEnabled) { string += String.format(",%s,%s,%s", latitude, longitude, accuracy); } if (isTrackingChangesEnabled) { string += String.format(",%s,%s", oldValue, newValue); } if (user != null) { if (user.contains(",") || user.contains("\n")) { string += String.format(",%s", getEscapedValueForCsv(user)); } else { string += String.format(",%s", user); } } if (isTrackingChangesReasonEnabled) { if (changeReason != null && (changeReason.contains(",") || changeReason.contains("\n"))) { string += String.format(",%s", getEscapedValueForCsv(changeReason)); } else if (changeReason != null) { string += String.format(",%s", changeReason); } else { string += ","; } } return string; } private  AuditEventCSVLine(); static String toCSVLine(AuditEvent auditEvent, boolean isTrackingLocationsEnabled, boolean isTrackingChangesEnabled, boolean isTrackingChangesReasonEnabled);  }
@Test public void toStringNullValues() { AuditEvent auditEvent = new AuditEvent(START_TIME, QUESTION, getTestFormIndex(), "Old value", null, null); assertNotNull(auditEvent); auditEvent.setLocationCoordinates("", "", ""); assertTrue(auditEvent.isIntervalAuditEventType()); assertFalse(auditEvent.isEndTimeSet()); auditEvent.setEnd(END_TIME); assertTrue(auditEvent.isEndTimeSet()); assertFalse(auditEvent.isLocationAlreadySet()); auditEvent.recordValueChange("New value"); assertEquals("question,/data/text1,1545392727685,1545392728527,,,,Old value,New value", toCSVLine(auditEvent, true, true, false)); }
@Test public void testEventTypes() { AuditEvent auditEvent = new AuditEvent(START_TIME, QUESTION); assertNotNull(auditEvent); assertTrue(auditEvent.isIntervalAuditEventType()); assertEquals("question,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, FORM_START); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("form start,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, END_OF_FORM); assertNotNull(auditEvent); assertTrue(auditEvent.isIntervalAuditEventType()); assertEquals("end screen,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, REPEAT); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("repeat,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, PROMPT_NEW_REPEAT); assertNotNull(auditEvent); assertTrue(auditEvent.isIntervalAuditEventType()); assertEquals("add repeat,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, GROUP); assertNotNull(auditEvent); assertTrue(auditEvent.isIntervalAuditEventType()); assertEquals("group questions,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, BEGINNING_OF_FORM); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("beginning of form,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, FORM_EXIT); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("form exit,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, FORM_RESUME); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("form resume,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, FORM_SAVE); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("form save,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, FORM_FINALIZE); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("form finalize,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, HIERARCHY); assertNotNull(auditEvent); assertTrue(auditEvent.isIntervalAuditEventType()); assertEquals("jump,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, SAVE_ERROR); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("save error,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, FINALIZE_ERROR); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("finalize error,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, CONSTRAINT_ERROR); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("constraint error,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, DELETE_REPEAT); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("delete repeat,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, GOOGLE_PLAY_SERVICES_NOT_AVAILABLE); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("google play services not available,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, LOCATION_PERMISSIONS_GRANTED); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("location permissions granted,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, LOCATION_PERMISSIONS_NOT_GRANTED); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("location permissions not granted,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, LOCATION_TRACKING_ENABLED); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("location tracking enabled,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, LOCATION_TRACKING_DISABLED); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("location tracking disabled,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, LOCATION_PROVIDERS_ENABLED); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("location providers enabled,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, LOCATION_PROVIDERS_DISABLED); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("location providers disabled,,1545392727685,", toCSVLine(auditEvent, false, false, false)); auditEvent = new AuditEvent(START_TIME, UNKNOWN_EVENT_TYPE); assertNotNull(auditEvent); assertFalse(auditEvent.isIntervalAuditEventType()); assertEquals("Unknown AuditEvent Type,,1545392727685,", toCSVLine(auditEvent, false, false, false)); }
@Test public void toString_() { AuditEvent auditEvent = new AuditEvent(START_TIME, QUESTION, getTestFormIndex(), "", null, null); assertNotNull(auditEvent); assertTrue(auditEvent.isIntervalAuditEventType()); assertEquals("question,/data/text1,1545392727685,", toCSVLine(auditEvent, false, false, false)); assertFalse(auditEvent.isEndTimeSet()); auditEvent.setEnd(END_TIME); assertTrue(auditEvent.isEndTimeSet()); assertFalse(auditEvent.isLocationAlreadySet()); assertEquals("question,/data/text1,1545392727685,1545392728527", toCSVLine(auditEvent, false, false, false)); }
@Test public void toString_withLocationCoordinates() { AuditEvent auditEvent = new AuditEvent(START_TIME, QUESTION, getTestFormIndex(), "", null, null); assertNotNull(auditEvent); auditEvent.setLocationCoordinates("54.35202520000001", "18.64663840000003", "10"); assertTrue(auditEvent.isIntervalAuditEventType()); assertFalse(auditEvent.isEndTimeSet()); auditEvent.setEnd(END_TIME); assertTrue(auditEvent.isEndTimeSet()); assertTrue(auditEvent.isLocationAlreadySet()); assertEquals("question,/data/text1,1545392727685,1545392728527,54.35202520000001,18.64663840000003,10", toCSVLine(auditEvent, true, false, false)); }
@Test public void toString_withTrackingChanges() { AuditEvent auditEvent = new AuditEvent(START_TIME, QUESTION, getTestFormIndex(), "First answer", null, null); assertNotNull(auditEvent); assertTrue(auditEvent.isIntervalAuditEventType()); assertFalse(auditEvent.isEndTimeSet()); auditEvent.setEnd(END_TIME); assertTrue(auditEvent.isEndTimeSet()); assertFalse(auditEvent.isLocationAlreadySet()); auditEvent.recordValueChange("Second answer"); assertEquals("question,/data/text1,1545392727685,1545392728527,First answer,Second answer", toCSVLine(auditEvent, false, true, false)); }
@Test public void toString_withLocationCoordinates_andTrackingChanges() { AuditEvent auditEvent = new AuditEvent(START_TIME, QUESTION, getTestFormIndex(), "First answer", null, null); assertNotNull(auditEvent); auditEvent.setLocationCoordinates("54.35202520000001", "18.64663840000003", "10"); assertTrue(auditEvent.isIntervalAuditEventType()); assertFalse(auditEvent.isEndTimeSet()); auditEvent.setEnd(END_TIME); assertTrue(auditEvent.isEndTimeSet()); assertTrue(auditEvent.isLocationAlreadySet()); auditEvent.recordValueChange("Second, answer"); assertEquals("question,/data/text1,1545392727685,1545392728527,54.35202520000001,18.64663840000003,10,First answer,\"Second, answer\"", toCSVLine(auditEvent, true, true, false)); }