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