2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.cache
;
11 import java
.util
.Arrays
;
12 import java
.util
.Iterator
;
13 import java
.util
.List
;
16 import java
.util
.UUID
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.junit
.Assert
;
20 import org
.junit
.Test
;
21 import org
.unitils
.dbunit
.annotation
.DataSet
;
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
;
39 public class CdmClientCachingTest
extends RemotingSessionAwareTest
{
41 @SuppressWarnings("unused")
42 private static final Logger logger
= Logger
.getLogger(CdmClientCachingTest
.class);
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");
49 private final IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
50 private final IPolytomousKeyNodeService polytomousKeyNodeService
= getRemoteApplicationController().getPolytomousKeyNodeService();
52 private static final List
<String
> PKEY_DEPTH1_INIT_STRATEGY
= Arrays
.asList(new String
[] {
55 private static final List
<String
> PKEY_DEPTH2_INIT_STRATEGY
= Arrays
.asList(new String
[] {
58 private static final List
<String
> PKEY_DEPTH3_INIT_STRATEGY
= Arrays
.asList(new String
[] {
62 public void recursiveLoadSubGraphDepth1Test() {
64 // this call will load into the session cache the graph
66 // |- root : polytomous key node
67 // in a recursive call
68 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
));
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()));
75 public void recursiveLoadSubGraphDepth2Test() {
77 // this call will load into the session cache the graph
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()));
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
96 public void lazyLoadRecursiveTest() {
98 // this call will load into the session cache the graph and update the objects in the sub-graph for a
100 // |- root : polytomous key node
101 // |- question : KeyStatement
102 // |- statement : KeyStatement
103 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
));
105 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
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
113 public void differentSubGraphDepthTest1() {
115 // this call will load into the session cache the graph and update the objects in the sub-graph for a
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());
122 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
124 // this call will load into the session cache the graph and update the objects in the sub-graph for a
126 // |- root : polytomous key node
127 // |- question : KeyStatement
128 // |- statement : KeyStatement
129 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
));
131 KeyStatement ks2
= CdmBase
.deproxy(pkey2
.getRoot().getStatement());
133 Assert
.assertSame(ks2
, cacher
.getFromCache(ks2
));
134 Assert
.assertSame(cacher
.getFromCache(ks1
), cacher
.getFromCache(ks2
));
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
142 public void differentSubGraphDepthTest2() {
144 // this call will load into the session cache the graph and update the objects in the sub-graph for a
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
);
154 // this call will load into the session cache the graph and update the objects in the sub-graph for a
156 // |- root : polytomous key node
157 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
));
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()));
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
169 public void recursiveLoadAndLazyLoadTest() {
171 // this call will load into the session cache the graph and update the objects in the sub-graph for a
173 // |- root : polytomous key node
174 // |- question : KeyStatement
175 // |- statement : KeyStatement
176 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
));
179 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
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()));
188 // this call will load into the session cache the graph and update the objects in the sub-graph for a
190 // |- root : polytomous key node
191 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
));
193 KeyStatement ks2
= CdmBase
.deproxy(pkey2
.getRoot().getStatement());
194 Assert
.assertSame(ks2
, cacher
.getFromCache(pkey2
.getRoot().getStatement()));
196 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
197 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
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
205 public void lazyLoadAndRecursiveLoadTest() {
207 // this call will load into the session cache the graph and update the objects in the sub-graph for a
209 // |- root : polytomous key node
211 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
));
213 // lazy initialising root.statement
214 KeyStatement st
= CdmBase
.deproxy(pkey1
.getRoot().getStatement());
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()));
225 // this call will load into the session cache the graph and update the objects in the sub-graph for a
227 // |- root : polytomous key node
228 // |- question : KeyStatement
229 // |- statement : KeyStatement
231 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
));
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()));
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
244 public void subGraphObjectLoadTest1() {
246 // this call will load into the session cache a polytomous key node object
247 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
));
249 Assert
.assertNotNull(rootPKNode
);
250 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
252 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
));
254 Assert
.assertNotNull(childOfRootPKNode
);
255 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
257 // this call will load into the session cache the graph and update the objects in the sub-graph for a
259 // |- root : polytomous key node
260 // |- question : KeyStatement
261 // |- statement : KeyStatement
263 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
));
265 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
266 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
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
274 public void subGraphObjectLoadTest2() {
276 // this call will load into the session cache the graph and update the objects in the sub-graph for a
278 // |- root : polytomous key node
279 // |- question : KeyStatement
280 // |- statement : KeyStatement
282 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
));
284 // this call will load into the session cache a polytomous key node object
285 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
));
287 Assert
.assertNotNull(rootPKNode
);
288 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
290 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
));
292 Assert
.assertNotNull(childOfRootPKNode
);
293 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
295 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
296 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
301 * when : loading objects from a collection
302 * then : the object stored in the cache should be the same
305 public void subGraphCollectionLoadTest() {
307 // this call will load into the session cache the graph and update the objects in the sub-graph for a
309 // |- root : polytomous key node
310 // |- question : KeyStatement
311 // |- statement : KeyStatement
313 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
));
316 PolytomousKeyNode childOfRootPKNode
= pkey
.getRoot().getChildAt(1);
318 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
319 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
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
328 public void nonLazyCollectionLoadTest() {
329 // need to find an example of this
333 * when : loading objects from a map
334 * then : the object stored in the cache should be the same
337 public void subGraphMapLoadTest() {
339 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
340 Language hindi
= Language
.getLanguageFromUuid(UUID
.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
343 // this call will load into the session cache the graph and update the objects in the sub-graph for a
345 // |- root : polytomous key node
346 // |- question : KeyStatement
347 // |- statement : KeyStatement
349 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
));
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
);
360 if(lang
.equals(hindi
)) {
361 Assert
.assertSame(lang
, hindi
);
363 Assert
.assertSame(lang
, cacher
.getFromCache(lang
));
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
373 public void nonLazyMapLoadTest() {
374 // need to find an example of this