View Javadoc

1   /*
2    * Copyright 2007-2008 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 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   * Tests NotificationMessageContentService
38   * @author Kuali Rice Team (rice.collab@kuali.org)
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          //setDefaultRollback(false);
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          //fail("Not yet implemented");
82  
83          notification.setCreationDateTime(new Timestamp(System.currentTimeMillis()));
84          services.getGenericDao().save(notification);
85          //setComplete();
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             // expected
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             // expected
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             // expected
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 }