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