View Javadoc

1   /**
2    * Copyright 2005-2013 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.ken.services.impl;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.core.api.util.xml.XmlException;
22  import org.kuali.rice.ken.api.KenApiConstants;
23  import org.kuali.rice.ken.api.notification.Notification;
24  import org.kuali.rice.ken.api.notification.NotificationResponse;
25  import org.kuali.rice.ken.api.service.SendNotificationService;
26  import org.kuali.rice.ken.bo.NotificationBo;
27  import org.kuali.rice.ken.bo.NotificationMessageDelivery;
28  import org.kuali.rice.ken.bo.NotificationResponseBo;
29  import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
30  import org.kuali.rice.ken.service.NotificationService;
31  import org.kuali.rice.ken.service.ProcessingResult;
32  import org.kuali.rice.ken.test.KENTestCase;
33  import org.kuali.rice.ken.test.TestConstants;
34  import org.kuali.rice.ken.util.NotificationConstants;
35  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
36  import org.kuali.rice.kew.api.WorkflowRuntimeException;
37  import org.kuali.rice.kew.doctype.bo.DocumentType;
38  import org.kuali.rice.kew.service.KEWServiceLocator;
39  import org.kuali.rice.test.BaselineTestCase;
40  import org.quartz.SchedulerException;
41  
42  import javax.xml.namespace.QName;
43  import java.io.IOException;
44  import java.util.Collection;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  import static org.junit.Assert.*;
50  
51  /**
52   * This class tests the notification service impl.
53   * @author Kuali Rice Team (rice.collab@kuali.org)
54   */
55  // this whole test case is suspect
56  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
57  public class SendNotificationServiceImplTest extends KENTestCase {
58  
59      public SendNotificationServiceImplTest() {
60      }
61  
62      @Test
63      // deadlocks
64      public void testSendNotificationAsXml_validInput() throws InterruptedException, SchedulerException, IOException, XmlException  {
65          services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
66          
67          Thread.sleep(10000);
68          services.getNotificationMessageDeliveryAutoRemovalService().processAutoRemovalOfDeliveredNotificationMessageDeliveries();
69  
70          // get the count of pending action requests
71          DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName("KualiNotification");
72          List<ActionRequestValue> list = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(docType.getDocumentTypeId());
73          int count_before = list.size();
74          LOG.info("ActionRequests: " + count_before);
75          for (ActionRequestValue v: list) {
76              LOG.info("Root request: " + v.getActionRequested() + " " + v.getPrincipalId() + " " + v.getStatus() + " " + v.getRoleName());
77          }
78  
79          // now send ours
80          //final NotificationService nSvc = services.getNotificationService();
81          final SendNotificationService sendNotificationService = (SendNotificationService) GlobalResourceLoader.getService(new QName(
82                  KenApiConstants.Namespaces.KEN_NAMESPACE_2_0, "sendNotificationService"));
83          
84          final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("valid_input.xml"));
85  
86          Map map = new HashMap();
87          map.put(NotificationConstants.BO_PROPERTY_NAMES.PROCESSING_FLAG, NotificationConstants.PROCESSING_FLAGS.UNRESOLVED);
88          Collection<NotificationBo> notifications = services.getGenericDao().findMatching(NotificationBo.class, map);
89          assertEquals(0, notifications.size());
90          final String[] result = new String[1];
91  
92          NotificationResponse response = sendNotificationService.invoke(notificationMessageAsXml);
93  
94          LOG.info("response XML: " + response);
95          assertEquals(NotificationConstants.RESPONSE_STATUSES.SUCCESS, response.getStatus());
96          notifications = services.getGenericDao().findMatching(NotificationBo.class, map);
97          assertEquals(1, notifications.size());
98          LOG.info("Notification: " + notifications.iterator().next());
99  
100         services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
101         services.getNotificationMessageDeliveryAutoRemovalService().processAutoRemovalOfDeliveredNotificationMessageDeliveries();
102 
103         list = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(docType.getDocumentTypeId());
104         int count_after = list.size();
105         LOG.info("ActionRequests before: " + count_before);
106         LOG.info("ActionRequests after: " + count_after);
107         for (ActionRequestValue v: list) {
108             LOG.info("Root request: " + v.getActionRequested() + " " + v.getPrincipalId() + " " + v.getStatus() + " " + v.getRoleName());
109         }
110 
111         // should have 6 requests, 1 to each user in in Rice Team group
112         assertEquals(6, count_after - count_before);
113     }
114 
115     @Test
116     public void testSendNotificationAsXml_invalidInput() throws IOException {
117         SendNotificationService sendNotificationService = (SendNotificationService) GlobalResourceLoader.getService(new QName(
118                 KenApiConstants.Namespaces.KEN_NAMESPACE_2_0, "sendNotificationService"));
119 
120         final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("invalid_input.xml"));
121 
122         try {
123             sendNotificationService.invoke(notificationMessageAsXml);
124             fail("XmlException not thrown");
125         } catch (XmlException ixe) {
126             // expected
127         } catch (WorkflowRuntimeException wre) {
128             // expected
129         } catch (Exception e) {
130             fail("Wrong exception thrown, expected XmlException: " + e);
131         }
132 
133     }
134 
135     @Test
136     public void testSendNotificationAsXml_producerNotAuthorized() throws IOException, XmlException {
137         SendNotificationService sendNotificationService = (SendNotificationService) GlobalResourceLoader.getService(new QName(
138                 KenApiConstants.Namespaces.KEN_NAMESPACE_2_0, "sendNotificationService"));
139 
140         final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("producer_not_authorized.xml"));
141 
142         NotificationResponse response = sendNotificationService.invoke(notificationMessageAsXml);
143         assertEquals(NotificationConstants.RESPONSE_STATUSES.FAILURE, response.getStatus());
144         assertEquals(NotificationConstants.RESPONSE_MESSAGES.PRODUCER_NOT_AUTHORIZED_FOR_CHANNEL, response.getMessage());
145     }
146 
147 
148     @Test
149     // deadlocks
150     public void testSendNotificationAsBo_validInput() throws InterruptedException, SchedulerException, IOException, XmlException  {
151         services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
152 
153         Thread.sleep(10000);
154         services.getNotificationMessageDeliveryAutoRemovalService().processAutoRemovalOfDeliveredNotificationMessageDeliveries();
155 
156         // get the count of pending action requests
157         DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName("KualiNotification");
158         List<ActionRequestValue> list = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(docType.getDocumentTypeId());
159         int count_before = list.size();
160         LOG.info("ActionRequests: " + count_before);
161         for (ActionRequestValue v: list) {
162             LOG.info("Root request: " + v.getActionRequested() + " " + v.getPrincipalId() + " " + v.getStatus() + " " + v.getRoleName());
163         }
164 
165         // now send ours
166         //final NotificationService nSvc = services.getNotificationService();
167         final SendNotificationService sendNotificationService = (SendNotificationService) GlobalResourceLoader.getService(new QName(
168                 KenApiConstants.Namespaces.KEN_NAMESPACE_2_0, "sendNotificationService"));
169 
170         final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("valid_input.xml"));
171         NotificationBo notificationBo = services.getNotificationMessageContentService().parseNotificationRequestMessage(
172                 notificationMessageAsXml);
173         Map map = new HashMap();
174         map.put(NotificationConstants.BO_PROPERTY_NAMES.PROCESSING_FLAG, NotificationConstants.PROCESSING_FLAGS.UNRESOLVED);
175         Collection<NotificationBo> notifications = services.getGenericDao().findMatching(NotificationBo.class, map);
176         assertEquals(0, notifications.size());
177         final String[] result = new String[1];
178         Notification notificiation = NotificationBo.to(notificationBo);
179         NotificationResponse response = sendNotificationService.sendNotification(notificiation);
180 
181         LOG.info("response XML: " + response);
182         assertEquals(NotificationConstants.RESPONSE_STATUSES.SUCCESS, response.getStatus());
183         notifications = services.getGenericDao().findMatching(NotificationBo.class, map);
184         assertEquals(1, notifications.size());
185         LOG.info("Notification: " + notifications.iterator().next());
186 
187         services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
188         services.getNotificationMessageDeliveryAutoRemovalService().processAutoRemovalOfDeliveredNotificationMessageDeliveries();
189 
190         list = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(docType.getDocumentTypeId());
191         int count_after = list.size();
192         LOG.info("ActionRequests before: " + count_before);
193         LOG.info("ActionRequests after: " + count_after);
194         for (ActionRequestValue v: list) {
195             LOG.info("Root request: " + v.getActionRequested() + " " + v.getPrincipalId() + " " + v.getStatus() + " " + v.getRoleName());
196         }
197 
198         // should have 6 requests, 1 to each user in in Rice Team group
199         assertEquals(6, count_after - count_before);
200     }
201 }