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
.BeforeClass
;
21 import org
.junit
.Test
;
22 import org
.unitils
.dbunit
.annotation
.DataSet
;
24 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyNodeService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
30 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
31 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
32 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
33 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
34 import eu
.etaxonomy
.taxeditor
.httpinvoker
.RemotingSessionAwareTest
;
35 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
43 public class CdmClientCachingTest
extends RemotingSessionAwareTest
{
45 private static final Logger logger
= Logger
.getLogger(CdmClientCachingTest
.class);
47 private final UUID polytomousKeyUuid
= UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
48 private final UUID taxon1Uuid
= UUID
.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
49 private final UUID polytomousKeyNodeUuid1
= UUID
.fromString("75e4c924-ff58-4ee7-a59d-fd9173517d08");
50 private final UUID polytomousKeyNodeUuid2
= UUID
.fromString("b775c027-13c0-4b87-8aa9-712faeaafbdc");
53 private final IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
54 private final IPolytomousKeyNodeService polytomousKeyNodeService
= getRemoteApplicationController().getPolytomousKeyNodeService();
55 private final ICommonService commonService
= getRemoteApplicationController().getCommonService();
56 private final ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
59 private static final List
<String
> PKEY_DEPTH1_INIT_STRATEGY
= Arrays
.asList(new String
[] {
62 private static final List
<String
> PKEY_DEPTH2_INIT_STRATEGY
= Arrays
.asList(new String
[] {
65 private static final List
<String
> PKEY_DEPTH3_INIT_STRATEGY
= Arrays
.asList(new String
[] {
70 public static void initializePolytomousKeyTest() {
78 public void recursiveLoadSubGraphDepth1Test() {
80 // this call will load into the session cache the graph
82 // |- root : polytomous key node
83 // in a recursive call
84 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
86 // checking to make sure the root object is in the session cache
87 Assert
.assertSame(CdmBase
.deproxy(pkey1
.getRoot(),PolytomousKeyNode
.class), cacher
.getFromCache(pkey1
.getRoot()));
93 public void recursiveLoadSubGraphDepth2Test() {
95 // this call will load into the session cache the graph
97 // |- root : polytomous key node
98 // |- question : KeyStatement
99 // |- statement : KeyStatement
100 // in a recursive call
101 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
102 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
103 // checking to make sure the root object is in the session cache
104 Assert
.assertSame(CdmBase
.deproxy(pkey
.getRoot(),PolytomousKeyNode
.class), cacher
.getFromCache(pkey
.getRoot()));
105 Assert
.assertSame(CdmBase
.deproxy(pkey
.getRoot().getStatement(),KeyStatement
.class), cacher
.getFromCache(pkey
.getRoot().getStatement()));
106 Assert
.assertSame(CdmBase
.deproxy(pkey
.getRoot().getQuestion(),KeyStatement
.class), cacher
.getFromCache(pkey
.getRoot().getQuestion()));
111 * when : retrieving objects using recursive caching of object graphs with different depths
112 * then : the objects in the sub-graph having the same persistence id should be the same
115 public void lazyLoadRecursiveTest() {
117 // this call will load into the session cache the graph and update the objects in the sub-graph for a
119 // |- root : polytomous key node
120 // |- question : KeyStatement
121 // |- statement : KeyStatement
122 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
124 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
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 differentSubGraphDepthTest1() {
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 // in a recursive call
140 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
141 KeyStatement ks1
= CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class);
143 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
145 // this call will load into the session cache the graph and update the objects in the sub-graph for a
147 // |- root : polytomous key node
148 // |- question : KeyStatement
149 // |- statement : KeyStatement
150 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
),PolytomousKey
.class);
152 KeyStatement ks2
= CdmBase
.deproxy(pkey2
.getRoot().getStatement(), KeyStatement
.class);
154 Assert
.assertSame(ks2
, cacher
.getFromCache(ks2
));
155 Assert
.assertSame(cacher
.getFromCache(KeyStatement
.class, ks1
.getId()), cacher
.getFromCache(ks2
));
160 * when : retrieving objects using recursive caching of object graphs with different depths
161 * then : the objects in the sub-graph having the same persistence id should be the same
164 public void differentSubGraphDepthTest2() {
166 // this call will load into the session cache the graph and update the objects in the sub-graph for a
168 // |- root : polytomous key node
169 // |- question : KeyStatement
170 // |- statement : KeyStatement
171 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
172 KeyStatement ks1
= pkey1
.getRoot().getStatement();
173 Assert
.assertSame(ks1
, pkey1
.getRoot().getStatement());
174 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
176 // this call will load into the session cache the graph and update the objects in the sub-graph for a
178 // |- root : polytomous key node
179 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
181 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
182 pkey2
.getRoot().getStatement().getCreatedBy();
183 Assert
.assertSame(cacher
.getFromCache(ks1
), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
187 * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
188 * then : the objects in the sub-graph having the same persistence id should be the same
191 public void recursiveLoadAndLazyLoadTest() {
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 // |- question : KeyStatement
197 // |- statement : KeyStatement
198 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
201 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
203 // checking that the root is not null and
204 // that it exists in the cache and
205 // that both the original object and the
206 // cached object are the same
207 Assert
.assertNotNull(CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class));
208 Assert
.assertNotNull(cacher
.getFromCache(pkey1
.getRoot().getStatement()));
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 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
215 KeyStatement ks2
= CdmBase
.deproxy(pkey2
.getRoot().getStatement(), KeyStatement
.class);
216 Assert
.assertSame(ks2
, cacher
.getFromCache(pkey2
.getRoot().getStatement()));
218 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
219 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
223 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
224 * then : the objects in the sub-graph having the same persistence id should be the same
227 public void lazyLoadAndRecursiveLoadTest() {
230 // this call will load into the session cache the graph and update the objects in the sub-graph for a
232 // |- root : polytomous key node
234 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
237 // lazy initialising root.statement
238 KeyStatement st
= CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class);
241 // checking that the root is not null and
242 // that it exists in the cache and
243 // that both the original object and the
244 // cached object are the same
245 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
246 Assert
.assertSame(CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class),
247 cacher
.getFromCache(pkey1
.getRoot().getStatement(), KeyStatement
.class));
249 // this call will load into the session cache the graph and update the objects in the sub-graph for a
251 // |- root : polytomous key node
252 // |- question : KeyStatement
253 // |- statement : KeyStatement
255 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
258 Assert
.assertSame(CdmBase
.deproxy(pkey2
.getRoot().getStatement(), KeyStatement
.class), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
259 Assert
.assertSame(st
, CdmBase
.deproxy(pkey2
.getRoot().getStatement(), KeyStatement
.class));
260 Assert
.assertSame(cacher
.getFromCache(st
), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
268 * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
269 * then : the object should be the same
272 public void subGraphObjectLoadTest1() {
274 // this call will load into the session cache a polytomous key node object
275 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
278 Assert
.assertNotNull(rootPKNode
);
279 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
281 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
284 Assert
.assertNotNull(childOfRootPKNode
);
285 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
287 // this call will load into the session cache the graph and update the objects in the sub-graph for a
289 // |- root : polytomous key node
290 // |- question : KeyStatement
291 // |- statement : KeyStatement
293 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
296 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
297 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
301 * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
302 * then : the object should be the same
305 public void subGraphObjectLoadTest2() {
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
),PolytomousKey
.class);
316 // this call will load into the session cache a polytomous key node object
317 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
320 Assert
.assertNotNull(rootPKNode
);
321 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
323 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
326 Assert
.assertNotNull(childOfRootPKNode
);
327 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
329 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
330 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
335 * when : loading objects from a collection
336 * then : the object stored in the cache should be the same
339 public void subGraphCollectionLoadTest() {
341 // this call will load into the session cache the graph and update the objects in the sub-graph for a
343 // |- root : polytomous key node
344 // |- question : KeyStatement
345 // |- statement : KeyStatement
347 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
350 PolytomousKeyNode childOfRootPKNode
= pkey
.getRoot().getChildAt(1);
352 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
353 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
358 * when : loading a non-lazy collection in a subgraph and loading the collection directly
359 * then : the object stored in the cache should be the same as the object in the sub-graph collection and
360 * the object in the directly loaded collection
363 public void nonLazyCollectionLoadTest() {
364 // need to find an example of this
368 * when : loading objects from a map
369 * then : the object stored in the cache should be the same
372 public void subGraphMapLoadTest() {
374 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
375 Language hindi
= Language
.getLanguageFromUuid(UUID
.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
378 // this call will load into the session cache the graph and update the objects in the sub-graph for a
380 // |- root : polytomous key node
381 // |- question : KeyStatement
382 // |- statement : KeyStatement
384 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
),PolytomousKey
.class);
387 Map
<Language
, LanguageString
> labelMap
= pkey
.getRoot().getStatement().getLabel();
388 Set
<Language
> languages
= labelMap
.keySet();
389 Iterator
<Language
> languagesItr
= languages
.iterator();
390 while(languagesItr
.hasNext()) {
391 Language lang
= languagesItr
.next();
392 if(lang
.equals(english
)) {
393 Assert
.assertSame(lang
, english
);
395 if(lang
.equals(hindi
)) {
396 Assert
.assertSame(lang
, hindi
);
398 Assert
.assertSame(lang
, cacher
.getFromCache(lang
));
403 * when : loading a non-lazy map in a subgraph and loading the map directly
404 * then : the object stored in the cache should be the same as the object in the sub-graph map and
405 * the object in the directly loaded map
408 public void nonLazyMapLoadTest() {
409 // need to find an example of this