diff --git a/nifi-framework-bundle/nifi-framework/nifi-administration/src/main/java/org/apache/nifi/admin/service/EntityStoreAuditService.java b/nifi-framework-bundle/nifi-framework/nifi-administration/src/main/java/org/apache/nifi/admin/service/EntityStoreAuditService.java index 097f099d65..1152ddc40a 100644 --- a/nifi-framework-bundle/nifi-framework/nifi-administration/src/main/java/org/apache/nifi/admin/service/EntityStoreAuditService.java +++ b/nifi-framework-bundle/nifi-framework/nifi-administration/src/main/java/org/apache/nifi/admin/service/EntityStoreAuditService.java @@ -336,10 +336,7 @@ public class EntityStoreAuditService implements AuditService, Closeable { endTimestamp = endDate.getTime(); } - final ActionEntity sortEntityProperty = getSortEntityProperty(actionQuery); - final boolean ascending = isAscending(actionQuery); - final EntityIterable sorted = storeTransaction.sort(EntityType.ACTION.getEntityType(), sortEntityProperty.getProperty(), ascending); - final EntityIterable entities = sorted.intersect(storeTransaction.find(EntityType.ACTION.getEntityType(), ActionEntity.TIMESTAMP.getProperty(), startTimestamp, endTimestamp)); + final EntityIterable entities = storeTransaction.find(EntityType.ACTION.getEntityType(), ActionEntity.TIMESTAMP.getProperty(), startTimestamp, endTimestamp); final EntityIterable sourceEntities; final String sourceId = actionQuery.getSourceId(); @@ -359,7 +356,9 @@ public class EntityStoreAuditService implements AuditService, Closeable { filteredEntities = sourceEntities.intersect(identityFiltered); } - return filteredEntities; + final ActionEntity sortEntityProperty = getSortEntityProperty(actionQuery); + final boolean ascending = isAscending(actionQuery); + return storeTransaction.sort(EntityType.ACTION.getEntityType(), sortEntityProperty.getProperty(), filteredEntities, ascending); } private boolean isAscending(final HistoryQuery historyQuery) { diff --git a/nifi-framework-bundle/nifi-framework/nifi-administration/src/test/java/org/apache/nifi/admin/service/EntityStoreAuditServiceTest.java b/nifi-framework-bundle/nifi-framework/nifi-administration/src/test/java/org/apache/nifi/admin/service/EntityStoreAuditServiceTest.java index 62492b9f34..203271e090 100644 --- a/nifi-framework-bundle/nifi-framework/nifi-administration/src/test/java/org/apache/nifi/admin/service/EntityStoreAuditServiceTest.java +++ b/nifi-framework-bundle/nifi-framework/nifi-administration/src/test/java/org/apache/nifi/admin/service/EntityStoreAuditServiceTest.java @@ -63,6 +63,8 @@ class EntityStoreAuditServiceTest { private static final Date SECOND_ACTION_TIMESTAMP = new Date(97500); + private static final Date THIRD_ACTION_TIMESTAMP = new Date(172800); + private static final Date PURGE_END_DATE = new Date(43200); private static final String USER_IDENTITY = "admin"; @@ -99,6 +101,8 @@ class EntityStoreAuditServiceTest { private static final String DATABASE_FILE_EXTENSION = ".xd"; + private static final String SORT_ASCENDING = "ASC"; + @TempDir File directory; @@ -248,6 +252,42 @@ class EntityStoreAuditServiceTest { assertFalse(actionsFiltered.hasNext()); } + @Test + void testAddActionsGetActionsQueryTimestampSortedSourceIdFiltered() { + final FlowChangeAction firstAction = newAction(); + final FlowChangeAction secondAction = newAction(); + secondAction.setTimestamp(SECOND_ACTION_TIMESTAMP); + final FlowChangeAction thirdAction = newAction(); + thirdAction.setTimestamp(THIRD_ACTION_TIMESTAMP); + final Collection actions = Arrays.asList(firstAction, secondAction, thirdAction); + + service.addActions(actions); + + final HistoryQuery historyQuery = new HistoryQuery(); + historyQuery.setSourceId(SOURCE_ID); + historyQuery.setSortOrder(SORT_ASCENDING); + final History actionsHistory = service.getActions(historyQuery); + + assertNotNull(actionsHistory); + assertEquals(actionsHistory.getTotal(), actions.size()); + assertNotNull(actionsHistory.getLastRefreshed()); + + final Collection actionsFound = actionsHistory.getActions(); + assertNotNull(actionsFound); + + final Iterator actionsFiltered = actionsFound.iterator(); + assertTrue(actionsFiltered.hasNext()); + + final Action firstActionFound = actionsFiltered.next(); + assertEquals(ACTION_TIMESTAMP, firstActionFound.getTimestamp()); + + final Action secondActionFound = actionsFiltered.next(); + assertEquals(SECOND_ACTION_TIMESTAMP, secondActionFound.getTimestamp()); + + final Action thirdActionFound = actionsFiltered.next(); + assertEquals(THIRD_ACTION_TIMESTAMP, thirdActionFound.getTimestamp()); + } + @Test void testAddActionsPurgeActionsGetAction() { final Action action = newAction(); @@ -349,7 +389,7 @@ class EntityStoreAuditServiceTest { final List firstPreviousValues = previousValues.get(FIRST_PROPERTY_NAME); assertNotNull(firstPreviousValues); - final PreviousValue firstPreviousValue = firstPreviousValues.get(0); + final PreviousValue firstPreviousValue = firstPreviousValues.getFirst(); assertNotNull(firstPreviousValue); assertEquals(FIRST_VALUE, firstPreviousValue.getPreviousValue()); assertNotNull(firstPreviousValue.getTimestamp()); @@ -358,7 +398,7 @@ class EntityStoreAuditServiceTest { final List secondPreviousValues = previousValues.get(SECOND_PROPERTY_NAME); assertNotNull(secondPreviousValues); - final PreviousValue thirdPreviousValue = secondPreviousValues.get(0); + final PreviousValue thirdPreviousValue = secondPreviousValues.getFirst(); assertNotNull(thirdPreviousValue); assertEquals(THIRD_VALUE, thirdPreviousValue.getPreviousValue()); assertNotNull(thirdPreviousValue.getTimestamp());