1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.server.applicationstate.dao.impl;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.kuali.student.common.exceptions.AlreadyExistsException;
26 import org.kuali.student.common.exceptions.DoesNotExistException;
27 import org.kuali.student.common.test.spring.AbstractTransactionalDaoTest;
28 import org.kuali.student.common.test.spring.Dao;
29 import org.kuali.student.common.test.spring.PersistenceFileLocation;
30 import org.kuali.student.common.ui.server.applicationstate.dao.ApplicationStateDao;
31 import org.kuali.student.common.ui.server.applicationstate.entity.ApplicationState;
32 import org.kuali.student.common.ui.server.applicationstate.entity.KeyValuePair;
33
34 @PersistenceFileLocation("classpath:META-INF/application-state-persistence.xml")
35 public class ApplicationStateDaoImplTest extends AbstractTransactionalDaoTest {
36
37 @Dao(value = "org.kuali.student.common.ui.server.applicationstate.dao.impl.ApplicationStateDaoImpl")
38 public ApplicationStateDao dao;
39
40 private Map<String, String> getMap(List<KeyValuePair> list) {
41 Map<String, String> map = new HashMap<String, String>();
42 for(KeyValuePair pair : list) {
43 map.put(pair.getKey(), pair.getValue());
44 }
45 return map;
46 }
47
48 private List<KeyValuePair> getKeyValuePairList(int startIndex, int n) {
49 List<KeyValuePair> list = new ArrayList<KeyValuePair>();
50 for(int i=startIndex; i<(startIndex+n); i++) {
51 list.add(new KeyValuePair("key-"+i, "value-"+i));
52 }
53 return list;
54 }
55
56 private ApplicationState getApplicationState(String appId, String refKey, String refType, String userId, List<KeyValuePair> list) {
57 ApplicationState appState = new ApplicationState();
58 appState.setApplicationId(appId);
59 appState.setKeyValueList(list);
60 appState.setReferenceKey(refKey);
61 appState.setReferenceType(refType);
62 appState.setUserId(userId);
63 return appState;
64 }
65
66 @Test
67 public void testUpdateApplicationState1() throws Exception {
68 List<KeyValuePair> list = getKeyValuePairList(1,3);
69 ApplicationState appState = getApplicationState("1", "2", "course", null, list);
70 ApplicationState newAppState = dao.createApplicationState(appState);
71
72 ApplicationState getAppState = dao.getApplicationState("1", "2", "course");
73
74 list = new ArrayList<KeyValuePair>();
75 list.add(new KeyValuePair("k1", "v1"));
76 list.add(new KeyValuePair("k2", "v2"));
77 getAppState.setKeyValueList(list);
78
79 dao.update(getAppState);
80
81 getAppState = dao.getApplicationState("1", "2", "course");
82 Assert.assertNotNull(getAppState);
83 Assert.assertEquals(newAppState.toString(), getAppState.toString());
84 Assert.assertEquals(newAppState.getKeyValueList().toString(), getAppState.getKeyValueList().toString());
85
86 ApplicationState getAppState2 = dao.fetch(ApplicationState.class, newAppState.getId());
87 Assert.assertEquals(getAppState.toString(), getAppState2.toString());
88 Assert.assertEquals(getAppState.getKeyValueList().toString(), getAppState2.getKeyValueList().toString());
89
90 dao.delete(ApplicationState.class, newAppState.getId());
91 }
92
93 @Test
94 public void testUpdateApplicationState2() throws Exception {
95 List<KeyValuePair> list = getKeyValuePairList(1,3);
96 ApplicationState appState = getApplicationState("1", "2", "course", null, list);
97 ApplicationState newAppState = dao.createApplicationState(appState);
98
99 ApplicationState getAppState = dao.getApplicationState("1", "2", "course");
100
101 getAppState.setApplicationId("A");
102 getAppState.setReferenceKey("B");
103 getAppState.setReferenceType("program");
104 list = new ArrayList<KeyValuePair>();
105 list.add(new KeyValuePair("k1", "v1"));
106 list.add(new KeyValuePair("k2", "v2"));
107 getAppState.setKeyValueList(list);
108
109 dao.update(getAppState);
110
111 getAppState = dao.getApplicationState("A", "B", "program");
112
113 Assert.assertNotNull(getAppState);
114 Assert.assertEquals(newAppState.toString(), getAppState.toString());
115 Assert.assertEquals(newAppState.getKeyValueList().toString(), getAppState.getKeyValueList().toString());
116
117 ApplicationState getAppState2 = dao.fetch(ApplicationState.class, newAppState.getId());
118 Assert.assertEquals(getAppState.toString(), getAppState2.toString());
119 Assert.assertEquals(getAppState.getKeyValueList().toString(), getAppState2.getKeyValueList().toString());
120
121 dao.delete(ApplicationState.class, newAppState.getId());
122 }
123
124 @Test
125 public void testCreateGetApplicationState1() throws Exception {
126 List<KeyValuePair> list = getKeyValuePairList(1,3);
127 ApplicationState appState = getApplicationState("1", "2", "course", null, list);
128 ApplicationState newAppState = dao.createApplicationState(appState);
129
130 ApplicationState getAppState = dao.getApplicationState("1", "2", "course");
131
132 Assert.assertNotNull(getAppState);
133 Map<String, String> map2 = getMap(getAppState.getKeyValueList());
134
135 Assert.assertEquals(3, map2.size());
136 Assert.assertEquals("value-1", map2.get("key-1"));
137 Assert.assertEquals("value-2", map2.get("key-2"));
138 Assert.assertEquals("value-3", map2.get("key-3"));
139
140 dao.delete(ApplicationState.class, newAppState.getId());
141 }
142
143 @Test
144 public void testGetApplicationState_NoApplicationState() throws Exception {
145 try {
146 dao.getApplicationState("1", "2", "course");
147 Assert.fail("Should have thrown an exception for no application state");
148 } catch(DoesNotExistException e) {
149 Assert.assertNotNull(e);
150 }
151 }
152
153 @Test
154 public void testGetApplicationState_NoApplicationStateforUserId() throws Exception {
155 try {
156 dao.getApplicationState("1", "2", "course", "wil");
157 Assert.fail("Should have thrown an exception for no application state");
158 } catch(DoesNotExistException e) {
159 Assert.assertNotNull(e);
160 }
161 }
162
163 @Test
164 public void testCreateApplicationState_NullParameters() throws Exception {
165 try {
166 ApplicationState appState = getApplicationState(null, null, null, null, null);
167 dao.createApplicationState(appState);
168 Assert.fail("Should have thrown a persistence exception for null parameters");
169 } catch(javax.persistence.PersistenceException e) {
170 Assert.assertNotNull(e);
171 }
172 }
173
174 @Test
175 public void testCreateApplicationState1_Error_DuplicateKey() throws Exception {
176 List<KeyValuePair> list = new ArrayList<KeyValuePair>();
177 list.add(new KeyValuePair("key-1", "value-1"));
178 ApplicationState appState = getApplicationState("1", "2", "course", null, list);
179
180 ApplicationState newAppState1 = null;
181
182 try {
183 newAppState1 = dao.createApplicationState(appState);
184 dao.createApplicationState(appState);
185 dao.getApplicationState("1", "2", "course");
186 Assert.fail("Should have thrown a duplicate key persistence exception");
187 } catch(AlreadyExistsException e ) {
188 Assert.assertTrue(true);
189 Assert.assertTrue(e.getMessage() != null);
190 } finally {
191 dao.delete(ApplicationState.class, newAppState1.getId());
192 }
193 }
194
195 @Test
196 public void testCreateApplicationState1_Error_DuplicateKeyUserId() throws Exception {
197 ApplicationState newAppState = null;
198 List<KeyValuePair> list = getKeyValuePairList(1,3);
199 ApplicationState appState = getApplicationState("1", "2", "course", "tom", list);
200
201 try {
202 newAppState = dao.createApplicationState(appState);
203 dao.createApplicationState(appState);
204 dao.getApplicationState("1", "2", "course");
205 Assert.fail("Should have thrown a duplicate key persistence exception");
206 } catch(AlreadyExistsException e ) {
207 Assert.assertTrue(true);
208 Assert.assertTrue(e.getMessage() != null);
209 } finally {
210 dao.delete(newAppState);
211 }
212 }
213
214 @Test
215 public void testCreateGetApplicationState3() throws Exception {
216 List<KeyValuePair> list1 = getKeyValuePairList(1,2);
217 ApplicationState appState1 = getApplicationState("1", "2", "course1", null, list1);
218 ApplicationState newAppState1 = dao.createApplicationState(appState1);
219
220 List<KeyValuePair> list2 = getKeyValuePairList(3,2);
221 ApplicationState appState2 = getApplicationState("11", "22", "course2", null, list2);
222 ApplicationState newAppState2 = dao.createApplicationState(appState2);
223
224 ApplicationState getAppState1 = dao.getApplicationState("1", "2", "course1");
225 Assert.assertNotNull(getAppState1);
226 Map<String, String> returnedMap1 = getMap(getAppState1.getKeyValueList());
227 Assert.assertEquals(2, returnedMap1.size());
228 Assert.assertEquals("value-1", returnedMap1.get("key-1"));
229 Assert.assertEquals("value-2", returnedMap1.get("key-2"));
230
231 ApplicationState getAppState2 = dao.getApplicationState("11", "22", "course2");
232 Assert.assertNotNull(getAppState2);
233 Map<String, String> returnedMap2 = getMap(getAppState2.getKeyValueList());
234 Assert.assertEquals(2, returnedMap2.size());
235 Assert.assertEquals("value-3", returnedMap2.get("key-3"));
236 Assert.assertEquals("value-4", returnedMap2.get("key-4"));
237
238 dao.delete(newAppState1);
239 dao.delete(newAppState2);
240 }
241
242 @Test
243 public void testGetApplicationStateByAppIdRefIdRefTypeUserId() throws Exception {
244 List<KeyValuePair> list1 = getKeyValuePairList(1,2);
245 ApplicationState appState1 = getApplicationState("1", "2", "course1", "tom", list1);
246 ApplicationState newAppState1 = dao.createApplicationState(appState1);
247
248 List<KeyValuePair> list2 = getKeyValuePairList(3,2);
249 ApplicationState appState2 = getApplicationState("11", "22", "course2", "jim", list2);
250 ApplicationState newAppState2 = dao.createApplicationState(appState2);
251
252 ApplicationState as1 = dao.getApplicationState("1", "2", "course1", "tom");
253 Map<String, String> returnedMap1 = getMap(as1.getKeyValueList());
254 Assert.assertEquals(2, returnedMap1.size());
255 Assert.assertEquals("value-1", returnedMap1.get("key-1"));
256 Assert.assertEquals("value-2", returnedMap1.get("key-2"));
257
258 ApplicationState as2 = dao.getApplicationState("11", "22", "course2", "jim");
259 Map<String, String> returnedMap2 = getMap(as2.getKeyValueList());
260 Assert.assertEquals(2, returnedMap2.size());
261 Assert.assertEquals("value-3", returnedMap2.get("key-3"));
262 Assert.assertEquals("value-4", returnedMap2.get("key-4"));
263
264 dao.delete(newAppState1);
265 dao.delete(newAppState2);
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365 }