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.ken.services.impl;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.util.xml.XmlException;
21  import org.kuali.rice.ken.bo.Notification;
22  import org.kuali.rice.ken.bo.NotificationMessageDelivery;
23  import org.kuali.rice.ken.bo.NotificationResponse;
24  import org.kuali.rice.ken.service.NotificationMessageDeliveryService;
25  import org.kuali.rice.ken.service.NotificationService;
26  import org.kuali.rice.ken.service.ProcessingResult;
27  import org.kuali.rice.ken.test.KENTestCase;
28  import org.kuali.rice.ken.test.TestConstants;
29  import org.kuali.rice.ken.util.NotificationConstants;
30  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
31  import org.kuali.rice.kew.doctype.bo.DocumentType;
32  import org.kuali.rice.kew.service.KEWServiceLocator;
33  import org.kuali.rice.test.BaselineTestCase;
34  import org.quartz.SchedulerException;
35  
36  import java.io.IOException;
37  import java.util.Collection;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import static org.junit.Assert.*;
43  
44  
45  /**
46   * This class tests the notification service impl.
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  // this whole test case is suspect
50  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
51  public class NotificationServiceImplTest extends KENTestCase {
52  
53      public NotificationServiceImplTest() {
54      }
55  
56      @Test
57      public void testGetNotification_validNotification() {
58          NotificationService nSvc = services.getNotificationService();
59  
60          Notification notification = nSvc.getNotification(TestConstants.NOTIFICATION_1);
61  
62          assertNotNull(notification.getContent());
63          assertTrue(notification.getDeliveryType().equals(TestConstants.NOTIFICATION_1_DELIVERY_TYPE));
64      }
65  
66      @Test
67      public void testGetNotification_nonExistentNotification() {
68          NotificationService nSvc = services.getNotificationService();
69  
70          Notification notification = nSvc.getNotification(TestConstants.NON_EXISTENT_ID);
71  
72          assertNull(notification);
73      }
74  
75      @Test
76      public void testGetNotificationsForRecipientByType_validInput() {
77          NotificationService nSvc = services.getNotificationService();
78  
79          assertTrue(nSvc.getNotificationsForRecipientByType(TestConstants.NOTIFICATION_RECIPIENT_CONTENT_TYPE, TestConstants.NOTIFICATION_RECIPIENT_ID).size() > 0);
80      }
81  
82      @Test
83      public void testGetNotificationsForRecipientByType_invalidInput() {
84          NotificationService nSvc = services.getNotificationService();
85  
86          assertTrue(nSvc.getNotificationsForRecipientByType(TestConstants.INVALID_CONTENT_TYPE, TestConstants.NOTIFICATION_RECIPIENT_ID).size() == 0);
87      }
88  
89      @Test
90      // deadlocks
91      public void testSendNotificationAsXml_validInput() throws InterruptedException, SchedulerException, IOException, XmlException  {
92          services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
93          
94          Thread.sleep(10000);
95          services.getNotificationMessageDeliveryAutoRemovalService().processAutoRemovalOfDeliveredNotificationMessageDeliveries();
96  
97          // get the count of pending action requests
98          DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName("KualiNotification");
99          List<ActionRequestValue> list = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(docType.getDocumentTypeId());
100         int count_before = list.size();
101         LOG.info("ActionRequests: " + count_before);
102         for (ActionRequestValue v: list) {
103             LOG.info("Root request: " + v.getActionRequested() + " " + v.getPrincipalId() + " " + v.getStatus() + " " + v.getRoleName());
104         }
105 
106         // now send ours
107         final NotificationService nSvc = services.getNotificationService();
108 
109         final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("valid_input.xml"));
110 
111         Map map = new HashMap();
112         map.put(NotificationConstants.BO_PROPERTY_NAMES.PROCESSING_FLAG, NotificationConstants.PROCESSING_FLAGS.UNRESOLVED);
113         Collection<Notification> notifications = services.getGenericDao().findMatching(Notification.class, map);
114         assertEquals(0, notifications.size());
115         final String[] result = new String[1];
116 
117         NotificationResponse response = nSvc.sendNotification(notificationMessageAsXml);
118 
119         LOG.info("response XML: " + response);
120         assertEquals(NotificationConstants.RESPONSE_STATUSES.SUCCESS, response.getStatus());
121         notifications = services.getGenericDao().findMatching(Notification.class, map);
122         assertEquals(1, notifications.size());
123         LOG.info("Notification: " + notifications.iterator().next());
124 
125         services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
126         services.getNotificationMessageDeliveryAutoRemovalService().processAutoRemovalOfDeliveredNotificationMessageDeliveries();
127 
128         list = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(docType.getDocumentTypeId());
129         int count_after = list.size();
130         LOG.info("ActionRequests before: " + count_before);
131         LOG.info("ActionRequests after: " + count_after);
132         for (ActionRequestValue v: list) {
133             LOG.info("Root request: " + v.getActionRequested() + " " + v.getPrincipalId() + " " + v.getStatus() + " " + v.getRoleName());
134         }
135 
136         // should have 6 requests, 1 to each user in in Rice Team group
137         assertEquals(6, count_after - count_before);
138     }
139 
140     @Test
141     public void testSendNotificationAsXml_invalidInput() throws IOException {
142         NotificationService nSvc = services.getNotificationService();
143 
144         final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("invalid_input.xml"));
145 
146         try {
147             nSvc.sendNotification(notificationMessageAsXml);
148             fail("XmlException not thrown");
149         } catch (IOException ioe) {
150             fail("Wrong exception thrown, expected XmlException: " + ioe);
151         } catch (XmlException ixe) {
152             // expected
153         } catch (Exception e) {
154             fail("Wrong exception thrown, expected XmlException: " + e);
155         }
156 
157     }
158 
159     @Test
160     public void testSendNotificationAsXml_producerNotAuthorized() throws IOException, XmlException {
161         NotificationService nSvc = services.getNotificationService();
162 
163         final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("producer_not_authorized.xml"));
164 
165         NotificationResponse response = nSvc.sendNotification(notificationMessageAsXml);
166         assertEquals(NotificationConstants.RESPONSE_STATUSES.FAILURE, response.getStatus());
167         assertEquals(NotificationConstants.RESPONSE_MESSAGES.PRODUCER_NOT_AUTHORIZED_FOR_CHANNEL, response.getMessage());
168     }
169 
170     /**
171      * Tests that dismissing a single message delivery for a given user, also removes all other message deliveries for that
172      * user that were generated for the same notification 
173      * @throws InterruptedException
174      */
175     @Test
176     public void testDismiss() throws InterruptedException {
177         // first check that the right amount of deliveries are present in the test data
178         Notification n = services.getNotificationService().getNotification(TestConstants.NOTIFICATION_1);
179         NotificationMessageDeliveryService nmds = services.getNotificationMessageDeliveryService();
180         Collection<NotificationMessageDelivery> deliveries = nmds.getNotificationMessageDeliveries(n, TestConstants.TEST_USER_FIVE);
181         assertNotNull(deliveries);
182         assertEquals(TestConstants.NUM_OF_MSG_DELIVS_FOR_NOTIF_1_TEST_USER_5, deliveries.size());
183         // make sure they are not yet delivered
184         for (NotificationMessageDelivery delivery: deliveries) {
185             assertEquals(NotificationConstants.MESSAGE_DELIVERY_STATUS.UNDELIVERED, delivery.getMessageDeliveryStatus());
186         }
187 
188         NotificationService nSvc = services.getNotificationService();
189 
190         // go ahead and dispatch the message deliveries...
191         ProcessingResult result = services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
192 
193         deliveries = nmds.getNotificationMessageDeliveries(n, TestConstants.TEST_USER_FIVE);
194         assertNotNull(deliveries);
195         assertEquals(TestConstants.NUM_OF_MSG_DELIVS_FOR_NOTIF_1_TEST_USER_5, deliveries.size());
196         // they should be delivered now
197         for (NotificationMessageDelivery delivery: deliveries) {
198             if (delivery.getId().equals(TestConstants.BAD_MESSAGE_DELIVERY_ID)) {
199                 assertEquals("Message Delivery #" + delivery.getId() + "was not delivered", NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED, delivery.getMessageDeliveryStatus());
200             }
201         }
202 
203         String action;
204         if (NotificationConstants.DELIVERY_TYPES.FYI.equals(TestConstants.NOTIFICATION_1_DELIVERY_TYPE)) {
205             action = "fyi";
206         } else if (NotificationConstants.DELIVERY_TYPES.ACK.equals(TestConstants.NOTIFICATION_1_DELIVERY_TYPE)) {
207             action = "ack";
208         } else {
209             throw new RuntimeException("A new delivery type was defined...this test needs to be updated");
210         }
211         nSvc.dismissNotificationMessageDelivery(TestConstants.NOT_MSG_DELIV_NOTIF_1_TEST_USER_5, TestConstants.TEST_USER_FIVE, action);
212 
213         // after dismissing ONE message delivery, they should ALL be dismissed/removed
214         deliveries = nmds.getNotificationMessageDeliveries(n, TestConstants.TEST_USER_FIVE);
215         assertNotNull(deliveries);
216         assertEquals(TestConstants.NUM_OF_MSG_DELIVS_FOR_NOTIF_1_TEST_USER_5, deliveries.size());
217         for (NotificationMessageDelivery delivery: deliveries) {
218             if (delivery.getId() != TestConstants.BAD_MESSAGE_DELIVERY_ID) {
219                 assertEquals("Message Delivery #" + delivery.getId() + "was not removed", NotificationConstants.MESSAGE_DELIVERY_STATUS.REMOVED, delivery.getMessageDeliveryStatus());
220             }
221         }
222 
223     }
224 
225 }