View Javadoc
1   /**
2    * Copyright 2005-2013 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.kcb.service.impl;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kcb.bo.Message;
20  import org.kuali.rice.kcb.bo.MessageDelivery;
21  import org.kuali.rice.kcb.bo.MessageDeliveryStatus;
22  import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
23  import org.kuali.rice.kcb.service.MessageDeliveryService;
24  import org.kuali.rice.kcb.service.MessageService;
25  import org.kuali.rice.kcb.test.BusinessObjectTestCase;
26  import org.kuali.rice.kcb.test.KCBTestData;
27  import org.springframework.dao.DataAccessException;
28  import org.springframework.dao.DataIntegrityViolationException;
29  
30  import java.util.Collection;
31  
32  import static org.junit.Assert.*;
33  
34  
35  /**
36   * Tests MessageDeliveryService
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   *
40   */
41  public class MessageDeliveryServiceTest extends BusinessObjectTestCase {
42      private MessageService messageService;
43      private MessageDeliveryService messageDeliveryService;
44      private Message MESSAGE;
45      private MessageDelivery MESSAGE_DELIV;
46      
47      @Override
48      public void setUp() throws Exception {
49          super.setUp();
50      
51          messageService = GlobalKCBServiceLocator.getInstance().getMessageService();
52          messageDeliveryService = GlobalKCBServiceLocator.getInstance().getMessageDeliveryService();
53  
54          MESSAGE = KCBTestData.getMessage1();
55          messageService.saveMessage(MESSAGE);
56          
57          MESSAGE_DELIV = KCBTestData.getMessageDelivery1();
58          MESSAGE_DELIV.setMessage(MESSAGE);
59          
60          messageDeliveryService.saveMessageDelivery(MESSAGE_DELIV);
61      }
62  
63      @Test
64      @Override
65      public void testCreate() {
66          MessageDelivery md = new MessageDelivery();
67          md.setDelivererTypeName("pigeon");
68          md.setMessage(MESSAGE);
69          
70          messageDeliveryService.saveMessageDelivery(md);
71  
72          assertNotNull(md.getId());
73          Collection<MessageDelivery> ms = messageDeliveryService.getAllMessageDeliveries();
74          assertNotNull(ms);
75          assertEquals(2, ms.size());
76          
77          MessageDelivery md2 = messageDeliveryService.getMessageDelivery(md.getId());
78          assertNotNull(md2);
79          
80          assertEqualsMD(md, md2);
81      }
82  
83      @Test
84      @Override
85      public void testDelete() {
86          messageDeliveryService.deleteMessageDelivery(MESSAGE_DELIV);
87  
88          Collection<MessageDelivery> ms = messageDeliveryService.getAllMessageDeliveries();
89          assertNotNull(ms);
90          assertEquals(0, ms.size());
91          
92          assertNull(messageDeliveryService.getMessageDelivery(MESSAGE_DELIV.getId()));
93      }
94  
95      @Test(expected = DataIntegrityViolationException.class)
96      @Override
97      public void testDuplicateCreate() {
98          // violates messageid-deliverer constraint
99          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 }