remove Ignore on saveNewTerm test
[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 KeyStatement ks1 = pkey1.getRoot().getStatement();
175 Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), pkey1.getRoot().getStatement());
176 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
177
178 // this call will load into the session cache the graph and update the objects in the sub-graph for a
179 // polytomous key
180 // |- root : polytomous key node
181 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
182
183 Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
184 pkey2.getRoot().getStatement().getCreatedBy();
185 Assert.assertSame(cacher.getFromCache(ks1), 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
203 CdmTransientEntityCacher cacher = getCacher(sessionOwner);
204
205 // checking that the root is not null and
206 // that it exists in the cache and
207 // that both the original object and the
208 // cached object are the same
209 Assert.assertNotNull(pkey1.getRoot().getStatement());
210 Assert.assertNotNull(cacher.getFromCache(pkey1.getRoot().getStatement()));
211
212 // this call will load into the session cache the graph and update the objects in the sub-graph for a
213 // polytomous key
214 // |- root : polytomous key node
215 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
216
217 KeyStatement ks2 = CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class);
218 Assert.assertSame(ks2, cacher.getFromCache(pkey2.getRoot().getStatement()));
219
220 Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
221 Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
222 }
223
224 /**
225 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
226 * then : the objects in the sub-graph having the same persistence id should be the same
227 */
228 @Test
229 public void lazyLoadAndRecursiveLoadTest() {
230
231
232 // this call will load into the session cache the graph and update the objects in the sub-graph for a
233 // polytomous key
234 // |- root : polytomous key node
235
236 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
237
238
239 // lazy initialising root.statement
240 KeyStatement st = CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class);
241 st.getLabel();
242
243 // checking that the root is not null and
244 // that it exists in the cache and
245 // that both the original object and the
246 // cached object are the same
247 Assert.assertNotNull(pkey1.getRoot().getStatement());
248 Assert.assertSame(CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class),
249 cacher.getFromCache(pkey1.getRoot().getStatement(), KeyStatement.class));
250
251 // this call will load into the session cache the graph and update the objects in the sub-graph for a
252 // polytomous key
253 // |- root : polytomous key node
254 // |- question : KeyStatement
255 // |- statement : KeyStatement
256
257 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
258
259
260 Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
261 Assert.assertSame(st, pkey2.getRoot().getStatement());
262 Assert.assertSame(cacher.getFromCache(st), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
263 }
264
265
266
267
268
269 /**
270 * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
271 * then : the object should be the same
272 */
273 @Test
274 public void subGraphObjectLoadTest1() {
275
276 // this call will load into the session cache a polytomous key node object
277 PolytomousKeyNode rootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid1),PolytomousKeyNode.class);
278
279
280 Assert.assertNotNull(rootPKNode);
281 Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
282
283 PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
284
285
286 Assert.assertNotNull(childOfRootPKNode);
287 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
288
289 // this call will load into the session cache the graph and update the objects in the sub-graph for a
290 // polytomous key
291 // |- root : polytomous key node
292 // |- question : KeyStatement
293 // |- statement : KeyStatement
294
295 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
296
297
298 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
299 Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
300 }
301
302 /**
303 * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
304 * then : the object should be the same
305 */
306 @Test
307 public void subGraphObjectLoadTest2() {
308
309 // this call will load into the session cache the graph and update the objects in the sub-graph for a
310 // polytomous key
311 // |- root : polytomous key node
312 // |- question : KeyStatement
313 // |- statement : KeyStatement
314
315 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
316
317
318 // this call will load into the session cache a polytomous key node object
319 PolytomousKeyNode rootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid1),PolytomousKeyNode.class);
320
321
322 Assert.assertNotNull(rootPKNode);
323 Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
324
325 PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
326
327
328 Assert.assertNotNull(childOfRootPKNode);
329 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
330
331 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
332 Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
333 }
334
335
336 /**
337 * when : loading objects from a collection
338 * then : the object stored in the cache should be the same
339 */
340 @Test
341 public void subGraphCollectionLoadTest() {
342
343 // this call will load into the session cache the graph and update the objects in the sub-graph for a
344 // polytomous key
345 // |- root : polytomous key node
346 // |- question : KeyStatement
347 // |- statement : KeyStatement
348
349 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
350
351
352 PolytomousKeyNode childOfRootPKNode = pkey.getRoot().getChildAt(1);
353
354 Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
355 Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
356
357 }
358
359 /**
360 * when : loading a non-lazy collection in a subgraph and loading the collection directly
361 * then : the object stored in the cache should be the same as the object in the sub-graph collection and
362 * the object in the directly loaded collection
363 */
364 @Test
365 public void nonLazyCollectionLoadTest() {
366 // need to find an example of this
367 }
368
369 /**
370 * when : loading objects from a map
371 * then : the object stored in the cache should be the same
372 */
373 @Test
374 public void subGraphMapLoadTest() {
375
376 Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
377 Language hindi = Language.getLanguageFromUuid(UUID.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
378
379
380 // this call will load into the session cache the graph and update the objects in the sub-graph for a
381 // polytomous key
382 // |- root : polytomous key node
383 // |- question : KeyStatement
384 // |- statement : KeyStatement
385
386 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH3_INIT_STRATEGY),PolytomousKey.class);
387
388
389 Map<Language, LanguageString> labelMap = pkey.getRoot().getStatement().getLabel();
390 Set<Language> languages = labelMap.keySet();
391 Iterator<Language> languagesItr = languages.iterator();
392 while(languagesItr.hasNext()) {
393 Language lang = languagesItr.next();
394 if(lang.equals(english)) {
395 Assert.assertSame(lang, english);
396 }
397 if(lang.equals(hindi)) {
398 Assert.assertSame(lang, hindi);
399 }
400 Assert.assertSame(lang, cacher.getFromCache(lang));
401 }
402 }
403
404 /**
405 * when : loading a non-lazy map in a subgraph and loading the map directly
406 * then : the object stored in the cache should be the same as the object in the sub-graph map and
407 * the object in the directly loaded map
408 */
409 @Test
410 public void nonLazyMapLoadTest() {
411 // need to find an example of this
412 }
413
414
415
416 }