merge from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / test / java / eu / etaxonomy / cdm / api / cache / CdmClientCachingTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.api.cache;
11
12 import java.util.Arrays;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.UUID;
16
17 import org.apache.log4j.Level;
18 import org.apache.log4j.Logger;
19 import org.hibernate.collection.spi.PersistentCollection;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.unitils.dbunit.annotation.DataSet;
25
26 import eu.etaxonomy.cdm.api.service.ICommonService;
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.BaseRemotingTest;
36 import eu.etaxonomy.taxeditor.httpinvoker.CDMServer;
37 import eu.etaxonomy.taxeditor.remoting.session.CdmEntitySessionManager;
38 import eu.etaxonomy.taxeditor.remoting.session.ISessionEventListener;
39 import eu.etaxonomy.taxeditor.remoting.session.MockSessionOwner;
40
41 /**
42 * @author cmathew
43 * @date 7 Oct 2014
44 *
45 */
46 @DataSet
47 public class CdmClientCachingTest extends BaseRemotingTest {
48
49 private static final Logger logger = Logger.getLogger(CdmClientCachingTest.class);
50
51 UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
52 UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
53
54
55 IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
56 ICommonService commonService = getRemoteApplicationController().getCommonService();
57 ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
58
59 CdmEntitySessionManager cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
60
61 CdmTransientEntityCacher cacher;
62
63 Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
64
65 private static final List<String> PKEY_DEPTH1_INIT_STRATEGY = Arrays.asList(new String[] {
66 });
67
68 private static final List<String> PKEY_DEPTH2_INIT_STRATEGY = Arrays.asList(new String[] {
69 "root"});
70
71 private static final List<String> PKEY_DEPTH3_INIT_STRATEGY = Arrays.asList(new String[] {
72 "root.statement"});
73
74 private ISessionEventListener sessionOwner;
75
76 @BeforeClass
77 public static void initializePolytomousKeyTest() {
78 logger.setLevel(Level.INFO);
79 CDMServer.getInstance().setKeepServerRunning(true);
80
81 CdmModelCacher cmdmc = new CdmModelCacher();
82 cmdmc.cacheGetterFields();
83
84 //initializeController("default", "127.0.0.1", 8080, "", NomenclaturalCode.ICNAFP);
85 }
86
87 @Before
88 public void initializeSession() {
89 sessionOwner = new MockSessionOwner();
90 cdmEntitySessionManager.bind(sessionOwner);
91 cacher = cdmEntitySessionManager.getCdmTransientEntityCacher(sessionOwner);
92 }
93
94 @Test
95 public void whenRecursiveCachingIsDisabledObjectsInSubGraphAreNotLoadedInSessionCache() {
96 CdmTransientEntityCacher.setRecursiveEnabled(false);
97 ISessionEventListener sessionOwner = new MockSessionOwner();
98 cdmEntitySessionManager.bind(sessionOwner);
99 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
100 cdmEntitySessionManager.add(sessionOwner, pkey);
101 CdmTransientEntityCacher cacher = cdmEntitySessionManager.getCdmTransientEntityCacher(sessionOwner);
102 Assert.assertNull(cacher.getFromCache(pkey));
103 CdmTransientEntityCacher.setRecursiveEnabled(true);
104 }
105
106
107 @Test
108 public void recursiveLoadSubGraphDepth1Test() {
109
110 // this call will load into the session cache the graph
111 // polytomous key
112 // |- root : polytomous key node
113 // in a recursive call
114 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
115 pkey1 = cdmEntitySessionManager.add(sessionOwner, pkey1);
116 CdmTransientEntityCacher cacher = cdmEntitySessionManager.getCdmTransientEntityCacher(sessionOwner);
117 // checking to make sure the root object is in the session cache
118 Assert.assertSame(pkey1.getRoot(), cacher.getFromCache(pkey1.getRoot()));
119
120 }
121
122
123 @Test
124 public void recursiveLoadSubGraphDepth2Test() {
125
126 // this call will load into the session cache the graph
127 // polytomous key
128 // |- root : polytomous key node
129 // |- question : KeyStatement
130 // |- statement : KeyStatement
131 // in a recursive call
132 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
133 pkey = cdmEntitySessionManager.add(sessionOwner, pkey);
134 CdmTransientEntityCacher cacher = cdmEntitySessionManager.getCdmTransientEntityCacher(sessionOwner);
135 // checking to make sure the root object is in the session cache
136 Assert.assertSame(pkey.getRoot(), cacher.getFromCache(pkey.getRoot()));
137 Assert.assertSame(pkey.getRoot().getStatement(), cacher.getFromCache(pkey.getRoot().getStatement()));
138 Assert.assertSame(pkey.getRoot().getQuestion(), cacher.getFromCache(pkey.getRoot().getQuestion()));
139
140 }
141
142 /**
143 * when : retrieving objects using recursive caching of object graphs with different depths
144 * then : the objects in the sub-graph having the same persistence id should be the same
145 */
146 @Test
147 public void lazyLoadRecursiveTest() {
148
149 // this call will load into the session cache the graph and update the objects in the sub-graph for a
150 // polytomous key
151 // |- root : polytomous key node
152 // |- question : KeyStatement
153 // |- statement : KeyStatement
154 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
155 pkey1 = cdmEntitySessionManager.add(sessionOwner, pkey1);
156 CdmTransientEntityCacher cacher = cdmEntitySessionManager.getCdmTransientEntityCacher(sessionOwner);
157
158 }
159
160
161 /**
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
164 */
165 @Test
166 public void differentSubGraphDepthTest1() {
167
168 // this call will load into the session cache the graph and update the objects in the sub-graph for a
169 // polytomous key
170 // |- root : polytomous key node
171 // in a recursive call
172 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
173 pkey1 = cdmEntitySessionManager.add(sessionOwner, pkey1);
174 CdmTransientEntityCacher cacher = cdmEntitySessionManager.getCdmTransientEntityCacher(sessionOwner);
175
176 // this call will load into the session cache the graph and update the objects in the sub-graph for a
177 // polytomous key
178 // |- root : polytomous key node
179 // |- question : KeyStatement
180 // |- statement : KeyStatement
181 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
182 pkey2 = cdmEntitySessionManager.add(sessionOwner, pkey2);
183
184 Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
185 Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
186 Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
187
188 }
189
190 /**
191 * when : retrieving objects using recursive caching of object graphs with different depths
192 * then : the objects in the sub-graph having the same persistence id should be the same
193 */
194 @Test
195 public void differentSubGraphDepthTest2() {
196
197 // this call will load into the session cache the graph and update the objects in the sub-graph for a
198 // polytomous key
199 // |- root : polytomous key node
200 // |- question : KeyStatement
201 // |- statement : KeyStatement
202 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
203 pkey1 = cdmEntitySessionManager.add(sessionOwner, pkey1);
204 CdmTransientEntityCacher cacher = cdmEntitySessionManager.getCdmTransientEntityCacher(sessionOwner);
205
206 // this call will load into the session cache the graph and update the objects in the sub-graph for a
207 // polytomous key
208 // |- root : polytomous key node
209 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
210 pkey2 = cdmEntitySessionManager.add(sessionOwner, pkey2);
211
212 Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
213 Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
214 Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
215 }
216
217 /**
218 * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
219 * then : the objects in the sub-graph having the same persistence id should be the same
220 */
221 @Test
222 public void recursiveLoadAndLazyLoadTest() {
223
224 // this call will load into the session cache the graph and update the objects in the sub-graph for a
225 // polytomous key
226 // |- root : polytomous key node
227 // |- question : KeyStatement
228 // |- statement : KeyStatement
229 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
230 pkey1 = cdmEntitySessionManager.add(sessionOwner, pkey1);
231 CdmTransientEntityCacher cacher = cdmEntitySessionManager.getCdmTransientEntityCacher(sessionOwner);
232
233 // checking that the root is not null and
234 // that it exists in the cache and
235 // that both the original object and the
236 // cached object are the same
237 Assert.assertNotNull(pkey1.getRoot().getStatement());
238 Assert.assertNotNull(cacher.getFromCache(pkey1.getRoot().getStatement()));
239
240 // this call will load into the session cache the graph and update the objects in the sub-graph for a
241 // polytomous key
242 // |- root : polytomous key node
243 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
244 pkey2 = cdmEntitySessionManager.add(sessionOwner, pkey2);
245
246
247 Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
248 Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
249 Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
250 }
251
252 /**
253 * when : retrieving objects using (first) lazy loading and (second) recursive load directly in the same session
254 * then : the objects in the sub-graph having the same persistence id should be the same
255 */
256 @Test
257 public void lazyLoadAndRecursiveLoadTest() {
258
259
260 // this call will load into the session cache the graph and update the objects in the sub-graph for a
261 // polytomous key
262 // |- root : polytomous key node
263
264 PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
265 pkey1 = cdmEntitySessionManager.add(sessionOwner, pkey1);
266
267 // lazy initialising root.statement
268 KeyStatement st = CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class);
269 st.getLabel();
270
271 // checking that the root is not null and
272 // that it exists in the cache and
273 // that both the original object and the
274 // cached object are the same
275 Assert.assertNotNull(pkey1.getRoot().getStatement());
276 Assert.assertSame(CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class),
277 cacher.getFromCache(pkey1.getRoot().getStatement(), KeyStatement.class));
278
279 // this call will load into the session cache the graph and update the objects in the sub-graph for a
280 // polytomous key
281 // |- root : polytomous key node
282 // |- question : KeyStatement
283 // |- statement : KeyStatement
284
285 PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
286 pkey2 = cdmEntitySessionManager.add(sessionOwner, pkey2);
287
288 Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
289 Assert.assertSame(st, pkey2.getRoot().getStatement());
290 Assert.assertSame(cacher.getFromCache(st), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
291 }
292
293
294
295 @Test
296 public void savePolytomousKeyNodeDataWithSameSubKey() {
297 //CachedCommonServiceImpl.setCacheEnabled(false);
298 ISessionEventListener sessionOwner = new MockSessionOwner();
299 cdmEntitySessionManager.bind(sessionOwner);
300 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
301 PolytomousKeyNode pkeynode = pkey.getRoot();
302
303 PolytomousKey subkey1 = pkeynode.getChildAt(0).getSubkey();
304 String subkey1title = subkey1.getTitleCache();
305 subkey1.setTitleCache(subkey1title + "test", true);
306
307
308 PolytomousKey subkey2 = pkeynode.getChildAt(1).getChildAt(0).getSubkey();
309 String subkey2title = subkey2.getTitleCache();
310 subkey2.setTitleCache(subkey2title + "test", true);
311
312 Assert.assertSame(subkey1, subkey2);
313
314 polytomousKeyService.merge(pkey);
315 }
316
317 @Test
318 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
319 CachedCommonServiceImpl.setCacheEnabled(false);
320 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
321 PolytomousKeyNode pkeynode = pkey.getRoot();
322
323 PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
324 PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(children, 0);
325 PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
326 String subkey1title = subkey1.getTitleCache();
327 subkey1.setTitleCache(subkey1title + "test", true);
328
329 PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(children, 1);
330 PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
331 String subkey2title = subkey2.getTitleCache();
332 subkey2.setTitleCache(subkey2title + "test", true);
333
334 Assert.assertNotSame(subkey1, subkey2);
335
336 polytomousKeyService.merge(pkey);
337 }
338
339
340 @Test
341 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
342 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
343 PolytomousKeyNode pkeynode = pkey.getRoot();
344
345 Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
346 label1.size();
347 //Language lang1 = label1.get(english).getLanguage();
348 //String title1 = lang1.getTitleCache();
349
350 Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
351 label2.size();
352 //Language lang2 = label2.get(english).getLanguage();
353 //String title2 = lang2.getTitleCache();
354
355 polytomousKeyService.merge(pkey);
356 }
357
358
359 }