Skip to content

Commit

Permalink
Merge pull request Azure#76 from WindowsAzure/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Albert Cheng committed Jan 4, 2013
2 parents a41d809 + f914116 commit 9a08f79
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

package com.microsoft.windowsazure.services.media.models;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.security.InvalidParameterException;

import javax.ws.rs.core.MultivaluedMap;

Expand Down Expand Up @@ -302,7 +305,15 @@ public static EntityDeleteOperation delete(String assetId) {
* the content key uri
* @return the entity action operation
*/
public static EntityLinkOperation linkContentKey(String assetId, URI contentKeyUri) {
public static EntityLinkOperation linkContentKey(String assetId, String contentKeyId) {
String escapedContentKeyId = null;
try {
escapedContentKeyId = URLEncoder.encode(contentKeyId, "UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new InvalidParameterException("contentKeyId");
}
URI contentKeyUri = URI.create(String.format("ContentKeys('%s')", escapedContentKeyId));
return new EntityLinkOperation("Assets", assetId, "ContentKeys", contentKeyUri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public CreateBatchOperation(String mediaProcessorId, String taskBody) {
* the options
* @return the creates the batch operation
*/
public CreateBatchOperation setOptions(int options) {
this.taskType.setOptions(options);
public CreateBatchOperation setOptions(TaskOption options) {
this.taskType.setOptions(options.getCode());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@

import static org.junit.Assert.*;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -283,17 +279,9 @@ public void linkAssetContentKeySuccess() throws ServiceException, URISyntaxExcep
String contentKeyId = String.format("nb:kid:UUID:%s", UUID.randomUUID());
String encryptedContentKey = "dummyEncryptedContentKey";
service.create(ContentKey.create(contentKeyId, ContentKeyType.StorageEncryption, encryptedContentKey));
String escapedContentKeyId;
try {
escapedContentKeyId = URLEncoder.encode(contentKeyId, "UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new InvalidParameterException(contentKeyId);
}
URI contentKeyUri = new URI(String.format("ContentKeys('%s')", escapedContentKeyId));

// Act
service.action(Asset.linkContentKey(assetInfo.getId(), contentKeyUri));
service.action(Asset.linkContentKey(assetInfo.getId(), contentKeyId));

// Assert

Expand All @@ -305,14 +293,12 @@ public void linkAssetContentKeySuccess() throws ServiceException, URISyntaxExcep
@Test
public void linkAssetContentKeyInvalidIdFailed() throws ServiceException, URISyntaxException {
// Arrange
URI invalidContentKeyUri = new URI("ContentKeys('nb%3akid%3aUUID%3ainvalidContentKeyId')");

// Act
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(400));
service.action(Asset.linkContentKey(validButNonexistAssetId, invalidContentKeyUri));
service.action(Asset.linkContentKey(validButNonexistAssetId, "nb:kid:UUID:invalidContentKeyId"));

// Assert

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.security.Security;
import java.util.EnumSet;
import java.util.List;
Expand Down Expand Up @@ -108,12 +105,6 @@ private MediaProcessorInfo GetMediaProcessor(String mediaProcessorName) throws S
return null;
}

private void linkContentKey(AssetInfo assetInfo, ContentKeyInfo contentKeyInfo) throws ServiceException,
UnsupportedEncodingException {
URI contentKeyUri = createContentKeyUri(contentKeyInfo.getId());
service.action(Asset.linkContentKey(assetInfo.getId(), contentKeyUri));
}

private String getProtectionKey(String protectionKeyId) throws ServiceException {
String protectionKey = (String) service.action(ProtectionKey.getProtectionKey(protectionKeyId));
return protectionKey;
Expand All @@ -131,11 +122,6 @@ private AssetFileInfo uploadEncryptedAssetFile(AssetInfo assetInfo, LocatorInfo
return assetFileInfo;
}

private URI createContentKeyUri(String contentKeyId) throws UnsupportedEncodingException {
String escapedContentKeyId = URLEncoder.encode(contentKeyId, "UTF-8");
return URI.create(String.format("ContentKeys('%s')", escapedContentKeyId));
}

private ContentKeyInfo createContentKey(byte[] aesKey, ContentKeyType contentKeyType, String protectionKeyId,
String protectionKey) throws Exception {
UUID contentKeyIdUuid = UUID.randomUUID();
Expand Down Expand Up @@ -182,7 +168,7 @@ public void uploadAesProtectedAssetAndDownloadSuccess() throws Exception {
protectionKey);

// link the content key with the asset.
linkContentKey(assetInfo, contentKeyInfo);
service.action(Asset.linkContentKey(assetInfo.getId(), contentKeyInfo.getId()));

// encrypt the file.
byte[] encryptedContent = EncryptionHelper.EncryptFile(smallWMVInputStream, aesKey, initializationVector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
Expand Down Expand Up @@ -145,8 +162,32 @@ 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 (isJobBusy(job.getState())) {
service.action(Job.cancel(job.getId()));
job = service.get(Job.get(job.getId()));
}

int retryCounter = 0;
while (isJobBusy(job.getState()) && retryCounter < 10) {
Thread.sleep(2000);
job = service.get(Job.get(job.getId()));
retryCounter++;
}

if (!isJobBusy(job.getState())) {
service.delete(Job.delete(job.getId()));
}
else {
// Not much to do so except wait.
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
Expand All @@ -155,10 +196,32 @@ private static void removeAllTestJobs() {
}
}

private static boolean isJobBusy(JobState state) {
return state == JobState.Canceling || state == JobState.Scheduled || state == JobState.Queued
|| state == JobState.Processing;
}

interface ComponentDelegate {
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 {

Expand Down Expand Up @@ -209,6 +272,15 @@ protected <T> void verifyListResultContains(String message, List<T> expectedInfo
}
}

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);
}
Expand Down
Loading

0 comments on commit 9a08f79

Please sign in to comment.