1   package com.atlassian.user.impl.ldap;
2   
3   import com.atlassian.user.EntityException;
4   import com.atlassian.user.Group;
5   import com.atlassian.user.GroupManager;
6   import com.atlassian.user.User;
7   import com.atlassian.user.UserManager;
8   import com.atlassian.user.impl.DefaultGroup;
9   import com.atlassian.user.search.page.PagerUtils;
10  
11  import java.util.List;
12  
13  public abstract class AbstractTestLdapGroupManager extends AbstractLdapTest
14  {
15      private GroupManager groupManager;
16      private UserManager userManager;
17  
18      public void testGetGroup() throws EntityException
19      {
20          for (int i=1; i<=8; i++)
21              assertNotNull("Group should be in LDAP: group" + i, groupManager.getGroup("group" + i));
22      }
23  
24      public void testGetGroups() throws EntityException
25      {
26          List groups = PagerUtils.toList(groupManager.getGroups());
27          assertEquals(8, groups.size());
28          for (int i = 1; i <= 8; i++)
29          {
30              assertTrue(groups.contains(groupManager.getGroup("group" + i)));
31          }
32      }
33  
34      public void testCreateGroupFails() throws EntityException
35      {
36          try
37          {
38              groupManager.createGroup("testgroup");
39              fail("Expected exception when trying to create group in read-only manager");
40          }
41          catch (UnsupportedOperationException e)
42          {
43              // expected exception
44          }
45      }
46  
47      public void testCreateNullGroupFails() throws EntityException
48      {
49          try
50          {
51              groupManager.createGroup(null);
52              fail("Expected exception when trying to create group in read-only manager");
53          }
54          catch (UnsupportedOperationException e)
55          {
56              // expected exception
57          }
58      }
59  
60      public void testRemoveGroupFails() throws EntityException
61      {
62          try
63          {
64              groupManager.removeGroup(groupManager.getGroup("group1"));
65              fail("Expected exception when trying to remove group in read-only manager");
66          }
67          catch (UnsupportedOperationException e)
68          {
69              // expected exception
70          }
71      }
72  
73      public void testRemoveNullGroupFails() throws EntityException
74      {
75          try
76          {
77              groupManager.removeGroup(null);
78              fail("Expected exception when trying to remove group in read-only manager");
79          }
80          catch (UnsupportedOperationException e)
81          {
82              // expected exception
83          }
84      }
85  
86      public void testRemoveNonExistentGroupFails() throws EntityException
87      {
88          try
89          {
90              groupManager.removeGroup(new DefaultLDAPGroup("foo", "dn=foo"));
91              fail("Expected exception when trying to remove group in read-only manager");
92          }
93          catch (UnsupportedOperationException e)
94          {
95              // expected exception
96          }
97      }
98  
99      public void testGetGroupsForUser() throws Exception
100     {
101         User user = userManager.getUser("user8");
102         assertNotNull("LDAP user should be found: user8", user);
103 
104         List groups = PagerUtils.toList(groupManager.getGroups(user));
105         assertEquals(8, groups.size());
106     }
107 
108     public void testAddMembershipFails() throws Exception
109     {
110         Group group = groupManager.getGroup("group1");
111         User user = userManager.getUser("user1");
112         try
113         {
114             groupManager.addMembership(group, user);
115             fail("Expected exception when trying to add membership to a read-only LDAP repository");
116         }
117         catch (UnsupportedOperationException e)
118         {
119             // expected exception
120         }
121     }
122 
123     public void testAddMembershipToExternalGroupFails() throws Exception
124     {
125         Group nonExistentGroup = new DefaultGroup("doesntExist");
126         User user = userManager.getUser("user1");
127         try
128         {
129             groupManager.addMembership(nonExistentGroup, user);
130             fail("Expected exception when trying to add membership to a read-only LDAP repository");
131         }
132         catch (UnsupportedOperationException e)
133         {
134             // expected exception
135         }
136     }
137 
138 
139     public void testRemoveMembershipFails() throws EntityException
140     {
141         User user = userManager.getUser("user1");
142         Group group = groupManager.getGroup("group1");
143 
144         try
145         {
146             groupManager.removeMembership(group, user);
147             fail("RO implementation is allow removing group membership!");
148         }
149         catch (UnsupportedOperationException e)
150         {
151 
152         }
153     }
154 
155     public void testHasMembership() throws Exception
156     {
157         User user1 = userManager.getUser("user1");
158         Group group1 = groupManager.getGroup("group1");
159 
160         assertNotNull("user1 should exist", user1);
161         assertNotNull("group1 should exist", group1);
162         assertTrue("user1 should be member of group1", groupManager.hasMembership(group1, user1));
163 
164         Group group3 = groupManager.getGroup("group3");
165         for (int i=1; i<=2; i++)
166         {
167             String userName = "user" + i;
168             User user = userManager.getUser(userName);
169             assertNotNull(userName + " should exist", user);
170             assertFalse(userName + " should not be in group3", groupManager.hasMembership(group3, user));
171         }
172         for (int i=3; i<=8; i++)
173         {
174             String userName = "user" + i;
175             User user = userManager.getUser(userName);
176             assertNotNull(userName + " should exist", user);
177             assertTrue(userName + " should be in group3", groupManager.hasMembership(group3, user));
178         }
179     }
180 
181     public void testGetMemberNames() throws Exception
182     {
183         Group group = groupManager.getGroup("group3");
184         assertNotNull("group3 should exist", group);
185 
186         List memberNames = PagerUtils.toList(groupManager.getMemberNames(group));
187         assertEquals(6, memberNames.size());
188 
189         for (int i=1; i<=2; i++)
190             assertFalse("user" + i + " should not be in group3", memberNames.contains("user" + i));
191         for (int i=3; i<=8; i++)
192             assertTrue("user" + i + " should be in group3", memberNames.contains("user" + i));
193     }
194 
195     public void setLdapGroupManager(GroupManager ldapGroupManager)
196     {
197         this.groupManager = ldapGroupManager;
198     }
199 
200     public void setLdapUserManager(UserManager ldapUserManager)
201     {
202         this.userManager = ldapUserManager;
203     }
204 }