001    /**
002     * Copyright 2005-2014 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.NotificationBo;
022    import org.kuali.rice.ken.bo.NotificationRecipientBo;
023    import org.kuali.rice.ken.bo.NotificationResponseBo;
024    import org.kuali.rice.ken.bo.NotificationSenderBo;
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.krad.data.PersistenceOption;
030    import org.kuali.rice.krad.service.KRADServiceLocator;
031    import org.kuali.rice.test.BaselineTestCase.BaselineMode;
032    import org.kuali.rice.test.BaselineTestCase.Mode;
033    
034    import java.io.InputStream;
035    import java.sql.Timestamp;
036    import java.util.List;
037    
038    import static org.junit.Assert.*;
039    
040    /**
041     * Tests NotificationMessageContentService
042     * @author Kuali Rice Team (rice.collab@kuali.org)
043     */
044    @BaselineMode(Mode.CLEAR_DB)
045    public class NotificationMessageContentServiceImplTest extends KENTestCase {
046        private static final String SAMPLE_EVENT_MESSAGE = "sample_message_event_type.xml";
047        private static final String SAMPLE_SIMPLE_MESSAGE = "sample_message_simple_type.xml";
048        private static final String SAMPLE_MALFORMED_EVENT_MESSAGE = "sample_malformed_message_event_type.xml";
049        private static final String SAMPLE_MALFORMED_SIMPLE_MESSAGE = "sample_malformed_message_simple_type.xml";
050        private static final String SAMPLE_BADNAMESPACE_EVENT_MESSAGE = "badnamespace_message_event_type.xml";
051        private static final String SAMPLE_CHANNEL = TestConstants.VALID_CHANNEL_ONE;
052        private static final String VALID_CHANNEL = TestConstants.VALID_CHANNEL_TWO;
053        private static final String VALID_TYPE = NotificationConstants.DELIVERY_TYPES.FYI;
054        private static final String VALID_CONTENT = "<content xmlns=\"ns:notification/ContentTypeSimple\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
055                                                    " xsi:schemaLocation=\"ns:notification/ContentTypeSimple resource:notification/ContentTypeSimple\">\n" +
056                                                    "    <message>Holiday-Ho-Out Starts Next Week - 11/20/2006!</message>\n" +
057                                                    "</content>";
058        
059        private static final String sampleEdlFile = "NotificationDocumentContent.xml";
060    
061        public NotificationMessageContentServiceImplTest() {
062            //setDefaultRollback(false);
063        }
064    
065        private void testParseNotificationRequestMessage(String samplePath) throws Exception {
066            NotificationMessageContentService impl = services.getNotificationMessageContentService();
067            InputStream is = this.getClass().getResourceAsStream(samplePath);
068            System.out.println(is);
069            NotificationBo notification = impl.parseNotificationRequestMessage(is);
070            assertEquals(SAMPLE_CHANNEL, notification.getChannel().getName());
071            System.out.println(notification.getSenders());
072            System.out.println("notification id: " + notification.getId());
073            List<NotificationSenderBo> sl = notification.getSenders();
074            assertTrue(sl.size() > 0);
075            for (NotificationSenderBo s :sl) {
076                assertNotNull(s);
077                assertNotNull(s.getSenderName());
078            }
079            List<NotificationRecipientBo> rl = notification.getRecipients();
080            assertTrue(rl.size() > 0);
081            for (NotificationRecipientBo r : rl) {
082                assertNotNull(r);
083                assertNotNull(r.getRecipientId());
084            }
085            //fail("Not yet implemented");
086    
087            notification.setCreationDateTimeValue(new Timestamp(System.currentTimeMillis()));
088            KRADServiceLocator.getDataObjectService().save(notification, PersistenceOption.FLUSH);
089            //setComplete();
090        }
091    
092        @Test
093        public void testParseEventNotificationRequestMessage() throws Exception {
094            testParseNotificationRequestMessage(SAMPLE_EVENT_MESSAGE);
095        }
096    
097        @Test
098        public void testParseSimpleNotificationRequestMessage() throws Exception {
099            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    }