diff --git a/src/org/labkey/test/util/AuditLogHelper.java b/src/org/labkey/test/util/AuditLogHelper.java index a5483f2793..82c01a8f64 100644 --- a/src/org/labkey/test/util/AuditLogHelper.java +++ b/src/org/labkey/test/util/AuditLogHelper.java @@ -4,6 +4,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.json.JSONException; +import org.json.JSONObject; import org.labkey.api.collections.CaseInsensitiveHashMap; import org.labkey.remoteapi.CommandException; import org.labkey.remoteapi.Connection; @@ -99,9 +100,11 @@ public enum AuditEvent PLATE_DATA_AUDIT_EVENT("PlateDataAuditEvent"), // available in Biologics module PLATE_SET_AUDIT_EVENT("PlateSetEvent"), // available in Biologics module QUERY_UPDATE_AUDIT_EVENT("QueryUpdateAuditEvent"), + SAMPLE_SET_AUDIT_EVENT("SampleSetAuditEvent"), SAMPLE_TIMELINE_EVENT("SampleTimelineEvent"), SAMPLE_WORKFLOW_AUDIT_EVENT("SamplesWorkflowAuditEvent"), - SOURCES_AUDIT_EVENT("SourcesAuditEvent"); // available with SampleManagement module + SOURCES_AUDIT_EVENT("SourcesAuditEvent"), // available with SampleManagement module + TRANSACTION_AUDIT_EVENT("TransactionAuditEvent"); private final String _name; @@ -116,6 +119,22 @@ public String getName() } } + public enum TransactionDetail + { + AuditEvents, + ImportFileName, + ClientLibrary, + Product, + Action, + QueryCommand, + DataIteratorUsed, + ImportOptions, + EditMethod, + RequestSource, + ETL, + FileWatcher; + } + public Integer getLatestAuditRowId(String auditTable) throws IOException, CommandException { String rowId = "rowId"; @@ -233,6 +252,49 @@ public void checkAuditEventValuesForTransactionId(String containerPath, AuditEve } } + public Map getTransactionAuditLogDetails(Integer transactionAuditId) + { + Connection cn = WebTestHelper.getRemoteApiConnection(); + SelectRowsCommand cmd = new SelectRowsCommand("auditLog", "TransactionAuditEvent"); + cmd.setRequiredVersion(9.1); + cmd.setColumns(Arrays.asList("TransactionDetails")); + cmd.addFilter("RowId", transactionAuditId, Filter.Operator.EQUAL); + cmd.setContainerFilter(ContainerFilter.AllFolders); + + Map event = executeSelectCommand(cn, cmd).get(0); + String detailJSON = getLogColumnValue(event, "TransactionDetails"); + log("TransactionAuditEvent Details: " + detailJSON); + if (detailJSON == null || detailJSON.isEmpty()) + return Collections.emptyMap(); + + return new JSONObject(detailJSON).toMap(); + } + + public void checkLastTransactionAuditLogDetails(String containerPath, Map expectedDetails) + { + Integer transactionAuditId = getLastTransactionId(containerPath); + if (transactionAuditId == null) + fail("No TransactionAuditEvent found in container: " + containerPath); + checkTransactionAuditLogDetails(transactionAuditId, expectedDetails); + } + + public void checkTransactionAuditLogDetails(Integer transactionAuditId, Map expectedDetails) + { + Map actualDetails = getTransactionAuditLogDetails(transactionAuditId); + assertEquals("Unexpected number of events for transactionId " + transactionAuditId, expectedDetails.size(), actualDetails.size()); + for (TransactionDetail key : expectedDetails.keySet()) + { + assertTrue("Expected detail key not found: " + key, actualDetails.containsKey(key.name())); + if (TransactionDetail.RequestSource.name().equals(key.name())) + { + String expectedValue = expectedDetails.get(key).toString(); + String actualValue = actualDetails.get(key.name()) != null ? actualDetails.get(key.name()).toString() : null; + assertTrue("Detail value for key " + key + " not as expected", actualValue != null && actualValue.contains(expectedValue)); + } + else + assertEquals("Detail value for key " + key + " not as expected", expectedDetails.get(key), actualDetails.get(key.name())); + } + } /** * Check the number of diffs in the audit event. This is a helper function to check the number of diffs in the * newRecordMap for an audit entry. If a transactionId is provided, it will check all rows for that @@ -308,6 +370,19 @@ public Integer getLastTransactionId(String containerPath, AuditEvent auditEventN } } + public Integer getLastTransactionId(String containerPath) + { + try + { + List> events = getAuditLogsFromLKS(containerPath, AuditEvent.TRANSACTION_AUDIT_EVENT, List.of("RowId"), Collections.emptyList(), 1, ContainerFilter.CurrentAndSubfolders).getRows(); + return events.size() == 1 ? (Integer) events.get(0).get("RowId") : null; + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } + public Integer getLastEventId(String containerPath, AuditEvent auditEventName) { try