CleanupSnapshotTaskRunner implements TaskRunner { @Override public String performTask(String taskParameters) { CleanupSnapshotTaskParameters taskParams = CleanupSnapshotTaskParameters.deserialize(taskParameters); final String spaceId = taskParams.getSpaceId(); final String userId = SecurityContextHolder.getContext().getAuthentication().getName(); String bucketName = unwrappedSnapshotProvider.getBucketName(spaceId); log.info("Performing Cleanup Snapshot Task for spaceID: " + spaceId); BucketLifecycleConfiguration.Rule expireRule = new BucketLifecycleConfiguration.Rule() .withId("clear-content-rule") .withExpirationInDays(EXPIRATION_DAYS) .withStatus(BucketLifecycleConfiguration.ENABLED.toString()); List<BucketLifecycleConfiguration.Rule> rules = new ArrayList<>(); rules.add(expireRule); BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration().withRules(rules); s3Client.setBucketLifecycleConfiguration(bucketName, configuration); queueContentDeleteAuditTasks(spaceId, userId); log.info("Cleanup Snapshot Task for space " + spaceId + " completed successfully"); return new CleanupSnapshotTaskResult(EXPIRATION_DAYS).serialize(); } CleanupSnapshotTaskRunner(SnapshotStorageProvider unwrappedSnapshotProvider,
                                     AmazonS3 s3Client,
                                     TaskQueue auditTaskQueue,
                                     ManifestStore manifestStore,
                                     String account,
                                     String storeId); @Override String getName(); @Override String performTask(String taskParameters);  }
@Test public void testPerformTask() throws Exception { String spaceId = "space-id"; String bucketName = "bucket-name"; expect(unwrappedSnapshotProvider.getBucketName(spaceId)) .andReturn(bucketName); Capture<BucketLifecycleConfiguration> lifecycleConfigCapture = new Capture<>(); s3Client.setBucketLifecycleConfiguration(eq(bucketName), capture( lifecycleConfigCapture)); expectLastCall().once(); List<ManifestItem> manifestItems = new LinkedList<>(); for (int i = 0; i < 10; i++) { ManifestItem item = new ManifestItem(); item.setContentId("content-id-" + i); item.setContentSize(i + ""); item.setContentChecksum("content-checksum-" + i); manifestItems.add(item); } Iterator<ManifestItem> it = manifestItems.iterator(); expect(manifestStore.getItems(account, storeId, spaceId)).andReturn(it); Capture<Set<Task>> taskCapture = new Capture<>(); expect(unwrappedSnapshotProvider.getStorageProviderType()) .andReturn(StorageProviderType.AMAZON_S3); auditQueue.put(capture(taskCapture)); expectLastCall().once(); Authentication auth = createMock(Authentication.class); String userId = "user-id"; expect(auth.getName()).andReturn(userId); SecurityContext context = createMock(SecurityContext.class); expect(context.getAuthentication()).andReturn(auth); SecurityContextHolder.setContext(context); replayAll(); taskRunner.performTask("{\"spaceId\":\"" + spaceId + "\"}"); BucketLifecycleConfiguration lifecycleConfig = lifecycleConfigCapture.getValue(); BucketLifecycleConfiguration.Rule rule = lifecycleConfig.getRules().get(0); assertEquals(1, rule.getExpirationInDays()); assertEquals("clear-content-rule", rule.getId()); assertEquals("Enabled", rule.getStatus()); Thread.sleep(500); Set<Task> tasks = taskCapture.getValue(); Map<String, Task> taskMapByContentId = new HashMap<>(); for (Task task : tasks) { taskMapByContentId.put(task.getProperty(AuditTask.CONTENT_ID_PROP), task); } assertEquals(manifestItems.size(), taskMapByContentId.size()); for (ManifestItem item : manifestItems) { Task task = taskMapByContentId.get(item.getContentId()); assertNotNull(task.getProperty(AuditTask.DATE_TIME_PROP)); assertNotNull(task.getProperty(AuditTask.CONTENT_ID_PROP)); assertEquals(ActionType.DELETE_CONTENT.name(), task.getProperty(AuditTask.ACTION_PROP)); assertEquals(account, task.getProperty(AuditTask.ACCOUNT_PROP)); assertEquals(storeId, task.getProperty(AuditTask.STORE_ID_PROP)); assertEquals(spaceId, task.getProperty(AuditTask.SPACE_ID_PROP)); assertEquals(item.getContentId(), task.getProperty(AuditTask.CONTENT_ID_PROP)); assertEquals(item.getContentChecksum(), task.getProperty(AuditTask.CONTENT_CHECKSUM_PROP)); assertEquals(item.getContentSize(), task.getProperty(AuditTask.CONTENT_SIZE_PROP)); } }