001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.workgroup;
018:
019: import java.util.HashSet;
020: import java.util.List;
021: import java.util.Set;
022:
023: import org.junit.Test;
024: import org.kuali.workflow.test.WorkflowTestCase;
025:
026: import edu.iu.uis.eden.EdenConstants;
027: import edu.iu.uis.eden.KEWServiceLocator;
028: import edu.iu.uis.eden.user.AuthenticationUserId;
029: import edu.iu.uis.eden.user.WorkflowUser;
030:
031: /**
032: * Tests the BaseWorkgroupService.
033: *
034: * @author Eric Westfall
035: */
036: public class BaseWorkgroupServiceTest extends WorkflowTestCase {
037:
038: /**
039: * Tests that Workgroup caching works as expected.
040: *
041: * @throws Exception
042: */
043: @Test
044: public void testCaching() throws Exception {
045: // Let's start by flushing out the workgroup caches.
046: // Test harness startup will invariably result in some workgroups getting cached.
047: KEWServiceLocator.getCacheAdministrator().flushGroup(
048: BaseWorkgroupService.WORKGROUP_ID_CACHE_GROUP);
049: KEWServiceLocator.getCacheAdministrator().flushGroup(
050: BaseWorkgroupService.WORKGROUP_NAME_CACHE_GROUP);
051:
052: // verify that the TestWorkgroup is not in the cache
053: Workgroup cachedTestWorkgroup = (Workgroup) KEWServiceLocator
054: .getCacheAdministrator().getFromCache(
055: getCacheNameKey("TestWorkgroup"));
056: assertNull("The test workgroup should not be cached yet.",
057: cachedTestWorkgroup);
058:
059: // fetch the TestWorkgroup from the service, it should then be cached
060: BaseWorkgroup testWorkgroup = (BaseWorkgroup) KEWServiceLocator
061: .getWorkgroupService().getWorkgroup(
062: new GroupNameId("TestWorkgroup"));
063: assertNotNull("TestWorkgroup should exist.", testWorkgroup);
064: cachedTestWorkgroup = (Workgroup) KEWServiceLocator
065: .getCacheAdministrator().getFromCache(
066: getCacheNameKey("TestWorkgroup"));
067: assertNotNull("TestWorkgroup should now be cached.",
068: cachedTestWorkgroup);
069:
070: // the cached version and the version returned from the service should be the same instance
071: assertEquals("Workgroups should be the same.", testWorkgroup,
072: cachedTestWorkgroup);
073:
074: // now, if we fetch the TestWorkgroup from the ID cache, we should also recieve the same workgroup
075: Workgroup testWorkgroupById = (Workgroup) KEWServiceLocator
076: .getCacheAdministrator().getFromCache(
077: getCacheIdKey(testWorkgroup
078: .getWorkflowGroupId().getGroupId()));
079: assertNotNull("TestWorkgroup should be in ID cache.",
080: testWorkgroupById);
081: assertEquals("Workgroups should be the same.", testWorkgroup,
082: testWorkgroupById);
083:
084: // Modify the workgroup and save it, it should get flushed from the cache
085:
086: // Let's verify that user1 is not currently a member of this workgroup for good measure
087: WorkflowUser user1 = KEWServiceLocator.getUserService()
088: .getWorkflowUser(new AuthenticationUserId("user1"));
089: assertFalse(
090: "user1 should not be a member of TestWorkgroup by default as configured in DefaultTestData.xml",
091: testWorkgroup.hasMember(user1));
092: // build the BaseWorkgroupMember
093: BaseWorkgroupMember member = new BaseWorkgroupMember();
094: member.setWorkflowId(user1.getWorkflowId());
095: member
096: .setMemberType(EdenConstants.ACTION_REQUEST_USER_RECIPIENT_CD);
097: member.setWorkgroup(testWorkgroup);
098: member.setWorkgroupId(testWorkgroup.getWorkflowGroupId()
099: .getGroupId());
100: member.setWorkgroupVersionNumber(testWorkgroup
101: .getVersionNumber());
102: testWorkgroup.getWorkgroupMembers().add(member);
103: KEWServiceLocator.getWorkgroupService().save(testWorkgroup);
104:
105: // the workgroup should no longer be cached
106: Workgroup cachedTestWorkgroup2 = (Workgroup) KEWServiceLocator
107: .getCacheAdministrator().getFromCache(
108: getCacheNameKey("TestWorkgroup"));
109: assertNull(
110: "TestWorkgroup should no longer be in the name cache.",
111: cachedTestWorkgroup2);
112: Workgroup testWorkgroupById2 = (Workgroup) KEWServiceLocator
113: .getCacheAdministrator().getFromCache(
114: getCacheIdKey(testWorkgroup.getWorkgroupId()));
115: assertNull(
116: "TestWorkgroup should no longer be in the ID cache.",
117: testWorkgroupById2);
118:
119: // fetch the workgroup from the service and verify that we have the updated version with the new member
120: Workgroup testWorkgroup2 = KEWServiceLocator
121: .getWorkgroupService().getWorkgroup(
122: testWorkgroup.getWorkflowGroupId());
123: assertNotNull("TestWorkgroup should exist.", testWorkgroup2);
124: assertTrue("Workgroup should have user1 as a member.",
125: testWorkgroup2.hasMember(user1));
126: // since the Workgroup should be newly fetched from the database, the instances should not be the same
127: assertFalse(
128: "The old and new TestWorkgroups should not be the same",
129: testWorkgroup.equals(testWorkgroup2));
130:
131: // the TestWorkgroup should now be in both caches
132: cachedTestWorkgroup2 = (Workgroup) KEWServiceLocator
133: .getCacheAdministrator().getFromCache(
134: getCacheNameKey("TestWorkgroup"));
135: assertNotNull(
136: "New workgroup should be available in name cache.",
137: cachedTestWorkgroup2);
138: testWorkgroupById2 = (Workgroup) KEWServiceLocator
139: .getCacheAdministrator().getFromCache(
140: getCacheIdKey(testWorkgroup
141: .getWorkflowGroupId().getGroupId()));
142: assertNotNull("New workgroup should be available in id cache.",
143: testWorkgroupById2);
144: }
145:
146: @Test
147: public void testNestedWorkgroups() throws Exception {
148: BaseWorkgroup workgroup1 = new BaseWorkgroup();
149: workgroup1.setActiveInd(Boolean.TRUE);
150: workgroup1.setCurrentInd(Boolean.TRUE);
151: workgroup1.setDescription("description");
152: workgroup1.setGroupNameId(new GroupNameId(
153: "TestNestedWorkgroups1"));
154: workgroup1.setWorkgroupType(null);
155:
156: // user 1 is not a member of TestWorkgroup, add as a member
157: BaseWorkgroupMember member1 = new BaseWorkgroupMember();
158: WorkflowUser user1 = KEWServiceLocator.getUserService()
159: .getWorkflowUser(new AuthenticationUserId("user1"));
160: member1.setWorkflowId(user1.getWorkflowId());
161: member1
162: .setMemberType(EdenConstants.ACTION_REQUEST_USER_RECIPIENT_CD);
163: member1.setWorkgroup(workgroup1);
164: workgroup1.getWorkgroupMembers().add(member1);
165:
166: // add TestWorkgroup as a member
167: BaseWorkgroupMember member2 = new BaseWorkgroupMember();
168: Workgroup testWorkgroup = KEWServiceLocator
169: .getWorkgroupService().getWorkgroup(
170: new GroupNameId("TestWorkgroup"));
171: member2.setWorkflowId(testWorkgroup.getWorkflowGroupId()
172: .getGroupId().toString());
173: member2
174: .setMemberType(EdenConstants.ACTION_REQUEST_WORKGROUP_RECIPIENT_CD);
175: member2.setWorkgroup(workgroup1);
176: workgroup1.getWorkgroupMembers().add(member2);
177:
178: // add ewestfal as a member (already in TestWorkgroup)
179: BaseWorkgroupMember member3 = new BaseWorkgroupMember();
180: WorkflowUser user2 = KEWServiceLocator.getUserService()
181: .getWorkflowUser(new AuthenticationUserId("ewestfal"));
182: member3.setWorkflowId(user2.getWorkflowId());
183: member3
184: .setMemberType(EdenConstants.ACTION_REQUEST_USER_RECIPIENT_CD);
185: member3.setWorkgroup(workgroup1);
186: workgroup1.getWorkgroupMembers().add(member3);
187:
188: KEWServiceLocator.getWorkgroupService().save(workgroup1);
189:
190: Workgroup testNestedWorkgroups1 = KEWServiceLocator
191: .getWorkgroupService().getWorkgroup(
192: new GroupNameId("TestNestedWorkgroups1"));
193: assertNotNull(testNestedWorkgroups1);
194: assertEquals("Workgroup should have 3 members.", 3,
195: testNestedWorkgroups1.getMembers().size());
196:
197: }
198:
199: @Test
200: public void testGetWorkgroupsGroups() throws Exception {
201: loadXmlFile("NestedWorkgroups.xml");
202: Workgroup NWG1 = KEWServiceLocator.getWorkgroupService()
203: .getWorkgroup(new GroupNameId("NWG1"));
204: Workgroup NWG2 = KEWServiceLocator.getWorkgroupService()
205: .getWorkgroup(new GroupNameId("NWG2"));
206: Workgroup NWGNested1 = KEWServiceLocator.getWorkgroupService()
207: .getWorkgroup(new GroupNameId("NWGNested1"));
208: Workgroup NWGNested2 = KEWServiceLocator.getWorkgroupService()
209: .getWorkgroup(new GroupNameId("NWGNested2"));
210:
211: List<Workgroup> workgroupsGroups = KEWServiceLocator
212: .getWorkgroupService().getWorkgroupsGroups(NWG1);
213: assertEquals(2, workgroupsGroups.size());
214: boolean foundNWGNested1 = false;
215: boolean foundNWGNested2 = false;
216: for (Workgroup workgroup : workgroupsGroups) {
217: if (workgroup.getGroupNameId().getNameId().equals(
218: "NWGNested1")) {
219: foundNWGNested1 = true;
220: } else if (workgroup.getGroupNameId().getNameId().equals(
221: "NWGNested2")) {
222: foundNWGNested2 = true;
223: }
224: }
225: assertTrue("Should have found NWGNested1", foundNWGNested1);
226: assertTrue("Should have found NWGNested2", foundNWGNested2);
227: workgroupsGroups = KEWServiceLocator.getWorkgroupService()
228: .getWorkgroupsGroups(NWG2);
229: assertEquals(1, workgroupsGroups.size());
230: assertEquals("NWGNested2", workgroupsGroups.get(0)
231: .getGroupNameId().getNameId());
232: workgroupsGroups = KEWServiceLocator.getWorkgroupService()
233: .getWorkgroupsGroups(NWGNested1);
234: assertEquals(1, workgroupsGroups.size());
235: assertEquals("NWGNested2", workgroupsGroups.get(0)
236: .getGroupNameId().getNameId());
237: workgroupsGroups = KEWServiceLocator.getWorkgroupService()
238: .getWorkgroupsGroups(NWGNested2);
239: assertEquals(0, workgroupsGroups.size());
240: }
241:
242: @Test
243: public void testNestedWorkgroupsDuplicateUsers() throws Exception {
244: loadXmlFile("NestedWorkgroups.xml");
245: Workgroup dupe1 = KEWServiceLocator.getWorkgroupService()
246: .getWorkgroup(new GroupNameId("NestedWGDupe1"));
247: Workgroup dupe2 = KEWServiceLocator.getWorkgroupService()
248: .getWorkgroup(new GroupNameId("NestedWGDupe2"));
249: Workgroup dupe3 = KEWServiceLocator.getWorkgroupService()
250: .getWorkgroup(new GroupNameId("NestedWGDupe3"));
251: Workgroup dupe4 = KEWServiceLocator.getWorkgroupService()
252: .getWorkgroup(new GroupNameId("NestedWGDupe4"));
253:
254: assertEquals("dupe1 should have 6 users.", 6, dupe1.getUsers()
255: .size());
256: assertEquals("dupe2 should have 5 users.", 5, dupe2.getUsers()
257: .size());
258: assertEquals("dupe3 should have 5 users.", 5, dupe3.getUsers()
259: .size());
260: assertEquals("dupe4 should have 3 users.", 3, dupe4.getUsers()
261: .size());
262:
263: Set<String> users = new HashSet<String>();
264: for (WorkflowUser user : dupe1.getUsers()) {
265: users.add(user.getAuthenticationUserId()
266: .getAuthenticationId());
267: }
268: assertEquals("dupe1 should have 6 unique users.", 6, users
269: .size());
270: assertTrue(users.contains("ewestfal"));
271: assertTrue(users.contains("rkirkend"));
272: assertTrue(users.contains("bmcgough"));
273: assertTrue(users.contains("xqi"));
274: assertTrue(users.contains("jhopf"));
275: assertTrue(users.contains("jitrue"));
276: }
277:
278: @Test
279: public void testGroupMembership() throws Exception {
280: loadXmlFile("NestedWorkgroups.xml");
281:
282: WorkgroupService wgService = KEWServiceLocator
283: .getWorkgroupService();
284:
285: // ewestfal should be a member of NWG1, NWGNested1, NWGNested2, NestedWGDupe1, NestedWGDupe2, NestedWGDupe3, NestedWGDupe4
286: WorkflowUser ewestfal = KEWServiceLocator.getUserService()
287: .getWorkflowUser(new AuthenticationUserId("ewestfal"));
288: List<Workgroup> groups = wgService.getUsersGroups(ewestfal);
289: assertTrue("Should be member of NWG1", wgService
290: .isUserMemberOfGroup(new GroupNameId("NWG1"), ewestfal));
291: assertTrue("Should be member of NWG1", isMember("NWG1", groups));
292: assertTrue("Should be member of NWGNested1", wgService
293: .isUserMemberOfGroup(new GroupNameId("NWGNested1"),
294: ewestfal));
295: assertTrue("Should be member of NWGNested1", isMember(
296: "NWGNested1", groups));
297: assertTrue("Should be member of NWGNested2", wgService
298: .isUserMemberOfGroup(new GroupNameId("NWGNested2"),
299: ewestfal));
300: assertTrue("Should be member of NWGNested2", isMember(
301: "NWGNested2", groups));
302: assertTrue("Should be member of NestedWGDupe1", wgService
303: .isUserMemberOfGroup(new GroupNameId("NestedWGDupe1"),
304: ewestfal));
305: assertTrue("Should be member of NestedWGDupe1", isMember(
306: "NestedWGDupe1", groups));
307: assertTrue("Should be member of NestedWGDupe2", wgService
308: .isUserMemberOfGroup(new GroupNameId("NestedWGDupe2"),
309: ewestfal));
310: assertTrue("Should be member of NestedWGDupe2", isMember(
311: "NestedWGDupe2", groups));
312: assertTrue("Should be member of NestedWGDupe3", wgService
313: .isUserMemberOfGroup(new GroupNameId("NestedWGDupe3"),
314: ewestfal));
315: assertTrue("Should be member of NestedWGDupe3", isMember(
316: "NestedWGDupe3", groups));
317: assertTrue("Should be member of NestedWGDupe4", wgService
318: .isUserMemberOfGroup(new GroupNameId("NestedWGDupe4"),
319: ewestfal));
320: assertTrue("Should be member of NestedWGDupe4", isMember(
321: "NestedWGDupe4", groups));
322: // ewestfal should not be a member of NWG2
323: assertFalse("Should NOT be member of NWG2", wgService
324: .isUserMemberOfGroup(new GroupNameId("NWG2"), ewestfal));
325: assertFalse("Should NOT be member of NWG2", isMember("NWG2",
326: groups));
327: }
328:
329: private boolean isMember(String groupName, List<Workgroup> groups) {
330: for (Workgroup group : groups) {
331: if (group.getGroupNameId().getNameId().equals(groupName)) {
332: return true;
333: }
334: }
335: return false;
336: }
337:
338: // @Test public void testNestedWorkgroupsCycles() throws Exception {
339: // BaseWorkgroup workgroup1 = new BaseWorkgroup();
340: // workgroup1.setActiveInd(Boolean.TRUE);
341: // workgroup1.setCurrentInd(Boolean.TRUE);
342: // workgroup1.setDescription("description");
343: // workgroup1.setGroupNameId(new GroupNameId("TestNestedWorkgroups1"));
344: // workgroup1.setWorkgroupType(null);
345: //
346: // // user 1 is not a member of TestWorkgroup, add as a member
347: // BaseWorkgroupMember member1 = new BaseWorkgroupMember();
348: // WorkflowUser user1 = KEWServiceLocator.getUserService().getWorkflowUser(new AuthenticationUserId("user1"));
349: // member1.setWorkflowId(user1.getWorkflowId());
350: // member1.setMemberType(EdenConstants.ACTION_REQUEST_USER_RECIPIENT_CD);
351: // member1.setWorkgroup(workgroup1);
352: // workgroup1.getWorkgroupMembers().add(member1);
353: //
354: // // add ewestfal as a member (already in TestWorkgroup)
355: // BaseWorkgroupMember member3 = new BaseWorkgroupMember();
356: // WorkflowUser user2 = KEWServiceLocator.getUserService().getWorkflowUser(new AuthenticationUserId("ewestfal"));
357: // member3.setWorkflowId(user2.getWorkflowId());
358: // member3.setMemberType(EdenConstants.ACTION_REQUEST_USER_RECIPIENT_CD);
359: // member3.setWorkgroup(workgroup1);
360: // workgroup1.getWorkgroupMembers().add(member3);
361: //
362: // KEWServiceLocator.getWorkgroupService().save(workgroup1);
363: //
364: // BaseWorkgroup workgroup2 = new BaseWorkgroup();
365: // workgroup2.setActiveInd(Boolean.TRUE);
366: // workgroup2.setCurrentInd(Boolean.TRUE);
367: // workgroup2.setDescription("description");
368: // workgroup2.setGroupNameId(new GroupNameId("TestNestedWorkgroups2"));
369: // workgroup2.setWorkgroupType(null);
370: //
371: // // user 1 is not a member of TestWorkgroup, add as a member
372: // member1 = new BaseWorkgroupMember();
373: // WorkflowUser rkirkend = KEWServiceLocator.getUserService().getWorkflowUser(new AuthenticationUserId("rkirkend"));
374: // member1.setWorkflowId(rkirkend.getWorkflowId());
375: // member1.setMemberType(EdenConstants.ACTION_REQUEST_USER_RECIPIENT_CD);
376: // member1.setWorkgroup(workgroup2);
377: // workgroup2.getWorkgroupMembers().add(member1);
378: //
379: // // add TestWorkgroup as a member
380: // BaseWorkgroupMember member2 = new BaseWorkgroupMember();
381: // member2.setWorkflowId(workgroup1.getWorkflowGroupId().getGroupId().toString());
382: // member2.setMemberType(EdenConstants.ACTION_REQUEST_WORKGROUP_RECIPIENT_CD);
383: // member2.setWorkgroup(workgroup2);
384: // workgroup2.getWorkgroupMembers().add(member2);
385: //
386: // KEWServiceLocator.getWorkgroupService().save(workgroup2);
387: //
388: // // now add TestedNestedWorkgroups2 as a member of TestNestedWorkgroups1
389: // member1 = new BaseWorkgroupMember();
390: // member1.setWorkflowId(workgroup2.getWorkflowGroupId().getGroupId().toString());
391: // member1.setMemberType(EdenConstants.ACTION_REQUEST_WORKGROUP_RECIPIENT_CD);
392: // member1.setWorkgroup(workgroup1);
393: // workgroup1.getWorkgroupMembers().add(member1);
394: //
395: // KEWServiceLocator.getWorkgroupService().save(workgroup1);
396: //
397: // Workgroup testNestedWorkgroups1 = KEWServiceLocator.getWorkgroupService().getWorkgroup(new GroupNameId("TestNestedWorkgroups1"));
398: // assertNotNull(testNestedWorkgroups1);
399: // assertEquals("Workgroup should have 3 members.", 3, testNestedWorkgroups1.getMembers().size());
400: // assertEquals("Workgroup should have 3 users.", 3, testNestedWorkgroups1.getUsers().size());
401: //
402: // Workgroup testNestedWorkgroups2 = KEWServiceLocator.getWorkgroupService().getWorkgroup(new GroupNameId("TestNestedWorkgroups2"));
403: // assertNotNull(testNestedWorkgroups2);
404: // assertEquals("Workgroup should have 3 members.", 3, testNestedWorkgroups2.getMembers().size());
405: // assertEquals("Workgroup should have 3 users.", 3, testNestedWorkgroups2.getUsers().size());
406: //
407: // }
408:
409: private String getCacheNameKey(String workgroupName) {
410: return new BaseWorkgroupService()
411: .generateCacheKey(new GroupNameId(workgroupName));
412: }
413:
414: private String getCacheIdKey(Long workgroupId) {
415: return new BaseWorkgroupService()
416: .generateCacheKey(new WorkflowGroupId(workgroupId));
417: }
418:
419: }
|