View Javadoc
1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.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   * Unit test for {@link groupServiceImpl}
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
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.getGroupByNamespaceCodeAndName("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  		// add it back in, and the two lists should contain the same elements
85          postGroupIds = new ArrayList<String>(postGroupIds);
86  		postGroupIds.add("g2");
87  		assertTrue(postGroupIds.containsAll(preGroupIds) && preGroupIds.containsAll(postGroupIds));
88  
89  		// historical information should be preserved
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  		// it exists
99  		assertNotNull("should have found g2", g2);
100 		// it is inactive
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 		// add p1 back to the list, and pre & post should contain the same elements
115         postDirectPrincipalMemberIds = new ArrayList<String>(postDirectPrincipalMemberIds);
116 		postDirectPrincipalMemberIds.add("p1");
117 		assertTrue(preDirectPrincipalMemberIds.containsAll(postDirectPrincipalMemberIds) &&
118 				postDirectPrincipalMemberIds.containsAll(preDirectPrincipalMemberIds));
119 
120 		// historical information should be preserved
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 		// it exists
130 		assertNotNull("should have found p1", p1);
131 		// it is inactive
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 		// historical information should be preserved
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 		// it exists
165 		assertNotNull("should have found p1", p1);
166 		assertNotNull("should have found g2", g2);
167 		// it is inactive
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 	/* Stubs to test other groupService methods: */
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.getGroupByNamespaceCodeAndName("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         //make sure g1 is not a member of gA
197         assertFalse(groupService.isGroupMemberOfGroup("g1", group.getId()));
198 
199         //add g1 to gA
200         groupService.addGroupToGroup("g1", group.getId());
201 
202         //make sure g1 is now a member of gA
203         groupService.isGroupMemberOfGroup("g1", group.getId());
204 
205     }
206 
207 	@Test
208 	public void testAddPrincipalToGroup() {
209         Group group = createGroup();
210 
211         //make sure g1 is not a member of gA
212         assertFalse(groupService.isMemberOfGroup("p1", group.getId()));
213 
214         //add g1 to gA
215         groupService.addPrincipalToGroup("p1", group.getId());
216 
217         //make sure g1 is now a member of gA
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 }