825fe626b8793128b494d2cfed4e3abfb146315d
[moon.git] /
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.shiro.authorization;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16
17 import com.google.common.collect.Sets;
18 import java.util.Collection;
19 import java.util.HashSet;
20 import org.junit.Test;
21
22 public class RBACRuleTest {
23
24     private static final String BASIC_RBAC_RULE_URL_PATTERN = "/*";
25     private static final Collection<String> BASIC_RBAC_RULE_ROLES = Sets.newHashSet("admin");
26     private RBACRule basicRBACRule = RBACRule.createAuthorizationRule(BASIC_RBAC_RULE_URL_PATTERN,
27             BASIC_RBAC_RULE_ROLES);
28
29     private static final String COMPLEX_RBAC_RULE_URL_PATTERN = "/auth/v1/";
30     private static final Collection<String> COMPLEX_RBAC_RULE_ROLES = Sets.newHashSet("admin",
31             "user");
32     private RBACRule complexRBACRule = RBACRule.createAuthorizationRule(
33             COMPLEX_RBAC_RULE_URL_PATTERN, COMPLEX_RBAC_RULE_ROLES);
34
35     @Test
36     public void testCreateAuthorizationRule() {
37         // positive test cases
38         assertNotNull(RBACRule.createAuthorizationRule(BASIC_RBAC_RULE_URL_PATTERN,
39                 BASIC_RBAC_RULE_ROLES));
40         assertNotNull(RBACRule.createAuthorizationRule(COMPLEX_RBAC_RULE_URL_PATTERN,
41                 COMPLEX_RBAC_RULE_ROLES));
42
43         // negative test cases
44         // both null
45         assertNull(RBACRule.createAuthorizationRule(null, null));
46
47         // url pattern is null
48         assertNull(RBACRule.createAuthorizationRule(null, BASIC_RBAC_RULE_ROLES));
49         // url pattern is empty string
50         assertNull(RBACRule.createAuthorizationRule("", BASIC_RBAC_RULE_ROLES));
51
52         // roles is null
53         assertNull(RBACRule.createAuthorizationRule(BASIC_RBAC_RULE_URL_PATTERN, null));
54         // roles is empty collection
55         assertNull(RBACRule.createAuthorizationRule(COMPLEX_RBAC_RULE_URL_PATTERN,
56                 new HashSet<String>()));
57     }
58
59     @Test
60     public void testGetUrlPattern() {
61         assertEquals(BASIC_RBAC_RULE_URL_PATTERN, basicRBACRule.getUrlPattern());
62         assertEquals(COMPLEX_RBAC_RULE_URL_PATTERN, complexRBACRule.getUrlPattern());
63     }
64
65     @Test
66     public void testGetRoles() {
67         assertTrue(BASIC_RBAC_RULE_ROLES.containsAll(basicRBACRule.getRoles()));
68         basicRBACRule.getRoles().clear();
69         // test that getRoles() produces a new object
70         assertFalse(basicRBACRule.getRoles().isEmpty());
71         assertTrue(basicRBACRule.getRoles().containsAll(BASIC_RBAC_RULE_ROLES));
72
73         assertTrue(COMPLEX_RBAC_RULE_ROLES.containsAll(complexRBACRule.getRoles()));
74         complexRBACRule.getRoles().add("newRole");
75         // test that getRoles() produces a new object
76         assertFalse(complexRBACRule.getRoles().contains("newRole"));
77         assertTrue(complexRBACRule.getRoles().containsAll(COMPLEX_RBAC_RULE_ROLES));
78     }
79
80     @Test
81     public void testGetRolesInShiroFormat() {
82         final String BASIC_RBAC_RULE_EXPECTED_SHIRO_FORMAT = "roles[admin]";
83         assertEquals(BASIC_RBAC_RULE_EXPECTED_SHIRO_FORMAT, basicRBACRule.getRolesInShiroFormat());
84
85         // set ordering is not predictable, so both formats must be considered
86         final String COMPLEX_RBAC_RULE_EXPECTED_SHIRO_FORMAT_1 = "roles[admin, user]";
87         final String COMPLEX_RBAC_RULE_EXPECTED_SHIRO_FORMAT_2 = "roles[user, admin]";
88         assertTrue(COMPLEX_RBAC_RULE_EXPECTED_SHIRO_FORMAT_1.equals(complexRBACRule
89                 .getRolesInShiroFormat())
90                 || COMPLEX_RBAC_RULE_EXPECTED_SHIRO_FORMAT_2.equals(complexRBACRule
91                         .getRolesInShiroFormat()));
92     }
93
94     @Test
95     public void testToString() {
96         final String BASIC_RBAC_RULE_EXPECTED_SHIRO_FORMAT = "/*=roles[admin]";
97         assertEquals(BASIC_RBAC_RULE_EXPECTED_SHIRO_FORMAT, basicRBACRule.toString());
98
99         // set ordering is not predictable,s o both formats must be considered
100         final String COMPLEX_RBAC_RULE_EXPECTED_SHIRO_FORMAT_1 = "/auth/v1/=roles[admin, user]";
101         final String COMPLEX_RBAC_RULE_EXPECTED_SHIRO_FORMAT_2 = "/auth/v1/=roles[user, admin]";
102         assertTrue(COMPLEX_RBAC_RULE_EXPECTED_SHIRO_FORMAT_1.equals(complexRBACRule.toString())
103                 || COMPLEX_RBAC_RULE_EXPECTED_SHIRO_FORMAT_2.equals(complexRBACRule.toString()));
104     }
105
106 }