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