Merge branch 'release/3.12.0'
[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 // checking to make sure the root object is in the session cache
88 Assert.assertSame(CdmBase.deproxy(pkey1.getRoot(),PolytomousKeyNode.class), cacher.getFromCache(pkey1.getRoot()));
89
90 }
91
92
93 @Test
94 public void recursiveLoadSubGraphDepth2Test() {
95
96 // this call will load into the session cache the graph
97 // polytomous key
98 // |- root : polytomous key node
99 // |- question : KeyStatement
100 // |- statement : KeyStatement
101 // in a recursive call
102 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
103 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
104 // checking to make sure the root object is in the session cache
105 Assert.assertSame(CdmBase.deproxy(pkey.getRoot(),PolytomousKeyNode.class), cacher.getFromCache(pkey.getRoot()));
106 Assert.assertSame(CdmBase.deproxy(pkey.getRoot().getStatement(),KeyStatement.class), cacher.getFromCache(pkey.getRoot().getStatement()));
107 Assert.assertSame(CdmBase.deproxy(pkey.getRoot().getQuestion(),KeyStatement.class), cacher.getFromCache(pkey.getRoot().getQuestion()));
108
109 }
110
111 /**
112 * when : retrieving objects using recursive caching of object graphs with different depths
113 * then : the objects in the sub-graph having the same persistence id should be the same
114 */
115 @Test
116 public void lazyLoadRecursiveTest() {
117
118 // this call will load into the session cache the graph and update the objects in the sub-graph for a
119 // polytomous key
120 // |- root : polytomous key node
121 // |- question : KeyStatement
122 // |- statement : KeyStatement
123 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
124
125 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
126
127 }
128
129
130 /**
131 * when : retrieving objects using recursive caching of object graphs with different depths
132 * then : the objects in the sub-graph having the same persistence id should be the same
133 */
134 @Test
135 public void differentSubGraphDepthTest1() {
136
137 // this call will load into the session cache the graph and update the objects in the sub-graph for a
138 // polytomous key
139 // |- root : polytomous key node
140 // in a recursive call
141 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
142 KeyStatement ks1 = CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class);
143
144 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
145
146 // this call will load into the session cache the graph and update the objects in the sub-graph for a
147 // polytomous key
148 // |- root : polytomous key node
149 // |- question : KeyStatement
150 // |- statement : KeyStatement
151 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH3_INIT_STRATEGY),PolytomousKey.class);
152
153 KeyStatement ks2 = CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class);
154
155 Assert.assertSame(ks2, cacher.getFromCache(ks2));
156 Assert.assertSame(cacher.getFromCache(KeyStatement.class, ks1.getId()), cacher.getFromCache(ks2));
157
158 }
159
160 /**
161 * when : retrieving objects using recursive caching of object graphs with different depths
162 * then : the objects in the sub-graph having the same persistence id should be the same
163 */
164 @Test
165 public void differentSubGraphDepthTest2() {
166
167 // this call will load into the session cache the graph and update the objects in the sub-graph for a
168 // polytomous key
169 // |- root : polytomous key node
170 // |- question : KeyStatement
171 // |- statement : KeyStatement
172 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
173 KeyStatement ks1 = pkey1.getRoot().getStatement();
174 Assert.assertSame(ks1, pkey1.getRoot().getStatement());
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 Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
183 pkey2.getRoot().getStatement().getCreatedBy();
184 Assert.assertSame(cacher.getFromCache(ks1), cacher.getFromCache(pkey2.getRoot().getStatement()));
185 }
186
187 /**
188 * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
189 * then : the objects in the sub-graph having the same persistence id should be the same
190 */
191 @Test
192 public void recursiveLoadAndLazyLoadTest() {
193
194 // this call will load into the session cache the graph and update the objects in the sub-graph for a
195 // polytomous key
196 // |- root : polytomous key node
197 // |- question : KeyStatement
198 // |- statement : KeyStatement
199 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
200
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(CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class));
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 KeyStatement ks2 = CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class);
217 Assert.assertSame(ks2, cacher.getFromCache(pkey2.getRoot().getStatement()));
218
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(CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
260 Assert.assertSame(st, CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class));
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 }