View Javadoc

1   /*
2    * Copyright 2007-2008 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.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.junit.Test;
26  import org.kuali.rice.ken.bo.NotificationProducer;
27  import org.kuali.rice.ken.test.TestConstants;
28  import org.springframework.dao.DataAccessException;
29  
30  /**
31   * This class tests the various methods offered up by the BusinessObjectDao.
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class BusinessObjectDaoTest extends BusinessObjectDaoTestCaseBase {
36      private static final Map<Long, NotificationProducer> producers = new HashMap<Long, NotificationProducer>();
37      static {
38          producers.put(TestConstants.PRODUCER_1.getId(), TestConstants.PRODUCER_1);
39          producers.put(TestConstants.PRODUCER_2.getId(), TestConstants.PRODUCER_2);
40      }
41      private static final NotificationProducer[] producerOrder = new NotificationProducer[] { TestConstants.PRODUCER_1, TestConstants.PRODUCER_2 };
42  
43      @Test
44      public void testFindByPrimaryKey() {
45  	Map primaryKeys = new HashMap();
46  	primaryKeys.put("id", TestConstants.PRODUCER_2.getId());
47  	NotificationProducer notificationProducer = (NotificationProducer)businessObjectDao.findByPrimaryKey(NotificationProducer.class, primaryKeys);
48  	assertEquals(TestConstants.PRODUCER_2.getId().longValue(), notificationProducer.getId().longValue());
49  	assertEquals(TestConstants.PRODUCER_2.getName(), notificationProducer.getName());
50  	assertEquals(TestConstants.PRODUCER_2.getDescription(), notificationProducer.getDescription());
51  	assertEquals(TestConstants.PRODUCER_2.getContactInfo(), notificationProducer.getContactInfo());
52      }
53      
54      @Test
55      public void testFindByUniqueKey() {
56  	Map uniqueKeys = new HashMap();
57  	uniqueKeys.put("name", TestConstants.PRODUCER_2.getName());
58  	NotificationProducer notificationProducer = (NotificationProducer)businessObjectDao.findByPrimaryKey(NotificationProducer.class, uniqueKeys);
59  	assertEquals(TestConstants.PRODUCER_2.getId().longValue(), notificationProducer.getId().longValue());
60  	assertEquals(TestConstants.PRODUCER_2.getName(), notificationProducer.getName());
61  	assertEquals(TestConstants.PRODUCER_2.getDescription(), notificationProducer.getDescription());
62  	assertEquals(TestConstants.PRODUCER_2.getContactInfo(), notificationProducer.getContactInfo());
63      }
64  
65      @Test
66      public void testFindAll() {
67  	Collection notificationProducers = businessObjectDao.findAll(NotificationProducer.class);
68  	assertEquals(5, notificationProducers.size());
69      }
70  
71      @Test
72      public void testFindAllOrderBy() {
73  	// test ascending order
74  	Collection notificationProducers = businessObjectDao.findAllOrderBy(NotificationProducer.class, "id", true);
75  	assertEquals(TestConstants.NUM_TEST_PRODUCERS, notificationProducers.size());
76  
77  	// test descending order
78  	notificationProducers = businessObjectDao.findAllOrderBy(NotificationProducer.class, "id", false);
79          assertEquals(TestConstants.NUM_TEST_PRODUCERS, notificationProducers.size());
80      }
81  
82      @Test
83      public void testFindMatching() {
84  	Map fieldValues = new HashMap();
85  	fieldValues.put("contactInfo", TestConstants.PRODUCER_1.getContactInfo());
86  	Collection notificationProducers = businessObjectDao.findMatching(NotificationProducer.class, fieldValues);
87  	assertEquals(2, notificationProducers.size());
88  	Iterator it = notificationProducers.iterator();
89  	while (it.hasNext()) {
90  	    NotificationProducer producer = (NotificationProducer)it.next();
91              NotificationProducer expected = producers.get(producer.getId());
92  	    assertEquals(expected.getName(), producer.getName());
93              assertEquals(expected.getDescription(), producer.getDescription());
94              assertEquals(expected.getContactInfo(), producer.getContactInfo());
95  	}
96      }
97  
98      @Test
99      public void testCountMatchingClassMap() {
100 	Map fieldValues = new HashMap();
101 	fieldValues.put("contactInfo", TestConstants.CONTACT_INFO_2_PRODUCERS);
102 	assertEquals(2, businessObjectDao.countMatching(NotificationProducer.class, fieldValues));
103 	
104 	fieldValues = new HashMap();
105 	fieldValues.put("contactInfo", TestConstants.CONTACT_INFO_1_PRODUCER);
106 	assertEquals(1, businessObjectDao.countMatching(NotificationProducer.class, fieldValues));
107 
108 	fieldValues = new HashMap();
109 	fieldValues.put("id", TestConstants.PRODUCER_2.getId());
110 	assertEquals(1, businessObjectDao.countMatching(NotificationProducer.class, fieldValues));
111 
112 	fieldValues = new HashMap();
113 	fieldValues.put("description", "xxxx");
114 	assertEquals(0, businessObjectDao.countMatching(NotificationProducer.class, fieldValues));
115     }
116 
117     @Test
118     public void testCountMatchingClassMapMap() {
119         // 2 producers with the same contact info
120         // exclude 1 of them
121         // 1 left
122 	Map positiveFieldValues = new HashMap();
123 	positiveFieldValues.put("contactInfo", TestConstants.PRODUCER_1.getContactInfo());
124 	Map negativeFieldValues = new HashMap();
125 	negativeFieldValues.put("id", TestConstants.PRODUCER_2.getId());
126 	assertEquals(1, businessObjectDao.countMatching(NotificationProducer.class, positiveFieldValues, negativeFieldValues));
127 
128         // 1 matching producer, exclude it, 0 left
129 	positiveFieldValues = new HashMap();
130 	positiveFieldValues.put("contactInfo", TestConstants.PRODUCER_3.getContactInfo());
131 	negativeFieldValues = new HashMap();
132 	negativeFieldValues.put("id", TestConstants.PRODUCER_3.getId());
133 	assertEquals(0, businessObjectDao.countMatching(NotificationProducer.class, positiveFieldValues, negativeFieldValues));
134 
135         // 1 matching producer, exclude a non-match, 1 left 
136 	positiveFieldValues = new HashMap();
137 	positiveFieldValues.put("contactInfo", TestConstants.PRODUCER_3.getContactInfo());
138 	negativeFieldValues = new HashMap();
139 	negativeFieldValues.put("id", TestConstants.PRODUCER_2.getId());
140 	assertEquals(1, businessObjectDao.countMatching(NotificationProducer.class, positiveFieldValues, negativeFieldValues));
141     }
142 
143     @Test
144     public void testFindMatchingOrderBy() {
145 	Map fieldValues = new HashMap();
146 	fieldValues.put("contactInfo", TestConstants.CONTACT_INFO_2_PRODUCERS);
147 
148 	// test ascending order
149 	Collection notificationProducers = businessObjectDao.findMatchingOrderBy(NotificationProducer.class, fieldValues, "id", true);
150 	assertEquals(2, notificationProducers.size());
151 	Iterator it = notificationProducers.iterator();
152 	int i=1;
153 	while (it.hasNext()) {
154 	    NotificationProducer producer = (NotificationProducer)it.next();
155             NotificationProducer expected = producerOrder[(i - 1)];
156 	    long id = producer.getId().longValue();
157 	    assertEquals(expected.getId().longValue(), id);
158 	    assertEquals(expected.getName(), producer.getName());
159             assertEquals(expected.getDescription(), producer.getDescription());
160             assertEquals(expected.getContactInfo(), producer.getContactInfo());
161             i++;
162 	}
163 
164 	// test descending order
165 	notificationProducers = businessObjectDao.findMatchingOrderBy(NotificationProducer.class, fieldValues, "id", false);
166 	assertEquals(2, notificationProducers.size());
167 	it = notificationProducers.iterator();
168 	i=2;
169 	while (it.hasNext()) {
170 	    NotificationProducer producer = (NotificationProducer)it.next();
171             NotificationProducer expected = producerOrder[(i - 1)];
172 	    long id = producer.getId().longValue();
173 	    assertEquals(expected.getId().longValue(), id);
174 	    assertEquals(expected.getName(), producer.getName());
175             assertEquals(expected.getDescription(), producer.getDescription());
176             assertEquals(expected.getContactInfo(), producer.getContactInfo());
177             i--;
178 	}
179     }
180 
181     @Test
182     public void testSaveObject() {
183 	NotificationProducer notificationProducer = new NotificationProducer();
184 	notificationProducer.setName("TestNotificationProducer");
185 	notificationProducer.setDescription("Notification Producer for Unit Tests");
186 	notificationProducer.setContactInfo("bh79@cornell.edu");
187 
188 	assertNull(notificationProducer.getId());
189 	businessObjectDao.save(notificationProducer);
190 	assertNotNull(notificationProducer.getId());
191 	
192 	Map primaryKeys = new HashMap();
193 	primaryKeys.put("id", notificationProducer.getId());
194 	NotificationProducer saved = (NotificationProducer)businessObjectDao.findByPrimaryKey(NotificationProducer.class, primaryKeys);
195 	assertEquals(notificationProducer.getId().longValue(), saved.getId().longValue());
196 	assertEquals(notificationProducer.getName(), saved.getName());
197 	assertEquals(notificationProducer.getDescription(), saved.getDescription());
198 	assertEquals(notificationProducer.getContactInfo(), saved.getContactInfo());
199     }
200 
201     @Test
202     public void testSaveList() {
203 	List notificationProducers = new ArrayList(2);
204 	
205 	NotificationProducer notificationProducer = new NotificationProducer();
206 	notificationProducer.setName("TestNotificationProducer");
207 	notificationProducer.setDescription("Notification Producer for Unit Tests");
208 	notificationProducer.setContactInfo("bh79@cornell.edu");
209 
210 	NotificationProducer notificationProducer2 = new NotificationProducer();
211 	notificationProducer2.setName("TestNotificationProducer2");
212 	notificationProducer2.setDescription("Notification Producer for Unit Tests 2");
213 	notificationProducer2.setContactInfo("bh79@cornell.edu");
214 
215 	notificationProducers.add(notificationProducer);
216 	notificationProducers.add(notificationProducer2);
217 	
218 	assertNull(notificationProducer.getId());
219 	assertNull(notificationProducer.getId());
220 	businessObjectDao.save(notificationProducers);
221 	assertNotNull(notificationProducer.getId());
222 	assertNotNull(notificationProducer2.getId());
223 	
224 	Map primaryKeys = new HashMap();
225 	primaryKeys.put("id", notificationProducer.getId());
226 	NotificationProducer saved = (NotificationProducer)businessObjectDao.findByPrimaryKey(NotificationProducer.class, primaryKeys);
227 	assertEquals(notificationProducer.getId().longValue(), saved.getId().longValue());
228 	assertEquals(notificationProducer.getName(), saved.getName());
229 	assertEquals(notificationProducer.getDescription(), saved.getDescription());
230 	assertEquals(notificationProducer.getContactInfo(), saved.getContactInfo());
231 
232 	primaryKeys = new HashMap();
233 	primaryKeys.put("id", notificationProducer2.getId());
234 	saved = (NotificationProducer)businessObjectDao.findByPrimaryKey(NotificationProducer.class, primaryKeys);
235 	assertEquals(notificationProducer2.getId().longValue(), saved.getId().longValue());
236 	assertEquals(notificationProducer2.getName(), saved.getName());
237 	assertEquals(notificationProducer2.getDescription(), saved.getDescription());
238 	assertEquals(notificationProducer2.getContactInfo(), saved.getContactInfo());
239     }
240 
241     @Test
242     public void testSaveListViolateConstraint() {
243         String exceptionDescription = null;
244         try {
245             List notificationProducers = new ArrayList(2);
246 
247             NotificationProducer notificationProducer = new NotificationProducer();
248             notificationProducer.setName("TestNotificationProducer");
249             notificationProducer.setDescription("Notification Producer for Unit Tests");
250             notificationProducer.setContactInfo("bh79@cornell.edu");
251 
252             NotificationProducer notificationProducer2 = new NotificationProducer();
253             notificationProducer2.setName("TestNotificationProducer");
254             notificationProducer2.setDescription("Notification Producer for Unit Tests");
255             notificationProducer2.setContactInfo("bh79@cornell.edu");
256 
257             notificationProducers.add(notificationProducer);
258             notificationProducers.add(notificationProducer2);
259 
260             assertNull(notificationProducer.getId());
261             assertNull(notificationProducer.getId());
262             businessObjectDao.save(notificationProducers);
263             
264             fail("No exception was thrown; expected constraint violation");
265         } catch (DataAccessException e) {
266         }
267 
268     }
269 
270     @Test
271     public void testDeleteObject() {
272 	Map primaryKeys = new HashMap();
273 	primaryKeys.put("id", TestConstants.PRODUCER_2.getId());
274 	NotificationProducer notificationProducer = (NotificationProducer)businessObjectDao.findByPrimaryKey(NotificationProducer.class, primaryKeys);
275 	businessObjectDao.delete(notificationProducer);
276 
277 	NotificationProducer saved = (NotificationProducer)businessObjectDao.findByPrimaryKey(NotificationProducer.class, primaryKeys);
278 	assertNull(saved);
279 
280 	Map fieldValues = new HashMap();
281 	fieldValues.put("contactInfo", TestConstants.CONTACT_INFO_2_PRODUCERS);
282 	assertEquals(1, businessObjectDao.countMatching(NotificationProducer.class, fieldValues));
283     }
284 
285     @Test
286     public void testDeleteListOfObject() {
287 	Map fieldValues = new HashMap();
288 	fieldValues.put("contactInfo", TestConstants.CONTACT_INFO_2_PRODUCERS);
289 	assertEquals(2, businessObjectDao.countMatching(NotificationProducer.class, fieldValues));
290 
291 	Collection notificationProducers = businessObjectDao.findMatching(NotificationProducer.class, fieldValues);
292 	ArrayList listToDelete = new ArrayList(notificationProducers);
293 	businessObjectDao.delete(listToDelete);
294 
295 	assertEquals(0, businessObjectDao.countMatching(NotificationProducer.class, fieldValues));
296     }
297 
298     @Test
299     public void testDeleteMatching() {
300 	Map fieldValues = new HashMap();
301 	fieldValues.put("contactInfo", TestConstants.CONTACT_INFO_2_PRODUCERS);
302 	assertEquals(2, businessObjectDao.countMatching(NotificationProducer.class, fieldValues));
303 	
304 	businessObjectDao.deleteMatching(NotificationProducer.class, fieldValues);
305 
306 	assertEquals(0, businessObjectDao.countMatching(NotificationProducer.class, fieldValues));
307     }
308 
309     @Test
310     public void testRetrieve() {
311 	NotificationProducer template = new NotificationProducer();
312 	template.setId(TestConstants.PRODUCER_2.getId());
313 
314 	NotificationProducer notificationProducer = (NotificationProducer) businessObjectDao.retrieve(template);
315 	assertEquals(TestConstants.PRODUCER_2.getId().longValue(), notificationProducer.getId().longValue());
316 	assertEquals(TestConstants.PRODUCER_2.getName(), notificationProducer.getName());
317 	assertEquals(TestConstants.PRODUCER_2.getDescription(), notificationProducer.getDescription());
318 	assertEquals(TestConstants.PRODUCER_2.getContactInfo(), notificationProducer.getContactInfo());
319     }
320 
321     @Test
322     public void testFindMatchingByExample() {
323 	NotificationProducer template = new NotificationProducer();
324 	template.setName(TestConstants.PRODUCER_1.getName());
325 	
326 	Collection producers = businessObjectDao.findMatchingByExample(template);
327 	
328 	assertEquals(producers.size(), 1);
329     }
330 }