ImportRecordCleanerJob implements AsyncJob { @Override public void execute(JobExecutionContext context) throws JobExecutionException { int toKeep = this.config.getInt(ConfigProperties.jobConfig(JOB_KEY, CFG_KEEP), DEFAULT_KEEP); if (toKeep < 0) { String errmsg = String.format( "Invalid value for number of records to keep, must be 0 or a positive integer: %s", toKeep); log.error(errmsg); throw new JobExecutionException(errmsg, true); } int deleted = 0; for (Owner owner : this.ownerCurator.listAll().list()) { List<ImportRecord> records = this.importRecordCurator.findRecords(owner).list(); if (toKeep < records.size()) { this.importRecordCurator.bulkDelete(records.subList(toKeep, records.size())); deleted += records.size() - toKeep; } } String outcome; if (deleted > 0) { outcome = String.format("Deleted %d import records.", deleted); log.info(outcome); } else { outcome = "No import records were deleted."; log.debug(outcome); } context.setJobResult(outcome); } @Inject  ImportRecordCleanerJob(ImportRecordCurator importRecordCurator,
        OwnerCurator ownerCurator, Configuration config); @Override void execute(JobExecutionContext context); static final String JOB_KEY; static final String JOB_NAME; static final String DEFAULT_SCHEDULE; static final String CFG_KEEP; static final int DEFAULT_KEEP; }
@Test public void noRecords() throws Exception { Owner owner = new Owner("owner"); Owner another = new Owner("another_owner"); ownerCurator.create(owner); ownerCurator.create(another); JobExecutionContext context = mock(JobExecutionContext.class); ImportRecordCleanerJob job = createJobInstance(); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); job.execute(context); verify(context, times(1)).setJobResult(captor.capture()); Object result = captor.getValue(); assertEquals("No import records were deleted.", result); }
@Test public void singleOwner() throws Exception { Owner owner = new Owner("owner"); ownerCurator.create(owner); for (int i = 0; i < 13; i++) { ImportRecord record = new ImportRecord(owner); record.recordStatus(ImportRecord.Status.SUCCESS, "great!"); this.importRecordCurator.create(record); } JobExecutionContext context = mock(JobExecutionContext.class); ImportRecordCleanerJob job = createJobInstance(); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); job.execute(context); verify(context, times(1)).setJobResult(captor.capture()); Object result = captor.getValue(); List<ImportRecord> records = this.importRecordCurator.findRecords(owner).list(); assertEquals(10, records.size()); assertEquals("Deleted 3 import records.", result); }
@Test public void lessThanThreshold() throws Exception { Owner owner = new Owner("owner"); ownerCurator.create(owner); for (int i = 0; i < 7; i++) { ImportRecord record = new ImportRecord(owner); record.recordStatus(ImportRecord.Status.SUCCESS, "great!"); this.importRecordCurator.create(record); } JobExecutionContext context = mock(JobExecutionContext.class); ImportRecordCleanerJob job = createJobInstance(); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); job.execute(context); verify(context, times(1)).setJobResult(captor.capture()); Object result = captor.getValue(); List<ImportRecord> records = this.importRecordCurator.findRecords(owner).list(); assertEquals(7, records.size()); assertEquals("No import records were deleted.", result); }
@Test public void multipleOwners() throws Exception { Owner owner1 = new Owner("owner1"); Owner owner2 = new Owner("owner2"); ownerCurator.create(owner1); ownerCurator.create(owner2); for (int i = 0; i < 23; i++) { ImportRecord record = new ImportRecord(owner1); record.recordStatus(ImportRecord.Status.FAILURE, "Bad bad"); this.importRecordCurator.create(record); } for (int i = 0; i < 4; i++) { ImportRecord record = new ImportRecord(owner2); record.recordStatus(ImportRecord.Status.SUCCESS, "Excellent"); this.importRecordCurator.create(record); } JobExecutionContext context = mock(JobExecutionContext.class); ImportRecordCleanerJob job = createJobInstance(); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); job.execute(context); verify(context, times(1)).setJobResult(captor.capture()); Object result = captor.getValue(); assertEquals(10, importRecordCurator.findRecords(owner1).list().size()); assertEquals(4, importRecordCurator.findRecords(owner2).list().size()); assertEquals("Deleted 13 import records.", result); }