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