Skip to content

Commit

Permalink
Merge pull request Azure#36 from christav/list-other-asset-links-507
Browse files Browse the repository at this point in the history
Add relationship navigation
  • Loading branch information
Chris Tavares committed Dec 15, 2012
2 parents a7347f2 + d9dc999 commit a9c8d4d
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,35 @@

package com.microsoft.windowsazure.services.media.implementation.entities;


/**
* Generic implementation of the get operation usable for most entities
*
*/
public class DefaultGetOperation<T> extends EntityOperationSingleResultBase<T> implements EntityGetOperation<T> {

/**
* @param uri
* Construct a new DefaultGetOperation to return the given entity id
*
* @param entityTypeUri
* Entity set URI
* @param entityId
* id of entity
* @param responseClass
* class to return from the get operation
*/
public DefaultGetOperation(String entityTypeUri, String entityId, Class<T> responseClass) {
super(new EntityOperationBase.EntityIdUriBuilder(entityTypeUri, entityId), responseClass);
}

/**
* Construct a new DefaultGetOperation to return the entity from the given uri
*
* @param uri
* Uri for entity
* @param responseClass
* class to return from the get operation
*/
public DefaultGetOperation(String uri, Class<T> responseClass) {
super(uri, responseClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ public static EntityGetOperation<AccessPolicyInfo> get(String accessPolicyId) {
return new DefaultGetOperation<AccessPolicyInfo>(ENTITY_SET, accessPolicyId, AccessPolicyInfo.class);
}

/**
* Create an operation that will retrieve the access policy at the given link
*
* @param link
* the link
* @return the operation
*/
public static EntityGetOperation<AccessPolicyInfo> get(LinkInfo link) {
return new DefaultGetOperation<AccessPolicyInfo>(link.getHref(), AccessPolicyInfo.class);
}

/**
* Create an operation that will retrieve all access policies
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ public static EntityGetOperation<AssetInfo> get(String assetId) {
return new DefaultGetOperation<AssetInfo>(ENTITY_SET, assetId, AssetInfo.class);
}

/**
* Get the asset at the given link
*
* @param link
* the link
* @return the get operation
*/
public static EntityGetOperation<AssetInfo> get(LinkInfo link) {
return new DefaultGetOperation<AssetInfo>(link.getHref(), AssetInfo.class);
}

/**
* Create an operation that will list all the assets.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,14 @@ public static DefaultListOperation<AssetFileInfo> list() {
}

/**
* Calls the service to list files for an asset
* Create an operation that will list all the AssetFiles at the given link.
*
* @param assetId
* asset to list files for
* @return the list operation object
* @param link
* Link to request AssetFiles from.
* @return The list operation.
*/
public static DefaultListOperation<AssetFileInfo> list(String assetId) {
String assetUri = new EntityOperationBase.EntityIdUriBuilder("Assets", assetId).getUri() + "/Files";
return new DefaultListOperation<AssetFileInfo>(assetUri, new GenericType<ListResult<AssetFileInfo>>() {
public static DefaultListOperation<AssetFileInfo> list(LinkInfo link) {
return new DefaultListOperation<AssetFileInfo>(link.getHref(), new GenericType<ListResult<AssetFileInfo>>() {
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,41 @@ public String getAlternateId() {
public AssetOption getOptions() {
return AssetOption.fromCode(getContent().getOptions());
}

/**
* Get a link to the asset's files
*
* @return the link
*/
public LinkInfo getAssetFilesLink() {
return getRelationLink("Files");
}

/**
* Get a link to the asset's content keys
*
* @return the link
*/
public LinkInfo getContentKeysLink() {
return getRelationLink("ContentKeys");
}

/**
* Get a link to the asset's locators
*
* @return the link
*/
public LinkInfo getLocatorsLink() {
return getRelationLink("Locators");
}

/**
* Get a link to the asset's locators
*
* @return the link
*/
public LinkInfo getParentAssetsLink() {
// TODO: NEEDS TESTS once we figure out how to create assets with parents
return getRelationLink("ParentAssets");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ public static DefaultListOperation<ContentKeyInfo> list(MultivaluedMap<String, S
}, queryParameters);
}

/**
* Create an operation that will list all the content keys at the given link.
*
* @param link
* Link to request content keys from.
* @return The list operation.
*/
public static DefaultListOperation<ContentKeyInfo> list(LinkInfo link) {
return new DefaultListOperation<ContentKeyInfo>(link.getHref(), new GenericType<ListResult<ContentKeyInfo>>() {
});
}

/**
* Create an operation to delete the given content key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ public static DefaultListOperation<LocatorInfo> list(MultivaluedMap<String, Stri
}, queryParameters);
}

/**
* Create an operation that will list all the locators at the given link.
*
* @param link
* Link to request locators from.
* @return The list operation.
*/
public static DefaultListOperation<LocatorInfo> list(LinkInfo link) {
return new DefaultListOperation<LocatorInfo>(link.getHref(), new GenericType<ListResult<LocatorInfo>>() {
});
}

/**
* Create an operation to update the given locator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,22 @@ public String getBaseUri() {
public String getContentAccessToken() {
return this.getContent().getContentAccessComponent();
}

/**
* Return a link that gets this locator's access policy
*
* @return the link
*/
public LinkInfo getAccessPolicyLink() {
return getRelationLink("AccessPolicy");
}

/**
* Return a link that gets this locator's asset
*
* @return the link
*/
public LinkInfo getAssetLink() {
return getRelationLink("Asset");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void canCreateFileForUploadedBlob() throws Exception {

service.action(AssetFile.createFileInfos(asset.getId()));

ListResult<AssetFileInfo> files = service.list(AssetFile.list(asset.getId()));
ListResult<AssetFileInfo> files = service.list(AssetFile.list(asset.getAssetFilesLink()));

assertEquals(1, files.size());
AssetFileInfo file = files.get(0);
Expand All @@ -85,7 +85,7 @@ public void canCreateFileEntityDirectly() throws Exception {

service.create(AssetFile.create(asset.getId(), BLOB_NAME_2));

ListResult<AssetFileInfo> files = service.list(AssetFile.list(asset.getId()));
ListResult<AssetFileInfo> files = service.list(AssetFile.list(asset.getAssetFilesLink()));

boolean found = false;
for (AssetFileInfo file : files) {
Expand Down Expand Up @@ -119,7 +119,7 @@ public void canCreateAssetWithMultipleFiles() throws Exception {
AssetFileInfo file3 = service.create(AssetFile.create(asset.getId(), "blob3.bin").setIsPrimary(false)
.setIsEncrypted(false).setContentFileSize(new Long(countingUp.length)).setContentChecksum("1234"));

ListResult<AssetFileInfo> files = service.list(AssetFile.list(asset.getId()));
ListResult<AssetFileInfo> files = service.list(AssetFile.list(asset.getAssetFilesLink()));

assertEquals(3, files.size());

Expand Down Expand Up @@ -166,7 +166,7 @@ public void canDeleteFileFromAsset() throws Exception {

service.action(AssetFile.createFileInfos(asset.getId()));

ListResult<AssetFileInfo> originalFiles = service.list(AssetFile.list(asset.getId()));
ListResult<AssetFileInfo> originalFiles = service.list(AssetFile.list(asset.getAssetFilesLink()));
assertEquals(2, originalFiles.size());

for (AssetFileInfo file : originalFiles) {
Expand All @@ -176,7 +176,7 @@ public void canDeleteFileFromAsset() throws Exception {
}
}

ListResult<AssetFileInfo> newFiles = service.list(AssetFile.list(asset.getId()));
ListResult<AssetFileInfo> newFiles = service.list(AssetFile.list(asset.getAssetFilesLink()));
assertEquals(1, newFiles.size());
assertEquals("tokeep.bin", newFiles.get(0).getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.List;
import java.util.UUID;

import org.junit.Ignore;
import org.junit.Test;

import com.microsoft.windowsazure.services.core.ServiceException;
Expand Down Expand Up @@ -275,7 +274,6 @@ public void deleteAssetFailedWithInvalidId() throws ServiceException {
service.delete(Asset.delete(validButNonexistAssetId));
}

@Ignore("due to issue 507")
@Test
public void linkAssetContentKeySuccess() throws ServiceException, URISyntaxException {
// Arrange
Expand All @@ -284,8 +282,7 @@ public void linkAssetContentKeySuccess() throws ServiceException, URISyntaxExcep
.setOptions(AssetOption.StorageEncrypted));
String contentKeyId = String.format("nb:kid:UUID:%s", UUID.randomUUID());
String encryptedContentKey = "dummyEncryptedContentKey";
ContentKeyInfo contentKeyInfo = service.create(ContentKey.create(contentKeyId,
ContentKeyType.StorageEncryption, encryptedContentKey));
service.create(ContentKey.create(contentKeyId, ContentKeyType.StorageEncryption, encryptedContentKey));
String escapedContentKeyId;
try {
escapedContentKeyId = URLEncoder.encode(contentKeyId, "UTF-8");
Expand All @@ -300,16 +297,14 @@ public void linkAssetContentKeySuccess() throws ServiceException, URISyntaxExcep

// Assert

// List<ContentKeyInfo> contentKeyInfos = service.list(ContentKey.list(assetInfo.getId()));
// ContentKeyInfo contentKeyInfo = contentKeyInfos.get(0)
// assertEquals(contentKeyId, contentKeyInfo.getId());

List<ContentKeyInfo> contentKeys = service.list(ContentKey.list(assetInfo.getContentKeysLink()));
assertEquals(1, contentKeys.size());
assertEquals(contentKeyId, contentKeys.get(0).getId());
}

@Test
public void linkAssetContentKeyInvalidIdFailed() throws ServiceException, URISyntaxException {
// Arrange
String originalTestName = testAssetPrefix + "linkAssetContentKeyInvalidIdFailed";
URI invalidContentKeyUri = new URI("ContentKeys('nb%3akid%3aUUID%3ainvalidContentKeyId')");

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,37 @@ public void deleteLocatorInvalidIdFailed() throws ServiceException {
expectedException.expect(new ServiceExceptionMatcher(400));
service.delete(Locator.delete(invalidId));
}

@Test
public void canGetLocatorBackFromAsset() throws Exception {
LocatorInfo locator = service.create(Locator.create(accessPolicyInfo.getId(), assetInfo.getId(),
LocatorType.SAS));

ListResult<LocatorInfo> locators = service.list(Locator.list(assetInfo.getLocatorsLink()));

assertEquals(1, locators.size());
assertEquals(locator.getId(), locators.get(0).getId());

}

@Test
public void canGetAssetFromLocator() throws Exception {
LocatorInfo locator = service.create(Locator.create(accessPolicyInfo.getId(), assetInfo.getId(),
LocatorType.SAS));

AssetInfo asset = service.get(Asset.get(locator.getAssetLink()));

assertEquals(assetInfo.getId(), asset.getId());
}

@Test
public void canGetAccessPolicyFromLocator() throws Exception {
LocatorInfo locator = service.create(Locator.create(accessPolicyInfo.getId(), assetInfo.getId(),
LocatorType.SAS));

AccessPolicyInfo accessPolicy = service.get(AccessPolicy.get(locator.getAccessPolicyLink()));

assertEquals(accessPolicyInfo.getId(), accessPolicy.getId());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ public void getByIdHasCorrectUri() throws Exception {
assertEquals(expectedUri, getter.getUri());
}

@Test
public void listFileInfosForAnAssetHasCorrectUri() throws Exception {
String expectedUri = String.format("Assets(%s)/Files", encodedAssetId);
EntityListOperation<AssetFileInfo> lister = AssetFile.list(exampleAssetId);

assertEquals(expectedUri, lister.getUri());
}

@Test
public void listAllFileInfosHasCorrectUri() throws Exception {
String expectedUri = "Files";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import com.microsoft.windowsazure.services.media.models.AssetFile;
import com.microsoft.windowsazure.services.media.models.AssetFileInfo;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.microsoft.windowsazure.services.media.models.AssetState;
import com.microsoft.windowsazure.services.media.models.AssetOption;
import com.microsoft.windowsazure.services.media.models.AssetState;
import com.microsoft.windowsazure.services.media.models.JobInfo;
import com.microsoft.windowsazure.services.media.models.ListResult;
import com.microsoft.windowsazure.services.media.models.Task;
Expand All @@ -54,7 +54,7 @@ public void validateAsset(AssetInfo asset, String name, AssetOption encryption)
assertEquals("asset.getOptions", encryption, asset.getOptions());

// Verify no files by default.
List<AssetFileInfo> initialFiles = service.list(AssetFile.list(asset.getId()));
List<AssetFileInfo> initialFiles = service.list(AssetFile.list(asset.getAssetFilesLink()));
assertNotNull("initialFiles", initialFiles);
assertEquals("initialFiles.size", 0, initialFiles.size());

Expand Down Expand Up @@ -90,7 +90,7 @@ public void validateAssetSortedPages(List<ListResult<AssetInfo>> pages, List<Str

public void validateAssetFiles(AssetInfo asset, Hashtable<String, InputStream> inputFiles) throws ServiceException,
IOException, NoSuchAlgorithmException {
List<AssetFileInfo> assetFiles = service.list(AssetFile.list(asset.getId()));
List<AssetFileInfo> assetFiles = service.list(AssetFile.list(asset.getAssetFilesLink()));

assertNotNull("assetFiles", assetFiles);
assertEquals("assetFiles.size", inputFiles.size(), assetFiles.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ public void uploadFilesToAsset(AssetInfo asset, int uploadWindowInMinutes, Hasht
}

service.action(AssetFile.createFileInfos(asset.getId()));
for (AssetFileInfo assetFile : service.list(AssetFile.list(asset.getId()))) {
for (AssetFileInfo assetFile : service.list(AssetFile.list(asset.getAssetFilesLink()))) {

AssetFileInfo x = infoToUpload.get(assetFile.getName());
System.out.println(x);
service.update(AssetFile.update(assetFile.getId()).setContentChecksum(x.getContentChecksum())
.setContentFileSize(x.getContentFileSize()).setIsPrimary(x.getIsPrimary()));
}

service.list(AssetFile.list(asset.getId()));
service.list(AssetFile.list(asset.getAssetFilesLink()));

service.delete(Locator.delete(locator.getId()));
service.delete(AccessPolicy.delete(accessPolicy.getId()));
Expand Down Expand Up @@ -321,7 +321,7 @@ private List<URL> createOriginUrlsForStreamingContentWorker(AssetInfo asset, int
availabilityWindowInMinutes, EnumSet.of(AccessPolicyPermission.READ)));
LocatorInfo readLocator = service.create(Locator.create(readAP.getId(), asset.getId(), locatorType));

List<AssetFileInfo> publishedFiles = service.list(AssetFile.list(asset.getId()));
List<AssetFileInfo> publishedFiles = service.list(AssetFile.list(asset.getAssetFilesLink()));
for (AssetFileInfo fi : publishedFiles) {
if (isSmooth) {
// Smooth Streaming format ends with ".ism*"
Expand Down

0 comments on commit a9c8d4d

Please sign in to comment.