Removed DOCUMENT ME comments that were generated and applied source code
[utils] / security / src / test / java / org / wamblee / security / authorization / UrlAuthorizationRuleTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.security.authorization;
17
18 import junit.framework.TestCase;
19 import static org.wamblee.security.authorization.AuthorizationResult.GRANTED;
20 import static org.wamblee.security.authorization.AuthorizationResult.UNDECIDED;
21 import static org.wamblee.security.authorization.AuthorizationResult.UNSUPPORTED_RESOURCE;
22
23 import org.wamblee.usermgt.User;
24
25 /**
26  * Tests for the {@link org.wamblee.security.authorization.UrlAuthorizationRule}
27  * .
28  * 
29  * @author Erik Brakkee
30  */
31 public class UrlAuthorizationRuleTest extends TestCase {
32     /**
33      * Constructs the rule with a result of UNDECIDED. Verifies that an
34      * IllegalArgumentException is thrown.
35      */
36     public void testConstructWithUndecidedResult() {
37         try {
38             new TestAuthorizationRule(UNDECIDED, "users", "/path",
39                 ReadOperation.class);
40             fail();
41         } catch (IllegalArgumentException e) {
42             // ok
43         }
44     }
45
46     /**
47      * Constructs the rule with a result of UNSUPPORTED_RESOURCE. Verifies that
48      * an IllegalArgumentException is thrown.
49      */
50     public void testConstructWithUnsupportedResult() {
51         try {
52             new TestAuthorizationRule(UNSUPPORTED_RESOURCE, "users", "/path",
53                 ReadOperation.class);
54             fail();
55         } catch (IllegalArgumentException e) {
56             // ok
57         }
58     }
59
60     /**
61      * Constructs the authorization rule and applies it to an unsupported object
62      * type. Verifies that the result is UNSUPPORTED_RESOURCE.
63      */
64     public void testUnsupportedObject() {
65         AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
66             "/path", ReadOperation.class);
67         assertEquals(UNSUPPORTED_RESOURCE, rule.isAllowed("hello",
68             new ReadOperation(), new TestUserAccessor().getCurrentUser()));
69     }
70
71     public void testMatchingScenarios() {
72         AuthorizationRule rule = new TestAuthorizationRule(GRANTED, "users",
73             "/path/", ReadOperation.class);
74         User user = new TestUserAccessor().getCurrentUser();
75
76         // everything matches
77         assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/a"),
78             new ReadOperation(), user));
79         assertEquals(GRANTED, rule.isAllowed(new TestResource("/path/"),
80             new ReadOperation(), user));
81
82         // path does not match.
83         assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path"),
84             new ReadOperation(), user));
85
86         // operation does not match.
87         assertEquals(UNDECIDED, rule.isAllowed(new TestResource("/path/"),
88             new WriteOperation(), user));
89
90         // group does not match.
91         AuthorizationRule rule2 = new TestAuthorizationRule(GRANTED, "users2",
92             "/path/", ReadOperation.class);
93         assertEquals(UNDECIDED, rule2.isAllowed(new TestResource("/path/a"),
94             new ReadOperation(), user));
95     }
96 }