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
.Level
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.junit
.Assert
;
22 import org
.junit
.Before
;
23 import org
.junit
.BeforeClass
;
24 import org
.junit
.Test
;
25 import org
.unitils
.dbunit
.annotation
.DataSet
;
27 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyNodeService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
33 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
34 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
35 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
36 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
37 import eu
.etaxonomy
.taxeditor
.httpinvoker
.BaseRemotingTest
;
38 import eu
.etaxonomy
.taxeditor
.httpinvoker
.CDMServer
;
39 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
40 import eu
.etaxonomy
.taxeditor
.session
.ISessionEventListener
;
41 import eu
.etaxonomy
.taxeditor
.session
.MockSessionOwner
;
49 public class CdmClientCachingTest
extends BaseRemotingTest
{
51 private static final Logger logger
= Logger
.getLogger(CdmClientCachingTest
.class);
53 private UUID polytomousKeyUuid
= UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
54 private UUID taxon1Uuid
= UUID
.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
55 private UUID polytomousKeyNodeUuid1
= UUID
.fromString("75e4c924-ff58-4ee7-a59d-fd9173517d08");
56 private UUID polytomousKeyNodeUuid2
= UUID
.fromString("b775c027-13c0-4b87-8aa9-712faeaafbdc");
59 private IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
60 private IPolytomousKeyNodeService polytomousKeyNodeService
= getRemoteApplicationController().getPolytomousKeyNodeService();
61 private ICommonService commonService
= getRemoteApplicationController().getCommonService();
62 private ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
65 private CdmTransientEntityCacher cacher
;
66 private ISessionEventListener sessionOwner
;
68 private static final List
<String
> PKEY_DEPTH1_INIT_STRATEGY
= Arrays
.asList(new String
[] {
71 private static final List
<String
> PKEY_DEPTH2_INIT_STRATEGY
= Arrays
.asList(new String
[] {
74 private static final List
<String
> PKEY_DEPTH3_INIT_STRATEGY
= Arrays
.asList(new String
[] {
80 public static void initializePolytomousKeyTest() {
81 logger
.setLevel(Level
.INFO
);
82 CDMServer
.getInstance().setKeepServerRunning(true);
86 public void initializeSession() {
87 sessionOwner
= new MockSessionOwner();
88 cdmEntitySessionManager
.bind(sessionOwner
);
89 cacher
= getCacher(sessionOwner
);
95 public void recursiveLoadSubGraphDepth1Test() {
97 // this call will load into the session cache the graph
99 // |- root : polytomous key node
100 // in a recursive call
101 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
102 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
104 // checking to make sure the root object is in the session cache
105 Assert
.assertSame(pkey1
.getRoot(), cacher
.getFromCache(pkey1
.getRoot()));
111 public void recursiveLoadSubGraphDepth2Test() {
113 // this call will load into the session cache the graph
115 // |- root : polytomous key node
116 // |- question : KeyStatement
117 // |- statement : KeyStatement
118 // in a recursive call
119 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
120 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
121 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
122 // checking to make sure the root object is in the session cache
123 Assert
.assertSame(pkey
.getRoot(), cacher
.getFromCache(pkey
.getRoot()));
124 Assert
.assertSame(pkey
.getRoot().getStatement(), cacher
.getFromCache(pkey
.getRoot().getStatement()));
125 Assert
.assertSame(pkey
.getRoot().getQuestion(), cacher
.getFromCache(pkey
.getRoot().getQuestion()));
130 * when : retrieving objects using recursive caching of object graphs with different depths
131 * then : the objects in the sub-graph having the same persistence id should be the same
134 public void lazyLoadRecursiveTest() {
136 // this call will load into the session cache the graph and update the objects in the sub-graph for a
138 // |- root : polytomous key node
139 // |- question : KeyStatement
140 // |- statement : KeyStatement
141 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
142 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
143 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
149 * when : retrieving objects using recursive caching of object graphs with different depths
150 * then : the objects in the sub-graph having the same persistence id should be the same
153 public void differentSubGraphDepthTest1() {
155 // this call will load into the session cache the graph and update the objects in the sub-graph for a
157 // |- root : polytomous key node
158 // in a recursive call
159 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
160 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
161 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
163 // this call will load into the session cache the graph and update the objects in the sub-graph for a
165 // |- root : polytomous key node
166 // |- question : KeyStatement
167 // |- statement : KeyStatement
168 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
169 pkey2
= cdmEntitySessionManager
.load(sessionOwner
, pkey2
);
171 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
172 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
173 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
178 * when : retrieving objects using recursive caching of object graphs with different depths
179 * then : the objects in the sub-graph having the same persistence id should be the same
182 public void differentSubGraphDepthTest2() {
184 // this call will load into the session cache the graph and update the objects in the sub-graph for a
186 // |- root : polytomous key node
187 // |- question : KeyStatement
188 // |- statement : KeyStatement
189 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
190 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
191 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
193 // this call will load into the session cache the graph and update the objects in the sub-graph for a
195 // |- root : polytomous key node
196 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
197 pkey2
= cdmEntitySessionManager
.load(sessionOwner
, pkey2
);
199 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
200 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
201 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
205 * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
206 * then : the objects in the sub-graph having the same persistence id should be the same
209 public void recursiveLoadAndLazyLoadTest() {
211 // this call will load into the session cache the graph and update the objects in the sub-graph for a
213 // |- root : polytomous key node
214 // |- question : KeyStatement
215 // |- statement : KeyStatement
216 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
217 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
218 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
220 // checking that the root is not null and
221 // that it exists in the cache and
222 // that both the original object and the
223 // cached object are the same
224 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
225 Assert
.assertNotNull(cacher
.getFromCache(pkey1
.getRoot().getStatement()));
227 // this call will load into the session cache the graph and update the objects in the sub-graph for a
229 // |- root : polytomous key node
230 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
231 pkey2
= cdmEntitySessionManager
.load(sessionOwner
, pkey2
);
234 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
235 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
236 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
240 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
241 * then : the objects in the sub-graph having the same persistence id should be the same
244 public void lazyLoadAndRecursiveLoadTest() {
247 // this call will load into the session cache the graph and update the objects in the sub-graph for a
249 // |- root : polytomous key node
251 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
252 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
254 // lazy initialising root.statement
255 KeyStatement st
= CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class);
258 // checking that the root is not null and
259 // that it exists in the cache and
260 // that both the original object and the
261 // cached object are the same
262 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
263 Assert
.assertSame(CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class),
264 cacher
.getFromCache(pkey1
.getRoot().getStatement(), KeyStatement
.class));
266 // this call will load into the session cache the graph and update the objects in the sub-graph for a
268 // |- root : polytomous key node
269 // |- question : KeyStatement
270 // |- statement : KeyStatement
272 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
273 pkey2
= cdmEntitySessionManager
.load(sessionOwner
, pkey2
);
275 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
276 Assert
.assertSame(st
, pkey2
.getRoot().getStatement());
277 Assert
.assertSame(cacher
.getFromCache(st
), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
285 * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
286 * then : the object should be the same
289 public void subGraphObjectLoadTest1() {
291 // this call will load into the session cache a polytomous key node object
292 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
293 rootPKNode
= cdmEntitySessionManager
.load(sessionOwner
, rootPKNode
);
295 Assert
.assertNotNull(rootPKNode
);
296 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
298 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
299 childOfRootPKNode
= cdmEntitySessionManager
.load(sessionOwner
, childOfRootPKNode
);
301 Assert
.assertNotNull(childOfRootPKNode
);
302 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
304 // this call will load into the session cache the graph and update the objects in the sub-graph for a
306 // |- root : polytomous key node
307 // |- question : KeyStatement
308 // |- statement : KeyStatement
310 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
311 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
313 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
314 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
318 * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
319 * then : the object should be the same
322 public void subGraphObjectLoadTest2() {
324 // this call will load into the session cache the graph and update the objects in the sub-graph for a
326 // |- root : polytomous key node
327 // |- question : KeyStatement
328 // |- statement : KeyStatement
330 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
331 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
333 // this call will load into the session cache a polytomous key node object
334 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
335 rootPKNode
= cdmEntitySessionManager
.load(sessionOwner
, rootPKNode
);
337 Assert
.assertNotNull(rootPKNode
);
338 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
340 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
341 childOfRootPKNode
= cdmEntitySessionManager
.load(sessionOwner
, childOfRootPKNode
);
343 Assert
.assertNotNull(childOfRootPKNode
);
344 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
346 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
347 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
352 * when : loading objects from a collection
353 * then : the object stored in the cache should be the same
356 public void subGraphCollectionLoadTest() {
358 // this call will load into the session cache the graph and update the objects in the sub-graph for a
360 // |- root : polytomous key node
361 // |- question : KeyStatement
362 // |- statement : KeyStatement
364 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
365 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
367 PolytomousKeyNode childOfRootPKNode
= pkey
.getRoot().getChildAt(1);
369 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
370 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
375 * when : loading a non-lazy collection in a subgraph and loading the collection directly
376 * then : the object stored in the cache should be the same as the object in the sub-graph collection and
377 * the object in the directly loaded collection
380 public void nonLazyCollectionLoadTest() {
381 // need to find an example of this
385 * when : loading objects from a map
386 * then : the object stored in the cache should be the same
389 public void subGraphMapLoadTest() {
391 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
392 Language hindi
= Language
.getLanguageFromUuid(UUID
.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
395 // this call will load into the session cache the graph and update the objects in the sub-graph for a
397 // |- root : polytomous key node
398 // |- question : KeyStatement
399 // |- statement : KeyStatement
401 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
),PolytomousKey
.class);
402 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
404 Map
<Language
, LanguageString
> labelMap
= pkey
.getRoot().getStatement().getLabel();
405 Set
<Language
> languages
= labelMap
.keySet();
406 Iterator
<Language
> languagesItr
= languages
.iterator();
407 while(languagesItr
.hasNext()) {
408 Language lang
= languagesItr
.next();
409 if(lang
.equals(english
)) {
410 Assert
.assertSame(lang
, english
);
412 if(lang
.equals(hindi
)) {
413 Assert
.assertSame(lang
, hindi
);
415 Assert
.assertSame(lang
, cacher
.getFromCache(lang
));
420 * when : loading a non-lazy map in a subgraph and loading the map directly
421 * then : the object stored in the cache should be the same as the object in the sub-graph map and
422 * the object in the directly loaded map
425 public void nonLazyMapLoadTest() {
426 // need to find an example of this