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 java.io.InputStream;
19 import java.sql.Timestamp;
20 import java.util.List;
21
22 import org.junit.Test;
23 import org.kuali.rice.ken.bo.Notification;
24 import org.kuali.rice.ken.bo.NotificationRecipient;
25 import org.kuali.rice.ken.bo.NotificationResponse;
26 import org.kuali.rice.ken.bo.NotificationSender;
27 import org.kuali.rice.ken.exception.InvalidXMLException;
28 import org.kuali.rice.ken.service.NotificationMessageContentService;
29 import org.kuali.rice.ken.test.KENTestCase;
30 import org.kuali.rice.ken.test.TestConstants;
31 import org.kuali.rice.ken.util.NotificationConstants;
32 import org.kuali.rice.ken.util.Util;
33 import org.kuali.rice.test.BaselineTestCase.BaselineMode;
34 import org.kuali.rice.test.BaselineTestCase.Mode;
35
36
37
38
39
40 @BaselineMode(Mode.ROLLBACK)
41 public class NotificationMessageContentServiceImplTest extends KENTestCase {
42 private static final String SAMPLE_EVENT_MESSAGE = "sample_message_event_type.xml";
43 private static final String SAMPLE_SIMPLE_MESSAGE = "sample_message_simple_type.xml";
44 private static final String SAMPLE_MALFORMED_EVENT_MESSAGE = "sample_malformed_message_event_type.xml";
45 private static final String SAMPLE_MALFORMED_SIMPLE_MESSAGE = "sample_malformed_message_simple_type.xml";
46 private static final String SAMPLE_BADNAMESPACE_EVENT_MESSAGE = "badnamespace_message_event_type.xml";
47 private static final String SAMPLE_CHANNEL = TestConstants.VALID_CHANNEL_ONE;
48 private static final String VALID_CHANNEL = TestConstants.VALID_CHANNEL_TWO;
49 private static final String VALID_TYPE = NotificationConstants.DELIVERY_TYPES.FYI;
50 private static final String VALID_CONTENT = "<content xmlns=\"ns:notification/ContentTypeSimple\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
51 " xsi:schemaLocation=\"ns:notification/ContentTypeSimple resource:notification/ContentTypeSimple\">\n" +
52 " <message>Holiday-Ho-Out Starts Next Week - 11/20/2006!</message>\n" +
53 "</content>";
54
55 private static final String sampleEdlFile = "NotificationDocumentContent.xml";
56
57 public NotificationMessageContentServiceImplTest() {
58
59 }
60
61 private void testParseNotificationRequestMessage(String samplePath) throws Exception {
62 NotificationMessageContentService impl = services.getNotificationMessageContentService();
63 InputStream is = this.getClass().getResourceAsStream(samplePath);
64 System.out.println(is);
65 Notification notification = impl.parseNotificationRequestMessage(is);
66 assertEquals(SAMPLE_CHANNEL, notification.getChannel().getName());
67 System.out.println(notification.getSenders());
68 System.out.println("notification id: " + notification.getId());
69 List<NotificationSender> sl = notification.getSenders();
70 assertTrue(sl.size() > 0);
71 for (NotificationSender s :sl) {
72 assertNotNull(s);
73 assertNotNull(s.getSenderName());
74 }
75 List<NotificationRecipient> rl = notification.getRecipients();
76 assertTrue(rl.size() > 0);
77 for (NotificationRecipient r : rl) {
78 assertNotNull(r);
79 assertNotNull(r.getRecipientId());
80 }
81
82
83 notification.setCreationDateTime(new Timestamp(System.currentTimeMillis()));
84 services.getGenericDao().save(notification);
85
86 }
87
88 @Test
89 public void testParseEventNotificationRequestMessage() throws Exception {
90 testParseNotificationRequestMessage(SAMPLE_EVENT_MESSAGE);
91 }
92
93 @Test
94 public void testParseSimpleNotificationRequestMessage() throws Exception {
95 testParseNotificationRequestMessage(SAMPLE_SIMPLE_MESSAGE);
96 }
97 @Test
98 public void testParseMalformedEventNotificationRequestMessage() throws Exception {
99 try {
100 testParseNotificationRequestMessage(SAMPLE_MALFORMED_EVENT_MESSAGE);
101 fail("malformed event message passed validation");
102 } catch (InvalidXMLException ixe) {
103
104 return;
105 }
106 }
107 @Test
108 public void testParseBadNamespaceEventNotificationRequestMessage() throws Exception {
109 try {
110 testParseNotificationRequestMessage(SAMPLE_BADNAMESPACE_EVENT_MESSAGE);
111 fail("malformed event message passed validation");
112 } catch (InvalidXMLException ixe) {
113
114 return;
115 }
116 }
117 @Test
118 public void testParseMalformedSimpleNotificationRequestMessage() throws Exception {
119 try {
120 testParseNotificationRequestMessage(SAMPLE_MALFORMED_SIMPLE_MESSAGE);
121 fail("malformed simple message passed validation");
122 } catch (InvalidXMLException ixe) {
123
124 }
125 }
126
127 @Test
128 public void testGenerateNotificationResponseMessage() throws Exception {
129 NotificationResponse response = new NotificationResponse();
130 response.setStatus("PASS");
131 response.setMessage("Here is your response");
132 NotificationMessageContentService impl = services.getNotificationMessageContentService();
133 String xml = impl.generateNotificationResponseMessage(response);
134 assertTrue(xml.length() == 89);
135 }
136
137 @Test
138 public void testGenerateNotificationMessage() throws Exception {
139 NotificationMessageContentService impl = services.getNotificationMessageContentService();
140 InputStream is = this.getClass().getResourceAsStream(SAMPLE_SIMPLE_MESSAGE);
141 System.out.println(is);
142 Notification notification = impl.parseNotificationRequestMessage(is);
143 String XML = impl.generateNotificationMessage(notification);
144 assertTrue(XML.length()>0);
145 }
146
147 @Test
148 public void testParseSerializedNotificationXml() throws Exception {
149 InputStream is = this.getClass().getResourceAsStream(sampleEdlFile);
150
151 byte[] bytes = Util.readFully(is);
152
153 NotificationMessageContentService impl = services.getNotificationMessageContentService();
154
155 Notification notification = impl.parseSerializedNotificationXml(bytes);
156
157 assertNotNull(notification);
158 assertEquals(VALID_CHANNEL, notification.getChannel().getName());
159
160 assertEquals(VALID_TYPE, notification.getDeliveryType());
161
162 assertEquals(VALID_CONTENT.replaceAll("\\s+", " "), notification.getContent().replaceAll("\\s+", " "));
163 }
164 }