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