View Javadoc

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