Permalink
Browse files

Promoting some Job method to base for use by Task tests

Cleanup of "throws" clauses
Cleanup of scenario tests for jobs
  • Loading branch information...
1 parent 85e893b commit d09c8f0a29dfe9bef8f5023794c2852e2fc8e10d @jcookems jcookems committed Dec 19, 2012
@@ -2,11 +2,15 @@
import static org.junit.Assert.*;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
+import java.util.EnumSet;
import java.util.List;
+import java.util.UUID;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -17,12 +21,15 @@
import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.media.models.AccessPolicy;
import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo;
+import com.microsoft.windowsazure.services.media.models.AccessPolicyPermission;
import com.microsoft.windowsazure.services.media.models.Asset;
+import com.microsoft.windowsazure.services.media.models.AssetFile;
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.JobState;
import com.microsoft.windowsazure.services.media.models.ListResult;
import com.microsoft.windowsazure.services.media.models.Locator;
import com.microsoft.windowsazure.services.media.models.LocatorInfo;
@@ -116,8 +123,18 @@ private static void removeAllTestAssets() {
try {
List<AssetInfo> listAssetsResult = service.list(Asset.list());
for (AssetInfo assetInfo : listAssetsResult) {
- if (assetInfo.getName().startsWith(testAssetPrefix)) {
- service.delete(Asset.delete(assetInfo.getId()));
+ try {
+ if (assetInfo.getName().startsWith(testAssetPrefix)) {
+ service.delete(Asset.delete(assetInfo.getId()));
+ }
+ else if (assetInfo.getName().startsWith("JobOutputAsset(")
+ && assetInfo.getName().contains(testJobPrefix)) {
+ // Delete the temp assets associated with Job results.
+ service.delete(Asset.delete(assetInfo.getId()));
+ }
+ }
+ catch (Exception e) {
+ e.printStackTrace();
}
}
}
@@ -145,8 +162,24 @@ 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()));
+ if (job.getName().startsWith(testJobPrefix)) {
+ // Job can't be deleted when it's state is
+ // canceling, scheduled,queued or processing
+ try {
+ if (job.getState() == JobState.Scheduled || job.getState() == JobState.Queued
+ || job.getState() == JobState.Processing) {
+ service.action(Job.cancel(job.getId()));
+ }
+ else if (job.getState() != JobState.Canceling) {
+ service.delete(Job.delete(job.getId()));
+ }
+ else {
+ // Not much to do so except wait.
+ }
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
}
}
}
@@ -159,6 +192,23 @@ private static void removeAllTestJobs() {
void verifyEquals(String message, Object expected, Object actual);
}
+ protected static AssetInfo setupAssetWithFile() throws ServiceException {
+ String name = UUID.randomUUID().toString();
+ String testBlobName = "test" + name + ".bin";
+ AssetInfo assetInfo = service.create(Asset.create().setName(testAssetPrefix + name));
+
+ AccessPolicyInfo accessPolicyInfo = service.create(AccessPolicy.create(testPolicyPrefix + name, 10,
+ EnumSet.of(AccessPolicyPermission.WRITE)));
+ LocatorInfo locator = createLocator(accessPolicyInfo, assetInfo, 5);
+ WritableBlobContainerContract blobWriter = service.createBlobWriter(locator);
+ InputStream blobContent = new ByteArrayInputStream(new byte[] { 4, 8, 15, 16, 23, 42 });
+ blobWriter.createBlockBlob(testBlobName, blobContent);
+
+ service.action(AssetFile.createFileInfos(assetInfo.getId()));
+
+ return assetInfo;
+ }
+
protected static LocatorInfo createLocator(AccessPolicyInfo accessPolicy, AssetInfo asset, int startDeltaMinutes)
throws ServiceException {
@@ -209,6 +259,15 @@ protected static LocatorInfo createLocator(AccessPolicyInfo accessPolicy, AssetI
}
}
+ protected 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);
+ }
+ }
+
protected void assertDateApproxEquals(Date expected, Date actual) {
assertDateApproxEquals("", expected, actual);
}
@@ -17,38 +17,28 @@
import static org.junit.Assert.*;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
-import java.util.EnumSet;
import java.util.List;
-import java.util.UUID;
import org.junit.BeforeClass;
import org.junit.Test;
import com.microsoft.windowsazure.services.core.ServiceException;
-import com.microsoft.windowsazure.services.media.models.AccessPolicy;
-import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo;
-import com.microsoft.windowsazure.services.media.models.AccessPolicyPermission;
import com.microsoft.windowsazure.services.media.models.Asset;
-import com.microsoft.windowsazure.services.media.models.AssetFile;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.microsoft.windowsazure.services.media.models.ErrorDetail;
import com.microsoft.windowsazure.services.media.models.Job;
import com.microsoft.windowsazure.services.media.models.JobInfo;
import com.microsoft.windowsazure.services.media.models.JobState;
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.microsoft.windowsazure.services.media.models.TaskInfo;
public class JobIntegrationTest extends IntegrationTestBase {
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(),
@@ -79,15 +69,6 @@ private void verifyJobProperties(String message, String testName, Integer priori
// 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 {
return service.create(Job.create().setName(name).setPriority(3).addInputMediaAsset(assetInfo.getId())
.addTaskCreator(getTaskCreator(0)));
@@ -104,23 +85,11 @@ private CreateBatchOperation getTaskCreator(int outputAssetPosition) {
@BeforeClass
public static void setup() throws Exception {
IntegrationTestBase.setup();
-
- String name = UUID.randomUUID().toString();
- String testBlobName = "test" + name + ".bin";
- assetInfo = service.create(Asset.create().setName(testAssetPrefix + name));
-
- AccessPolicyInfo accessPolicyInfo = service.create(AccessPolicy.create(testPolicyPrefix + name, 10,
- EnumSet.of(AccessPolicyPermission.WRITE)));
- LocatorInfo locator = createLocator(accessPolicyInfo, assetInfo, 5);
- WritableBlobContainerContract blobWriter = service.createBlobWriter(locator);
- InputStream blobContent = new ByteArrayInputStream(testBlobData);
- blobWriter.createBlockBlob(testBlobName, blobContent);
-
- service.action(AssetFile.createFileInfos(assetInfo.getId()));
+ assetInfo = setupAssetWithFile();
}
@Test
- public void createJobSuccess() throws Exception {
+ public void createJobSuccess() throws ServiceException {
// Arrange
String name = testJobPrefix + "createJobSuccess";
int priority = 3;
@@ -142,7 +111,7 @@ public void createJobSuccess() throws Exception {
}
@Test
- public void createJobTwoTasksSuccess() throws Exception {
+ public void createJobTwoTasksSuccess() throws ServiceException {
// Arrange
String name = testJobPrefix + "createJobSuccess";
int priority = 3;
@@ -167,7 +136,7 @@ public void createJobTwoTasksSuccess() throws Exception {
}
@Test
- public void getJobSuccess() throws Exception {
+ public void getJobSuccess() throws ServiceException {
// Arrange
String name = testJobPrefix + "getJobSuccess";
int priority = 3;
@@ -231,7 +200,7 @@ public void canListJobsWithOptions() throws ServiceException {
}
@Test
- public void cancelJobSuccess() throws Exception {
+ public void cancelJobSuccess() throws ServiceException {
// Arrange
JobInfo jobInfo = createJob(testJobPrefix + "cancelJobSuccess");
@@ -257,12 +226,13 @@ public void cancelJobFailedWithInvalidId() throws ServiceException {
}
@Test
- public void deleteJobSuccess() throws ServiceException {
+ public void deleteJobSuccess() throws ServiceException, InterruptedException {
// Arrange
JobInfo jobInfo = createJob(testJobPrefix + "deleteJobSuccess");
service.action(Job.cancel(jobInfo.getId()));
JobInfo cancellingJobInfo = service.get(Job.get(jobInfo.getId()));
while (cancellingJobInfo.getState() == JobState.Canceling) {
+ Thread.sleep(2000);
cancellingJobInfo = service.get(Job.get(jobInfo.getId()));
}
@@ -106,7 +106,7 @@ public void createJob() throws Exception {
wrapper.uploadFilesToAsset(asset, 10, getTestAssetFiles());
signalSetupFinished();
- String jobName = "my job createJob";
+ String jobName = "my job createJob" + UUID.randomUUID().toString();
JobInfo job = wrapper.createJob(jobName, asset, createTasks());
validator.validateJob(job, jobName, asset, createTasks());
}
@@ -116,13 +116,13 @@ public void transformAsset() throws Exception {
signalSetupStarting();
AssetInfo asset = wrapper.createAsset(testAssetPrefix + "transformAsset", AssetOption.None);
wrapper.uploadFilesToAsset(asset, 10, getTestAssetFiles());
- String jobName = "my job transformAsset";
+ String jobName = "my job transformAsset" + UUID.randomUUID().toString();
JobInfo job = wrapper.createJob(jobName, asset, createTasks());
signalSetupFinished();
waitForJobToFinish(job);
List<AssetInfo> outputAssets = wrapper.getJobOutputMediaAssets(job);
- validator.validateOutputAssets(outputAssets);
+ validator.validateOutputAssets(outputAssets, getTestAssetFiles().keys());
}
private void waitForJobToFinish(JobInfo job) throws InterruptedException, ServiceException {
@@ -23,6 +23,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
+import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
@@ -147,14 +148,16 @@ public void validateJob(JobInfo job, String name, AssetInfo asset, List<Task.Cre
// assertEquals("outputAssets.size()", createTasks.size(), outputAssets.size());
}
- public void validateOutputAssets(List<AssetInfo> outputAssets) throws ServiceException {
- // TODO: Uncomment when fixed:
- // https://github.com/WindowsAzure/azure-sdk-for-java-pr/issues/508
+ public void validateOutputAssets(List<AssetInfo> outputAssets, Enumeration<String> enumeration) {
+ assertNotNull("outputAssets", outputAssets);
+ for (AssetInfo asset : outputAssets) {
- // assertNotNull("outputAssets", outputAssets);
- // for (AssetInfo asset : outputAssets) {
- // this.validateAsset(asset, null, null);
- // }
+ assertNotNull("asset", asset);
+ assertNotNull("asset.getId", asset.getId());
+ assertFalse("asset.getId != ''", "".equals(asset.getId()));
+ assertEquals("asset.state", AssetState.Initialized, asset.getState());
+ assertEquals("asset.getOptions", AssetOption.None, asset.getOptions());
+ }
}
// This method is needed because there can be a delay before a new read locator

0 comments on commit d09c8f0

Please sign in to comment.