MaintenanceService { @SuppressFBWarnings(value = "BAS_BLOATED_ASSIGNMENT_SCOPE", justification = "periodicLogger is defined in correct scope") public MaintenanceResults cleanupWorkflows(MaintenanceConfiguration configuration) { validateConfiguration(configuration); if (configuration.archiveWorkflows != null || configuration.deleteArchivedWorkflows != null) { ARCHIVABLE_TABLES.forEach(table -> tableMetadataChecker.ensureCopyingPossible(MAIN.nameOf(table), ARCHIVE.nameOf(table))); } Builder builder = new MaintenanceResults.Builder(); if (configuration.deleteArchivedWorkflows != null) { builder.setDeletedArchivedWorkflows(doAction("Deleting archived workflows", configuration.deleteArchivedWorkflows, ARCHIVE, idList -> maintenanceDao.deleteWorkflows(ARCHIVE, idList))); } if (configuration.archiveWorkflows != null) { builder.setArchivedWorkflows( doAction("Archiving workflows", configuration.archiveWorkflows, MAIN, maintenanceDao::archiveWorkflows)); } if (configuration.deleteWorkflows != null) { builder.setDeletedWorkflows(doAction("Deleting workflows", configuration.deleteWorkflows, MAIN, idList -> maintenanceDao.deleteWorkflows(MAIN, idList))); } return builder.build(); } @Inject  MaintenanceService(MaintenanceDao maintenanceDao, TableMetadataChecker tableMetadataChecker,
      WorkflowDefinitionService workflowDefinitionService); @SuppressFBWarnings(value = "BAS_BLOATED_ASSIGNMENT_SCOPE", justification = "periodicLogger is defined in correct scope") MaintenanceResults cleanupWorkflows(MaintenanceConfiguration configuration);  }
@Test public void deletingFromArchiveTablesContinuesAsLongAsOldWorkflowsAreFound() { doReturn(oldWorkdlowIds, oldWorkdlowIds, oldWorkdlowIds, emptyList).when(dao).getOldWorkflowIds(ARCHIVE, limit, BATCH_SIZE, emptySet()); when(dao.deleteWorkflows(ARCHIVE, oldWorkdlowIds)).thenReturn(oldWorkdlowIds.size()); MaintenanceResults results = service.cleanupWorkflows(deleteArchiveConfig); assertEquals(oldWorkdlowIds.size() * 3, results.deletedArchivedWorkflows); assertValidArchiveTablesAreChecked(); verify(dao, times(4)).getOldWorkflowIds(ARCHIVE, limit, BATCH_SIZE, emptySet()); verify(dao, times(3)).deleteWorkflows(ARCHIVE, oldWorkdlowIds); verifyNoMoreInteractions(dao, tableMetadataChecker); }
@Test public void nothingIsArchivedWhenValidArchiveTablesDoNotExist() { doThrow(IllegalArgumentException.class).when(tableMetadataChecker).ensureCopyingPossible(anyString(), anyString()); assertThrows(IllegalArgumentException.class, () -> service.cleanupWorkflows(archiveConfig)); verifyNoMoreInteractions(dao, tableMetadataChecker); }
@Test public void nothingIsDeletedFromArchiveTablesWhenValidArchiveTablesDoNotExist() { doThrow(IllegalArgumentException.class).when(tableMetadataChecker).ensureCopyingPossible(anyString(), anyString()); assertThrows(IllegalArgumentException.class, () -> service.cleanupWorkflows(deleteArchiveConfig)); verifyNoMoreInteractions(dao, tableMetadataChecker); }
@Test public void withZeroOldWorkflowsNothingIsArchived() { when(dao.getOldWorkflowIds(MAIN, limit, BATCH_SIZE, emptySet())).thenReturn(emptyList); MaintenanceResults results = service.cleanupWorkflows(archiveConfig); assertEquals(0, results.archivedWorkflows); assertValidArchiveTablesAreChecked(); verify(dao).getOldWorkflowIds(MAIN, limit, BATCH_SIZE, emptySet()); verifyNoMoreInteractions(dao, tableMetadataChecker); }
@Test public void withZeroOldWorkflowsNothingIsDeleted() { when(dao.getOldWorkflowIds(MAIN, limit, BATCH_SIZE, emptySet())).thenReturn(emptyList); MaintenanceResults results = service.cleanupWorkflows(deleteMainConfig); assertEquals(0, results.deletedWorkflows); verify(dao).getOldWorkflowIds(MAIN, limit, BATCH_SIZE, emptySet()); verifyNoMoreInteractions(dao, tableMetadataChecker); }
@Test public void withZeroOldWorkflowsNothingIsDeletedFromArchiveTables() { when(dao.getOldWorkflowIds(ARCHIVE, limit, BATCH_SIZE, emptySet())).thenReturn(emptyList); MaintenanceResults results = service.cleanupWorkflows(deleteArchiveConfig); assertEquals(0, results.deletedArchivedWorkflows); assertValidArchiveTablesAreChecked(); verify(dao).getOldWorkflowIds(ARCHIVE, limit, BATCH_SIZE, emptySet()); verifyNoMoreInteractions(dao, tableMetadataChecker); }
@Test public void archivingContinuesAsLongAsOldWorkflowsAreFound() { doReturn(oldWorkdlowIds, oldWorkdlowIds, oldWorkdlowIds, emptyList).when(dao).getOldWorkflowIds(MAIN, limit, BATCH_SIZE, emptySet()); when(dao.archiveWorkflows(oldWorkdlowIds)).thenReturn(oldWorkdlowIds.size()); MaintenanceResults results = service.cleanupWorkflows(archiveConfig); assertEquals(oldWorkdlowIds.size() * 3, results.archivedWorkflows); assertValidArchiveTablesAreChecked(); verify(dao, times(4)).getOldWorkflowIds(MAIN, limit, BATCH_SIZE, emptySet()); verify(dao, times(3)).archiveWorkflows(oldWorkdlowIds); verifyNoMoreInteractions(dao, tableMetadataChecker); }
@Test public void deletingFromMainTablesContinuesAsLongAsOldWorkflowsAreFound() { doReturn(oldWorkdlowIds, oldWorkdlowIds, oldWorkdlowIds, emptyList).when(dao).getOldWorkflowIds(MAIN, limit, BATCH_SIZE, emptySet()); when(dao.deleteWorkflows(MAIN, oldWorkdlowIds)).thenReturn(oldWorkdlowIds.size()); MaintenanceResults results = service.cleanupWorkflows(deleteMainConfig); assertEquals(oldWorkdlowIds.size() * 3, results.deletedWorkflows); verify(dao, times(4)).getOldWorkflowIds(MAIN, limit, BATCH_SIZE, emptySet()); verify(dao, times(3)).deleteWorkflows(MAIN, oldWorkdlowIds); verifyNoMoreInteractions(dao, tableMetadataChecker); }