Skip to content

Commit

Permalink
Merge pull request Azure#353 from christav/dev
Browse files Browse the repository at this point in the history
AccessPolicyCreateOptions api usability, issue Azure#340
  • Loading branch information
Chris Tavares committed Oct 4, 2012
2 parents dc52d4b + 93b309b commit 13737cd
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import java.util.EnumSet;
Expand Down Expand Up @@ -28,6 +43,20 @@ public CreateAccessPolicyOptions addPermissions(EnumSet<AccessPolicyPermission>
return this;
}

/**
* Add the given permissions to this creation request
*
* @param permissionsToAdd
* varargs - permissions to add
* @return the CreateAccessPolicyOptions object
*/
public CreateAccessPolicyOptions addPermissions(AccessPolicyPermission... permissionsToAdd) {
for (AccessPolicyPermission permission : permissionsToAdd) {
permissions.add(permission);
}
return this;
}

/**
* Remove the given permissions from this creation request
*
Expand All @@ -38,4 +67,18 @@ public CreateAccessPolicyOptions removePermissions(EnumSet<AccessPolicyPermissio
permissions.removeAll(permissionsToRemove);
return this;
}

/**
* Remove the given permissions from this creation request
*
* @param permissionsToRemove
* vararg - permissions to remove
* @return the CreateAccessPolicyOptions object
*/
public CreateAccessPolicyOptions removePermissions(AccessPolicyPermission... permissionsToRemove) {
for (AccessPolicyPermission permission : permissionsToRemove) {
permissions.remove(permission);
}
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Copyright 2012 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import static org.junit.Assert.*;

import java.util.EnumSet;

import org.junit.Test;

public class CreateAccessPolicyOptionsTest {

private static void assertPermissions(EnumSet<AccessPolicyPermission> expected,
EnumSet<AccessPolicyPermission> actual) {
assertTrue(actual.containsAll(expected));
assertTrue(EnumSet.complementOf(actual).containsAll(EnumSet.complementOf(expected)));
}

@Test
public void optionsCreatedWithNoPermissions() throws Exception {
CreateAccessPolicyOptions options = new CreateAccessPolicyOptions();

assertPermissions(EnumSet.noneOf(AccessPolicyPermission.class), options.getPermissions());
}

@Test
public void canGetSetPermissionsAsEnumSet() throws Exception {
EnumSet<AccessPolicyPermission> expectedPermissions = EnumSet.of(AccessPolicyPermission.LIST,
AccessPolicyPermission.DELETE);

CreateAccessPolicyOptions options = new CreateAccessPolicyOptions();

options.addPermissions(expectedPermissions);

EnumSet<AccessPolicyPermission> permissions = options.getPermissions();

assertPermissions(expectedPermissions, permissions);
}

@Test
public void canGetSetPermissionsAsVarargs() throws Exception {
EnumSet<AccessPolicyPermission> expectedPermissions = EnumSet.of(AccessPolicyPermission.READ,
AccessPolicyPermission.WRITE);

CreateAccessPolicyOptions options = new CreateAccessPolicyOptions().addPermissions(AccessPolicyPermission.READ,
AccessPolicyPermission.WRITE);

EnumSet<AccessPolicyPermission> permissions = options.getPermissions();

assertPermissions(expectedPermissions, permissions);
}

@Test
public void canRemovePermissionsAsEnumSet() throws Exception {
EnumSet<AccessPolicyPermission> originalPermissions = EnumSet.of(AccessPolicyPermission.READ,
AccessPolicyPermission.WRITE, AccessPolicyPermission.DELETE);
EnumSet<AccessPolicyPermission> permissionsToRemove = EnumSet.of(AccessPolicyPermission.READ,
AccessPolicyPermission.DELETE);
EnumSet<AccessPolicyPermission> expectedPermissions = EnumSet.of(AccessPolicyPermission.WRITE);

CreateAccessPolicyOptions options = new CreateAccessPolicyOptions().addPermissions(originalPermissions);

options.removePermissions(permissionsToRemove);

EnumSet<AccessPolicyPermission> actualPermissions = options.getPermissions();

assertPermissions(expectedPermissions, actualPermissions);
}

@Test
public void canRemovePermissionsAsVarargs() throws Exception {
EnumSet<AccessPolicyPermission> originalPermissions = EnumSet.of(AccessPolicyPermission.READ,
AccessPolicyPermission.WRITE, AccessPolicyPermission.DELETE);
EnumSet<AccessPolicyPermission> expectedPermissions = EnumSet.of(AccessPolicyPermission.WRITE);

CreateAccessPolicyOptions options = new CreateAccessPolicyOptions().addPermissions(originalPermissions);

options.removePermissions(AccessPolicyPermission.READ, AccessPolicyPermission.DELETE);

EnumSet<AccessPolicyPermission> actualPermissions = options.getPermissions();

assertPermissions(expectedPermissions, actualPermissions);
}
}

0 comments on commit 13737cd

Please sign in to comment.