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);
175 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
177 // this call will load into the session cache the graph and update the objects in the sub-graph for a
179 // |- root : polytomous key node
180 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH1_INIT_STRATEGY
),PolytomousKey
.class);
183 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
184 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
185 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), 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);
202 CdmTransientEntityCacher cacher
= getCacher(sessionOwner
);
204 // checking that the root is not null and
205 // that it exists in the cache and
206 // that both the original object and the
207 // cached object are the same
208 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
209 Assert
.assertNotNull(cacher
.getFromCache(pkey1
.getRoot().getStatement()));
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 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
218 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
219 Assert
.assertSame(pkey1
.getRoot().getStatement(), pkey2
.getRoot().getStatement());
220 Assert
.assertSame(cacher
.getFromCache(pkey1
.getRoot().getStatement()), cacher
.getFromCache(pkey2
.getRoot().getStatement()));
224 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
225 * then : the objects in the sub-graph having the same persistence id should be the same
228 public void lazyLoadAndRecursiveLoadTest() {
231 // this call will load into the session cache the graph and update the objects in the sub-graph for a
233 // |- root : polytomous key node
235 PolytomousKey pkey1
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
238 // lazy initialising root.statement
239 KeyStatement st
= CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class);
242 // checking that the root is not null and
243 // that it exists in the cache and
244 // that both the original object and the
245 // cached object are the same
246 Assert
.assertNotNull(pkey1
.getRoot().getStatement());
247 Assert
.assertSame(CdmBase
.deproxy(pkey1
.getRoot().getStatement(), KeyStatement
.class),
248 cacher
.getFromCache(pkey1
.getRoot().getStatement(), KeyStatement
.class));
250 // this call will load into the session cache the graph and update the objects in the sub-graph for a
252 // |- root : polytomous key node
253 // |- question : KeyStatement
254 // |- statement : KeyStatement
256 PolytomousKey pkey2
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
259 Assert
.assertSame(pkey2
.getRoot().getStatement(), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
260 Assert
.assertSame(st
, pkey2
.getRoot().getStatement());
261 Assert
.assertSame(cacher
.getFromCache(st
), cacher
.getFromCache(pkey2
.getRoot().getStatement(), KeyStatement
.class));
269 * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
270 * then : the object should be the same
273 public void subGraphObjectLoadTest1() {
275 // this call will load into the session cache a polytomous key node object
276 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
279 Assert
.assertNotNull(rootPKNode
);
280 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
282 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
285 Assert
.assertNotNull(childOfRootPKNode
);
286 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
288 // this call will load into the session cache the graph and update the objects in the sub-graph for a
290 // |- root : polytomous key node
291 // |- question : KeyStatement
292 // |- statement : KeyStatement
294 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
297 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
298 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
302 * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
303 * then : the object should be the same
306 public void subGraphObjectLoadTest2() {
308 // this call will load into the session cache the graph and update the objects in the sub-graph for a
310 // |- root : polytomous key node
311 // |- question : KeyStatement
312 // |- statement : KeyStatement
314 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
317 // this call will load into the session cache a polytomous key node object
318 PolytomousKeyNode rootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid1
),PolytomousKeyNode
.class);
321 Assert
.assertNotNull(rootPKNode
);
322 Assert
.assertSame(rootPKNode
, cacher
.getFromCache(rootPKNode
));
324 PolytomousKeyNode childOfRootPKNode
= CdmBase
.deproxy(polytomousKeyNodeService
.find(polytomousKeyNodeUuid2
),PolytomousKeyNode
.class);
327 Assert
.assertNotNull(childOfRootPKNode
);
328 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
330 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
331 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
336 * when : loading objects from a collection
337 * then : the object stored in the cache should be the same
340 public void subGraphCollectionLoadTest() {
342 // this call will load into the session cache the graph and update the objects in the sub-graph for a
344 // |- root : polytomous key node
345 // |- question : KeyStatement
346 // |- statement : KeyStatement
348 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH2_INIT_STRATEGY
),PolytomousKey
.class);
351 PolytomousKeyNode childOfRootPKNode
= pkey
.getRoot().getChildAt(1);
353 Assert
.assertSame(childOfRootPKNode
, cacher
.getFromCache(childOfRootPKNode
));
354 Assert
.assertSame(pkey
.getRoot().getChildAt(1), childOfRootPKNode
);
359 * when : loading a non-lazy collection in a subgraph and loading the collection directly
360 * then : the object stored in the cache should be the same as the object in the sub-graph collection and
361 * the object in the directly loaded collection
364 public void nonLazyCollectionLoadTest() {
365 // need to find an example of this
369 * when : loading objects from a map
370 * then : the object stored in the cache should be the same
373 public void subGraphMapLoadTest() {
375 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
376 Language hindi
= Language
.getLanguageFromUuid(UUID
.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
379 // this call will load into the session cache the graph and update the objects in the sub-graph for a
381 // |- root : polytomous key node
382 // |- question : KeyStatement
383 // |- statement : KeyStatement
385 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.load(polytomousKeyUuid
, PKEY_DEPTH3_INIT_STRATEGY
),PolytomousKey
.class);
388 Map
<Language
, LanguageString
> labelMap
= pkey
.getRoot().getStatement().getLabel();
389 Set
<Language
> languages
= labelMap
.keySet();
390 Iterator
<Language
> languagesItr
= languages
.iterator();
391 while(languagesItr
.hasNext()) {
392 Language lang
= languagesItr
.next();
393 if(lang
.equals(english
)) {
394 Assert
.assertSame(lang
, english
);
396 if(lang
.equals(hindi
)) {
397 Assert
.assertSame(lang
, hindi
);
399 Assert
.assertSame(lang
, cacher
.getFromCache(lang
));
404 * when : loading a non-lazy map in a subgraph and loading the map directly
405 * then : the object stored in the cache should be the same as the object in the sub-graph map and
406 * the object in the directly loaded map
409 public void nonLazyMapLoadTest() {
410 // need to find an example of this