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 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
54
55
56
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
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
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
87
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
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
135 } catch (WorkflowRuntimeException wre) {
136
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
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
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
174
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
207 assertEquals(6, count_after - count_before);
208 }
209 }