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
.session
.ISessionEventListener
;
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 UUID polytomousKeyUuid
= UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
53 private UUID taxon1Uuid
= UUID
.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
54 private UUID polytomousKeyNodeUuid1
= UUID
.fromString("75e4c924-ff58-4ee7-a59d-fd9173517d08");
55 private UUID polytomousKeyNodeUuid2
= UUID
.fromString("b775c027-13c0-4b87-8aa9-712faeaafbdc");
58 private IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
59 private IPolytomousKeyNodeService polytomousKeyNodeService
= getRemoteApplicationController().getPolytomousKeyNodeService();
60 private ICommonService commonService
= getRemoteApplicationController().getCommonService();
61 private ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
64 private CdmTransientEntityCacher cacher
;
65 private ISessionEventListener sessionOwner
;
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
[] {
79 public static void initializePolytomousKeyTest() {
80 logger
.setLevel(Level
.INFO
);
81 CDMServer
.getInstance().setKeepServerRunning(true);
85 public void initializeSession() {
86 sessionOwner
= new MockSessionOwner();
87 cdmEntitySessionManager
.bind(sessionOwner
);
88 cacher
= getCacher(sessionOwner
);
94 public void recursiveLoadSubGraphDepth1Test() {
96 // this call will load into the session cache the graph
98 // |- root : polytomous key node
99 // in a recursive call
100 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
101 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
103 // checking to make sure the root object is in the session cache
104 Assert
.assertSame(pkey1
.getRoot(), cacher
.getFromCache(pkey1
.getRoot()));
110 public void recursiveLoadSubGraphDepth2Test() {
112 // this call will load into the session cache the graph
114 // |- root : polytomous key node
115 // |- question : KeyStatement
116 // |- statement : KeyStatement
117 // in a recursive call
118 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
119 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
120 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
121 // checking to make sure the root object is in the session cache
122 Assert
.assertSame(pkey
.getRoot(), cacher
.getFromCache(pkey
.getRoot()));
123 Assert
.assertSame(pkey
.getRoot().getStatement(), cacher
.getFromCache(pkey
.getRoot().getStatement()));
124 Assert
.assertSame(pkey
.getRoot().getQuestion(), cacher
.getFromCache(pkey
.getRoot().getQuestion()));
129 * when : retrieving objects using recursive caching of object graphs with different depths
130 * then : the objects in the sub-graph having the same persistence id should be the same
133 public void lazyLoadRecursiveTest() {
135 // this call will load into the session cache the graph and update the objects in the sub-graph for a
137 // |- root : polytomous key node
138 // |- question : KeyStatement
139 // |- statement : KeyStatement
140 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
141 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
142 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
148 * when : retrieving objects using recursive caching of object graphs with different depths
149 * then : the objects in the sub-graph having the same persistence id should be the same
152 public void differentSubGraphDepthTest1() {
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 // in a recursive call
158 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
159 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
160 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
162 // this call will load into the session cache the graph and update the objects in the sub-graph for a
164 // |- root : polytomous key node
165 // |- question : KeyStatement
166 // |- statement : KeyStatement
167 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
168 pkey2
= cdmEntitySessionManager
.load(sessionOwner
, pkey2
);
170 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
171 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
172 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
177 * when : retrieving objects using recursive caching of object graphs with different depths
178 * then : the objects in the sub-graph having the same persistence id should be the same
181 public void differentSubGraphDepthTest2() {
183 // this call will load into the session cache the graph and update the objects in the sub-graph for a
185 // |- root : polytomous key node
186 // |- question : KeyStatement
187 // |- statement : KeyStatement
188 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
189 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
190 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
192 // this call will load into the session cache the graph and update the objects in the sub-graph for a
194 // |- root : polytomous key node
195 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
196 pkey2
= cdmEntitySessionManager
.load(sessionOwner
, pkey2
);
198 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
199 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
200 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
204 * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
205 * then : the objects in the sub-graph having the same persistence id should be the same
208 public void recursiveLoadAndLazyLoadTest() {
210 // this call will load into the session cache the graph and update the objects in the sub-graph for a
212 // |- root : polytomous key node
213 // |- question : KeyStatement
214 // |- statement : KeyStatement
215 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
216 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
217 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
219 // checking that the root is not null and
220 // that it exists in the cache and
221 // that both the original object and the
222 // cached object are the same
223 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
224 Assert
.assertNotNull(cacher
.getFromCache(pkey1
.getRoot().getStatement()));
226 // this call will load into the session cache the graph and update the objects in the sub-graph for a
228 // |- root : polytomous key node
229 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
230 pkey2
= cdmEntitySessionManager
.load(sessionOwner
, pkey2
);
233 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
234 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
235 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
239 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
240 * then : the objects in the sub-graph having the same persistence id should be the same
243 public void lazyLoadAndRecursiveLoadTest() {
246 // this call will load into the session cache the graph and update the objects in the sub-graph for a
248 // |- root : polytomous key node
250 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
251 pkey1
= cdmEntitySessionManager
.load(sessionOwner
, pkey1
);
253 // lazy initialising root.statement
254 KeyStatement st
= CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class);
257 // checking that the root is not null and
258 // that it exists in the cache and
259 // that both the original object and the
260 // cached object are the same
261 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
262 Assert
.assertSame(CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class),
263 cacher
.getFromCache(pkey1
.getRoot().getStatement(), KeyStatement
.class));
265 // this call will load into the session cache the graph and update the objects in the sub-graph for a
267 // |- root : polytomous key node
268 // |- question : KeyStatement
269 // |- statement : KeyStatement
271 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
272 pkey2
= cdmEntitySessionManager
.load(sessionOwner
, pkey2
);
274 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
275 Assert
.assertSame(st
, pkey2
.getRoot().getStatement());
276 Assert
.assertSame(cacher
.getFromCache(st
), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
284 * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
285 * then : the object should be the same
288 public void subGraphObjectLoadTest1() {
290 // this call will load into the session cache a polytomous key node object
291 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
292 rootPKNode
= cdmEntitySessionManager
.load(sessionOwner
, rootPKNode
);
294 Assert
.assertNotNull(rootPKNode
);
295 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
297 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
298 childOfRootPKNode
= cdmEntitySessionManager
.load(sessionOwner
, childOfRootPKNode
);
300 Assert
.assertNotNull(childOfRootPKNode
);
301 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
303 // this call will load into the session cache the graph and update the objects in the sub-graph for a
305 // |- root : polytomous key node
306 // |- question : KeyStatement
307 // |- statement : KeyStatement
309 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
310 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
312 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
313 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
317 * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
318 * then : the object should be the same
321 public void subGraphObjectLoadTest2() {
323 // this call will load into the session cache the graph and update the objects in the sub-graph for a
325 // |- root : polytomous key node
326 // |- question : KeyStatement
327 // |- statement : KeyStatement
329 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
330 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
332 // this call will load into the session cache a polytomous key node object
333 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
334 rootPKNode
= cdmEntitySessionManager
.load(sessionOwner
, rootPKNode
);
336 Assert
.assertNotNull(rootPKNode
);
337 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
339 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
340 childOfRootPKNode
= cdmEntitySessionManager
.load(sessionOwner
, childOfRootPKNode
);
342 Assert
.assertNotNull(childOfRootPKNode
);
343 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
345 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
346 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
351 * when : loading objects from a collection
352 * then : the object stored in the cache should be the same
355 public void subGraphCollectionLoadTest() {
357 // this call will load into the session cache the graph and update the objects in the sub-graph for a
359 // |- root : polytomous key node
360 // |- question : KeyStatement
361 // |- statement : KeyStatement
363 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
364 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
366 PolytomousKeyNode childOfRootPKNode
= pkey
.getRoot().getChildAt(1);
368 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
369 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
374 * when : loading a non-lazy collection in a subgraph and loading the collection directly
375 * then : the object stored in the cache should be the same as the object in the sub-graph collection and
376 * the object in the directly loaded collection
379 public void nonLazyCollectionLoadTest() {
380 // need to find an example of this
384 * when : loading objects from a map
385 * then : the object stored in the cache should be the same
388 public void subGraphMapLoadTest() {
390 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
391 Language hindi
= Language
.getLanguageFromUuid(UUID
.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
394 // this call will load into the session cache the graph and update the objects in the sub-graph for a
396 // |- root : polytomous key node
397 // |- question : KeyStatement
398 // |- statement : KeyStatement
400 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
),PolytomousKey
.class);
401 pkey
= cdmEntitySessionManager
.load(sessionOwner
, pkey
);
403 Map
<Language
, LanguageString
> labelMap
= pkey
.getRoot().getStatement().getLabel();
404 Set
<Language
> languages
= labelMap
.keySet();
405 Iterator
<Language
> languagesItr
= languages
.iterator();
406 while(languagesItr
.hasNext()) {
407 Language lang
= languagesItr
.next();
408 if(lang
.equals(english
)) {
409 Assert
.assertSame(lang
, english
);
411 if(lang
.equals(hindi
)) {
412 Assert
.assertSame(lang
, hindi
);
414 Assert
.assertSame(lang
, cacher
.getFromCache(lang
));
419 * when : loading a non-lazy map in a subgraph and loading the map directly
420 * then : the object stored in the cache should be the same as the object in the sub-graph map and
421 * the object in the directly loaded map
424 public void nonLazyMapLoadTest() {
425 // need to find an example of this