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