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