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.GroupPrincipal;
22  import org.apache.jetspeed.security.impl.GroupPrincipalImpl;
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 TestLdapGroupSecurityHandler 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.seedGroupData(gpUid1);
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.removeGroupData(gpUid1);
47      }
48  
49      /***
50       * @throws Exception
51       */
52      public void testGetGroupPrincipal() throws Exception
53      {
54          String fullPath = (new GroupPrincipalImpl(gpUid1)).getFullPath();
55          //GroupPrincipal groupPrincipal = grHandler.getGroupPrincipal(fullPath);
56          GroupPrincipal groupPrincipal = grHandler.getGroupPrincipal(gpUid1);
57          assertNotNull("Group was not found.", groupPrincipal);
58          assertEquals(gpUid1,groupPrincipal.getName());
59          assertEquals(fullPath,groupPrincipal.getFullPath());
60      }
61  
62      /***
63       * @throws Exception
64       */
65      public void testAddDuplicateGroupPrincipal() throws Exception
66      {
67          grHandler.setGroupPrincipal(new GroupPrincipalImpl(gpUid1));
68          List groups = grHandler.getGroupPrincipals("");
69          assertEquals(1,groups.size());
70      }
71      
72      /***
73       * @throws Exception
74       */
75      public void testGetNonExistingGroupPrincipal() throws Exception
76      {
77          GroupPrincipal group = grHandler.getGroupPrincipal(gpUid1 + "FAKE");
78          assertNull(group);
79      }
80  
81      /***
82       * @throws Exception
83       */
84      public void testRemoveExistantUserPrincipal() throws Exception
85      {
86          GroupPrincipal gp = new GroupPrincipalImpl(gpUid1);
87          grHandler.removeGroupPrincipal(gp);
88          GroupPrincipal groupPrincipal = grHandler.getGroupPrincipal(gp.getFullPath());
89          assertNull("Group was found and should have been removed.", groupPrincipal);
90          List groups = grHandler.getGroupPrincipals("");
91          assertEquals(0,groups.size());        
92      }
93  
94      /***
95       * @throws Exception
96       */
97      public void testRemoveNonExistantUserPrincipal() throws Exception
98      {
99          String localUid = Integer.toString(rand.nextInt()).toString();
100         GroupPrincipal localPrin = new GroupPrincipalImpl(localUid);
101         grHandler.removeGroupPrincipal(localPrin);
102         List groups = grHandler.getGroupPrincipals("");
103         assertEquals(1,groups.size());
104     }
105 
106     /***
107      * @throws Exception
108      */
109     public void testGetGroupPrincipals() throws Exception
110     {
111         try
112         {
113             LdapDataHelper.seedGroupData(gpUid2);
114             assertTrue("getUserPrincipals should have returned more than one user.", grHandler.getGroupPrincipals("*")
115                     .size() > 1);
116 
117             String fullPath = (new GroupPrincipalImpl(gpUid1)).getFullPath();
118             List groups = grHandler.getGroupPrincipals(fullPath);
119             assertTrue("getGroupPrincipals should have returned one group.", groups.size() == 1);
120             assertTrue("List should have consisted of GroupPrincipal objects.", groups.get(0) instanceof GroupPrincipal);
121 
122             String localUid = Integer.toString(rand.nextInt()).toString();
123             assertTrue("getGroupPrincipals should not have found any groups with the specified filter.", grHandler
124                     .getGroupPrincipals(new GroupPrincipalImpl(localUid).getFullPath()).isEmpty());
125         }
126         finally
127         {
128             LdapDataHelper.removeGroupData(gpUid2);
129         }
130     }
131 
132 }