3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.cache
;
12 import java
.util
.Arrays
;
13 import java
.util
.Iterator
;
14 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.junit
.Assert
;
21 import org
.junit
.Before
;
22 import org
.junit
.BeforeClass
;
23 import org
.junit
.Test
;
24 import org
.unitils
.dbunit
.annotation
.DataSet
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyNodeService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
32 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
33 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
34 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
35 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
36 import eu
.etaxonomy
.taxeditor
.httpinvoker
.BaseRemotingTest
;
37 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
38 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
39 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
40 import eu
.etaxonomy
.taxeditor
.session
.MockSessionOwner
;
48 public class CdmClientCachingTest
extends BaseRemotingTest
{
50 private static final Logger logger
= Logger
.getLogger(CdmClientCachingTest
.class);
52 private final UUID polytomousKeyUuid
= UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
53 private final UUID taxon1Uuid
= UUID
.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
54 private final UUID polytomousKeyNodeUuid1
= UUID
.fromString("75e4c924-ff58-4ee7-a59d-fd9173517d08");
55 private final UUID polytomousKeyNodeUuid2
= UUID
.fromString("b775c027-13c0-4b87-8aa9-712faeaafbdc");
58 private final IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
59 private final IPolytomousKeyNodeService polytomousKeyNodeService
= getRemoteApplicationController().getPolytomousKeyNodeService();
60 private final ICommonService commonService
= getRemoteApplicationController().getCommonService();
61 private final ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
64 private CdmTransientEntityCacher cacher
;
65 private ICdmEntitySession cdmEntitySession
;
67 private static final List
<String
> PKEY_DEPTH1_INIT_STRATEGY
= Arrays
.asList(new String
[] {
70 private static final List
<String
> PKEY_DEPTH2_INIT_STRATEGY
= Arrays
.asList(new String
[] {
73 private static final List
<String
> PKEY_DEPTH3_INIT_STRATEGY
= Arrays
.asList(new String
[] {
76 private ICdmEntitySessionEnabled sessionOwner
;
79 public static void initializePolytomousKeyTest() {
84 public void initializeSession() {
85 sessionOwner
= new MockSessionOwner();
86 cdmEntitySession
= cdmEntitySessionManager
.newSession(sessionOwner
, true);
87 cacher
= getCacher(sessionOwner
);
93 public void recursiveLoadSubGraphDepth1Test() {
95 // this call will load into the session cache the graph
97 // |- root : polytomous key node
98 // in a recursive call
99 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
102 // checking to make sure the root object is in the session cache
103 Assert
.assertSame(pkey1
.getRoot(), cacher
.getFromCache(pkey1
.getRoot()));
109 public void recursiveLoadSubGraphDepth2Test() {
111 // this call will load into the session cache the graph
113 // |- root : polytomous key node
114 // |- question : KeyStatement
115 // |- statement : KeyStatement
116 // in a recursive call
117 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
118 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
119 // checking to make sure the root object is in the session cache
120 Assert
.assertSame(pkey
.getRoot(), cacher
.getFromCache(pkey
.getRoot()));
121 Assert
.assertSame(pkey
.getRoot().getStatement(), cacher
.getFromCache(pkey
.getRoot().getStatement()));
122 Assert
.assertSame(pkey
.getRoot().getQuestion(), cacher
.getFromCache(pkey
.getRoot().getQuestion()));
127 * when : retrieving objects using recursive caching of object graphs with different depths
128 * then : the objects in the sub-graph having the same persistence id should be the same
131 public void lazyLoadRecursiveTest() {
133 // this call will load into the session cache the graph and update the objects in the sub-graph for a
135 // |- root : polytomous key node
136 // |- question : KeyStatement
137 // |- statement : KeyStatement
138 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
140 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
146 * when : retrieving objects using recursive caching of object graphs with different depths
147 * then : the objects in the sub-graph having the same persistence id should be the same
150 public void differentSubGraphDepthTest1() {
152 // this call will load into the session cache the graph and update the objects in the sub-graph for a
154 // |- root : polytomous key node
155 // in a recursive call
156 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
158 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
160 // this call will load into the session cache the graph and update the objects in the sub-graph for a
162 // |- root : polytomous key node
163 // |- question : KeyStatement
164 // |- statement : KeyStatement
165 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
168 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
169 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
170 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
175 * when : retrieving objects using recursive caching of object graphs with different depths
176 * then : the objects in the sub-graph having the same persistence id should be the same
179 public void differentSubGraphDepthTest2() {
181 // this call will load into the session cache the graph and update the objects in the sub-graph for a
183 // |- root : polytomous key node
184 // |- question : KeyStatement
185 // |- statement : KeyStatement
186 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
188 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
190 // this call will load into the session cache the graph and update the objects in the sub-graph for a
192 // |- root : polytomous key node
193 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
196 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
197 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
198 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
202 * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
203 * then : the objects in the sub-graph having the same persistence id should be the same
206 public void recursiveLoadAndLazyLoadTest() {
208 // this call will load into the session cache the graph and update the objects in the sub-graph for a
210 // |- root : polytomous key node
211 // |- question : KeyStatement
212 // |- statement : KeyStatement
213 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
215 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
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
.assertNotNull(cacher
.getFromCache(pkey1
.getRoot().getStatement()));
224 // this call will load into the session cache the graph and update the objects in the sub-graph for a
226 // |- root : polytomous key node
227 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
231 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
232 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
233 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
237 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
238 * then : the objects in the sub-graph having the same persistence id should be the same
241 public void lazyLoadAndRecursiveLoadTest() {
244 // this call will load into the session cache the graph and update the objects in the sub-graph for a
246 // |- root : polytomous key node
248 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
251 // lazy initialising root.statement
252 KeyStatement st
= CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class);
255 // checking that the root is not null and
256 // that it exists in the cache and
257 // that both the original object and the
258 // cached object are the same
259 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
260 Assert
.assertSame(CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class),
261 cacher
.getFromCache(pkey1
.getRoot().getStatement(), KeyStatement
.class));
263 // this call will load into the session cache the graph and update the objects in the sub-graph for a
265 // |- root : polytomous key node
266 // |- question : KeyStatement
267 // |- statement : KeyStatement
269 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
272 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
273 Assert
.assertSame(st
, pkey2
.getRoot().getStatement());
274 Assert
.assertSame(cacher
.getFromCache(st
), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
282 * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
283 * then : the object should be the same
286 public void subGraphObjectLoadTest1() {
288 // this call will load into the session cache a polytomous key node object
289 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
292 Assert
.assertNotNull(rootPKNode
);
293 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
295 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
298 Assert
.assertNotNull(childOfRootPKNode
);
299 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
301 // this call will load into the session cache the graph and update the objects in the sub-graph for a
303 // |- root : polytomous key node
304 // |- question : KeyStatement
305 // |- statement : KeyStatement
307 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
310 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
311 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
315 * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
316 * then : the object should be the same
319 public void subGraphObjectLoadTest2() {
321 // this call will load into the session cache the graph and update the objects in the sub-graph for a
323 // |- root : polytomous key node
324 // |- question : KeyStatement
325 // |- statement : KeyStatement
327 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
330 // this call will load into the session cache a polytomous key node object
331 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
334 Assert
.assertNotNull(rootPKNode
);
335 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
337 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
340 Assert
.assertNotNull(childOfRootPKNode
);
341 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
343 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
344 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
349 * when : loading objects from a collection
350 * then : the object stored in the cache should be the same
353 public void subGraphCollectionLoadTest() {
355 // this call will load into the session cache the graph and update the objects in the sub-graph for a
357 // |- root : polytomous key node
358 // |- question : KeyStatement
359 // |- statement : KeyStatement
361 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
364 PolytomousKeyNode childOfRootPKNode
= pkey
.getRoot().getChildAt(1);
366 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
367 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
372 * when : loading a non-lazy collection in a subgraph and loading the collection directly
373 * then : the object stored in the cache should be the same as the object in the sub-graph collection and
374 * the object in the directly loaded collection
377 public void nonLazyCollectionLoadTest() {
378 // need to find an example of this
382 * when : loading objects from a map
383 * then : the object stored in the cache should be the same
386 public void subGraphMapLoadTest() {
388 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
389 Language hindi
= Language
.getLanguageFromUuid(UUID
.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
392 // this call will load into the session cache the graph and update the objects in the sub-graph for a
394 // |- root : polytomous key node
395 // |- question : KeyStatement
396 // |- statement : KeyStatement
398 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
),PolytomousKey
.class);
401 Map
<Language
, LanguageString
> labelMap
= pkey
.getRoot().getStatement().getLabel();
402 Set
<Language
> languages
= labelMap
.keySet();
403 Iterator
<Language
> languagesItr
= languages
.iterator();
404 while(languagesItr
.hasNext()) {
405 Language lang
= languagesItr
.next();
406 if(lang
.equals(english
)) {
407 Assert
.assertSame(lang
, english
);
409 if(lang
.equals(hindi
)) {
410 Assert
.assertSame(lang
, hindi
);
412 Assert
.assertSame(lang
, cacher
.getFromCache(lang
));
417 * when : loading a non-lazy map in a subgraph and loading the map directly
418 * then : the object stored in the cache should be the same as the object in the sub-graph map and
419 * the object in the directly loaded map
422 public void nonLazyMapLoadTest() {
423 // need to find an example of this