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