Permalink
Browse files

Merge pull request #16 from jcookems/test467

Update Job tests for #467
  • Loading branch information...
2 parents dc5f270 + e00f3f8 commit 009c74d37934a83b745ad8ce6306dab7164560a9 @jcookems jcookems committed Dec 12, 2012
@@ -21,6 +21,8 @@
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.microsoft.windowsazure.services.media.models.ContentKey;
import com.microsoft.windowsazure.services.media.models.ContentKeyInfo;
+import com.microsoft.windowsazure.services.media.models.Job;
+import com.microsoft.windowsazure.services.media.models.JobInfo;
import com.microsoft.windowsazure.services.media.models.ListResult;
import com.microsoft.windowsazure.services.media.models.Locator;
import com.microsoft.windowsazure.services.media.models.LocatorInfo;
@@ -33,6 +35,7 @@
protected static final String testAssetPrefix = "testAsset";
protected static final String testPolicyPrefix = "testPolicy";
protected static final String testContentKeyPrefix = "testContentKey";
+ protected static final String testJobPrefix = "testJobPrefix";
protected static final String validButNonexistAssetId = "nb:cid:UUID:0239f11f-2d36-4e5f-aa35-44d58ccc0973";
protected static final String validButNonexistAccessPolicyId = "nb:pid:UUID:38dcb3a0-ef64-4ad0-bbb5-67a14c6df2f7";
@@ -52,7 +55,6 @@ public static void setup() throws Exception {
overrideWithEnv(config, MediaConfiguration.OAUTH_CLIENT_SECRET);
overrideWithEnv(config, MediaConfiguration.OAUTH_SCOPE);
- // TODO: Replace with call to MediaService.create once that's updated
service = MediaService.create(config);
cleanupEnvironment();
@@ -79,6 +81,7 @@ private static void cleanupEnvironment() {
removeAllTestAssets();
removeAllTestAccessPolicies();
removeAllTestContentKeys();
+ removeAllTestJobs();
}
private static void removeAllTestContentKeys() {
@@ -138,6 +141,20 @@ private static void removeAllTestLocators() {
}
}
+ private static void removeAllTestJobs() {
+ try {
+ ListResult<JobInfo> jobs = service.list(Job.list());
+ for (JobInfo job : jobs) {
+ if (job.getName().startsWith(testAssetPrefix)) {
+ service.delete(Job.delete(job.getId()));
+ }
+ }
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
interface ComponentDelegate {
void verifyEquals(String message, Object expected, Object actual);
}
@@ -21,15 +21,17 @@
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
+import java.util.Date;
import java.util.EnumSet;
import java.util.List;
+import java.util.UUID;
import javax.ws.rs.core.MultivaluedMap;
+import org.junit.BeforeClass;
import org.junit.Test;
import com.microsoft.windowsazure.services.core.ServiceException;
-import com.microsoft.windowsazure.services.media.implementation.content.JobType;
import com.microsoft.windowsazure.services.media.models.AccessPolicy;
import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo;
import com.microsoft.windowsazure.services.media.models.AccessPolicyPermission;
@@ -42,124 +44,166 @@
import com.microsoft.windowsazure.services.media.models.ListResult;
import com.microsoft.windowsazure.services.media.models.LocatorInfo;
import com.microsoft.windowsazure.services.media.models.Task;
+import com.microsoft.windowsazure.services.media.models.Task.CreateBatchOperation;
import com.sun.jersey.core.util.MultivaluedMapImpl;
public class JobIntegrationTest extends IntegrationTestBase {
- private final String testJobPrefix = "testJobPrefix";
- private final byte[] testBlobData = new byte[] { 0, 1, 2 };
- private final String taskBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?><taskBody>"
- + "<inputAsset>JobInputAsset(0)</inputAsset><outputAsset>JobOutputAsset(0)</outputAsset>" + "</taskBody>";
+ private static AssetInfo assetInfo;
+ private static final byte[] testBlobData = new byte[] { 4, 8, 15, 16, 23, 42 };
private void verifyJobInfoEqual(String message, JobInfo expected, JobInfo actual) {
verifyJobProperties(message, expected.getName(), expected.getPriority(), expected.getRunningDuration(),
- expected.getState(), expected.getTemplateId(), expected.getInputMediaAssets(),
+ expected.getState(), expected.getTemplateId(), expected.getCreated(), expected.getLastModified(),
+ expected.getStartTime(), expected.getEndTime(), expected.getInputMediaAssets(),
expected.getOutputMediaAssets(), actual);
}
- private AccessPolicyInfo createWritableAccessPolicy(String name, int durationInMinutes) throws ServiceException {
- return service.create(AccessPolicy.create(testPolicyPrefix + name, durationInMinutes,
- EnumSet.of(AccessPolicyPermission.WRITE)));
- }
-
- private void createAndUploadBlob(WritableBlobContainerContract blobWriter, String blobName, byte[] blobData)
- throws ServiceException {
- InputStream blobContent = new ByteArrayInputStream(blobData);
- blobWriter.createBlockBlob(blobName, blobContent);
- }
-
- private String createFileAsset(String name) throws ServiceException {
- String testBlobName = "test" + name + ".bin";
- AssetInfo assetInfo = service.create(Asset.create().setName(name));
- AccessPolicyInfo accessPolicyInfo = createWritableAccessPolicy(name, 10);
- LocatorInfo locator = createLocator(accessPolicyInfo, assetInfo, 5, 10);
- WritableBlobContainerContract blobWriter = service.createBlobWriter(locator);
- createAndUploadBlob(blobWriter, testBlobName, testBlobData);
-
- service.create(AssetFile.create(assetInfo.getId(), testBlobName).setIsPrimary(true).setIsEncrypted(false)
- .setContentFileSize(new Long(testBlobData.length)));
-
- service.action(AssetFile.createFileInfos(assetInfo.getId()));
- return assetInfo.getId();
- }
-
private void verifyJobProperties(String message, String testName, Integer priority, Double runningDuration,
- JobState state, String templateId, List<String> inputMediaAssets, List<String> outputMediaAssets,
- JobInfo actualJob) {
+ JobState state, String templateId, Date created, Date lastModified, Date startTime, Date endTime,
+ List<String> inputMediaAssets, List<String> outputMediaAssets, JobInfo actualJob) {
assertNotNull(message, actualJob);
+
+ assertNotNull(message + "Id", actualJob.getId());
+
assertEquals(message + " Name", testName, actualJob.getName());
// comment out due to issue 464
// assertEquals(message + " Priority", priority, actualJob.getPriority());
assertEquals(message + " RunningDuration", runningDuration, actualJob.getRunningDuration());
assertEquals(message + " State", state, actualJob.getState());
- // commented out due to issue 463
- // assertEquals(message + " TemplateId", templateId, actualJob.getTemplateId());
+ assertEqualsNullEmpty(message + " TemplateId", templateId, actualJob.getTemplateId());
+
+ assertDateApproxEquals(message + " Created", created, actualJob.getCreated());
+ assertDateApproxEquals(message + " LastModified", lastModified, actualJob.getLastModified());
+ assertDateApproxEquals(message + " StartTime", startTime, actualJob.getStartTime());
+ assertDateApproxEquals(message + " EndTime", endTime, actualJob.getEndTime());
+
+ // TODO: Add test for accessing the input and output media assets when fixed:
+ // https://github.com/WindowsAzure/azure-sdk-for-java-pr/issues/508
assertEquals(message + " InputMediaAssets", inputMediaAssets, actualJob.getInputMediaAssets());
assertEquals(message + " OutputMediaAssets", outputMediaAssets, actualJob.getOutputMediaAssets());
+
+ // TODO: Add test for accessing the tasks when fixed:
+ // https://github.com/WindowsAzure/azure-sdk-for-java-pr/issues/531
+ }
+
+ private void assertEqualsNullEmpty(String message, String expected, String actual) {
+ if ((expected == null || expected.length() == 0) && (actual == null || actual.length() == 0)) {
+ // both nullOrEmpty, so match.
+ }
+ else {
+ assertEquals(message, expected, actual);
+ }
}
private JobInfo createJob(String name) throws ServiceException {
- String assetId = createFileAsset(name);
URI serviceUri = service.getRestServiceUri();
- return service.create(Job
- .create(serviceUri)
- .setName("My Encoding Job")
- .setPriority(3)
- .addInputMediaAsset(assetId)
- .addTaskCreator(
- Task.create().setConfiguration("H.264 256k DSL CBR")
- .setMediaProcessorId("nb:mpid:UUID:2f381738-c504-4e4a-a38e-d199e207fcd5")
- .setName("My encoding Task").setTaskBody(taskBody)));
+ return service.create(Job.create(serviceUri).setName(name).setPriority(3).addInputMediaAsset(assetInfo.getId())
+ .addTaskCreator(getTaskCreator(0)));
}
- @Test
- public void createJobSuccess() throws Exception {
- // Arrange
- AssetInfo assetInfo = service.create(Asset.create());
+ private CreateBatchOperation getTaskCreator(int outputAssetPosition) {
+ return Task
+ .create()
+ .setConfiguration("H.264 256k DSL CBR")
+ .setMediaProcessorId("nb:mpid:UUID:2f381738-c504-4e4a-a38e-d199e207fcd5")
+ .setName("My encoding Task")
+ .setTaskBody(
+ "<taskBody>" + "<inputAsset>JobInputAsset(0)</inputAsset>" + "<outputAsset>JobOutputAsset("
+ + outputAssetPosition + ")</outputAsset>" + "</taskBody>");
+ }
+
+ @BeforeClass
+ public static void setup() throws Exception {
+ IntegrationTestBase.setup();
- JobInfo expectedJob = new JobInfo(null, new JobType().setName("My Encoding Job").setPriority(3)
- .setRunningDuration(0.0).setState(JobState.Queued.getCode()));
+ String name = UUID.randomUUID().toString();
+ String testBlobName = "test" + name + ".bin";
+ assetInfo = service.create(Asset.create().setName(testAssetPrefix + name));
- AccessPolicyInfo accessPolicyInfo = createWritableAccessPolicy("createJobSuccess", 10);
+ AccessPolicyInfo accessPolicyInfo = service.create(AccessPolicy.create(testPolicyPrefix + name, 10,
+ EnumSet.of(AccessPolicyPermission.WRITE)));
LocatorInfo locator = createLocator(accessPolicyInfo, assetInfo, 5, 10);
WritableBlobContainerContract blobWriter = service.createBlobWriter(locator);
- createAndUploadBlob(blobWriter, "blob1.bin", testBlobData);
-
- service.create(AssetFile.create(assetInfo.getId(), "blob1.bin").setIsPrimary(true).setIsEncrypted(false)
- .setContentFileSize(new Long(testBlobData.length)));
+ InputStream blobContent = new ByteArrayInputStream(testBlobData);
+ blobWriter.createBlockBlob(testBlobName, blobContent);
service.action(AssetFile.createFileInfos(assetInfo.getId()));
+ }
- URI serviceURI = service.getRestServiceUri();
+ @Test
+ public void createJobSuccess() throws Exception {
+ // Arrange
+ String name = testJobPrefix + "createJobSuccess";
+ int priority = 3;
+ double duration = 0.0;
+ JobState state = JobState.Queued;
+ String templateId = null;
+ List<String> inputMediaAssets = null;
+ List<String> outputMediaAssets = null;
+ Date created = new Date();
+ Date lastModified = new Date();
+ Date stateTime = null;
+ Date endTime = null;
// Act
- JobInfo actualJob = service.create(Job
- .create(serviceURI)
- .setName("My Encoding Job")
- .setPriority(3)
- .addInputMediaAsset(assetInfo.getId())
- .addTaskCreator(
- Task.create().setConfiguration("H.264 256k DSL CBR")
- .setMediaProcessorId("nb:mpid:UUID:2f381738-c504-4e4a-a38e-d199e207fcd5")
- .setName("My encoding Task").setTaskBody(taskBody)));
+ JobInfo actualJob = service.create(Job.create(service.getRestServiceUri()).setName(name).setPriority(priority)
+ .addInputMediaAsset(assetInfo.getId()).addTaskCreator(getTaskCreator(0)));
// Assert
- verifyJobInfoEqual("actualJob", expectedJob, actualJob);
+ verifyJobProperties("actualJob", name, priority, duration, state, templateId, created, lastModified, stateTime,
+ endTime, inputMediaAssets, outputMediaAssets, actualJob);
}
@Test
- public void getJobSuccess() throws Exception {
+ public void createJobTwoTasksSuccess() throws Exception {
// Arrange
- JobInfo expectedJob = new JobInfo(null, new JobType().setName("My Encoding Job").setPriority(3)
- .setRunningDuration(0.0).setState(JobState.Queued.getCode()));
+ String name = testJobPrefix + "createJobSuccess";
+ int priority = 3;
+ double duration = 0.0;
+ JobState state = JobState.Queued;
+ String templateId = null;
+ List<String> inputMediaAssets = null;
+ List<String> outputMediaAssets = null;
+ Date created = new Date();
+ Date lastModified = new Date();
+ Date stateTime = null;
+ Date endTime = null;
+ List<CreateBatchOperation> tasks = new ArrayList<CreateBatchOperation>();
+ tasks.add(getTaskCreator(0));
+ tasks.add(getTaskCreator(1));
+
+ // Act
+ JobInfo actualJob = service.create(Job.create(service.getRestServiceUri()).setName(name).setPriority(priority)
+ .addInputMediaAsset(assetInfo.getId()).addTaskCreator(tasks.get(0)).addTaskCreator(tasks.get(1)));
+
+ // Assert
+ verifyJobProperties("actualJob", name, priority, duration, state, templateId, created, lastModified, stateTime,
+ endTime, inputMediaAssets, outputMediaAssets, actualJob);
+ }
- String jobId = createJob("getJobSuccess").getId();
+ @Test
+ public void getJobSuccess() throws Exception {
+ // Arrange
+ String name = testJobPrefix + "getJobSuccess";
+ int priority = 3;
+ double duration = 0.0;
+ JobState state = JobState.Queued;
+ String templateId = null;
+ List<String> inputMediaAssets = null;
+ List<String> outputMediaAssets = null;
+ String jobId = createJob(name).getId();
+ Date created = new Date();
+ Date lastModified = new Date();
+ Date stateTime = null;
+ Date endTime = null;
// Act
JobInfo actualJob = service.get(Job.get(jobId));
// Assert
- verifyJobInfoEqual("actualJob", expectedJob, actualJob);
+ verifyJobProperties("actualJob", name, priority, duration, state, templateId, created, lastModified, stateTime,
+ endTime, inputMediaAssets, outputMediaAssets, actualJob);
}
@Test
@@ -172,7 +216,7 @@ public void getJobInvalidIdFailed() throws ServiceException {
@Test
public void listJobSuccess() throws ServiceException {
// Arrange
- JobInfo jobInfo = createJob("listJobSuccess");
+ JobInfo jobInfo = createJob(testJobPrefix + "listJobSuccess");
List<JobInfo> jobInfos = new ArrayList<JobInfo>();
jobInfos.add(jobInfo);
ListResult<JobInfo> expectedListJobsResult = new ListResult<JobInfo>(jobInfos);
@@ -191,12 +235,10 @@ public void verifyEquals(String message, Object expected, Object actual) {
@Test
public void canListJobsWithOptions() throws ServiceException {
- String[] assetNames = new String[] { testJobPrefix + "assetListOptionsA", testJobPrefix + "assetListOptionsB",
- testJobPrefix + "assetListOptionsC", testJobPrefix + "assetListOptionsD" };
+ String[] assetNameSuffixes = new String[] { "A", "B", "C", "D" };
List<JobInfo> expectedJobs = new ArrayList<JobInfo>();
- for (int i = 0; i < assetNames.length; i++) {
- String name = assetNames[i];
- JobInfo jobInfo = createJob(name);
+ for (String suffix : assetNameSuffixes) {
+ JobInfo jobInfo = createJob(testJobPrefix + "assetListOptions" + suffix);
expectedJobs.add(jobInfo);
}
@@ -211,7 +253,7 @@ public void canListJobsWithOptions() throws ServiceException {
@Test
public void cancelJobSuccess() throws Exception {
// Arrange
- JobInfo jobInfo = createJob("cancelJobSuccess");
+ JobInfo jobInfo = createJob(testJobPrefix + "cancelJobSuccess");
// Act
service.action(Job.cancel(jobInfo.getId()));
@@ -237,7 +279,7 @@ public void cancelJobFailedWithInvalidId() throws ServiceException {
@Test
public void deleteJobSuccess() throws ServiceException {
// Arrange
- JobInfo jobInfo = createJob("deleteJobSuccess");
+ JobInfo jobInfo = createJob(testJobPrefix + "deleteJobSuccess");
service.action(Job.cancel(jobInfo.getId()));
JobInfo cancellingJobInfo = service.get(Job.get(jobInfo.getId()));
while (cancellingJobInfo.getState() == JobState.Canceling) {

0 comments on commit 009c74d

Please sign in to comment.