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 java.util.HashMap;
19  
20  import org.kuali.rice.ken.bo.NotificationChannelBo;
21  import org.kuali.rice.ken.bo.NotificationRecipientListBo;
22  import org.kuali.rice.ken.test.util.MockObjectsUtil;
23  import org.kuali.rice.ken.util.NotificationConstants;
24  
25  
26  /**
27   * This class test basic persistence for the NotificationRecipientList business object.
28   * 
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class NotificationRecipientListDaoTest extends BusinessObjectPersistenceTestCaseBase {
32      NotificationChannelBo channel1 = MockObjectsUtil.getTestChannel1();
33      NotificationChannelBo channel2 = MockObjectsUtil.getTestChannel2();
34      
35      NotificationRecipientListBo recipientList1 = new NotificationRecipientListBo();
36      NotificationRecipientListBo recipientList2 = new NotificationRecipientListBo();
37      
38      private String[] recipientTypes = {"Type 1", "Type 2"};
39      private String[] recipientIds = {"ag266", "jaf30"};
40      private String[] updatedRecipientIds = {"bh79", "arh14"};
41      
42      /**
43       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#setup()
44       */
45      @Override
46      protected void setup() {
47  	businessObjectDao.save(channel1);
48  	businessObjectDao.save(channel2);
49      }
50      
51      /**
52       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#delete()
53       */
54      @Override
55      protected boolean delete() {
56  	try {
57  	    businessObjectDao.delete(recipientList1);
58  	    businessObjectDao.delete(recipientList2);
59  	} catch(Exception e) {
60  	    return false;
61  	}
62  	return true;
63      }
64      
65      /**
66       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#retrieve()
67       */
68      @Override
69      protected boolean retrieve() {
70  	recipientList1 = new NotificationRecipientListBo();
71  	recipientList2 = new NotificationRecipientListBo();
72  	
73  	HashMap criteria = new HashMap();
74  	
75  	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.CHANNEL_ID, channel1.getId());
76  	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENT_TYPE, recipientTypes[0]);
77  	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENT_ID, recipientIds[0]);
78  	recipientList1 = (NotificationRecipientListBo) (businessObjectDao.findMatching(NotificationRecipientListBo.class, criteria)).iterator().next();
79  	
80  	criteria.clear();
81  	
82  	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.CHANNEL_ID, channel2.getId());
83  	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENT_TYPE, recipientTypes[1]);
84  	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENT_ID, recipientIds[1]);
85  	recipientList2 = (NotificationRecipientListBo) (businessObjectDao.findMatching(NotificationRecipientListBo.class, criteria)).iterator().next();
86  	
87  	boolean success = true;
88  	
89  	success &= recipientList1 != null;
90  	success &= recipientList1.getRecipientId().equals(recipientIds[0]);
91  	
92  	success &= recipientList2 != null;
93  	success &= recipientList2.getRecipientId().equals(recipientIds[1]);
94  	
95  	return success;
96      }
97      
98      /**
99       * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#insert()
100      */
101     @Override
102     protected boolean insert() {
103 	recipientList1.setChannel(channel1);
104 	recipientList1.setRecipientType(recipientTypes[0]);
105 	recipientList1.setRecipientId(recipientIds[0]);
106 	
107 	recipientList2.setChannel(channel2);
108 	recipientList2.setRecipientType(recipientTypes[1]);
109 	recipientList2.setRecipientId(recipientIds[1]);
110 	
111 	try {
112 	    businessObjectDao.save(recipientList1);
113 	    businessObjectDao.save(recipientList2);
114 	} catch(Exception e) {
115         LOG.error("Error saving recipients", e);
116 	    return false;
117 	}
118 	return true;
119     }
120     
121     /**
122      * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#update()
123      */
124     @Override
125     protected boolean update() {
126 	recipientList1.setRecipientId(updatedRecipientIds[0]);
127 	
128 	recipientList2.setRecipientId(updatedRecipientIds[1]);
129 	
130 	try {
131 	    businessObjectDao.save(recipientList1);
132 	    businessObjectDao.save(recipientList2);
133 	} catch(Exception e) {
134 	    return false;
135 	}
136 	return true;
137     }
138     
139     /**
140      * @see org.kuali.rice.ken.dao.BusinessObjectPersistenceTestCaseBase#validateChanges()
141      */
142     @Override
143     protected boolean validateChanges() {
144 	recipientList1 = new NotificationRecipientListBo();
145 	recipientList2 = new NotificationRecipientListBo();
146 	
147 	HashMap criteria = new HashMap();
148 	
149 	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.CHANNEL_ID, channel1.getId());
150 	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENT_TYPE, recipientTypes[0]);
151 	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENT_ID, updatedRecipientIds[0]);
152 	recipientList1 = (NotificationRecipientListBo) (businessObjectDao.findMatching(NotificationRecipientListBo.class, criteria)).iterator().next();
153 	
154 	criteria.clear();
155 	
156 	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.CHANNEL_ID, channel2.getId());
157 	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENT_TYPE, recipientTypes[1]);
158 	criteria.put(NotificationConstants.BO_PROPERTY_NAMES.RECIPIENT_ID, updatedRecipientIds[1]);
159 	recipientList2 = (NotificationRecipientListBo) (businessObjectDao.findMatching(NotificationRecipientListBo.class, criteria)).iterator().next();
160 	
161 	boolean success = true;
162 	
163 	success &= recipientList1.getRecipientId().equals(updatedRecipientIds[0]);
164 	success &= recipientList2.getRecipientId().equals(updatedRecipientIds[1]);
165 	    
166 	return success;
167     }
168 }