1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.spi.ldap;
18  
19  import java.util.List;
20  
21  import org.apache.jetspeed.security.RolePrincipal;
22  import org.apache.jetspeed.security.impl.RolePrincipalImpl;
23  
24  /***
25   * @author <a href="mailto:mike.long@dataline.com">Mike Long </a>, <a
26   *         href="mailto:dlestrat@apache.org">David Le Strat</a>
27   */
28  public class TestLdapRoleSecurityHandler extends AbstractLdapTest
29  {
30  
31      /***
32       * @see org.apache.jetspeed.security.spi.ldap.AbstractLdapTest#setUp()
33       */
34      protected void setUp() throws Exception
35      {
36          super.setUp();
37          LdapDataHelper.seedRoleData(roleUid1);
38      }
39  
40      /***
41       * @see org.apache.jetspeed.security.spi.ldap.AbstractLdapTest#tearDown()
42       */
43      protected void tearDown() throws Exception
44      {
45          super.tearDown();
46          LdapDataHelper.removeRoleData(roleUid1);
47      }
48  
49      /***
50       * @throws Exception
51       */
52      public void testGetRolePrincipal() throws Exception
53      {
54          String fullPath = (new RolePrincipalImpl(roleUid1)).getFullPath();
55          RolePrincipal rolePrincipal = roleHandler.getRolePrincipal(roleUid1);
56          assertNotNull("Role was not found.", rolePrincipal);
57          assertEquals(roleUid1,rolePrincipal.getName());
58          assertEquals(fullPath,rolePrincipal.getFullPath());
59      }
60  
61      /***
62       * @throws Exception
63       */
64      public void testAddDuplicateRolePrincipal() throws Exception
65      {
66      	roleHandler.setRolePrincipal(new RolePrincipalImpl(roleUid1));
67          List roles = roleHandler.getRolePrincipals("");
68          assertEquals(1,roles.size());
69      }
70      
71      /***
72       * @throws Exception
73       */
74      public void testGetNonExistingRolePrincipal() throws Exception
75      {
76          RolePrincipal role = roleHandler.getRolePrincipal(roleUid1 + "FAKE");
77          assertNull(role);
78      }
79  
80      /***
81       * @throws Exception
82       */
83      public void testRemoveExistantUserPrincipal() throws Exception
84      {
85          RolePrincipal gp = new RolePrincipalImpl(roleUid1);
86          roleHandler.removeRolePrincipal(gp);
87          RolePrincipal rolePrincipal = roleHandler.getRolePrincipal(gp.getFullPath());
88          assertNull("Role was found and should have been removed.", rolePrincipal);
89          List roles = roleHandler.getRolePrincipals("");
90          assertEquals(0,roles.size());        
91      }
92  
93      /***
94       * @throws Exception
95       */
96      public void testRemoveNonExistantUserPrincipal() throws Exception
97      {
98          String localUid = Integer.toString(rand.nextInt()).toString();
99          RolePrincipal localPrin = new RolePrincipalImpl(localUid);
100         roleHandler.removeRolePrincipal(localPrin);
101         List roles = roleHandler.getRolePrincipals("");
102         assertEquals(1,roles.size());
103     }
104 
105     /***
106      * @throws Exception
107      */
108     public void testGetRolePrincipals() throws Exception
109     {
110         try
111         {
112             LdapDataHelper.seedRoleData(gpUid2);
113             assertTrue("getUserPrincipals should have returned more than one user.", roleHandler.getRolePrincipals("*")
114                     .size() > 1);
115 
116             String fullPath = (new RolePrincipalImpl(roleUid1)).getFullPath();
117             List roles = roleHandler.getRolePrincipals(fullPath);
118             assertTrue("getRolePrincipals should have returned one role.", roles.size() == 1);
119             assertTrue("List should have consisted of RolePrincipal objects.", roles.get(0) instanceof RolePrincipal);
120 
121             String localUid = Integer.toString(rand.nextInt()).toString();
122             assertTrue("getRolePrincipals should not have found any roles with the specified filter.", roleHandler
123                     .getRolePrincipals(new RolePrincipalImpl(localUid).getFullPath()).isEmpty());
124         }
125         finally
126         {
127             LdapDataHelper.removeRoleData(gpUid2);
128         }
129     }
130 
131 }