View Javadoc

1   /**
2    * Copyright 2005-2011 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.service.GlobalKCBServiceLocator;
21  import org.kuali.rice.kcb.service.MessageService;
22  import org.kuali.rice.kcb.test.BusinessObjectTestCase;
23  import org.kuali.rice.kcb.test.KCBTestData;
24  import org.springframework.dao.DataAccessException;
25  import org.springframework.dao.DataIntegrityViolationException;
26  
27  import java.util.Collection;
28  
29  import static org.junit.Assert.*;
30  
31  
32  /**
33   * Tests MessageService 
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public class MessageServiceTest extends BusinessObjectTestCase {
39      private MessageService messageService;
40      private Message MESSAGE;
41      
42      @Override
43      public void setUp() throws Exception {
44          super.setUp();
45      
46          messageService = GlobalKCBServiceLocator.getInstance().getMessageService();
47          MESSAGE = KCBTestData.getMessage1();
48          messageService.saveMessage(MESSAGE);
49      }
50  
51      @Test
52      @Override
53      public void testCreate() {
54          Message m = new Message();
55          m.setContent("test content 2");
56          m.setChannel("channel2");
57          m.setContentType("test content type 2");
58          m.setDeliveryType("test delivery type 2");
59          m.setRecipient("test recipient 2");
60          m.setTitle("test title 2");
61  
62          messageService.saveMessage(m);
63          assertNotNull(m.getId());
64  
65          Collection<Message> ms = messageService.getAllMessages();
66          assertNotNull(ms);
67          assertEquals(2, ms.size());
68          
69          Message m2 = messageService.getMessage(m.getId());
70          assertNotNull(m2);
71  
72          assertEqualsMD(m, m2);
73          
74          Message m1 = new Message();
75          m1.setContent("a");
76          m1.setChannel("a");
77          m1.setContentType("a");
78          m1.setDeliveryType("a");
79          m1.setRecipient("a");
80          m1.setTitle("a");
81          
82          // should allow more than one record with NULL origin id
83          messageService.saveMessage(m1);
84      }
85  
86      @Test
87      @Override
88      public void testDelete() {
89          messageService.deleteMessage(MESSAGE);
90          
91          Collection<Message> ms = messageService.getAllMessages();
92          assertNotNull(ms);
93          assertEquals(0, ms.size());
94          
95          assertNull(messageService.getMessage(MESSAGE.getId()));
96      }
97  
98      /* since OJB treats creates and updates the same, and we have no constraints,
99         this test doesn't really test anything under OJB */
100     @Test
101     @Override
102     public void testDuplicateCreate() {
103         Message m = new Message(MESSAGE);
104         messageService.saveMessage(m);
105     }
106 
107     @Test(expected = DataIntegrityViolationException.class)
108     @Override
109     public void testInvalidCreate() {
110         final Message m = new Message();
111         messageService.saveMessage(m);
112     }
113 
114     @Test(expected = DataAccessException.class)
115     @Override
116     public void testInvalidDelete() {
117         final Message m = new Message();
118         m.setId(new Long(-1));
119         // OJB yields an org.springmodules.orm.ojb.OjbOperationException/OptimisticLockException and claims the object
120         // may have been deleted by somebody else
121         messageService.deleteMessage(m);
122     }
123 
124     @Test
125     @Override
126     public void testInvalidRead() {
127         Message m = messageService.getMessage(Long.valueOf(-1));
128         assertNull(m);
129     }
130 
131     @Test(expected = DataAccessException.class)
132     @Override
133     public void testInvalidUpdate() {
134         final Message m = messageService.getMessage(MESSAGE.getId());
135         m.setChannel(null);
136         messageService.saveMessage(m);
137     }
138 
139     @Test
140     @Override
141     public void testReadById() {
142         Message m = messageService.getMessage(MESSAGE.getId());
143 
144         assertEqualsMD(MESSAGE, m);
145     }
146 
147     @Test
148     @Override
149     public void testUpdate() {
150         Message m = messageService.getMessage(MESSAGE.getId());
151         m.setTitle("A better title");
152         m.setContent("different content");
153         messageService.saveMessage(m);
154         
155         Message m2 = messageService.getMessage(m.getId());
156         assertNotNull(m2);
157         
158         assertEqualsMD(m, m2);
159     }
160     
161     /**
162      * Asserts that an actual Message is equal to an expected Message
163      * @param expected the expected Message
164      * @param actual the actual Message
165      */
166     private void assertEqualsMD(Message expected, Message actual) {
167         assertEquals(expected.getId(), actual.getId());
168         assertEquals(expected.getCreationDateTime(), actual.getCreationDateTime());
169         assertEquals(expected.getContent(), actual.getContent());
170         assertEquals(expected.getContentType(), actual.getContentType());
171         assertEquals(expected.getDeliveryType(), actual.getDeliveryType());
172         assertEquals(expected.getRecipient(), actual.getRecipient());
173         assertEquals(expected.getTitle(), actual.getTitle());
174     }
175 }