Project

General

Profile

Download (16.9 KB) Statistics
| Branch: | Tag: | Revision:
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.Iterator;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.apache.log4j.Logger;
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.IPolytomousKeyNodeService;
28
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
29
import eu.etaxonomy.cdm.api.service.ITaxonService;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.common.Language;
32
import eu.etaxonomy.cdm.model.common.LanguageString;
33
import eu.etaxonomy.cdm.model.description.KeyStatement;
34
import eu.etaxonomy.cdm.model.description.PolytomousKey;
35
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
36
import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
37
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
38
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
39
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
40
import eu.etaxonomy.taxeditor.session.MockSessionOwner;
41

    
42
/**
43
 * @author cmathew
44
 * @date 7 Oct 2014
45
 *
46
 */
47
@DataSet
48
public class CdmClientCachingTest extends BaseRemotingTest {
49

    
50
    private static final Logger logger = Logger.getLogger(CdmClientCachingTest.class);
51

    
52
    private final UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
53
    private final UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
54
    private final UUID polytomousKeyNodeUuid1 = UUID.fromString("75e4c924-ff58-4ee7-a59d-fd9173517d08");
55
    private final UUID polytomousKeyNodeUuid2 = UUID.fromString("b775c027-13c0-4b87-8aa9-712faeaafbdc");
56

    
57

    
58
    private final IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
59
    private final IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
60
    private final ICommonService commonService = getRemoteApplicationController().getCommonService();
61
    private final ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
62

    
63

    
64
	private CdmTransientEntityCacher cacher;
65
    private ICdmEntitySession cdmEntitySession;
66

    
67
    private static final List<String> PKEY_DEPTH1_INIT_STRATEGY = Arrays.asList(new String[] {
68
			});
69

    
70
    private static final List<String> PKEY_DEPTH2_INIT_STRATEGY = Arrays.asList(new String[] {
71
    		"root"});
72

    
73
    private static final List<String> PKEY_DEPTH3_INIT_STRATEGY = Arrays.asList(new String[] {
74
    		"root.statement"});
75

    
76
    private ICdmEntitySessionEnabled sessionOwner;
77

    
78
    @BeforeClass
79
    public static void initializePolytomousKeyTest() {
80

    
81
    }
82

    
83
    @Before
84
    public void initializeSession() {
85
    	sessionOwner = new MockSessionOwner();
86
    	cdmEntitySession = cdmEntitySessionManager.newSession(sessionOwner, true);
87
    	cacher = getCacher(sessionOwner);
88
    }
89

    
90

    
91

    
92
    @Test
93
    public void recursiveLoadSubGraphDepth1Test() {
94

    
95
    	// this call will load into the session cache the graph
96
    	// polytomous key
97
    	// 	|- root : polytomous key node
98
    	// in a recursive call
99
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
100

    
101

    
102
        // checking to make sure the root object is in the session cache
103
        Assert.assertSame(pkey1.getRoot(), cacher.getFromCache(pkey1.getRoot()));
104

    
105
    }
106

    
107

    
108
    @Test
109
    public void recursiveLoadSubGraphDepth2Test() {
110

    
111
    	// this call will load into the session cache the graph
112
    	// polytomous key
113
    	// 	|- root : polytomous key node
114
    	//		|- question : KeyStatement
115
    	//		|- statement : KeyStatement
116
    	// in a recursive call
117
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
118
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
119
        // checking to make sure the root object is in the session cache
120
        Assert.assertSame(pkey.getRoot(), cacher.getFromCache(pkey.getRoot()));
121
        Assert.assertSame(pkey.getRoot().getStatement(), cacher.getFromCache(pkey.getRoot().getStatement()));
122
        Assert.assertSame(pkey.getRoot().getQuestion(), cacher.getFromCache(pkey.getRoot().getQuestion()));
123

    
124
    }
125

    
126
    /**
127
     * when : retrieving objects using recursive caching of object graphs with different depths
128
     * then : the objects in the sub-graph having the same persistence id should be the same
129
     */
130
    @Test
131
    public void lazyLoadRecursiveTest() {
132

    
133
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
134
    	// polytomous key
135
    	// 	|- root : polytomous key node
136
    	//		|- question : KeyStatement
137
    	//		|- statement : KeyStatement
138
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
139

    
140
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
141

    
142
    }
143

    
144

    
145
    /**
146
     * when : retrieving objects using recursive caching of object graphs with different depths
147
     * then : the objects in the sub-graph having the same persistence id should be the same
148
     */
149
    @Test
150
    public void differentSubGraphDepthTest1() {
151

    
152
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
153
    	// polytomous key
154
    	// 	|- root : polytomous key node
155
    	// in a recursive call
156
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
157

    
158
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
159

    
160
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
161
    	// polytomous key
162
    	// 	|- root : polytomous key node
163
    	//		|- question : KeyStatement
164
    	//		|- statement : KeyStatement
165
        PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
166

    
167

    
168
        Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
169
        Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
170
        Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
171

    
172
    }
173

    
174
    /**
175
     * when : retrieving objects using recursive caching of object graphs with different depths
176
     * then : the objects in the sub-graph having the same persistence id should be the same
177
     */
178
    @Test
179
    public void differentSubGraphDepthTest2() {
180

    
181
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
182
    	// polytomous key
183
    	// 	|- root : polytomous key node
184
    	//		|- question : KeyStatement
185
    	//		|- statement : KeyStatement
186
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
187

    
188
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
189

    
190
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
191
    	// polytomous key
192
    	// 	|- root : polytomous key node
193
        PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
194

    
195

    
196
        Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
197
        Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
198
        Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
199
    }
200

    
201
    /**
202
     * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
203
     * then : the objects in the sub-graph having the same persistence id should be the same
204
     */
205
    @Test
206
    public void recursiveLoadAndLazyLoadTest() {
207

    
208
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
209
    	// polytomous key
210
    	// 	|- root : polytomous key node
211
    	//		|- question : KeyStatement
212
    	//		|- statement : KeyStatement
213
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
214

    
215
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
216

    
217
        // checking that the root is not null and
218
        // that it exists in the cache and
219
        // that both the original object and the
220
        // cached object are the same
221
        Assert.assertNotNull(pkey1.getRoot().getStatement());
222
        Assert.assertNotNull(cacher.getFromCache(pkey1.getRoot().getStatement()));
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
        PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
228

    
229

    
230

    
231
        Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
232
        Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
233
        Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
234
    }
235

    
236
    /**
237
     * when : retrieving objects using (first) lazy loading  and (second) recursive load directly in the same session
238
     * then : the objects in the sub-graph having the same persistence id should be the same
239
     */
240
    @Test
241
    public void lazyLoadAndRecursiveLoadTest() {
242

    
243

    
244
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
245
    	// polytomous key
246
    	// 	|- root : polytomous key node
247

    
248
    	PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
249

    
250

    
251
        // lazy initialising root.statement
252
        KeyStatement st = CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class);
253
        st.getLabel();
254

    
255
        // checking that the root is not null and
256
        // that it exists in the cache and
257
        // that both the original object and the
258
        // cached object are the same
259
    	Assert.assertNotNull(pkey1.getRoot().getStatement());
260
    	Assert.assertSame(CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class),
261
    			cacher.getFromCache(pkey1.getRoot().getStatement(), KeyStatement.class));
262

    
263
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
264
    	// polytomous key
265
    	// 	|- root : polytomous key node
266
    	//		|- question : KeyStatement
267
    	//		|- statement : KeyStatement
268

    
269
    	PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
270

    
271

    
272
    	Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
273
    	Assert.assertSame(st, pkey2.getRoot().getStatement());
274
    	Assert.assertSame(cacher.getFromCache(st), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
275
    }
276

    
277

    
278

    
279

    
280

    
281
    /**
282
     * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
283
     * then : the object should be the same
284
     */
285
    @Test
286
    public void subGraphObjectLoadTest1() {
287

    
288
    	// this call will load into the session cache a polytomous key node object
289
    	PolytomousKeyNode rootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid1),PolytomousKeyNode.class);
290

    
291

    
292
    	Assert.assertNotNull(rootPKNode);
293
    	Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
294

    
295
    	PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
296

    
297

    
298
    	Assert.assertNotNull(childOfRootPKNode);
299
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
300

    
301
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
302
    	// polytomous key
303
    	// 	|- root : polytomous key node
304
    	//		|- question : KeyStatement
305
    	//		|- statement : KeyStatement
306

    
307
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
308

    
309

    
310
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
311
    	Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
312
    }
313

    
314
    /**
315
     * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
316
     * then : the object should be the same
317
     */
318
    @Test
319
    public void subGraphObjectLoadTest2() {
320

    
321
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
322
    	// polytomous key
323
    	// 	|- root : polytomous key node
324
    	//		|- question : KeyStatement
325
    	//		|- statement : KeyStatement
326

    
327
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
328

    
329

    
330
    	// this call will load into the session cache a polytomous key node object
331
    	PolytomousKeyNode rootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid1),PolytomousKeyNode.class);
332

    
333

    
334
    	Assert.assertNotNull(rootPKNode);
335
    	Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
336

    
337
    	PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
338

    
339

    
340
    	Assert.assertNotNull(childOfRootPKNode);
341
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
342

    
343
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
344
    	Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
345
    }
346

    
347

    
348
    /**
349
     * when : loading objects from a collection
350
     * then : the object stored in the cache should be the same
351
     */
352
    @Test
353
    public void subGraphCollectionLoadTest() {
354

    
355
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
356
    	// polytomous key
357
    	// 	|- root : polytomous key node
358
    	//		|- question : KeyStatement
359
    	//		|- statement : KeyStatement
360

    
361
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
362

    
363

    
364
    	PolytomousKeyNode childOfRootPKNode = pkey.getRoot().getChildAt(1);
365

    
366
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
367
    	Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
368

    
369
    }
370

    
371
    /**
372
     * when : loading a non-lazy collection in a subgraph and loading the collection directly
373
     * then : the object stored in the cache should be the same as the object in the sub-graph collection and
374
     *        the object in the directly loaded collection
375
     */
376
    @Test
377
    public void nonLazyCollectionLoadTest() {
378
    	// need to find an example of this
379
    }
380

    
381
    /**
382
     * when : loading objects from a map
383
     * then : the object stored in the cache should be the same
384
     */
385
    @Test
386
    public void subGraphMapLoadTest() {
387

    
388
    	Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
389
        Language hindi = Language.getLanguageFromUuid(UUID.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
390

    
391

    
392
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
393
    	// polytomous key
394
    	// 	|- root : polytomous key node
395
    	//		|- question : KeyStatement
396
    	//		|- statement : KeyStatement
397

    
398
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH3_INIT_STRATEGY),PolytomousKey.class);
399

    
400

    
401
    	Map<Language, LanguageString> labelMap = pkey.getRoot().getStatement().getLabel();
402
    	Set<Language> languages = labelMap.keySet();
403
    	Iterator<Language> languagesItr = languages.iterator();
404
    	while(languagesItr.hasNext()) {
405
    		Language lang = languagesItr.next();
406
    		if(lang.equals(english)) {
407
    			Assert.assertSame(lang, english);
408
    		}
409
    		if(lang.equals(hindi)) {
410
    			Assert.assertSame(lang, hindi);
411
    		}
412
    		Assert.assertSame(lang, cacher.getFromCache(lang));
413
    	}
414
    }
415

    
416
    /**
417
     * when : loading a non-lazy map in a subgraph and loading the map directly
418
     * then : the object stored in the cache should be the same as the object in the sub-graph map and
419
     *        the object in the directly loaded map
420
     */
421
    @Test
422
    public void nonLazyMapLoadTest() {
423
    	// need to find an example of this
424
    }
425

    
426

    
427

    
428
}
(1-1/2)