RemotingSessionAwareTest : added new class which handles pre-config of test classes...
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / cdm / api / cache / CdmClientCachingTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.api.cache;
11
12 import java.util.Arrays;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import org.apache.log4j.Logger;
20 import org.junit.Assert;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.unitils.dbunit.annotation.DataSet;
24
25 import eu.etaxonomy.cdm.api.service.ICommonService;
26 import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
27 import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
28 import eu.etaxonomy.cdm.api.service.ITaxonService;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.model.common.Language;
31 import eu.etaxonomy.cdm.model.common.LanguageString;
32 import eu.etaxonomy.cdm.model.description.KeyStatement;
33 import eu.etaxonomy.cdm.model.description.PolytomousKey;
34 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
35 import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
36 import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
37
38 /**
39 * @author cmathew
40 * @date 7 Oct 2014
41 *
42 */
43 @DataSet
44 public class CdmClientCachingTest extends RemotingSessionAwareTest {
45
46 private static final Logger logger = Logger.getLogger(CdmClientCachingTest.class);
47
48 private final UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
49 private final UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
50 private final UUID polytomousKeyNodeUuid1 = UUID.fromString("75e4c924-ff58-4ee7-a59d-fd9173517d08");
51 private final UUID polytomousKeyNodeUuid2 = UUID.fromString("b775c027-13c0-4b87-8aa9-712faeaafbdc");
52
53
54 private final IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
55 private final IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
56 private final ICommonService commonService = getRemoteApplicationController().getCommonService();
57 private final ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
58
59
60 private static final List<String> PKEY_DEPTH1_INIT_STRATEGY = Arrays.asList(new String[] {
61 });
62
63 private static final List<String> PKEY_DEPTH2_INIT_STRATEGY = Arrays.asList(new String[] {
64 "root"});
65
66 private static final List<String> PKEY_DEPTH3_INIT_STRATEGY = Arrays.asList(new String[] {
67 "root.statement"});
68
69
70 @BeforeClass
71 public static void initializePolytomousKeyTest() {
72
73 }
74
75
76
77
78 @Test
79 public void recursiveLoadSubGraphDepth1Test() {
80
81 // this call will load into the session cache the graph
82 // polytomous key
83 // |- root : polytomous key node
84 // in a recursive call
85 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
86
87
88 // checking to make sure the root object is in the session cache
89 Assert.assertSame(pkey1.getRoot(), cacher.getFromCache(pkey1.getRoot()));
90
91 }
92
93
94 @Test
95 public void recursiveLoadSubGraphDepth2Test() {
96
97 // this call will load into the session cache the graph
98 // polytomous key
99 // |- root : polytomous key node
100 // |- question : KeyStatement
101 // |- statement : KeyStatement
102 // in a recursive call
103 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
104 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
105 // checking to make sure the root object is in the session cache
106 Assert.assertSame(pkey.getRoot(), cacher.getFromCache(pkey.getRoot()));
107 Assert.assertSame(pkey.getRoot().getStatement(), cacher.getFromCache(pkey.getRoot().getStatement()));
108 Assert.assertSame(pkey.getRoot().getQuestion(), cacher.getFromCache(pkey.getRoot().getQuestion()));
109
110 }
111
112 /**
113 * when : retrieving objects using recursive caching of object graphs with different depths
114 * then : the objects in the sub-graph having the same persistence id should be the same
115 */
116 @Test
117 public void lazyLoadRecursiveTest() {
118
119 // this call will load into the session cache the graph and update the objects in the sub-graph for a
120 // polytomous key
121 // |- root : polytomous key node
122 // |- question : KeyStatement
123 // |- statement : KeyStatement
124 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
125
126 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
127
128 }
129
130
131 /**
132 * when : retrieving objects using recursive caching of object graphs with different depths
133 * then : the objects in the sub-graph having the same persistence id should be the same
134 */
135 @Test
136 public void differentSubGraphDepthTest1() {
137
138 // this call will load into the session cache the graph and update the objects in the sub-graph for a
139 // polytomous key
140 // |- root : polytomous key node
141 // in a recursive call
142 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
143 KeyStatement ks1 = CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class);
144
145 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
146
147 // this call will load into the session cache the graph and update the objects in the sub-graph for a
148 // polytomous key
149 // |- root : polytomous key node
150 // |- question : KeyStatement
151 // |- statement : KeyStatement
152 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH3_INIT_STRATEGY),PolytomousKey.class);
153
154 KeyStatement ks2 = CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class);
155
156 Assert.assertSame(ks2, cacher.getFromCache(ks2));
157 Assert.assertSame(cacher.getFromCache(KeyStatement.class, ks1.getId()), cacher.getFromCache(ks2));
158
159 }
160
161 /**
162 * when : retrieving objects using recursive caching of object graphs with different depths
163 * then : the objects in the sub-graph having the same persistence id should be the same
164 */
165 @Test
166 public void differentSubGraphDepthTest2() {
167
168 // this call will load into the session cache the graph and update the objects in the sub-graph for a
169 // polytomous key
170 // |- root : polytomous key node
171 // |- question : KeyStatement
172 // |- statement : KeyStatement
173 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
174
175 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
176
177 // this call will load into the session cache the graph and update the objects in the sub-graph for a
178 // polytomous key
179 // |- root : polytomous key node
180 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
181
182
183 Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
184 Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
185 Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
186 }
187
188 /**
189 * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
190 * then : the objects in the sub-graph having the same persistence id should be the same
191 */
192 @Test
193 public void recursiveLoadAndLazyLoadTest() {
194
195 // this call will load into the session cache the graph and update the objects in the sub-graph for a
196 // polytomous key
197 // |- root : polytomous key node
198 // |- question : KeyStatement
199 // |- statement : KeyStatement
200 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
201
202 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
203
204 // checking that the root is not null and
205 // that it exists in the cache and
206 // that both the original object and the
207 // cached object are the same
208 Assert.assertNotNull(pkey1.getRoot().getStatement());
209 Assert.assertNotNull(cacher.getFromCache(pkey1.getRoot().getStatement()));
210
211 // this call will load into the session cache the graph and update the objects in the sub-graph for a
212 // polytomous key
213 // |- root : polytomous key node
214 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
215
216
217
218 Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
219 Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
220 Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
221 }
222
223 /**
224 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
225 * then : the objects in the sub-graph having the same persistence id should be the same
226 */
227 @Test
228 public void lazyLoadAndRecursiveLoadTest() {
229
230
231 // this call will load into the session cache the graph and update the objects in the sub-graph for a
232 // polytomous key
233 // |- root : polytomous key node
234
235 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
236
237
238 // lazy initialising root.statement
239 KeyStatement st = CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class);
240 st.getLabel();
241
242 // checking that the root is not null and
243 // that it exists in the cache and
244 // that both the original object and the
245 // cached object are the same
246 Assert.assertNotNull(pkey1.getRoot().getStatement());
247 Assert.assertSame(CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class),
248 cacher.getFromCache(pkey1.getRoot().getStatement(), KeyStatement.class));
249
250 // this call will load into the session cache the graph and update the objects in the sub-graph for a
251 // polytomous key
252 // |- root : polytomous key node
253 // |- question : KeyStatement
254 // |- statement : KeyStatement
255
256 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
257
258
259 Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
260 Assert.assertSame(st, pkey2.getRoot().getStatement());
261 Assert.assertSame(cacher.getFromCache(st), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
262 }
263
264
265
266
267
268 /**
269 * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
270 * then : the object should be the same
271 */
272 @Test
273 public void subGraphObjectLoadTest1() {
274
275 // this call will load into the session cache a polytomous key node object
276 PolytomousKeyNode rootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid1),PolytomousKeyNode.class);
277
278
279 Assert.assertNotNull(rootPKNode);
280 Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
281
282 PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
283
284
285 Assert.assertNotNull(childOfRootPKNode);
286 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
287
288 // this call will load into the session cache the graph and update the objects in the sub-graph for a
289 // polytomous key
290 // |- root : polytomous key node
291 // |- question : KeyStatement
292 // |- statement : KeyStatement
293
294 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
295
296
297 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
298 Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
299 }
300
301 /**
302 * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
303 * then : the object should be the same
304 */
305 @Test
306 public void subGraphObjectLoadTest2() {
307
308 // this call will load into the session cache the graph and update the objects in the sub-graph for a
309 // polytomous key
310 // |- root : polytomous key node
311 // |- question : KeyStatement
312 // |- statement : KeyStatement
313
314 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
315
316
317 // this call will load into the session cache a polytomous key node object
318 PolytomousKeyNode rootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid1),PolytomousKeyNode.class);
319
320
321 Assert.assertNotNull(rootPKNode);
322 Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
323
324 PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
325
326
327 Assert.assertNotNull(childOfRootPKNode);
328 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
329
330 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
331 Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
332 }
333
334
335 /**
336 * when : loading objects from a collection
337 * then : the object stored in the cache should be the same
338 */
339 @Test
340 public void subGraphCollectionLoadTest() {
341
342 // this call will load into the session cache the graph and update the objects in the sub-graph for a
343 // polytomous key
344 // |- root : polytomous key node
345 // |- question : KeyStatement
346 // |- statement : KeyStatement
347
348 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
349
350
351 PolytomousKeyNode childOfRootPKNode = pkey.getRoot().getChildAt(1);
352
353 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
354 Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
355
356 }
357
358 /**
359 * when : loading a non-lazy collection in a subgraph and loading the collection directly
360 * then : the object stored in the cache should be the same as the object in the sub-graph collection and
361 * the object in the directly loaded collection
362 */
363 @Test
364 public void nonLazyCollectionLoadTest() {
365 // need to find an example of this
366 }
367
368 /**
369 * when : loading objects from a map
370 * then : the object stored in the cache should be the same
371 */
372 @Test
373 public void subGraphMapLoadTest() {
374
375 Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
376 Language hindi = Language.getLanguageFromUuid(UUID.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
377
378
379 // this call will load into the session cache the graph and update the objects in the sub-graph for a
380 // polytomous key
381 // |- root : polytomous key node
382 // |- question : KeyStatement
383 // |- statement : KeyStatement
384
385 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH3_INIT_STRATEGY),PolytomousKey.class);
386
387
388 Map<Language, LanguageString> labelMap = pkey.getRoot().getStatement().getLabel();
389 Set<Language> languages = labelMap.keySet();
390 Iterator<Language> languagesItr = languages.iterator();
391 while(languagesItr.hasNext()) {
392 Language lang = languagesItr.next();
393 if(lang.equals(english)) {
394 Assert.assertSame(lang, english);
395 }
396 if(lang.equals(hindi)) {
397 Assert.assertSame(lang, hindi);
398 }
399 Assert.assertSame(lang, cacher.getFromCache(lang));
400 }
401 }
402
403 /**
404 * when : loading a non-lazy map in a subgraph and loading the map directly
405 * then : the object stored in the cache should be the same as the object in the sub-graph map and
406 * the object in the directly loaded map
407 */
408 @Test
409 public void nonLazyMapLoadTest() {
410 // need to find an example of this
411 }
412
413
414
415 }