DisableHlsTaskRunner extends BaseHlsTaskRunner { public String performTask(String taskParameters) { DisableStreamingTaskParameters taskParams = DisableStreamingTaskParameters.deserialize(taskParameters); String spaceId = taskParams.getSpaceId(); log.info("Performing " + TASK_NAME + " task on space " + spaceId); String bucketName = unwrappedS3Provider.getBucketName(spaceId); DisableStreamingTaskResult taskResult = new DisableStreamingTaskResult(); removeHlsStreamingHostFromSpaceProps(spaceId); DistributionSummary existingDist = getExistingDistribution(bucketName); if (existingDist != null) { s3Client.deleteBucketPolicy(bucketName); } else { throw new RuntimeException("No streaming distribution exists for space " + spaceId); } taskResult.setResult(TASK_NAME + " task completed successfully"); String toReturn = taskResult.serialize(); log.info("Result of " + TASK_NAME + " task: " + toReturn); return toReturn; } DisableHlsTaskRunner(StorageProvider s3Provider,
                                S3StorageProvider unwrappedS3Provider,
                                AmazonS3 s3Client,
                                AmazonCloudFrontClient cfClient); String getName(); String performTask(String taskParameters);  }
@Test public void testPerformTaskNoDistribution() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); Map<String, String> props = new HashMap<>(); props.put(HLS_STREAMING_HOST_PROP, domainName); EasyMock.expect(s3Provider.getSpaceProperties(spaceId)) .andReturn(props); unwrappedS3Provider.setNewSpaceProperties(EasyMock.eq(spaceId), EasyMock.isA(Map.class)); EasyMock.expectLastCall().once(); ListDistributionsResult listDistResult = new ListDistributionsResult().withDistributionList( new DistributionList().withItems(new ArrayList()).withIsTruncated(false)); EasyMock.expect(cfClient.listDistributions(EasyMock.isA(ListDistributionsRequest.class))) .andReturn(listDistResult) .once(); DisableHlsTaskRunner runner = new DisableHlsTaskRunner(s3Provider, unwrappedS3Provider, s3Client, cfClient); replayMocks(); try { runner.performTask(null); fail("Exception expected"); } catch (Exception expected) { assertNotNull(expected); } DisableStreamingTaskParameters taskParams = new DisableStreamingTaskParameters(); taskParams.setSpaceId(spaceId); try { runner.performTask(taskParams.serialize()); fail("Exception expected"); } catch (Exception expected) { assertNotNull(expected); } }
@Test public void testPerformTaskSuccess() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); Map<String, String> props = new HashMap<>(); props.put(HLS_STREAMING_HOST_PROP, domainName); props.put(HLS_STREAMING_TYPE_PROP, BaseHlsTaskRunner.STREAMING_TYPE.SECURE.name()); EasyMock.expect(s3Provider.getSpaceProperties(spaceId)) .andReturn(props); Capture<Map<String, String>> spacePropsCapture = Capture.newInstance(); unwrappedS3Provider.setNewSpaceProperties(EasyMock.eq(spaceId), EasyMock.capture(spacePropsCapture)); EasyMock.expectLastCall().once(); cfClientExpectValidDistribution(cfClient); s3Client.deleteBucketPolicy(EasyMock.eq(bucketName)); EasyMock.expectLastCall().once(); DisableHlsTaskRunner runner = new DisableHlsTaskRunner(s3Provider, unwrappedS3Provider, s3Client, cfClient); replayMocks(); DisableStreamingTaskParameters taskParams = new DisableStreamingTaskParameters(); taskParams.setSpaceId(spaceId); String results = runner.performTask(taskParams.serialize()); assertNotNull(results); assertTrue(results.contains("completed")); Map<String, String> spaceProps = spacePropsCapture.getValue(); assertFalse(spaceProps.containsKey(HLS_STREAMING_HOST_PROP)); assertFalse(spaceProps.containsKey(HLS_STREAMING_TYPE_PROP)); }