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
.BeforeClass
;
22 import org
.junit
.Test
;
23 import org
.unitils
.dbunit
.annotation
.DataSet
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyNodeService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
31 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
32 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
33 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
34 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
35 import eu
.etaxonomy
.taxeditor
.httpinvoker
.RemotingSessionAwareTest
;
36 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
44 public class CdmClientCachingTest
extends RemotingSessionAwareTest
{
46 private static final Logger logger
= Logger
.getLogger(CdmClientCachingTest
.class);
48 private final UUID polytomousKeyUuid
= UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
49 private final UUID taxon1Uuid
= UUID
.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
50 private final UUID polytomousKeyNodeUuid1
= UUID
.fromString("75e4c924-ff58-4ee7-a59d-fd9173517d08");
51 private final UUID polytomousKeyNodeUuid2
= UUID
.fromString("b775c027-13c0-4b87-8aa9-712faeaafbdc");
54 private final IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
55 private final IPolytomousKeyNodeService polytomousKeyNodeService
= getRemoteApplicationController().getPolytomousKeyNodeService();
56 private final ICommonService commonService
= getRemoteApplicationController().getCommonService();
57 private final ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
60 private static final List
<String
> PKEY_DEPTH1_INIT_STRATEGY
= Arrays
.asList(new String
[] {
63 private static final List
<String
> PKEY_DEPTH2_INIT_STRATEGY
= Arrays
.asList(new String
[] {
66 private static final List
<String
> PKEY_DEPTH3_INIT_STRATEGY
= Arrays
.asList(new String
[] {
71 public static void initializePolytomousKeyTest() {
79 public void recursiveLoadSubGraphDepth1Test() {
81 // this call will load into the session cache the graph
83 // |- root : polytomous key node
84 // in a recursive call
85 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
88 // checking to make sure the root object is in the session cache
89 Assert
.assertSame(pkey1
.getRoot(), cacher
.getFromCache(pkey1
.getRoot()));
95 public void recursiveLoadSubGraphDepth2Test() {
97 // this call will load into the session cache the graph
99 // |- root : polytomous key node
100 // |- question : KeyStatement
101 // |- statement : KeyStatement
102 // in a recursive call
103 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
104 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
105 // checking to make sure the root object is in the session cache
106 Assert
.assertSame(pkey
.getRoot(), cacher
.getFromCache(pkey
.getRoot()));
107 Assert
.assertSame(pkey
.getRoot().getStatement(), cacher
.getFromCache(pkey
.getRoot().getStatement()));
108 Assert
.assertSame(pkey
.getRoot().getQuestion(), cacher
.getFromCache(pkey
.getRoot().getQuestion()));
113 * when : retrieving objects using recursive caching of object graphs with different depths
114 * then : the objects in the sub-graph having the same persistence id should be the same
117 public void lazyLoadRecursiveTest() {
119 // this call will load into the session cache the graph and update the objects in the sub-graph for a
121 // |- root : polytomous key node
122 // |- question : KeyStatement
123 // |- statement : KeyStatement
124 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
126 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
132 * when : retrieving objects using recursive caching of object graphs with different depths
133 * then : the objects in the sub-graph having the same persistence id should be the same
136 public void differentSubGraphDepthTest1() {
138 // this call will load into the session cache the graph and update the objects in the sub-graph for a
140 // |- root : polytomous key node
141 // in a recursive call
142 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
143 KeyStatement ks1
= CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class);
145 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
147 // this call will load into the session cache the graph and update the objects in the sub-graph for a
149 // |- root : polytomous key node
150 // |- question : KeyStatement
151 // |- statement : KeyStatement
152 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
),PolytomousKey
.class);
154 KeyStatement ks2
= CdmBase
.deproxy(pkey2
.getRoot().getStatement(), KeyStatement
.class);
156 Assert
.assertSame(ks2
, cacher
.getFromCache(ks2
));
157 Assert
.assertSame(cacher
.getFromCache(KeyStatement
.class, ks1
.getId()), cacher
.getFromCache(ks2
));
162 * when : retrieving objects using recursive caching of object graphs with different depths
163 * then : the objects in the sub-graph having the same persistence id should be the same
166 public void differentSubGraphDepthTest2() {
168 // this call will load into the session cache the graph and update the objects in the sub-graph for a
170 // |- root : polytomous key node
171 // |- question : KeyStatement
172 // |- statement : KeyStatement
173 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
174 KeyStatement ks1
= pkey1
.getRoot().getStatement();
175 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), pkey1
.getRoot().getStatement());
176 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
178 // this call will load into the session cache the graph and update the objects in the sub-graph for a
180 // |- root : polytomous key node
181 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
183 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
184 pkey2
.getRoot().getStatement().getCreatedBy();
185 Assert
.assertSame(cacher
.getFromCache(ks1
), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
189 * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
190 * then : the objects in the sub-graph having the same persistence id should be the same
193 public void recursiveLoadAndLazyLoadTest() {
195 // this call will load into the session cache the graph and update the objects in the sub-graph for a
197 // |- root : polytomous key node
198 // |- question : KeyStatement
199 // |- statement : KeyStatement
200 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
203 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
205 // checking that the root is not null and
206 // that it exists in the cache and
207 // that both the original object and the
208 // cached object are the same
209 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
210 Assert
.assertNotNull(cacher
.getFromCache(pkey1
.getRoot().getStatement()));
212 // this call will load into the session cache the graph and update the objects in the sub-graph for a
214 // |- root : polytomous key node
215 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
217 KeyStatement ks2
= CdmBase
.deproxy(pkey2
.getRoot().getStatement(), KeyStatement
.class);
218 Assert
.assertSame(ks2
, cacher
.getFromCache(pkey2
.getRoot().getStatement()));
220 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
221 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
225 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
226 * then : the objects in the sub-graph having the same persistence id should be the same
229 public void lazyLoadAndRecursiveLoadTest() {
232 // this call will load into the session cache the graph and update the objects in the sub-graph for a
234 // |- root : polytomous key node
236 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
239 // lazy initialising root.statement
240 KeyStatement st
= CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class);
243 // checking that the root is not null and
244 // that it exists in the cache and
245 // that both the original object and the
246 // cached object are the same
247 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
248 Assert
.assertSame(CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class),
249 cacher
.getFromCache(pkey1
.getRoot().getStatement(), KeyStatement
.class));
251 // this call will load into the session cache the graph and update the objects in the sub-graph for a
253 // |- root : polytomous key node
254 // |- question : KeyStatement
255 // |- statement : KeyStatement
257 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
260 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
261 Assert
.assertSame(st
, pkey2
.getRoot().getStatement());
262 Assert
.assertSame(cacher
.getFromCache(st
), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
270 * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
271 * then : the object should be the same
274 public void subGraphObjectLoadTest1() {
276 // this call will load into the session cache a polytomous key node object
277 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
280 Assert
.assertNotNull(rootPKNode
);
281 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
283 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
286 Assert
.assertNotNull(childOfRootPKNode
);
287 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
289 // this call will load into the session cache the graph and update the objects in the sub-graph for a
291 // |- root : polytomous key node
292 // |- question : KeyStatement
293 // |- statement : KeyStatement
295 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
298 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
299 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
303 * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
304 * then : the object should be the same
307 public void subGraphObjectLoadTest2() {
309 // this call will load into the session cache the graph and update the objects in the sub-graph for a
311 // |- root : polytomous key node
312 // |- question : KeyStatement
313 // |- statement : KeyStatement
315 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
318 // this call will load into the session cache a polytomous key node object
319 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
322 Assert
.assertNotNull(rootPKNode
);
323 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
325 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
328 Assert
.assertNotNull(childOfRootPKNode
);
329 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
331 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
332 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
337 * when : loading objects from a collection
338 * then : the object stored in the cache should be the same
341 public void subGraphCollectionLoadTest() {
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_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
352 PolytomousKeyNode childOfRootPKNode
= pkey
.getRoot().getChildAt(1);
354 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
355 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
360 * when : loading a non-lazy collection in a subgraph and loading the collection directly
361 * then : the object stored in the cache should be the same as the object in the sub-graph collection and
362 * the object in the directly loaded collection
365 public void nonLazyCollectionLoadTest() {
366 // need to find an example of this
370 * when : loading objects from a map
371 * then : the object stored in the cache should be the same
374 public void subGraphMapLoadTest() {
376 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
377 Language hindi
= Language
.getLanguageFromUuid(UUID
.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
380 // this call will load into the session cache the graph and update the objects in the sub-graph for a
382 // |- root : polytomous key node
383 // |- question : KeyStatement
384 // |- statement : KeyStatement
386 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
),PolytomousKey
.class);
389 Map
<Language
, LanguageString
> labelMap
= pkey
.getRoot().getStatement().getLabel();
390 Set
<Language
> languages
= labelMap
.keySet();
391 Iterator
<Language
> languagesItr
= languages
.iterator();
392 while(languagesItr
.hasNext()) {
393 Language lang
= languagesItr
.next();
394 if(lang
.equals(english
)) {
395 Assert
.assertSame(lang
, english
);
397 if(lang
.equals(hindi
)) {
398 Assert
.assertSame(lang
, hindi
);
400 Assert
.assertSame(lang
, cacher
.getFromCache(lang
));
405 * when : loading a non-lazy map in a subgraph and loading the map directly
406 * then : the object stored in the cache should be the same as the object in the sub-graph map and
407 * the object in the directly loaded map
410 public void nonLazyMapLoadTest() {
411 // need to find an example of this