Skip to content

Commit

Permalink
Merge pull request Azure#394 from jcookems/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jcookems committed Oct 17, 2012
2 parents ca93fd6 + 3d66271 commit 74118a1
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ public interface MediaContract extends FilterableService<MediaContract> {
/**
* Create the access policy.
*
* @param name
* @param accessPolicyName
* name of access policy
* @param durationInMinutes
* Duration in minutes that blob access will be granted when using this access policy
* @return Created access policy
* @throws ServiceException
* the service exception
*/
AccessPolicyInfo createAccessPolicy(String name, double durationInMinutes) throws ServiceException;
AccessPolicyInfo createAccessPolicy(String accessPolicyName, double durationInMinutes) throws ServiceException;

/**
* Create the access policy with the given options.
*
* @param name
* @param accessPolicyName
* name of access policy
* @param durationInMinutes
* Duration in minutes that blob access will be granted when using this access policy
Expand All @@ -136,29 +136,29 @@ public interface MediaContract extends FilterableService<MediaContract> {
* @throws ServiceException
* the service exception
*/
AccessPolicyInfo createAccessPolicy(String name, double durationInMinutes, CreateAccessPolicyOptions options)
throws ServiceException;
AccessPolicyInfo createAccessPolicy(String accessPolicyName, double durationInMinutes,
CreateAccessPolicyOptions options) throws ServiceException;

/**
* Delete the access policy with the given id.
*
* @param id
* @param accessPolicyId
* of access policy to delete
* @throws ServiceException
* the service exception
*/
void deleteAccessPolicy(String id) throws ServiceException;
void deleteAccessPolicy(String accessPolicyId) throws ServiceException;

/**
* Get a single access policy.
*
* @param id
* @param accessPolicyId
* the id of the asset to retrieve
* @return the asset
* @throws ServiceException
* the service exception
*/
AccessPolicyInfo getAccessPolicy(String id) throws ServiceException;
AccessPolicyInfo getAccessPolicy(String accessPolicyId) throws ServiceException;

/**
* List access policies.
Expand Down Expand Up @@ -236,14 +236,14 @@ public LocatorInfo createLocator(String accessPolicyId, String assetId, LocatorT
/**
* Delete locator.
*
* @param id
* @param locatorId
* the id
* @throws UniformInterfaceException
* the uniform interface exception
* @throws ServiceException
* the service exception
*/
public void deleteLocator(String id) throws UniformInterfaceException, ServiceException;
public void deleteLocator(String locatorId) throws UniformInterfaceException, ServiceException;

/**
* List locators.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ public void updateAsset(String assetId, UpdateAssetOptions updateAssetOptions) t
* @see com.microsoft.windowsazure.services.media.MediaContract#createAccessPolicy(double)
*/
@Override
public AccessPolicyInfo createAccessPolicy(String name, double durationInMinutes) throws ServiceException {
public AccessPolicyInfo createAccessPolicy(String accessPolicyName, double durationInMinutes)
throws ServiceException {
try {
return service.createAccessPolicy(name, durationInMinutes);
return service.createAccessPolicy(accessPolicyName, durationInMinutes);
}
catch (UniformInterfaceException e) {
throw processCatch(new ServiceException(e));
Expand All @@ -228,10 +229,10 @@ public AccessPolicyInfo createAccessPolicy(String name, double durationInMinutes
* @see com.microsoft.windowsazure.services.media.MediaContract#createAccessPolicy(double, com.microsoft.windowsazure.services.media.models.CreateAccessPolicyOptions)
*/
@Override
public AccessPolicyInfo createAccessPolicy(String name, double durationInMinutes, CreateAccessPolicyOptions options)
throws ServiceException {
public AccessPolicyInfo createAccessPolicy(String accessPolicyName, double durationInMinutes,
CreateAccessPolicyOptions options) throws ServiceException {
try {
return service.createAccessPolicy(name, durationInMinutes, options);
return service.createAccessPolicy(accessPolicyName, durationInMinutes, options);
}
catch (UniformInterfaceException e) {
throw processCatch(new ServiceException(e));
Expand Down Expand Up @@ -261,9 +262,9 @@ public List<AccessPolicyInfo> listAccessPolicies() throws ServiceException {
* @see com.microsoft.windowsazure.services.media.MediaContract#deleteAccessPolicy(java.lang.String)
*/
@Override
public void deleteAccessPolicy(String id) throws ServiceException {
public void deleteAccessPolicy(String accessPolicyId) throws ServiceException {
try {
service.deleteAccessPolicy(id);
service.deleteAccessPolicy(accessPolicyId);
}
catch (UniformInterfaceException e) {
throw processCatch(new ServiceException(e));
Expand All @@ -277,9 +278,9 @@ public void deleteAccessPolicy(String id) throws ServiceException {
* @see com.microsoft.windowsazure.services.media.MediaContract#getAccessPolicy(java.lang.String)
*/
@Override
public AccessPolicyInfo getAccessPolicy(String id) throws ServiceException {
public AccessPolicyInfo getAccessPolicy(String accessPolicyId) throws ServiceException {
try {
return service.getAccessPolicy(id);
return service.getAccessPolicy(accessPolicyId);
}
catch (UniformInterfaceException e) {
throw processCatch(new ServiceException(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,24 @@ public void deleteAsset(String assetId) throws ServiceException {
* @see com.microsoft.windowsazure.services.media.MediaContract#createAccessPolicy(double)
*/
@Override
public AccessPolicyInfo createAccessPolicy(String name, double durationInMinutes) throws ServiceException {
return createAccessPolicy(name, durationInMinutes, null);
public AccessPolicyInfo createAccessPolicy(String accessPolicyName, double durationInMinutes)
throws ServiceException {
return createAccessPolicy(accessPolicyName, durationInMinutes, null);
}

/* (non-Javadoc)
* @see com.microsoft.windowsazure.services.media.MediaContract#createAccessPolicy(double, com.microsoft.windowsazure.services.media.models.CreateAccessPolicyOptions)
*/
@Override
public AccessPolicyInfo createAccessPolicy(String name, double durationInMinutes, CreateAccessPolicyOptions options)
throws ServiceException {
public AccessPolicyInfo createAccessPolicy(String accessPolicyName, double durationInMinutes,
CreateAccessPolicyOptions options) throws ServiceException {

if (options == null) {
options = new CreateAccessPolicyOptions().addPermissions(EnumSet.of(AccessPolicyPermission.WRITE));
}

AccessPolicyType requestData = new AccessPolicyType().setDurationInMinutes(durationInMinutes).setName(name)
AccessPolicyType requestData = new AccessPolicyType().setDurationInMinutes(durationInMinutes)
.setName(accessPolicyName)
.setPermissions(AccessPolicyPermission.bitsFromPermissions(options.getPermissions()));

WebResource resource = getResource("AccessPolicies");
Expand All @@ -319,8 +321,8 @@ public AccessPolicyInfo createAccessPolicy(String name, double durationInMinutes
* @see com.microsoft.windowsazure.services.media.MediaContract#getAccessPolicy(java.lang.String)
*/
@Override
public AccessPolicyInfo getAccessPolicy(String id) throws ServiceException {
WebResource resource = getResource("AccessPolicies", id);
public AccessPolicyInfo getAccessPolicy(String accessPolicyId) throws ServiceException {
WebResource resource = getResource("AccessPolicies", accessPolicyId);
return resource.type(MediaType.APPLICATION_ATOM_XML).accept(MediaType.APPLICATION_ATOM_XML)
.get(AccessPolicyInfo.class);
}
Expand All @@ -329,8 +331,8 @@ public AccessPolicyInfo getAccessPolicy(String id) throws ServiceException {
* @see com.microsoft.windowsazure.services.media.MediaContract#deleteAccessPolicy(java.lang.String)
*/
@Override
public void deleteAccessPolicy(String id) throws ServiceException {
getResource("AccessPolicies", id).delete();
public void deleteAccessPolicy(String accessPolicyId) throws ServiceException {
getResource("AccessPolicies", accessPolicyId).delete();
}

/* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,22 @@

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo;
import com.microsoft.windowsazure.services.media.models.AccessPolicyPermission;
import com.microsoft.windowsazure.services.media.models.CreateAccessPolicyOptions;

public class AccessPolicyIntegrationTest extends IntegrationTestBase {
private MediaContract service;

private final String invalidId = "notAValidId";
private final String validButNonexistId = "nb:pid:UUID:bce3863e-830b-49f5-9199-7cfaff52935f";

private static final String testPrefix = "testPolicy";

@Rule
public ExpectedException expected = ExpectedException.none();

@BeforeClass
public static void setup() throws Exception {
cleanupEnvironment();
}

@AfterClass
public static void cleanup() throws Exception {
cleanupEnvironment();
}

private static void cleanupEnvironment() {
config = createConfig();
MediaContract service = MediaService.create(config);
try {
List<AccessPolicyInfo> policies = service.listAccessPolicies();
for (AccessPolicyInfo policy : policies) {
if (policy.getName().startsWith(testPrefix)) {
service.deleteAccessPolicy(policy.getId());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
private void verifyInfosEqual(String message, AccessPolicyInfo expected, AccessPolicyInfo actual) {
verifyPolicyProperties(message, expected.getName(), expected.getDurationInMinutes(), expected.getPermissions(),
actual);
}

private void verifyPolicyProperties(String message, String testName, double duration,
Expand Down Expand Up @@ -100,7 +66,7 @@ public void setupInstance() throws Exception {

@Test
public void canCreateAccessPolicy() throws Exception {
String testName = testPrefix + "CanCreate";
String testName = testPolicyPrefix + "CanCreate";
double duration = 5;

AccessPolicyInfo policy = service.createAccessPolicy(testName, duration);
Expand All @@ -110,9 +76,9 @@ public void canCreateAccessPolicy() throws Exception {

@Test
public void canCreateAccessPolicyOptions() throws Exception {
String testName = testPrefix + "CanCreateOptions";
String testName = testPolicyPrefix + "CanCreateOptions";
double duration = 5;
AccessPolicyPermission permission = AccessPolicyPermission.WRITE;
AccessPolicyPermission permission = AccessPolicyPermission.READ;
CreateAccessPolicyOptions options = new CreateAccessPolicyOptions().addPermissions(permission);

AccessPolicyInfo policy = service.createAccessPolicy(testName, duration, options);
Expand All @@ -124,7 +90,7 @@ public void canCreateAccessPolicyOptions() throws Exception {

@Test
public void canGetSinglePolicyById() throws Exception {
String expectedName = testPrefix + "GetOne";
String expectedName = testPolicyPrefix + "GetOne";
double duration = 1;
AccessPolicyInfo policyToGet = service.createAccessPolicy(expectedName, duration);

Expand All @@ -136,54 +102,48 @@ public void canGetSinglePolicyById() throws Exception {

@Test
public void canGetSinglePolicyByInvalidId() throws Exception {
expected.expect(ServiceException.class);
expected.expect(new ServiceExceptionMatcher(500));
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(500));
service.getAccessPolicy(invalidId);
}

@Test
public void canGetSinglePolicyByNonexistId() throws Exception {
expected.expect(ServiceException.class);
expected.expect(new ServiceExceptionMatcher(404));
service.getAccessPolicy(validButNonexistId);
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(404));
service.getAccessPolicy(validButNonexistAccessPolicyId);
}

@Test
public void canRetrieveListOfAccessPolicies() throws Exception {
String[] policyNames = new String[] { testPrefix + "ListOne", testPrefix + "ListTwo" };
String[] policyNames = new String[] { testPolicyPrefix + "ListOne", testPolicyPrefix + "ListTwo" };
double duration = 3;
EnumSet<AccessPolicyPermission> permissions = EnumSet.of(AccessPolicyPermission.WRITE,
AccessPolicyPermission.LIST);

for (String name : policyNames) {
service.createAccessPolicy(name, duration, new CreateAccessPolicyOptions().addPermissions(permissions));
List<AccessPolicyInfo> expectedAccessPolicies = new ArrayList<AccessPolicyInfo>();
for (int i = 0; i < policyNames.length; i++) {
AccessPolicyInfo policy = service.createAccessPolicy(policyNames[i], duration,
new CreateAccessPolicyOptions().addPermissions(permissions));
expectedAccessPolicies.add(policy);
}

List<AccessPolicyInfo> policies = service.listAccessPolicies();

assertNotNull(policies);
assertTrue(policies.size() >= 2);
List<AccessPolicyInfo> actualAccessPolicies = service.listAccessPolicies();

AccessPolicyInfo[] orderedPolicies = new AccessPolicyInfo[policyNames.length];

for (AccessPolicyInfo policy : policies) {
for (int i = 0; i < policyNames.length; i++) {
if (policy.getName().equals(policyNames[i])) {
orderedPolicies[i] = policy;
}
}
}

for (int i = 0; i < policyNames.length; i++) {
verifyPolicyProperties("orderedPolicies " + i, policyNames[i], duration, permissions, orderedPolicies[i]);
}
verifyListResultContains("listAccessPolicies", expectedAccessPolicies, actualAccessPolicies,
new ComponentDelegate() {
@Override
public void verifyEquals(String message, Object expected, Object actual) {
verifyInfosEqual(message, (AccessPolicyInfo) expected, (AccessPolicyInfo) actual);
}
});
}

// Note: Access Policy cannot be updated.

@Test
public void canDeleteAccessPolicyById() throws Exception {
String policyName = testPrefix + "ToDelete";
String policyName = testPolicyPrefix + "ToDelete";
double duration = 1;
AccessPolicyInfo policyToDelete = service.createAccessPolicy(policyName, duration);
List<AccessPolicyInfo> listPoliciesResult = service.listAccessPolicies();
Expand All @@ -198,22 +158,22 @@ public void canDeleteAccessPolicyById() throws Exception {
assertFalse(policyToDelete.getId().equals(policy.getId()));
}

expected.expect(ServiceException.class);
expected.expect(new ServiceExceptionMatcher(404));
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(404));
service.getAccessPolicy(policyToDelete.getId());
}

@Test
public void canDeleteAccessPolicyByInvalidId() throws Exception {
expected.expect(ServiceException.class);
expected.expect(new ServiceExceptionMatcher(500));
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(500));
service.deleteAccessPolicy(invalidId);
}

@Test
public void canDeleteAccessPolicyByNonexistId() throws Exception {
expected.expect(ServiceException.class);
expected.expect(new ServiceExceptionMatcher(404));
service.deleteAccessPolicy(validButNonexistId);
expectedException.expect(ServiceException.class);
expectedException.expect(new ServiceExceptionMatcher(404));
service.deleteAccessPolicy(validButNonexistAccessPolicyId);
}
}
Loading

0 comments on commit 74118a1

Please sign in to comment.