View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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 	@Test
270 	public void testGetApplicationState_User_Perf1() throws Exception {
271 		int n = 100;
272 		long t1 = System.nanoTime();
273 		List<KeyValuePair> list1 = getKeyValuePairList(0,n);
274 		ApplicationState appState1 = getApplicationState("1", "2", "course1", "lisa", list1);
275 		dao.createApplicationState(appState1);
276 		long t2 = System.nanoTime();
277 		long total = (t2-t1)/1000000;
278 		System.out.println("\ntestGetApplicationState_User_Perf1:createApplicationState(" + n + ") = " + total + " millis");
279 
280 		t1 = System.nanoTime();
281 		ApplicationState appState = dao.getApplicationState("1", "2", "course1", "lisa");
282 		t2 = System.nanoTime();
283 		total = (t2-t1)/1000000;
284 		Assert.assertEquals(n, appState.getKeyValueList().size());
285 		System.out.println("testGetApplicationState_User_Perf1:getApplicationState(" + n + ") = " + total + " millis");
286 	}	
287 
288 	@Test
289 	public void testGetApplicationState_User_Perf2() throws Exception {
290 		int n = 100;
291 		long t1 = System.nanoTime();
292 		for(int i=0; i<n; i++) {
293 			List<KeyValuePair> list = getKeyValuePairList(1,1);
294 			ApplicationState appState = getApplicationState("appId"+i, "refId"+i, "refType"+i, "tom"+i, list);
295 			dao.createApplicationState(appState);
296 		}
297 		long t2 = System.nanoTime();
298 		long total = (t2-t1)/1000000;
299 		System.out.println("\ntestGetApplicationState_User_Perf2:createApplicationState(" + n + ") = " + total + " millis");
300 		
301 		t1 = System.nanoTime();
302 		for(int i=0; i<n; i++) {
303 			ApplicationState appState = dao.getApplicationState("appId"+i, "refId"+i, "refType"+i, "tom"+i);
304 		}
305 		t2 = System.nanoTime();
306 		total = (t2-t1)/1000000;
307 		System.out.println("testGetApplicationState_User_Perf2:getApplicationState(" + n + ") = " + total + " millis");
308 	}	
309 
310 	@Test
311 	public void testGetApplicationState_User_Perf3() throws Exception {
312 		int n = 100;
313 		long t1 = System.nanoTime();
314 		for(int i=0; i<n; i++) {
315 			List<KeyValuePair> list = getKeyValuePairList(0,n/5);
316 			ApplicationState appState = getApplicationState("appId"+i, "refId"+i, "refType"+i, "tom"+i, list);
317 			dao.createApplicationState(appState);
318 		}
319 		long t2 = System.nanoTime();
320 		long total = (t2-t1)/1000000;
321 		System.out.println("\ntestGetApplicationState_User_Perf3:createApplicationState(" + n + ") = " + total + " millis");
322 		
323 		t1 = System.nanoTime();
324 		for(int i=0; i<n; i++) {
325 			ApplicationState appState = dao.getApplicationState("appId"+i, "refId"+i, "refType"+i, "tom"+i);
326 		}
327 		t2 = System.nanoTime();
328 		total = (t2-t1)/1000000;
329 		System.out.println("testGetApplicationState_User_Perf3:getApplicationState(" + n + ") = " + total + " millis");
330 	}
331 
332 	@Test
333 	public void testCreateApplicationStateCollection_Perf4() throws Exception {
334 		int n = 100;
335 		List<ApplicationState> newAppStateList = new ArrayList<ApplicationState>();
336 		for(int i=0; i<n; i++) {
337 			List<KeyValuePair> list = getKeyValuePairList(1,n/5);
338 			ApplicationState appState = getApplicationState("appId-1", "refKey"+i, "course"+i, null, list);
339 			newAppStateList.add(appState);
340 		}
341 		long t1 = System.nanoTime();
342 		List<String> idList = dao.createApplicationState(newAppStateList);
343 		long t2 = System.nanoTime();
344 		long total = (t2-t1)/1000000;
345 		System.out.println("\ntestCreateApplicationStateCollection_Perf4:createApplicationState(" + n + ") = " + total + " millis");
346 		
347 		t1 = System.nanoTime();
348 		List<ApplicationState> appStateList = dao.getApplicationState("appId-1");
349 		t2 = System.nanoTime();
350 		total = (t2-t1)/1000000;
351 		System.out.println("testCreateApplicationStateCollection_Perf4:getApplicationState(" + n + ") = " + total + " millis");
352 		
353 		Assert.assertEquals(n, appStateList.size());
354 		Map<String, String> map2 = getMap(appStateList.get(0).getKeyValueList());
355 
356 		t1 = System.nanoTime();
357 		for(String id : idList) {
358 			dao.delete(ApplicationState.class, id);
359 		}
360 		t2 = System.nanoTime();
361 		total = (t2-t1)/1000000;
362 		System.out.println("testCreateApplicationStateCollection_Perf4:delete(" + n + ") = " + total + " millis");
363 	}
364 */
365 }