OrphanCleanupJob implements AsyncJob { @Override @Transactional public void execute(JobExecutionContext context) throws JobExecutionException { log.debug("Deleting orphaned entities"); int orphanedContent = this.deleteOrphanedContent(); int orphanedProducts = this.deleteOrphanedProducts(); String format = "Orphan cleanup completed;" + "\n %d orphaned content deleted" + "\n %d orphaned products deleted"; context.setJobResult(format, orphanedContent, orphanedProducts); } @Inject  OrphanCleanupJob(ContentCurator contentCurator, OwnerContentCurator ownerContentCurator,
        ProductCurator productCurator, OwnerProductCurator ownerProductCurator); @Override @Transactional void execute(JobExecutionContext context); static final String JOB_KEY; static final String JOB_NAME; static final String DEFAULT_SCHEDULE; }
@Test public void testStandardExecution() throws Exception { Set<Owner> owners = new HashSet<>(); for (int i = 0; i < 3; ++i) { Owner owner = this.createOwner(); owners.add(owner); } Set<Content> existingContent = new HashSet<>(); Set<Content> orphanedContent = new HashSet<>(); Set<Product> existingProducts = new HashSet<>(); Set<Product> orphanedProducts = new HashSet<>(); for (Owner owner : owners) { for (int i = 0; i < 5; ++i) { Content content = this.createContent(owner); existingContent.add(content); Product product = this.createProduct(owner); existingProducts.add(product); } for (int i = 0; i < 2; ++i) { Content content = this.createOrphanedContent(); orphanedContent.add(content); Product product = this.createOrphanedProduct(); orphanedProducts.add(product); } } this.ownerCurator.flush(); OrphanCleanupJob job = this.createJobInstance(); AsyncJobStatus status = mock(AsyncJobStatus.class); JobExecutionContext context = spy(new JobExecutionContext(status)); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); job.execute(context); verify(context, times(1)).setJobResult(captor.capture()); Object result = captor.getValue(); assertThat(result, instanceOf(String.class)); this.ownerCurator.flush(); this.ownerCurator.clear(); for (Content existing : existingContent) { Content content = this.contentCurator.get(existing.getUuid()); assertNotNull(content); assertEquals(existing, content); } for (Product existing : existingProducts) { Product product = this.productCurator.get(existing.getUuid()); assertNotNull(product); assertEquals(existing, product); } for (Content orphan : orphanedContent) { Content content = this.contentCurator.get(orphan.getUuid()); assertNull(content); } for (Product orphan : orphanedProducts) { Product product = this.productCurator.get(orphan.getUuid()); assertNull(product); } }