1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.test.service;
17
18 import org.junit.Test;
19 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20 import org.kuali.rice.kim.api.KimApiConstants;
21 import org.kuali.rice.kim.api.KimConstants.KimGroupMemberTypes;
22 import org.kuali.rice.kim.api.group.Group;
23 import org.kuali.rice.kim.api.group.GroupMember;
24 import org.kuali.rice.kim.api.group.GroupService;
25 import org.kuali.rice.kim.impl.KIMPropertyConstants;
26 import org.kuali.rice.kim.impl.group.GroupMemberBo;
27 import org.kuali.rice.kim.impl.group.GroupServiceImpl;
28 import org.kuali.rice.kim.test.KIMTestCase;
29 import org.kuali.rice.krad.service.BusinessObjectService;
30 import org.kuali.rice.krad.service.KRADServiceLocator;
31 import org.springframework.util.CollectionUtils;
32
33 import javax.xml.namespace.QName;
34 import java.sql.Timestamp;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 import static org.junit.Assert.*;
41
42
43
44
45
46
47
48 public class GroupUpdateServiceImplTest extends KIMTestCase {
49
50 private GroupService groupService;
51 private BusinessObjectService businessObjectService;
52
53 public void setUp() throws Exception {
54 super.setUp();
55 groupService = (GroupService)GlobalResourceLoader.getService(
56 new QName(KimApiConstants.Namespaces.KIM_NAMESPACE_2_0, KimApiConstants.ServiceNames.GROUP_SERVICE_SOAP));
57 businessObjectService = KRADServiceLocator.getBusinessObjectService();
58 }
59
60 @Test
61 public void testCreateGroup() {
62 Group groupInfo = createGroup();
63
64 Group result = groupService.getGroupByNameAndNamespaceCode("KUALI", "gA");
65
66 assertEquals(groupInfo.isActive(), result.isActive());
67 assertTrue(groupInfo.getNamespaceCode().equals(result.getNamespaceCode()));
68 assertTrue(groupInfo.getName().equals(result.getName()));
69 assertTrue(groupInfo.getKimTypeId().equals(result.getKimTypeId()));
70 }
71
72 @Test
73 public void testRemoveGroupFromGroup() {
74 List<String> preGroupIds = groupService.getDirectMemberGroupIds("g1");
75
76 assertTrue( "g1 must contain group g2", preGroupIds.contains( "g2" ) );
77
78 groupService.removeGroupFromGroup("g2", "g1");
79
80 List<String> postGroupIds = groupService.getDirectMemberGroupIds("g1");
81
82 assertFalse( "g1 must not contain group g2", postGroupIds.contains( "g2" ) );
83
84
85 postGroupIds = new ArrayList<String>(postGroupIds);
86 postGroupIds.add("g2");
87 assertTrue(postGroupIds.containsAll(preGroupIds) && preGroupIds.containsAll(postGroupIds));
88
89
90 List<GroupMemberBo> members = getActiveAndInactiveGroupTypeMembers("g1");
91 GroupMemberBo g2 = null;
92 for (GroupMemberBo member : members) {
93 if (member.getMemberId().equals("g2")) {
94 g2 = member;
95 }
96 }
97
98
99 assertNotNull("should have found g2", g2);
100
101 assertFalse("g2 should be inactive", g2.isActive(new Timestamp(System.currentTimeMillis())));
102 }
103
104 @Test
105 public void testRemovePrincipalFromGroup() {
106 List<String> preDirectPrincipalMemberIds = groupService.getDirectMemberPrincipalIds("g2");
107 assertTrue( "p1 must be direct member of g2", preDirectPrincipalMemberIds.contains("p1") );
108
109 groupService.removePrincipalFromGroup("p1", "g2");
110
111 List<String> postDirectPrincipalMemberIds = groupService.getDirectMemberPrincipalIds("g2");
112 assertFalse( "p1 must no longer be a direct member of g2", postDirectPrincipalMemberIds.contains("p1") );
113
114
115 postDirectPrincipalMemberIds = new ArrayList<String>(postDirectPrincipalMemberIds);
116 postDirectPrincipalMemberIds.add("p1");
117 assertTrue(preDirectPrincipalMemberIds.containsAll(postDirectPrincipalMemberIds) &&
118 postDirectPrincipalMemberIds.containsAll(preDirectPrincipalMemberIds));
119
120
121 List<GroupMemberBo> members = getActiveAndInactivePrincipalTypeMembers("g2");
122 GroupMemberBo p1 = null;
123 for (GroupMemberBo member : members) {
124 if (member.getMemberId().equals("p1")) {
125 p1 = member;
126 }
127 }
128
129
130 assertNotNull("should have found p1", p1);
131
132 assertFalse("p1 should be inactive", p1.isActive(new Timestamp(System.currentTimeMillis())));
133 }
134
135 @Test
136 public void testRemoveGroupMembers() {
137 List<String> before = groupService.getMemberPrincipalIds("g1");
138
139 groupService.addPrincipalToGroup("p1", "g1");
140
141 assertTrue( "p1 must be direct member of g1", groupService.isDirectMemberOfGroup("p1", "g1") );
142 assertTrue( "g2 must be direct member of g1", groupService.isGroupMemberOfGroup("g2", "g1") );
143
144 groupService.removeAllMembers("g1");
145
146 List<GroupMember> memberInfos = groupService.getMembersOfGroup("g1");
147 assertTrue("should be no active members", CollectionUtils.isEmpty(memberInfos));
148
149
150 List<GroupMemberBo> members = getActiveAndInactivePrincipalTypeMembers("g1");
151 members.addAll(getActiveAndInactiveGroupTypeMembers("g1"));
152
153 GroupMemberBo p1 = null;
154 GroupMemberBo g2 = null;
155 for (GroupMemberBo member : members) {
156 if (member.getMemberId().equals("p1")) {
157 p1 = member;
158 }
159 if (member.getMemberId().equals("g2")) {
160 g2 = member;
161 }
162 }
163
164
165 assertNotNull("should have found p1", p1);
166 assertNotNull("should have found g2", g2);
167
168 assertFalse("p1 should be inactive", p1.isActive(new Timestamp(System.currentTimeMillis())));
169 assertFalse("g2 should be inactive", g2.isActive(new Timestamp(System.currentTimeMillis())));
170 }
171
172
173 @Test
174 public void testUpdateGroup() {
175
176 Group group = createGroup();
177 Group.Builder builder = Group.Builder.create(group);
178 builder.setDescription("This is a new description. It is useful.");
179
180
181 groupService.updateGroup(group.getId(), builder.build());
182
183 Group result = groupService.getGroupByNameAndNamespaceCode("KUALI", "gA");
184
185 assertEquals(group.isActive(), result.isActive());
186 assertEquals(group.getNamespaceCode(), result.getNamespaceCode());
187 assertEquals(group.getName(), result.getName());
188 assertEquals(group.getKimTypeId(), result.getKimTypeId());
189 assertEquals(builder.getDescription(), result.getDescription());
190 }
191
192 @Test
193 public void testAddGroupToGroup() {
194 Group group = createGroup();
195
196
197 assertFalse(groupService.isGroupMemberOfGroup("g1", group.getId()));
198
199
200 groupService.addGroupToGroup("g1", group.getId());
201
202
203 groupService.isGroupMemberOfGroup("g1", group.getId());
204
205 }
206
207 @Test
208 public void testAddPrincipalToGroup() {
209 Group group = createGroup();
210
211
212 assertFalse(groupService.isMemberOfGroup("p1", group.getId()));
213
214
215 groupService.addPrincipalToGroup("p1", group.getId());
216
217
218 groupService.isMemberOfGroup("p1", group.getId());
219 }
220
221
222 private List<GroupMemberBo> getActiveAndInactiveGroupTypeMembers(String groupId) {
223
224 Map<String,Object> criteria = new HashMap<String,Object>();
225 criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, groupId);
226 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimGroupMemberTypes.GROUP_MEMBER_TYPE.getCode());
227
228 return new ArrayList<GroupMemberBo>(businessObjectService.findMatching(GroupMemberBo.class, criteria));
229 }
230
231 private List<GroupMemberBo> getActiveAndInactivePrincipalTypeMembers(String groupId) {
232
233 Map<String,Object> criteria = new HashMap<String,Object>();
234 criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, groupId);
235 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode());
236
237 return new ArrayList<GroupMemberBo>(businessObjectService.findMatching(GroupMemberBo.class, criteria));
238 }
239
240 private Group createGroup() {
241 Group.Builder groupInfo = Group.Builder.create("KUALI", "gA", "1");
242 groupInfo.setActive(true);
243
244 return groupService.createGroup(groupInfo.build());
245 }
246 }