View Javadoc

1   /**
2    * Copyright 2005-2011 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.kew.mail;
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.assertNull;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.util.Collection;
26  
27  import org.junit.Ignore;
28  import org.junit.Test;
29  import org.kuali.rice.core.api.CoreApiServiceLocator;
30  import org.kuali.rice.core.mail.EmailContent;
31  import org.kuali.rice.kew.api.WorkflowDocument;
32  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
33  import org.kuali.rice.kew.api.action.ActionItem;
34  import org.kuali.rice.kew.mail.service.impl.ActionListEmailServiceImpl;
35  import org.kuali.rice.kew.mail.service.impl.StyleableEmailContentServiceImpl;
36  import org.kuali.rice.kew.service.KEWServiceLocator;
37  import org.kuali.rice.kew.test.KEWTestCase;
38  import org.kuali.rice.kim.api.identity.Person;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  
41  /**
42   * Tests email content generation
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  @Ignore
46  public class EmailMessageTest extends KEWTestCase {
47      private ActionListEmailServiceImpl actionListEmailService = new ActionListEmailServiceImpl();
48      private StyleableEmailContentServiceImpl styleableContentService = new StyleableEmailContentServiceImpl();
49  
50      @Override
51      public void setUp() throws Exception {
52          super.setUp();
53          actionListEmailService.setDeploymentEnvironment("dev");
54         // hardCodedEmailContentService.setDeploymentEnvironment("dev");
55          styleableContentService.setDeploymentEnvironment("dev");
56          styleableContentService.setStyleService(CoreApiServiceLocator.getStyleService());
57      }
58  
59      @Override
60      protected void loadTestData() throws Exception {
61          loadXmlFile("EmailMessageDocType.xml");
62      }
63  
64      private int generateDocs(String[] docTypes, Person user) throws Exception {
65          String nid = getPrincipalNameForId(user.getPrincipalName());
66  
67          for (String docType: docTypes) {
68              WorkflowDocument document = WorkflowDocumentFactory.createDocument(nid, docType);
69              document.setTitle("a title");
70              document.route("");
71              document = WorkflowDocumentFactory.createDocument(nid, docType);
72              document.setTitle("a title");
73              document.route("");
74              document = WorkflowDocumentFactory.createDocument(nid, docType);
75              document.setTitle("a title");
76              document.route("");
77              document = WorkflowDocumentFactory.createDocument(nid, docType);
78              document.setTitle("a title");
79              document.route("");
80              document = WorkflowDocumentFactory.createDocument(nid, docType);
81              document.setTitle("a title");
82              document.route("");
83          }
84  
85          return 5 * docTypes.length;
86      }
87  
88      /**
89       * tests custom stylesheet
90       * @throws Exception
91       */
92      @Test
93      public void testGenerateRemindersCustomStyleSheet() throws Exception {
94          loadXmlFile("customEmailStyleData.xml");
95          assertNotNull(CoreApiServiceLocator.getStyleService().getStyle("kew.email.style"));
96  
97          Person user = KimApiServiceLocator.getPersonService().getPersonByPrincipalName("arh14");
98          int count = generateDocs(new String[] { "PingDocument", "PingDocumentWithEmailAttrib" }, user);
99  
100         Collection<ActionItem> actionItems = org.kuali.rice.kew.actionitem.ActionItem.to(KEWServiceLocator.getActionListService().getActionList(user.getPrincipalId(), null));
101         assertEquals("user should have " + count + " items in his action list.", count, actionItems.size());
102 
103         EmailContent content = styleableContentService.generateImmediateReminder(user, actionItems.iterator().next(), KEWServiceLocator.getDocumentTypeService().findByName(actionItems.iterator().next().getDocName()));
104         assertTrue("Unexpected subject", content.getSubject().startsWith("CUSTOM:"));
105         assertTrue("Unexpected body", content.getBody().startsWith("CUSTOM:"));
106 
107         content = styleableContentService.generateDailyReminder(user, actionItems);
108         assertTrue("Unexpected subject", content.getSubject().startsWith("CUSTOM:"));
109         assertTrue("Unexpected body", content.getBody().startsWith("CUSTOM:"));
110 
111         content = styleableContentService.generateWeeklyReminder(user, actionItems);
112         assertTrue("Unexpected subject", content.getSubject().startsWith("CUSTOM:"));
113         assertTrue("Unexpected body", content.getBody().startsWith("CUSTOM:"));
114     }
115 
116     /**
117      * tests custom stylesheet
118      * @throws Exception
119      */
120     @Test
121     public void testGenerateRemindersDocCustomStyleSheet() throws Exception {
122         // we need to make sure that the immediate email message is customized on a per-doc basis
123         // so we need to easily distinguish from the global style and the custom style
124         // an easy way to do that is use two styles that have introduced obvious and blatent
125         // distinguishing marker...so we just reuse the global custom email style here
126         loadXmlFile("customEmailStyleData.xml");
127         loadXmlFile("docCustomEmailStyleData.xml");
128         assertNotNull(CoreApiServiceLocator.getStyleService().getStyle("kew.email.style"));
129         assertNotNull(CoreApiServiceLocator.getStyleService().getStyle("doc.custom.email.style"));
130 
131         Person user = KimApiServiceLocator.getPersonService().getPersonByPrincipalName("arh14");
132         int count = generateDocs(new String[] { "PingDocumentCustomStyle" }, user);
133 
134         Collection<ActionItem> actionItems = org.kuali.rice.kew.actionitem.ActionItem.to(KEWServiceLocator.getActionListService().getActionList(user.getPrincipalId(), null));
135         assertEquals("user should have " + count + " items in his action list.", count, actionItems.size());
136 
137         EmailContent content = styleableContentService.generateImmediateReminder(user, actionItems.iterator().next(), KEWServiceLocator.getDocumentTypeService().findByName(actionItems.iterator().next().getDocName()));
138         // immediate email reminder should have used the doc type email style and NOT the global style
139         assertFalse("Unexpected subject", content.getSubject().startsWith("CUSTOM:"));
140         assertFalse("Unexpected body", content.getBody().startsWith("CUSTOM:"));
141         assertTrue("Unexpected subject", content.getSubject().startsWith("DOCTYPE CUSTOM:"));
142         assertTrue("Unexpected body", content.getBody().startsWith("DOCTYPE CUSTOM:"));
143 
144 
145         // daily and weekly are unchanged since they are not document type specific
146         content = styleableContentService.generateDailyReminder(user, actionItems);
147         assertTrue("Unexpected subject", content.getSubject().startsWith("CUSTOM:"));
148         assertTrue("Unexpected body", content.getBody().startsWith("CUSTOM:"));
149 
150         content = styleableContentService.generateWeeklyReminder(user, actionItems);
151         assertTrue("Unexpected subject", content.getSubject().startsWith("CUSTOM:"));
152         assertTrue("Unexpected body", content.getBody().startsWith("CUSTOM:"));
153     }
154 
155     /**
156      * tests loading a custom stylesheet that has entities that causes XPath to get confused down the ingestion pipeline...
157      * @throws Exception
158      */
159     @Test
160     public void testBadCustomStyleSheet() throws Exception {
161     	try {
162     		loadXmlFile("badCustomEmailStyleData.xml");
163     		fail("Loading of badCustomEmailStyleData.xml should have failed!");
164     	} catch (Exception e) {}
165         // this doesn't get loaded
166         assertNull(CoreApiServiceLocator.getStyleService().getStyle("bad.kew.email.style"));
167     }
168 }