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