View Javadoc

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