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.ken.dao;
17  
18  import org.kuali.rice.ken.bo.NotificationChannelBo;
19  import org.kuali.rice.ken.bo.NotificationChannelReviewerBo;
20  import org.kuali.rice.ken.bo.NotificationProducerBo;
21  import org.kuali.rice.ken.test.util.MockObjectsUtil;
22  import org.kuali.rice.ken.util.NotificationConstants;
23  import org.kuali.rice.kim.api.KimConstants.KimGroupMemberTypes;
24  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
25  import org.kuali.rice.test.BaselineTestCase.Mode;
26  
27  import java.util.HashMap;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertNotNull;
31  
32  /**
33   * This class test basic persistence for the NotificationChannel business object.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  @BaselineMode(Mode.CLEAR_DB) // this test can't run in a transaction because of how it is using ojb
38  public class NotificationChannelDaoTest extends BusinessObjectPersistenceTestCaseBase {
39      NotificationChannelBo channel1 = MockObjectsUtil.getTestChannel1();
40      NotificationChannelBo channel2 = MockObjectsUtil.getTestChannel2();
41  
42      NotificationProducerBo mockProducer1 = MockObjectsUtil.getTestProducer1();
43  
44      private String[] updatedDescriptions = {"Test 1 - updated description", "Test 2 - updated description"};
45      private boolean[] updatedSubscribables = {false, true};
46  
47      /**
48       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#setup()
49       */
50      @Override
51      protected void setup() {
52          super.setup();
53          businessObjectDao.save(mockProducer1);
54      }
55  
56      /**
57       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#delete()
58       */
59      @Override
60      protected boolean delete() {
61          NotificationChannelBo mockChannel1 = MockObjectsUtil.getTestChannel1();
62          NotificationChannelBo mockChannel2 = MockObjectsUtil.getTestChannel2();
63  
64          channel1 = new NotificationChannelBo();
65          channel2 = new NotificationChannelBo();
66  
67          HashMap criteria = new HashMap();
68  
69          criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel1.getName());
70          channel1 = (NotificationChannelBo) (businessObjectDao.findMatching(NotificationChannelBo.class, criteria)).iterator().next();
71  
72          criteria.clear();
73  
74          criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel2.getName());
75          channel2 = (NotificationChannelBo) (businessObjectDao.findMatching(NotificationChannelBo.class, criteria)).iterator().next();
76  
77          try {
78              businessObjectDao.delete(channel1);
79              businessObjectDao.delete(channel2);
80          } catch(Exception e) {
81              return false;
82          }
83          return true;
84      }
85  
86      /**
87       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#retrieve()
88       */
89      @Override
90      protected boolean retrieve() {
91          NotificationChannelBo mockChannel1 = MockObjectsUtil.getTestChannel1();
92          NotificationChannelBo mockChannel2 = MockObjectsUtil.getTestChannel2();
93  
94          channel1 = new NotificationChannelBo();
95          channel2 = new NotificationChannelBo();
96  
97          HashMap criteria = new HashMap();
98  
99          criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel1.getName());
100         channel1 = (NotificationChannelBo) (businessObjectDao.findMatching(NotificationChannelBo.class, criteria)).iterator().next();
101 
102         criteria.clear();
103 
104         criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel2.getName());
105         channel2 = (NotificationChannelBo) (businessObjectDao.findMatching(NotificationChannelBo.class, criteria)).iterator().next();
106 
107         boolean success = true;
108 
109         success &= channel1 != null;
110         success &= channel1.getDescription().equals(mockChannel1.getDescription());
111         success &= (channel1.isSubscribable()==mockChannel1.isSubscribable());
112         success &= channel1.getProducers().size() == 1;
113 
114         success &= channel2 != null;
115         success &= channel2.getDescription().equals(mockChannel2.getDescription());
116         success &= (channel2.isSubscribable()==mockChannel2.isSubscribable());
117         success &= channel2.getProducers().size() == 1;
118 
119         return success;
120     }
121 
122     /**
123      * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#insert()
124      */
125     @Override
126     protected boolean insert() {
127         // add in a notification channel producer join object
128         try {
129             channel1.getProducers().add(mockProducer1);
130             businessObjectDao.save(channel1);
131 
132             // reload for collections
133             mockProducer1 = (NotificationProducerBo) businessObjectDao.findById(NotificationProducerBo.class, mockProducer1.getId());
134 
135             channel2.getProducers().add(mockProducer1);
136             businessObjectDao.save(channel2);
137             assertEquals(1, channel2.getProducers().size());
138 
139             mockProducer1 = (NotificationProducerBo) businessObjectDao.findById(NotificationProducerBo.class, mockProducer1.getId());
140             assertEquals(2, mockProducer1.getChannels().size());
141 
142             channel2 = (NotificationChannelBo) businessObjectDao.findById(NotificationChannelBo.class, channel2.getId());
143             assertEquals(1, channel2.getProducers().size());
144         } catch(Exception e) {
145             return false;
146         }
147         return true;
148     }
149 
150     /**
151      * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#update()
152      */
153     @Override
154     protected boolean update() {
155         try {
156 
157             channel2 = (NotificationChannelBo) businessObjectDao.findById(NotificationChannelBo.class, channel2.getId());
158             assertEquals(1, channel2.getProducers().size());
159 
160             channel1.setDescription(updatedDescriptions[0]);
161             channel1.setSubscribable(updatedSubscribables[0]);
162             channel1.getProducers().clear();
163 
164             businessObjectDao.save(channel1);
165 
166             mockProducer1 = (NotificationProducerBo) businessObjectDao.findById(NotificationProducerBo.class, mockProducer1.getId());
167             assertNotNull(mockProducer1);
168             assertEquals(1, mockProducer1.getChannels().size());
169 
170             channel2 = (NotificationChannelBo) businessObjectDao.findById(NotificationChannelBo.class, channel2.getId());
171             assertEquals(1, channel2.getProducers().size());
172 
173             channel2.setDescription(updatedDescriptions[1]);
174             channel2.setSubscribable(updatedSubscribables[1]);
175             NotificationChannelReviewerBo reviewer = MockObjectsUtil.buildTestNotificationChannelReviewer(KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE, "aReviewer");
176             reviewer.setChannel(channel2);
177             channel2.getReviewers().add(reviewer);
178 
179             businessObjectDao.save(channel2);
180 
181         } catch(Exception e) {
182             return false;
183         }
184         return true;
185     }
186 
187     /**
188      * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#validateChanges()
189      */
190     @Override
191     protected boolean validateChanges() {
192         //retrieve fresh again
193         NotificationChannelBo mockChannel1 = MockObjectsUtil.getTestChannel1();
194         NotificationChannelBo mockChannel2 = MockObjectsUtil.getTestChannel2();
195 
196         channel1 = new NotificationChannelBo();
197         channel2 = new NotificationChannelBo();
198 
199         HashMap criteria = new HashMap();
200 
201         criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel1.getName());
202         channel1 = (NotificationChannelBo) (businessObjectDao.findMatching(NotificationChannelBo.class, criteria)).iterator().next();
203 
204         criteria.clear();
205 
206         criteria.put(NotificationConstants.BO_PROPERTY_NAMES.NAME, mockChannel2.getName());
207         channel2 = (NotificationChannelBo) (businessObjectDao.findMatching(NotificationChannelBo.class, criteria)).iterator().next();
208 
209         boolean success = true;
210 
211         success &= channel1.getDescription().equals(updatedDescriptions[0]);
212         success &= (channel1.isSubscribable()==updatedSubscribables[0]);
213         success &= channel1.getProducers().size() == 0;
214 
215         success &= channel2.getDescription().equals(updatedDescriptions[1]);
216         success &= (channel2.isSubscribable()==updatedSubscribables[1]);
217         success &= channel2.getProducers().size() == 1;
218         success &= channel2.getReviewers().size() == 1;
219 
220         return success;
221     }
222 }