Skip to content

Commit 4ded09b

Browse files
committed
Merge remote-tracking branch 'origin/develop' into fb_deriveActions
2 parents 50371a9 + 3359c0c commit 4ded09b

1 file changed

Lines changed: 40 additions & 82 deletions

File tree

src/org/labkey/test/util/AuditLogHelper.java

Lines changed: 40 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
import org.labkey.remoteapi.query.Sort;
1616
import org.labkey.test.Locator;
1717
import org.labkey.test.WebDriverWrapper;
18-
import org.labkey.test.WebTestHelper;
1918
import org.labkey.test.pages.core.admin.ShowAdminPage;
2019
import org.labkey.test.pages.core.admin.ShowAuditLogPage;
2120

2221
import java.io.IOException;
2322
import java.net.URL;
2423
import java.util.ArrayList;
25-
import java.util.Arrays;
2624
import java.util.Collection;
2725
import java.util.Collections;
2826
import java.util.HashMap;
@@ -92,6 +90,8 @@ public enum AuditEvent
9290
ASSAY_AUDIT_EVENT("AssayAuditEvent"), // available with SampleManagement module
9391
ASSAY_RESULT_AUDIT_EVENT("AssayResultAuditEvent"), // available with SampleManagement module
9492
ATTACHMENT_AUDIT_EVENT("AttachmentAuditEvent"),
93+
DOMAIN_AUDIT_EVENT("DomainAuditEvent"),
94+
DOMAIN_PROPERTY_AUDIT_EVENT("DomainPropertyAuditEvent"),
9595
EXPERIMENT_AUDIT_EVENT("ExperimentAuditEvent"),
9696
FILE_SYSTEM_EVENT("FileSystem"),
9797
GRID_VIEW_AUDIT_EVENT("GridViewAuditEvent"),
@@ -101,6 +101,7 @@ public enum AuditEvent
101101
PLATE_DATA_AUDIT_EVENT("PlateDataAuditEvent"), // available in Biologics module
102102
PLATE_SET_AUDIT_EVENT("PlateSetEvent"), // available in Biologics module
103103
QUERY_UPDATE_AUDIT_EVENT("QueryUpdateAuditEvent"),
104+
REGISTRY_AUDIT_EVENT("RegistryEvent"), // available in Biologics module
104105
SAMPLE_SET_AUDIT_EVENT("SampleSetAuditEvent"),
105106
SAMPLE_TIMELINE_EVENT("SampleTimelineEvent"),
106107
SAMPLE_WORKFLOW_AUDIT_EVENT("SamplesWorkflowAuditEvent"),
@@ -137,7 +138,7 @@ public enum TransactionDetail
137138
FileWatcher;
138139
}
139140

140-
public Integer getLatestAuditRowId(String auditTable) throws IOException, CommandException
141+
public Integer getLatestAuditRowId(String auditTable)
141142
{
142143
String rowId = "rowId";
143144

@@ -147,13 +148,12 @@ public Integer getLatestAuditRowId(String auditTable) throws IOException, Comman
147148
selectRows.setMaxRows(1);
148149
selectRows.setContainerFilter(ContainerFilter.AllFolders);
149150

150-
SelectRowsResponse response = selectRows.execute(_connectionSupplier.get(), null);
151-
List<Map<String, Object>> rows = response.getRows();
151+
List<Map<String, Object>> rows = executeSelectCommand(selectRows);
152152
if (rows.isEmpty())
153153
{
154154
return 0;
155155
}
156-
return (Integer) rows.get(0).get(rowId);
156+
return (Integer) rows.getFirst().get(rowId);
157157
}
158158

159159
public DataRegionTable beginAtAuditEventView(String auditTable, Integer rowIdCutoff)
@@ -179,25 +179,25 @@ public DataRegionTable goToAuditEventView(String eventType)
179179
* Get the audit logs from the LabKey server filtered to the given project.
180180
*
181181
* @param containerPath Path of the LK container to use for the select command.
182-
* @param auditEventName Name of the audit event to filter on. Example 'SamplesWorkflowAuditEvent'.
182+
* @param auditEvent Name of the audit event to filter on. Example 'SamplesWorkflowAuditEvent'.
183183
* @param columnNames The name of the columns to return.
184184
* @param filters The filters to be applied
185185
* @param maxRows The maximum number of rows to return. If null, all rows for the provided filters will be returned.
186-
* @param containerFilter The container filter to be applied. If null, default is ContainerFilter.Current.
186+
* @param containerFilter The container filter to be applied. If null, the default is ContainerFilter.Current.
187187
* @return A rowResponse with the query logs.
188188
* @throws IOException Can be thrown by the SelectRowsCommand.
189189
* @throws CommandException Can be thrown by the SelectRowsCommand.
190190
*/
191-
public SelectRowsResponse getAuditLogsFromLKS(String containerPath, AuditEvent auditEventName, List<String> columnNames,
191+
public SelectRowsResponse getAuditLogsFromLKS(String containerPath, AuditEvent auditEvent, List<String> columnNames,
192192
@Nullable List<Filter> filters, @Nullable Integer maxRows, @Nullable ContainerFilter containerFilter) throws IOException, CommandException
193193
{
194-
return getAuditLogsFromLKS(containerPath, _wrapper.getCurrentProject(), auditEventName, columnNames, filters, maxRows, containerFilter);
194+
return getAuditLogsFromLKS(containerPath, _wrapper.getCurrentProject(), auditEvent, columnNames, filters, maxRows, containerFilter);
195195
}
196196

197-
public SelectRowsResponse getAuditLogsFromLKS(String containerPath, @NotNull String projectName, AuditEvent auditEventName, List<String> columnNames,
197+
public SelectRowsResponse getAuditLogsFromLKS(String containerPath, @NotNull String projectName, AuditEvent auditEvent, List<String> columnNames,
198198
@Nullable List<Filter> filters, @Nullable Integer maxRows, @Nullable ContainerFilter containerFilter) throws IOException, CommandException
199199
{
200-
SelectRowsCommand cmd = new SelectRowsCommand("auditLog", auditEventName.getName());
200+
SelectRowsCommand cmd = new SelectRowsCommand("auditLog", auditEvent.getName());
201201
cmd.setColumns(columnNames);
202202
cmd.addFilter("ProjectId/Name", projectName, Filter.Operator.EQUAL);
203203
if (filters != null)
@@ -220,7 +220,7 @@ public List<Map<String, Object>> getAuditLogsForTransactionId(String containerPa
220220
{
221221
List<Filter> transactionFilter = new ArrayList<>();
222222
if (transactionId != null)
223-
transactionFilter.add(new Filter("TransactionId", transactionId, Filter.Operator.EQUAL));
223+
transactionFilter.add(new Filter("TransactionId", transactionId));
224224
if (eventFilters != null && !eventFilters.isEmpty())
225225
transactionFilter.addAll(eventFilters);
226226
return getAuditLogsFromLKS(containerPath, auditEventName, columnNames, transactionFilter, null, containerFilter).getRows();
@@ -236,7 +236,7 @@ public List<Map<String, Object>> getAuditLogsForTransactionId(String containerPa
236236
{
237237
List<Filter> transactionFilter = new ArrayList<>();
238238
if (transactionId != null)
239-
transactionFilter.add(new Filter("TransactionId", transactionId, Filter.Operator.EQUAL));
239+
transactionFilter.add(new Filter("TransactionId", transactionId));
240240
if (eventFilters != null && !eventFilters.isEmpty())
241241
transactionFilter.addAll(eventFilters);
242242
return getAuditLogsFromLKS(containerPath, projectName, auditEventName, columnNames, transactionFilter, null, containerFilter).getRows();
@@ -261,7 +261,7 @@ public void checkAuditEventValuesForTransactionId(String containerPath, AuditEve
261261

262262
public void checkAuditEventValuesForTransactionId(String containerPath, AuditEvent auditEventName, Integer transactionId, List<Map<String, Object>> expectedValues) throws IOException, CommandException
263263
{
264-
List<String> columnNames = expectedValues.get(0).keySet().stream().map(Object::toString).toList();
264+
List<String> columnNames = expectedValues.getFirst().keySet().stream().map(Object::toString).toList();
265265
checkAuditEventValuesForTransactionId(containerPath, auditEventName, columnNames, transactionId, expectedValues);
266266
}
267267

@@ -278,14 +278,13 @@ public void checkAuditEventValuesForTransactionId(String containerPath, AuditEve
278278

279279
public Map<String, Object> getTransactionAuditLogDetails(Integer transactionAuditId)
280280
{
281-
Connection cn = WebTestHelper.getRemoteApiConnection();
282-
SelectRowsCommand cmd = new SelectRowsCommand("auditLog", "TransactionAuditEvent");
281+
SelectRowsCommand cmd = new SelectRowsCommand("auditLog", AuditEvent.TRANSACTION_AUDIT_EVENT.getName());
283282
cmd.setRequiredVersion(9.1);
284-
cmd.setColumns(Arrays.asList("TransactionDetails"));
283+
cmd.setColumns(List.of("TransactionDetails"));
285284
cmd.addFilter("RowId", transactionAuditId, Filter.Operator.EQUAL);
286285
cmd.setContainerFilter(ContainerFilter.AllFolders);
287286

288-
Map<String, Object> event = executeSelectCommand(cn, cmd).get(0);
287+
Map<String, Object> event = executeSelectCommand(cmd).getFirst();
289288
String detailJSON = getLogColumnValue(event, "TransactionDetails");
290289
log("TransactionAuditEvent Details: " + detailJSON);
291290
if (detailJSON == null || detailJSON.isEmpty())
@@ -376,14 +375,13 @@ public void checkAuditEventDiffCount(String containerPath, AuditEvent auditEvent
376375

377376
public void checkAuditLogDataChanges(AuditEvent auditEventName, int transactionId, List<String> changes)
378377
{
379-
Connection cn = WebTestHelper.getRemoteApiConnection();
380378
SelectRowsCommand cmd = new SelectRowsCommand("auditLog", auditEventName.getName());
381379
cmd.setRequiredVersion(9.1);
382-
cmd.setColumns(Arrays.asList("oldvalues", "newvalues", "datachanges"));
380+
cmd.setColumns(List.of("oldvalues", "newvalues", "datachanges"));
383381
cmd.addFilter("transactionauditid", transactionId, Filter.Operator.EQUAL);
384382
cmd.setContainerFilter(ContainerFilter.AllFolders);
385383

386-
Map<String, Object> event = executeSelectCommand(cn, cmd).get(0);
384+
Map<String, Object> event = executeSelectCommand(cmd).getFirst();
387385

388386
String datachanges = getLogColumnDisplayValue(event, "datachanges").toLowerCase();
389387

@@ -397,7 +395,7 @@ public Integer getLastTransactionId(String containerPath, AuditEvent auditEventN
397395
try
398396
{
399397
List<Map<String, Object>> events = getAuditLogsFromLKS(containerPath, auditEventName, List.of("TransactionId"), Collections.emptyList(), 1, ContainerFilter.CurrentAndSubfolders).getRows();
400-
return events.size() == 1 ? (Integer) events.get(0).get("TransactionId") : null;
398+
return events.size() == 1 ? (Integer) events.getFirst().get("TransactionId") : null;
401399
}
402400
catch (Exception e)
403401
{
@@ -410,7 +408,7 @@ public Integer getLastTransactionId(String containerPath)
410408
try
411409
{
412410
List<Map<String, Object>> events = getAuditLogsFromLKS(containerPath, AuditEvent.TRANSACTION_AUDIT_EVENT, List.of("RowId"), Collections.emptyList(), 1, ContainerFilter.CurrentAndSubfolders).getRows();
413-
return events.size() == 1 ? (Integer) events.get(0).get("RowId") : null;
411+
return events.size() == 1 ? (Integer) events.getFirst().get("RowId") : null;
414412
}
415413
catch (Exception e)
416414
{
@@ -423,7 +421,7 @@ public Integer getLastEventId(String containerPath, AuditEvent auditEventName)
423421
try
424422
{
425423
List<Map<String, Object>> events = getAuditLogsFromLKS(containerPath, auditEventName, List.of("RowId"), Collections.emptyList(), 1, ContainerFilter.CurrentAndSubfolders).getRows();
426-
return events.size() == 1 ? (Integer) events.get(0).get("RowId") : null;
424+
return events.size() == 1 ? (Integer) events.getFirst().get("RowId") : null;
427425
}
428426
catch (Exception e)
429427
{
@@ -470,14 +468,14 @@ public Integer checkAuditEventDiffCountForLastTransaction(String containerPath,
470468
Integer transactionId = getLastTransactionId(containerPath, auditEventName);
471469
List<Filter> transactionFilter = new ArrayList<>();
472470
if (transactionId != null)
473-
transactionFilter.add(new Filter("TransactionId", transactionId, Filter.Operator.EQUAL));
471+
transactionFilter.add(new Filter("TransactionId", transactionId));
474472
if (eventFilters != null && !eventFilters.isEmpty())
475473
transactionFilter.addAll(eventFilters);
476474
List<Map<String, Object>> events = getAuditLogsFromLKS(containerPath, auditEventName, List.of("Comment", "UserComment", "NewRecordMap"), transactionFilter, null, ContainerFilter.CurrentAndSubfolders).getRows();
477475
if (expectedEventCount != null)
478476
{
479-
if (expectedEventCount.intValue() != events.size())
480-
log("Last audit event info: " + events.get(0));
477+
if (expectedEventCount != events.size())
478+
log("Last audit event info: " + events.getFirst());
481479
assertEquals("Unexpected number of events for transactionId " + transactionId, expectedEventCount.intValue(), events.size());
482480
}
483481
List<Integer> expectedChangeCounts = Collections.nCopies(events.size(), expectedDiffCount);
@@ -558,7 +556,7 @@ public boolean validateDomainPropertiesAuditLog(String domainName, Integer domai
558556
if (expectedAuditDetails.size() != actualAuditDetails.size())
559557
{
560558
pass = false;
561-
log(String.format("Number of DomainPropertyAuditEvent events not as expected. Expected %d, Actual %d.", expectedAuditDetails.size(), actualAuditDetails.size()));
559+
log(String.format("Number of %s events not as expected. Expected %d, Actual %d.", AuditEvent.DOMAIN_PROPERTY_AUDIT_EVENT.getName(), expectedAuditDetails.size(), actualAuditDetails.size()));
562560
}
563561

564562
for (String key : expectedAuditDetails.keySet())
@@ -568,7 +566,7 @@ public boolean validateDomainPropertiesAuditLog(String domainName, Integer domai
568566
if (actualAuditDetail == null)
569567
{
570568
pass = false;
571-
log("Field " + key + " is missing DomainPropertyAuditEvent.");
569+
log(String.format("Field %s is missing %s", key, AuditEvent.DOMAIN_PROPERTY_AUDIT_EVENT.getName()));
572570
}
573571
else
574572
pass = pass && validateDetailAuditLog(expectedAuditDetail, actualAuditDetail);
@@ -607,7 +605,7 @@ public List<Integer> getDomainEventIds(String projectName, String domainName, @N
607605
List<DetailedAuditEventRow> eventLog = getDomainAuditEventLog(projectName, domainName, null, 1);
608606
if (eventLog.isEmpty())
609607
return null;
610-
return eventLog.get(0);
608+
return eventLog.getFirst();
611609
}
612610

613611
public @Nullable Integer getLastDomainEventId(String projectName, String domainName)
@@ -757,10 +755,9 @@ private List<DetailedAuditEventRow> getDomainAuditEventLog(String projectName, S
757755
log("Get a list of the Domain Events for project '" + projectName + "'. ");
758756
domainName = domainName.trim();
759757

760-
Connection cn = WebTestHelper.getRemoteApiConnection();
761-
SelectRowsCommand cmd = new SelectRowsCommand("auditLog", "DomainAuditEvent");
758+
SelectRowsCommand cmd = new SelectRowsCommand("auditLog", AuditEvent.DOMAIN_AUDIT_EVENT.getName());
762759
cmd.setRequiredVersion(9.1);
763-
cmd.setColumns(Arrays.asList("rowid", "domainuri", "domainname", "comment", "usercomment", "oldvalues", "newvalues", "datachanges"));
760+
cmd.setColumns(List.of("rowid", "domainuri", "domainname", "comment", "usercomment", "oldvalues", "newvalues", "datachanges"));
764761
cmd.addFilter("projectid/DisplayName", projectName, Filter.Operator.EQUAL);
765762
cmd.addFilter("domainname", domainName, Filter.Operator.EQUAL);
766763
if (null != ignoreIds)
@@ -774,7 +771,7 @@ private List<DetailedAuditEventRow> getDomainAuditEventLog(String projectName, S
774771
if (maxRows != null)
775772
cmd.setMaxRows(maxRows);
776773

777-
List<Map<String, Object>> domainAuditEventAllRows = executeSelectCommand(cn, cmd);
774+
List<Map<String, Object>> domainAuditEventAllRows = executeSelectCommand(cmd);
778775
log(String.format("Number of Domain Event log entries for domain '%s' in '%s': %d", domainName, projectName, domainAuditEventAllRows.size()));
779776

780777
List<DetailedAuditEventRow> domainAuditEventRows = new ArrayList<>();
@@ -796,10 +793,10 @@ private List<DetailedAuditEventRow> getDomainAuditEventLog(String projectName, S
796793

797794
private List<Map<String, Object>> getDomainPropertyEventLog(String domainName, @Nullable List<Integer> eventIds)
798795
{
799-
Connection cn = WebTestHelper.getRemoteApiConnection();
800-
SelectRowsCommand cmd = new SelectRowsCommand("auditLog", "DomainPropertyAuditEvent");
796+
SelectRowsCommand cmd = new SelectRowsCommand("auditLog", AuditEvent.DOMAIN_PROPERTY_AUDIT_EVENT.getName());
801797
cmd.setRequiredVersion(9.1);
802-
cmd.setColumns(Arrays.asList("Created", "CreatedBy", "ImpersonatedBy", "propertyname", "action", "domainname", "domaineventid", "Comment", "UserComment", "oldvalues", "newvalues", "datachanges"));
798+
cmd.setColumns(List.of("Created", "CreatedBy", "ImpersonatedBy", "propertyname", "action", "domainname", "domaineventid", "Comment", "UserComment", "oldvalues", "newvalues", "datachanges"));
799+
cmd.setContainerFilter(ContainerFilter.AllFolders);
803800
cmd.addFilter("domainname", domainName, Filter.Operator.EQUAL);
804801

805802
if (null != eventIds)
@@ -808,63 +805,24 @@ private List<Map<String, Object>> getDomainPropertyEventLog(String domainName, @
808805
cmd.addFilter("domaineventid/rowid", rowIds, Filter.Operator.IN);
809806
}
810807

811-
cmd.setContainerFilter(ContainerFilter.AllFolders);
812-
813-
return executeSelectCommand(cn, cmd);
808+
return executeSelectCommand(cmd);
814809
}
815810

816-
private List<Map<String, Object>> executeSelectCommand(Connection cn, SelectRowsCommand cmd)
811+
private List<Map<String, Object>> executeSelectCommand(SelectRowsCommand cmd)
817812
{
818-
List<Map<String, Object>> rowsReturned = new ArrayList<>();
819813
try
820814
{
821-
SelectRowsResponse response = cmd.execute(cn, "/");
815+
SelectRowsResponse response = cmd.execute(_connectionSupplier.get(), "/");
822816
log("Number of rows: " + response.getRowCount());
823-
rowsReturned.addAll(response.getRows());
817+
return response.getRows();
824818
}
825819
catch (IOException | CommandException ex)
826820
{
827821
// Just fail here, don't toss the exception up the stack.
828822
fail("There was a command exception when getting the log: " + ex);
829823
}
830824

831-
return rowsReturned;
832-
}
833-
834-
private Map<String, String> getDomainPropertyEventComment(Map<String, Object> row)
835-
{
836-
String comment = getLogColumnValue(row, "Comment");
837-
if (comment != null)
838-
return null;
839-
840-
String[] commentAsArray = comment.split(";");
841-
842-
Map<String, String> fieldComments = new HashMap<>();
843-
844-
for (String s : commentAsArray)
845-
{
846-
String[] fieldValue = s.split(":");
847-
848-
// If the split on the ':' produced more than two entries in the array it most likely means that the
849-
// comment for that property had a : in it. So treat the first entry as the field name and then concat the
850-
// other fields together.
851-
// For example the ConditionalFormats field will log the following during an update:
852-
// ConditionalFormats: old: <none>, new: 1;
853-
// And a create of a Lookup will log as:
854-
// Lookup: [Schema: lists, Query: LookUp01];
855-
StringBuilder sb = new StringBuilder();
856-
sb.append(fieldValue[1].trim());
857-
858-
for (int j = 2; j < fieldValue.length; j++)
859-
{
860-
sb.append(":");
861-
sb.append(fieldValue[j]);
862-
}
863-
864-
fieldComments.put(fieldValue[0].trim(), sb.toString());
865-
}
866-
867-
return fieldComments;
825+
return Collections.emptyList();
868826
}
869827

870828
private String getLogColumnValue(Map<String, Object> rowEntry, String columnName, String valueType)

0 commit comments

Comments
 (0)