Skip to content

Commit

Permalink
Merge pull request #7 from jcookems/fix372
Browse files Browse the repository at this point in the history
Fix372
  • Loading branch information
jcookems committed Oct 17, 2012
2 parents d21a283 + 847dcc8 commit 3d66271
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 237 deletions.
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
package com.microsoft.windowsazure.services.media;

import org.junit.Before;
import static org.junit.Assert.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

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

import com.microsoft.windowsazure.services.core.Configuration;
import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.microsoft.windowsazure.services.media.models.ListLocatorsResult;
import com.microsoft.windowsazure.services.media.models.LocatorInfo;

public abstract class IntegrationTestBase {
protected static MediaContract service;
protected static Configuration config;

@Before
public void beforeEachTest() {
protected static final String testAssetPrefix = "testAsset";
protected static final String testPolicyPrefix = "testPolicy";

protected static final String validButNonexistAssetId = "nb:cid:UUID:00000000-0000-4a00-0000-000000000000";
protected static final String validButNonexistAccessPolicyId = "nb:pid:UUID:bce3863e-830b-49f5-9199-7cfaff52935f";

protected static final String invalidId = "notAValidId";

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

@BeforeClass
public static void setup() throws Exception {
config = Configuration.getInstance();
overrideWithEnv(config, MediaConfiguration.URI);
overrideWithEnv(config, MediaConfiguration.OAUTH_URI);
overrideWithEnv(config, MediaConfiguration.OAUTH_CLIENT_ID);
overrideWithEnv(config, MediaConfiguration.OAUTH_CLIENT_SECRET);
overrideWithEnv(config, MediaConfiguration.OAUTH_SCOPE);
}

public static Configuration createConfig() {
Configuration config = Configuration.getInstance();
overrideWithEnv(config, MediaConfiguration.URI);
overrideWithEnv(config, MediaConfiguration.OAUTH_URI);
overrideWithEnv(config, MediaConfiguration.OAUTH_CLIENT_ID);
overrideWithEnv(config, MediaConfiguration.OAUTH_CLIENT_SECRET);
overrideWithEnv(config, MediaConfiguration.OAUTH_SCOPE);
return config;
service = MediaService.create(config);

cleanupEnvironment();
}

protected static void overrideWithEnv(Configuration config, String key) {
Expand All @@ -34,4 +54,104 @@ protected static void overrideWithEnv(Configuration config, String key) {

config.setProperty(key, value);
}

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

private static void cleanupEnvironment() {
// TODO: This should be removed once cascade delete is implemented for Assets.
// But for now, trying to delete an asset with fail if there are any
// existing Locators associated with it.
removeAllTestLocators();
removeAllTestAssets();
removeAllTestAccessPolicies();
}

private static void removeAllTestAccessPolicies() {
try {
List<AccessPolicyInfo> policies = service.listAccessPolicies();
for (AccessPolicyInfo policy : policies) {
if (policy.getName().startsWith(testPolicyPrefix)) {
service.deleteAccessPolicy(policy.getId());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}

private static void removeAllTestAssets() {
try {
List<AssetInfo> listAssetsResult = service.listAssets();
for (AssetInfo assetInfo : listAssetsResult) {
if (assetInfo.getName().startsWith(testAssetPrefix)) {
service.deleteAsset(assetInfo.getId());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}

private static void removeAllTestLocators() {
try {
ListLocatorsResult listLocatorsResult = service.listLocators();
for (LocatorInfo locatorInfo : listLocatorsResult.getLocatorInfos()) {
AssetInfo ai = service.getAsset(locatorInfo.getAssetId());
if (ai.getName().startsWith(testAssetPrefix)) {
service.deleteLocator(locatorInfo.getId());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}

interface ComponentDelegate {
void verifyEquals(String message, Object expected, Object actual);
}

protected <T> void verifyListResultContains(List<T> expectedInfos, Collection<T> actualInfos,
ComponentDelegate delegate) {
verifyListResultContains("", expectedInfos, actualInfos, delegate);
}

protected <T> void verifyListResultContains(String message, List<T> expectedInfos, Collection<T> actualInfos,
ComponentDelegate delegate) {
assertNotNull(message + ": actualInfos", actualInfos);
assertTrue(message + ": actual size should be same size or larger than expected size",
actualInfos.size() >= expectedInfos.size());

List<T> orderedAndFilteredActualInfo = new ArrayList<T>();
try {
for (T expectedInfo : expectedInfos) {
Method getId = expectedInfo.getClass().getMethod("getId");
for (T actualInfo : actualInfos) {
if (((String) getId.invoke(actualInfo)).equals(getId.invoke(expectedInfo))) {
orderedAndFilteredActualInfo.add(actualInfo);
break;
}
}
}
}
catch (Exception e) {
// Don't worry about problems here.
e.printStackTrace();
}

assertEquals(message + ": actual filtered size should be same as expected size", expectedInfos.size(),
orderedAndFilteredActualInfo.size());

if (delegate != null) {
for (int i = 0; i < expectedInfos.size(); i++) {
delegate.verifyEquals(message + ": orderedAndFilteredActualInfo " + i, expectedInfos.get(i),
orderedAndFilteredActualInfo.get(i));
}
}
}
}
Loading

0 comments on commit 3d66271

Please sign in to comment.