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