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