MaintenanceDao { public List<Long> getOldWorkflowIds(TablePrefix table, DateTime before, int maxWorkflows, Set<String> workflowTypes) { StringBuilder sql = new StringBuilder("select id from ").append(table.nameOf("workflow")) .append(" where next_activation is null and ").append(sqlVariants.dateLtEqDiff("modified", "?")); List<Object> args = new ArrayList<>(); args.add(sqlVariants.toTimestampObject(before)); if (!workflowTypes.isEmpty()) { sql.append(" and type in (").append(generate(() -> "?").limit(workflowTypes.size()).collect(joining(","))).append(")"); args.addAll(workflowTypes); } sql.append(" order by id asc"); return jdbc.queryForList(sqlVariants.limit(sql.toString(), maxWorkflows), Long.class, args.toArray(new Object[0])); } @Inject  MaintenanceDao(SQLVariants sqlVariants, @NFlow JdbcTemplate jdbcTemplate,
      @NFlow NamedParameterJdbcTemplate nflowNamedParameterJdbcTemplate); List<Long> getOldWorkflowIds(TablePrefix table, DateTime before, int maxWorkflows, Set<String> workflowTypes); @Transactional int archiveWorkflows(Collection<Long> workflowIds); @Transactional int deleteWorkflows(TablePrefix table, Collection<Long> workflowIds); @Transactional void deleteActionAndStateHistory(long workflowInstanceId, DateTime olderThan);  }
@Test public void getOldWorkflowIdsReturnPassiveWorkflowsModifiedBeforeGivenTimeOrderedById() { List<Long> expectedIds = new ArrayList<>(); storeActiveWorkflow(archiveTime1); storeActiveWorkflow(prodTime1); storePassiveWorkflow(prodTime2); expectedIds.add(storePassiveWorkflow(archiveTime1)); expectedIds.add(storePassiveWorkflow(archiveTime2)); List<Long> oldWorkflowIds = maintenanceDao.getOldWorkflowIds(MAIN, archiveTimeLimit, 10, emptySet()); assertArrayEquals(oldWorkflowIds.toArray(), expectedIds.toArray()); }
@Test public void getOldWorkflowIdsReturnsRequestedNumberOfItemsOrderedById() { List<Long> expectedIds = new ArrayList<>(); expectedIds.add(storePassiveWorkflow(archiveTime2)); for (int i = 0; i < 9; i++) { expectedIds.add(storePassiveWorkflow(archiveTime4)); } long eleventh = storePassiveWorkflow(archiveTime3); storeActiveWorkflow(archiveTime1); storeActiveWorkflow(prodTime3); storePassiveWorkflow(prodTime4); List<Long> oldWorkflowIds = maintenanceDao.getOldWorkflowIds(MAIN, archiveTimeLimit, 10, emptySet()); assertArrayEquals(oldWorkflowIds.toArray(), expectedIds.toArray()); expectedIds.add(eleventh); oldWorkflowIds = maintenanceDao.getOldWorkflowIds(MAIN, archiveTimeLimit, 11, emptySet()); assertArrayEquals(oldWorkflowIds.toArray(), expectedIds.toArray()); }