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.plugin;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.List;
024:
025: import javax.servlet.FilterChain;
026: import javax.servlet.ServletException;
027: import javax.servlet.ServletRequest;
028: import javax.servlet.ServletResponse;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.junit.Ignore;
033: import org.junit.Test;
034: import org.kuali.rice.resourceloader.GlobalResourceLoader;
035: import org.kuali.workflow.test.WorkflowTestCase;
036: import org.springframework.mock.web.MockHttpServletRequest;
037: import org.springframework.mock.web.MockHttpServletResponse;
038:
039: import edu.iu.uis.eden.KEWServiceLocator;
040: import edu.iu.uis.eden.actionitem.ActionItem;
041: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
042: import edu.iu.uis.eden.mail.EmailBody;
043: import edu.iu.uis.eden.mail.EmailFrom;
044: import edu.iu.uis.eden.mail.EmailService;
045: import edu.iu.uis.eden.mail.EmailSubject;
046: import edu.iu.uis.eden.mail.EmailTo;
047: import edu.iu.uis.eden.notification.NotificationService;
048: import edu.iu.uis.eden.plugin.attributes.WorkflowLookupable;
049: import edu.iu.uis.eden.user.AuthenticationUserId;
050: import edu.iu.uis.eden.user.BaseWorkflowUser;
051: import edu.iu.uis.eden.user.UserService;
052: import edu.iu.uis.eden.user.WorkflowUser;
053: import edu.iu.uis.eden.web.UserLoginFilter;
054: import edu.iu.uis.eden.web.WebAuthenticationService;
055: import edu.iu.uis.eden.web.session.UserSession;
056: import edu.iu.uis.eden.workgroup.BaseWorkgroup;
057: import edu.iu.uis.eden.workgroup.GroupNameId;
058: import edu.iu.uis.eden.workgroup.Workgroup;
059: import edu.iu.uis.eden.workgroup.WorkgroupService;
060:
061: public class InstitutionalPluginTestOld extends WorkflowTestCase {
062:
063: private static int numNotifications = 0;
064: private static List inbox = new ArrayList();
065:
066: public List getLifecycles() {
067: List super Cycles = super .getLifecycles();
068: // TODO fix this test!!!
069: //superCycles.add(new PluginRegistryLifeCycle());
070: return super Cycles;
071: }
072:
073: @Test
074: public void testUserService() throws Exception {
075: // extract the underlying User Service implementation
076: UserService userService = KEWServiceLocator.getUserService();
077: assertTrue(
078: "The user service should be an instance of UserService",
079: userService instanceof UserService);
080:
081: // test retrieving a user
082: WorkflowUser user = userService
083: .getWorkflowUser(new AuthenticationUserId("ewestfal"));
084: assertNotNull(user);
085: assertFalse(
086: "The classloader of the user should be different then the classloader of this test.",
087: user.getClass().getClassLoader().equals(
088: getClass().getClassLoader()));
089: assertTrue(
090: "The classloader of the user should be the plugin classloader.",
091: user.getClass().getClassLoader() instanceof PluginClassLoader);
092: Class pluginUserClass = user.getClass();
093: assertEquals(
094: "The user should be an instance of PluginTestUser",
095: "edu.iu.uis.eden.user.PluginTestUser", pluginUserClass
096: .getName());
097: // we coded the getTransposedName method so that it will return TransposedName
098: assertEquals("Invalid transposed name.", "TransposedName", user
099: .getTransposedName());
100:
101: // test creating a blank user
102: WorkflowUser blankUser = KEWServiceLocator.getUserService()
103: .getBlankUser();
104: assertNotNull("Blank user should be non-null", blankUser);
105: assertTrue(
106: "WorkflowUser should be an instance of WorkflowUser.",
107: blankUser instanceof WorkflowUser);
108: assertTrue(
109: "WorkflowUser should be an instance of SimpleWorkflowUser.",
110: blankUser instanceof BaseWorkflowUser);
111: assertTrue(
112: "WorkflowUser should be an instance of PluginTestUser.",
113: pluginUserClass.isInstance(blankUser));
114:
115: // the following methods will throw unsupported operations
116: try {
117: KEWServiceLocator.getUserService().getWorkflowUser(
118: new NetworkIdVO("ewestfal"));
119: failIfNoException();
120: } catch (UnsupportedOperationException e) {
121: }
122: try {
123: KEWServiceLocator.getUserService().search(user, false);
124: failIfNoException();
125: } catch (UnsupportedOperationException e) {
126: }
127: }
128:
129: @Test
130: public void testWorkgroupService() throws Exception {
131: // extract the underlying Workgroup Service implementation
132: WorkgroupService workgroupService = KEWServiceLocator
133: .getWorkgroupService();
134: assertTrue(
135: "The workgroup service should be an instance of WorkgroupService",
136: workgroupService instanceof WorkgroupService);
137:
138: // test retrieving a workgroup
139: Workgroup workgroup = workgroupService
140: .getWorkgroup(new GroupNameId("TestWorkgroup"));
141: assertNotNull(workgroup);
142: assertFalse(
143: "The classloader of the workgroup should be different then the classloader of this test.",
144: workgroup.getClass().getClassLoader().equals(
145: getClass().getClassLoader()));
146: assertTrue(
147: "The classloader of the workgroup should be the plugin classloader.",
148: workgroup.getClass().getClassLoader() instanceof PluginClassLoader);
149: Class pluginWorkgroupClass = workgroup.getClass();
150: assertEquals(
151: "The workgroup should be an instance of PluginTestWorkgroup",
152: "edu.iu.uis.eden.workgroup.PluginTestWorkgroup",
153: pluginWorkgroupClass.getName());
154: // we coded the getDescription method so that it will return "Description"
155: assertEquals("Invalid description.", "Description", workgroup
156: .getDescription());
157:
158: // test creating a blank workgroup
159: Workgroup blankWorkgroup = KEWServiceLocator
160: .getWorkgroupService().getBlankWorkgroup();
161: assertNotNull("Blank workgroup should be non-null",
162: blankWorkgroup);
163: assertTrue(
164: "WorkflowWorkgroup should be an instance of WorkflowWorkgroup.",
165: blankWorkgroup instanceof Workgroup);
166: assertTrue(
167: "WorkflowWorkgroup should be an instance of SimpleWorkflowWorkgroup.",
168: blankWorkgroup instanceof BaseWorkgroup);
169: assertTrue(
170: "WorkflowWorkgroup should be an instance of PluginTestWorkgroup.",
171: pluginWorkgroupClass.isInstance(blankWorkgroup));
172:
173: // invoke the following methods to check that they can be called and to cover the code in the PluginWorkgroupService
174: assertNotNull(workgroupService.copy(blankWorkgroup));
175:
176: /*assertEquals(0, workgroupService.getUsersGroups(null).size());
177:
178: assertEquals(0, workgroupService.getUserWorkgroupsDropDownList(null).size());
179:
180: // the following methods will throw unsupported operations
181: try {
182: workgroupService.getWorkgroup(new WorkgroupNameIdVO("TestWorkgroup"));
183: failIfNoException();
184: } catch (UnsupportedOperationException e) {}
185: try {
186: workgroupService.search(workgroup, false);
187: failIfNoException();
188: } catch (UnsupportedOperationException e) {}
189: try {
190: workgroupService.export(null);
191: failIfNoException();
192: } catch (UnsupportedOperationException e) {}
193: try {
194: workgroupService.isUserMemberOfGroup(null, null);
195: failIfNoException();
196: } catch (UnsupportedOperationException e) {}
197: try {
198: workgroupService.search(blankWorkgroup, true);
199: failIfNoException();
200: } catch (UnsupportedOperationException e) {}
201: try {
202: workgroupService.search(blankWorkgroup, SpringServiceLocator.getUserService().getBlankExampleUser());
203: failIfNoException();
204: } catch (UnsupportedOperationException e) {}*/
205: }
206:
207: @Test
208: public void testWebAuthenticationService() throws Exception {
209: WebAuthenticationService webAuthenticationService = KEWServiceLocator
210: .getWebAuthenticationService();
211: HttpServletRequest request = new MockHttpServletRequest();
212: HttpServletResponse response = new MockHttpServletResponse();
213: final String givenNetworkId = "ewestfal";
214: request.setAttribute("networkId", givenNetworkId);
215: assertEquals("Wrong networkId.", givenNetworkId,
216: webAuthenticationService.getNetworkId(request));
217:
218: // try calling it through the UserLoginFilter
219: UserLoginFilter loginFilter = new UserLoginFilter();
220: FilterChain chain = new FilterChain() {
221: public void doFilter(ServletRequest request,
222: ServletResponse response) throws IOException,
223: ServletException {
224: // there should now be a UserSession setup for the given network id
225: UserSession session = UserSession
226: .getAuthenticatedUser();
227: assertNotNull("There should be an authenticated user.",
228: session);
229: assertEquals("Wrong network id.", givenNetworkId,
230: session.getWorkflowUser()
231: .getAuthenticationUserId().getId());
232: }
233: };
234: loginFilter.doFilter(request, response, chain);
235: }
236:
237: @Test
238: public void testLookupableService() throws Exception {
239: WorkflowLookupable lookupable = (WorkflowLookupable) GlobalResourceLoader
240: .getService("PluginTest");// SpringServiceLocator.getExtensionService().getLookupable("PluginTest");
241: assertNotNull("Lookupable should be non-null.", lookupable);
242: List results = lookupable.getSearchResults(new HashMap(),
243: new HashMap());
244: assertEquals("Should be 2 search results.", 2, results.size());
245: assertEquals("1", results.get(0));
246: assertEquals("2", results.get(1));
247:
248: // test looking for a non-existant lookupable
249: // try {
250: lookupable = (WorkflowLookupable) GlobalResourceLoader
251: .getService("ThisIsMyBadLookupableName");//SpringServiceLocator.getExtensionService().getLookupable("ThisIsMyBadLookupableName");
252: fail("Extension should have thrown an exception.");
253: // } catch (ResourceUnavailableException e) {}
254: }
255:
256: // public void testDefaultNote() throws Exception {
257: // CustomNoteAttribute customNoteAttribute = SpringServiceLocator.getExtensionService().getDefaultCustomNoteAttribute();
258: // // the custom note attribute we are testing always returns true for isAuthorizedToAddNotes
259: // assertTrue("Should be authorized to add notes.", customNoteAttribute.isAuthorizedToAddNotes());
260: //
261: // // In our custom note attribute isAuthorizedToEditNote will be based on the boolean value
262: // // of the note text (converted to a boolean from a string)
263: // Note note = new Note();
264: // note.setNoteText("true");
265: // assertTrue("Should be authorized to edit.", customNoteAttribute.isAuthorizedToEditNote(note));
266: // note.setNoteText("false");
267: // assertFalse("Should not be authorized to edit.", customNoteAttribute.isAuthorizedToEditNote(note));
268: //
269: // // In our custom note attribute, passing a null Note should result in a NullPointerException
270: // try {
271: // customNoteAttribute.isAuthorizedToEditNote(null);
272: // fail("An NPE should have been thrown.");
273: // } catch (NullPointerException e) {}
274: // }
275:
276: @Test
277: public void testNotificationService() throws Exception {
278: // the notification service should be pulled from our institutional plugin
279: NotificationService service = KEWServiceLocator
280: .getNotificationService();
281: List actionItems = Collections.nCopies(50, new ActionItem());
282: service.notify(actionItems);
283: assertEquals("Should be 50 notifications.", 50,
284: numNotifications);
285: actionItems = Collections.nCopies(100, new ActionItem());
286: service.notify(actionItems);
287: assertEquals("Should be 100 notifications.", 100,
288: numNotifications);
289: }
290:
291: @Test
292: public void testEmailService() throws Exception {
293: EmailService emailService = KEWServiceLocator.getEmailService();
294: assertEquals("Should have no messages in my inbox.", 0, inbox
295: .size());
296: String from = "ewestfal@MCHammer.com";
297: String to = "rkirkend@VanillaIce.com";
298: String subject = "A Matter of Utmost Importance!!!";
299: String body = "You can't touch this.";
300: emailService.sendEmail(new EmailFrom(from), new EmailTo(to),
301: new EmailSubject(subject), new EmailBody(body), false);
302: assertEquals("Should have one message in my inbox.", 1, inbox
303: .size());
304:
305: // check that it's got the email we just sent
306: EmailMessage message = (EmailMessage) inbox.get(0);
307: assertEquals(from, message.from);
308: assertEquals(to, message.to);
309: assertEquals(subject, message.subject);
310: assertEquals(body, message.body);
311:
312: // send another message, we should have two message in our rad inbox now
313: emailService.sendEmail(new EmailFrom(from), new EmailTo(to),
314: new EmailSubject(subject), new EmailBody(body), false);
315: assertEquals("Should have two messages in my inbox.", 2, inbox
316: .size());
317: }
318:
319: @Ignore("This test needs to be implemented!")
320: @Test
321: public void testPluginListener() throws Exception {
322: // load the class but pass searchCore=false because the way eclipse compiles and loads classes,
323: // this class will exist both in the core and the plugin if running the tests from eclipse
324:
325: // TODO Fix this test!!!
326:
327: //Class pluginListenerClass = SpringServiceLocator.getExtensionService().loadClass("edu.iu.uis.eden.plugin.PluginTestListener", false);
328: //assertNotNull("PluginListenerClass should be non-null", pluginListenerClass);
329: //Field initializedField = pluginListenerClass.getField("initialized");
330: //Field destroyedField = pluginListenerClass.getField("destroyed");
331:
332: //assertNotNull(initializedField);
333: //assertNotNull(destroyedField);
334:
335: // query the static fields from our listener
336: //boolean initialized = initializedField.getBoolean(null);
337: //boolean destroyed = destroyedField.getBoolean(null);
338: //assertTrue("Listener should be initialized.", initialized);
339: //assertFalse("Listener should NOT be destroyed.", destroyed);
340:
341: // now shut down the registry
342: //SpringServiceLocator.getPluginRegistry().stop();
343:
344: // re-query the static fields
345: //initialized = initializedField.getBoolean(null);
346: //destroyed = destroyedField.getBoolean(null);
347: //assertTrue("Listener should be initialized.", initialized);
348: //assertTrue("Listener should be destroyed.", destroyed);
349: }
350:
351: public static void setNumNotifications(int numNotifications) {
352: InstitutionalPluginTestOld.numNotifications = numNotifications;
353: }
354:
355: /**
356: * The PluginTestEmailService will invoke this method when sendEmail is called.
357: */
358: public static void receiveEmailMessage(EmailMessage message) {
359: inbox.add(message);
360: }
361:
362: private void failIfNoException() {
363: fail("Exception should have been thrown");
364: }
365:
366: public static class EmailMessage {
367: public String from;
368: public String to;
369: public String subject;
370: public String body;
371:
372: public EmailMessage(String from, String to, String subject,
373: String body) {
374: this.from = from;
375: this.to = to;
376: this.subject = subject;
377: this.body = body;
378: }
379: }
380:
381: }
|