001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.ken.services.impl;
017    
018    import org.apache.commons.io.IOUtils;
019    import org.junit.Test;
020    import org.kuali.rice.core.api.util.xml.XmlException;
021    import org.kuali.rice.ken.bo.Notification;
022    import org.kuali.rice.ken.bo.NotificationRecipient;
023    import org.kuali.rice.ken.bo.NotificationResponse;
024    import org.kuali.rice.ken.bo.NotificationSender;
025    import org.kuali.rice.ken.service.NotificationMessageContentService;
026    import org.kuali.rice.ken.test.KENTestCase;
027    import org.kuali.rice.ken.test.TestConstants;
028    import org.kuali.rice.ken.util.NotificationConstants;
029    import org.kuali.rice.test.BaselineTestCase.BaselineMode;
030    import org.kuali.rice.test.BaselineTestCase.Mode;
031    
032    import java.io.InputStream;
033    import java.sql.Timestamp;
034    import java.util.List;
035    
036    import static org.junit.Assert.*;
037    
038    /**
039     * Tests NotificationMessageContentService
040     * @author Kuali Rice Team (rice.collab@kuali.org)
041     */
042    @BaselineMode(Mode.ROLLBACK_CLEAR_DB)
043    public class NotificationMessageContentServiceImplTest extends KENTestCase {
044        private static final String SAMPLE_EVENT_MESSAGE = "sample_message_event_type.xml";
045        private static final String SAMPLE_SIMPLE_MESSAGE = "sample_message_simple_type.xml";
046        private static final String SAMPLE_MALFORMED_EVENT_MESSAGE = "sample_malformed_message_event_type.xml";
047        private static final String SAMPLE_MALFORMED_SIMPLE_MESSAGE = "sample_malformed_message_simple_type.xml";
048        private static final String SAMPLE_BADNAMESPACE_EVENT_MESSAGE = "badnamespace_message_event_type.xml";
049        private static final String SAMPLE_CHANNEL = TestConstants.VALID_CHANNEL_ONE;
050        private static final String VALID_CHANNEL = TestConstants.VALID_CHANNEL_TWO;
051        private static final String VALID_TYPE = NotificationConstants.DELIVERY_TYPES.FYI;
052        private static final String VALID_CONTENT = "<content xmlns=\"ns:notification/ContentTypeSimple\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
053                                                    " xsi:schemaLocation=\"ns:notification/ContentTypeSimple resource:notification/ContentTypeSimple\">\n" +
054                                                    "    <message>Holiday-Ho-Out Starts Next Week - 11/20/2006!</message>\n" +
055                                                    "</content>";
056        
057        private static final String sampleEdlFile = "NotificationDocumentContent.xml";
058    
059        public NotificationMessageContentServiceImplTest() {
060            //setDefaultRollback(false);
061        }
062    
063        private void testParseNotificationRequestMessage(String samplePath) throws Exception {
064            NotificationMessageContentService impl = services.getNotificationMessageContentService();
065            InputStream is = this.getClass().getResourceAsStream(samplePath);
066            System.out.println(is);
067            Notification notification = impl.parseNotificationRequestMessage(is);
068            assertEquals(SAMPLE_CHANNEL, notification.getChannel().getName());
069            System.out.println(notification.getSenders());
070            System.out.println("notification id: " + notification.getId());
071            List<NotificationSender> sl = notification.getSenders();
072            assertTrue(sl.size() > 0);
073            for (NotificationSender s :sl) {
074                assertNotNull(s);
075                assertNotNull(s.getSenderName());
076            }
077            List<NotificationRecipient> rl = notification.getRecipients();
078            assertTrue(rl.size() > 0);
079            for (NotificationRecipient r : rl) {
080                assertNotNull(r);
081                assertNotNull(r.getRecipientId());
082            }
083            //fail("Not yet implemented");
084    
085            notification.setCreationDateTime(new Timestamp(System.currentTimeMillis()));
086            services.getGenericDao().save(notification);
087            //setComplete();
088        }
089    
090        @Test
091        public void testParseEventNotificationRequestMessage() throws Exception {
092            testParseNotificationRequestMessage(SAMPLE_EVENT_MESSAGE);
093        }
094    
095        @Test
096        public void testParseSimpleNotificationRequestMessage() throws Exception {
097            testParseNotificationRequestMessage(SAMPLE_SIMPLE_MESSAGE);
098        }
099        @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            NotificationResponse response = new NotificationResponse();
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            Notification 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            Notification 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    }