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.kcb.service.impl;
017    
018    import org.junit.Test;
019    import org.kuali.rice.kcb.bo.Message;
020    import org.kuali.rice.kcb.bo.MessageDelivery;
021    import org.kuali.rice.kcb.bo.MessageDeliveryStatus;
022    import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
023    import org.kuali.rice.kcb.service.MessageDeliveryService;
024    import org.kuali.rice.kcb.service.MessageService;
025    import org.kuali.rice.kcb.test.BusinessObjectTestCase;
026    import org.kuali.rice.kcb.test.KCBTestData;
027    import org.springframework.dao.DataAccessException;
028    import org.springframework.dao.DataIntegrityViolationException;
029    
030    import java.util.Collection;
031    
032    import static org.junit.Assert.*;
033    
034    
035    /**
036     * Tests MessageDeliveryService
037     * 
038     * @author Kuali Rice Team (rice.collab@kuali.org)
039     *
040     */
041    public class MessageDeliveryServiceTest extends BusinessObjectTestCase {
042        private MessageService messageService;
043        private MessageDeliveryService messageDeliveryService;
044        private Message MESSAGE;
045        private MessageDelivery MESSAGE_DELIV;
046        
047        @Override
048        public void setUp() throws Exception {
049            super.setUp();
050        
051            messageService = GlobalKCBServiceLocator.getInstance().getMessageService();
052            messageDeliveryService = GlobalKCBServiceLocator.getInstance().getMessageDeliveryService();
053    
054            MESSAGE = KCBTestData.getMessage1();
055            messageService.saveMessage(MESSAGE);
056            
057            MESSAGE_DELIV = KCBTestData.getMessageDelivery1();
058            MESSAGE_DELIV.setMessage(MESSAGE);
059            
060            messageDeliveryService.saveMessageDelivery(MESSAGE_DELIV);
061        }
062    
063        @Test
064        @Override
065        public void testCreate() {
066            MessageDelivery md = new MessageDelivery();
067            md.setDelivererTypeName("pigeon");
068            md.setMessage(MESSAGE);
069            
070            messageDeliveryService.saveMessageDelivery(md);
071    
072            assertNotNull(md.getId());
073            Collection<MessageDelivery> ms = messageDeliveryService.getAllMessageDeliveries();
074            assertNotNull(ms);
075            assertEquals(2, ms.size());
076            
077            MessageDelivery md2 = messageDeliveryService.getMessageDelivery(md.getId());
078            assertNotNull(md2);
079            
080            assertEqualsMD(md, md2);
081        }
082    
083        @Test
084        @Override
085        public void testDelete() {
086            messageDeliveryService.deleteMessageDelivery(MESSAGE_DELIV);
087    
088            Collection<MessageDelivery> ms = messageDeliveryService.getAllMessageDeliveries();
089            assertNotNull(ms);
090            assertEquals(0, ms.size());
091            
092            assertNull(messageDeliveryService.getMessageDelivery(MESSAGE_DELIV.getId()));
093        }
094    
095        @Test(expected = DataIntegrityViolationException.class)
096        @Override
097        public void testDuplicateCreate() {
098            // violates messageid-deliverer constraint
099            final MessageDelivery md = new MessageDelivery();
100            md.setId(KCBTestData.FAKE_ID);
101            md.setDelivererSystemId(MESSAGE_DELIV.getDelivererSystemId());
102            md.setDelivererTypeName(MESSAGE_DELIV.getDelivererTypeName());
103            md.setDeliveryStatus(MESSAGE_DELIV.getDeliveryStatus());
104            md.setLockVerNbr(MESSAGE_DELIV.getLockVerNbr());
105            md.setMessage(MESSAGE);
106            messageDeliveryService.saveMessageDelivery(md);
107        }
108    
109        @Test(expected = DataIntegrityViolationException.class)
110        @Override
111        public void testInvalidCreate() {
112            final MessageDelivery m = new MessageDelivery();
113            messageDeliveryService.saveMessageDelivery(m);
114        }
115    
116        @Test(expected = DataAccessException.class)
117        @Override
118        public void testInvalidDelete() {
119            final MessageDelivery m = new MessageDelivery();
120            m.setId(new Long(-1));
121            // OJB yields an org.springmodules.orm.ojb.OjbOperationException/OptimisticLockException and claims the object
122            // may have been deleted by somebody else
123            messageDeliveryService.deleteMessageDelivery(m);
124        }
125    
126        @Test
127        @Override
128        public void testInvalidRead() {
129            MessageDelivery m = messageDeliveryService.getMessageDelivery(Long.valueOf(-1));
130            assertNull(m);
131        }
132    
133        @Test(expected = DataAccessException.class)
134        @Override
135        public void testInvalidUpdate() {
136            final MessageDelivery m = messageDeliveryService.getMessageDelivery(MESSAGE_DELIV.getId());
137            m.setDelivererTypeName(null);
138            messageDeliveryService.saveMessageDelivery(m);
139        }
140    
141        @Test
142        @Override
143        public void testReadById() {
144            MessageDelivery m = messageDeliveryService.getMessageDelivery(MESSAGE_DELIV.getId());
145    
146            assertEqualsMD(MESSAGE_DELIV, m);
147        }
148    
149        @Test
150        @Override
151        public void testUpdate() {
152            MessageDelivery m = messageDeliveryService.getMessageDelivery(MESSAGE_DELIV.getId());
153            m.setDelivererTypeName("eagle");
154            m.setDeliveryStatus(MessageDeliveryStatus.UNDELIVERED);
155            m.setDelivererSystemId("1234");
156            messageDeliveryService.saveMessageDelivery(m);
157            
158            MessageDelivery m2 = messageDeliveryService.getMessageDelivery(m.getId());
159            assertNotNull(m2);
160            
161            assertEqualsMD(m, m2);
162        }
163        
164        private void assertEqualsMD(MessageDelivery expected, MessageDelivery actual) {
165            assertEquals(expected.getId(), actual.getId());
166            assertEquals(expected.getDelivererSystemId(), actual.getDelivererSystemId());
167            assertEquals(expected.getDelivererTypeName(), actual.getDelivererTypeName());
168            assertEquals(expected.getDeliveryStatus(), actual.getDeliveryStatus());
169            assertEquals(expected.getMessage().getId(), actual.getMessage().getId());
170        }
171    }