1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.quartz.SchedulerException;
34
35 import java.io.IOException;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 import static org.junit.Assert.*;
42
43
44
45
46
47
48
49 public class NotificationServiceImplTest extends KENTestCase {
50
51 public NotificationServiceImplTest() {
52 }
53
54 @Test
55 public void testGetNotification_validNotification() {
56 NotificationService nSvc = services.getNotificationService();
57
58 Notification notification = nSvc.getNotification(TestConstants.NOTIFICATION_1);
59
60 assertNotNull(notification.getContent());
61 assertTrue(notification.getDeliveryType().equals(TestConstants.NOTIFICATION_1_DELIVERY_TYPE));
62 }
63
64 @Test
65 public void testGetNotification_nonExistentNotification() {
66 NotificationService nSvc = services.getNotificationService();
67
68 Notification notification = nSvc.getNotification(TestConstants.NON_EXISTENT_ID);
69
70 assertNull(notification);
71 }
72
73 @Test
74 public void testGetNotificationsForRecipientByType_validInput() {
75 NotificationService nSvc = services.getNotificationService();
76
77 assertTrue(nSvc.getNotificationsForRecipientByType(TestConstants.NOTIFICATION_RECIPIENT_CONTENT_TYPE, TestConstants.NOTIFICATION_RECIPIENT_ID).size() > 0);
78 }
79
80 @Test
81 public void testGetNotificationsForRecipientByType_invalidInput() {
82 NotificationService nSvc = services.getNotificationService();
83
84 assertTrue(nSvc.getNotificationsForRecipientByType(TestConstants.INVALID_CONTENT_TYPE, TestConstants.NOTIFICATION_RECIPIENT_ID).size() == 0);
85 }
86
87 @Test
88
89 public void testSendNotificationAsXml_validInput() throws InterruptedException, SchedulerException, IOException, XmlException {
90 services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
91
92 Thread.sleep(10000);
93 services.getNotificationMessageDeliveryAutoRemovalService().processAutoRemovalOfDeliveredNotificationMessageDeliveries();
94
95
96 DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName("KualiNotification");
97 List<ActionRequestValue> list = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(docType.getDocumentTypeId());
98 int count_before = list.size();
99 LOG.info("ActionRequests: " + count_before);
100 for (ActionRequestValue v: list) {
101 LOG.info("Root request: " + v.getActionRequested() + " " + v.getPrincipalId() + " " + v.getStatus() + " " + v.getRoleName());
102 }
103
104
105 final NotificationService nSvc = services.getNotificationService();
106
107 final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("valid_input.xml"));
108
109 Map map = new HashMap();
110 map.put(NotificationConstants.BO_PROPERTY_NAMES.PROCESSING_FLAG, NotificationConstants.PROCESSING_FLAGS.UNRESOLVED);
111 Collection<Notification> notifications = services.getGenericDao().findMatching(Notification.class, map);
112 assertEquals(0, notifications.size());
113 final String[] result = new String[1];
114
115 NotificationResponse response = nSvc.sendNotification(notificationMessageAsXml);
116
117 LOG.info("response XML: " + response);
118 assertEquals(NotificationConstants.RESPONSE_STATUSES.SUCCESS, response.getStatus());
119 notifications = services.getGenericDao().findMatching(Notification.class, map);
120 assertEquals(1, notifications.size());
121 LOG.info("Notification: " + notifications.iterator().next());
122
123 services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
124 services.getNotificationMessageDeliveryAutoRemovalService().processAutoRemovalOfDeliveredNotificationMessageDeliveries();
125
126 list = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocumentType(docType.getDocumentTypeId());
127 int count_after = list.size();
128 LOG.info("ActionRequests before: " + count_before);
129 LOG.info("ActionRequests after: " + count_after);
130 for (ActionRequestValue v: list) {
131 LOG.info("Root request: " + v.getActionRequested() + " " + v.getPrincipalId() + " " + v.getStatus() + " " + v.getRoleName());
132 }
133
134
135 assertEquals(6, count_after - count_before);
136 }
137
138 @Test
139 public void testSendNotificationAsXml_invalidInput() throws IOException {
140 NotificationService nSvc = services.getNotificationService();
141
142 final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("invalid_input.xml"));
143
144 try {
145 nSvc.sendNotification(notificationMessageAsXml);
146 fail("XmlException not thrown");
147 } catch (IOException ioe) {
148 fail("Wrong exception thrown, expected XmlException: " + ioe);
149 } catch (XmlException ixe) {
150
151 } catch (Exception e) {
152 fail("Wrong exception thrown, expected XmlException: " + e);
153 }
154
155 }
156
157 @Test
158 public void testSendNotificationAsXml_producerNotAuthorized() throws IOException, XmlException {
159 NotificationService nSvc = services.getNotificationService();
160
161 final String notificationMessageAsXml = IOUtils.toString(getClass().getResourceAsStream("producer_not_authorized.xml"));
162
163 NotificationResponse response = nSvc.sendNotification(notificationMessageAsXml);
164 assertEquals(NotificationConstants.RESPONSE_STATUSES.FAILURE, response.getStatus());
165 assertEquals(NotificationConstants.RESPONSE_MESSAGES.PRODUCER_NOT_AUTHORIZED_FOR_CHANNEL, response.getMessage());
166 }
167
168
169
170
171
172
173 @Test
174 public void testDismiss() throws InterruptedException {
175
176 Notification n = services.getNotificationService().getNotification(TestConstants.NOTIFICATION_1);
177 NotificationMessageDeliveryService nmds = services.getNotificationMessageDeliveryService();
178 Collection<NotificationMessageDelivery> deliveries = nmds.getNotificationMessageDeliveries(n, TestConstants.TEST_USER_FIVE);
179 assertNotNull(deliveries);
180 assertEquals(TestConstants.NUM_OF_MSG_DELIVS_FOR_NOTIF_1_TEST_USER_5, deliveries.size());
181
182 for (NotificationMessageDelivery delivery: deliveries) {
183 assertEquals(NotificationConstants.MESSAGE_DELIVERY_STATUS.UNDELIVERED, delivery.getMessageDeliveryStatus());
184 }
185
186 NotificationService nSvc = services.getNotificationService();
187
188
189 ProcessingResult result = services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
190
191 deliveries = nmds.getNotificationMessageDeliveries(n, TestConstants.TEST_USER_FIVE);
192 assertNotNull(deliveries);
193 assertEquals(TestConstants.NUM_OF_MSG_DELIVS_FOR_NOTIF_1_TEST_USER_5, deliveries.size());
194
195 for (NotificationMessageDelivery delivery: deliveries) {
196 if (delivery.getId().equals(TestConstants.BAD_MESSAGE_DELIVERY_ID)) {
197 assertEquals("Message Delivery #" + delivery.getId() + "was not delivered", NotificationConstants.MESSAGE_DELIVERY_STATUS.DELIVERED, delivery.getMessageDeliveryStatus());
198 }
199 }
200
201 String action;
202 if (NotificationConstants.DELIVERY_TYPES.FYI.equals(TestConstants.NOTIFICATION_1_DELIVERY_TYPE)) {
203 action = "fyi";
204 } else if (NotificationConstants.DELIVERY_TYPES.ACK.equals(TestConstants.NOTIFICATION_1_DELIVERY_TYPE)) {
205 action = "ack";
206 } else {
207 throw new RuntimeException("A new delivery type was defined...this test needs to be updated");
208 }
209 nSvc.dismissNotificationMessageDelivery(TestConstants.NOT_MSG_DELIV_NOTIF_1_TEST_USER_5, TestConstants.TEST_USER_FIVE, action);
210
211
212 deliveries = nmds.getNotificationMessageDeliveries(n, TestConstants.TEST_USER_FIVE);
213 assertNotNull(deliveries);
214 assertEquals(TestConstants.NUM_OF_MSG_DELIVS_FOR_NOTIF_1_TEST_USER_5, deliveries.size());
215 for (NotificationMessageDelivery delivery: deliveries) {
216 if (delivery.getId() != TestConstants.BAD_MESSAGE_DELIVERY_ID) {
217 assertEquals("Message Delivery #" + delivery.getId() + "was not removed", NotificationConstants.MESSAGE_DELIVERY_STATUS.REMOVED, delivery.getMessageDeliveryStatus());
218 }
219 }
220
221 }
222
223 }