Project

General

Profile

Download (17 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.Level;
20
import org.apache.log4j.Logger;
21
import org.junit.Assert;
22
import org.junit.Before;
23
import org.junit.BeforeClass;
24
import org.junit.Test;
25
import org.unitils.dbunit.annotation.DataSet;
26

    
27
import eu.etaxonomy.cdm.api.service.ICommonService;
28
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
29
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
30
import eu.etaxonomy.cdm.api.service.ITaxonService;
31
import eu.etaxonomy.cdm.model.common.CdmBase;
32
import eu.etaxonomy.cdm.model.common.Language;
33
import eu.etaxonomy.cdm.model.common.LanguageString;
34
import eu.etaxonomy.cdm.model.description.KeyStatement;
35
import eu.etaxonomy.cdm.model.description.PolytomousKey;
36
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
37
import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
38
import eu.etaxonomy.taxeditor.httpinvoker.CDMServer;
39
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
40
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
41
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
42
import eu.etaxonomy.taxeditor.session.MockSessionOwner;
43

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

    
52
    private static final Logger logger = Logger.getLogger(CdmClientCachingTest.class);
53

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

    
59

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

    
65

    
66
	private CdmTransientEntityCacher cacher;
67
    private ICdmEntitySession cdmEntitySession;
68

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

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

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

    
78
    private ICdmEntitySessionEnabled sessionOwner;
79

    
80
    @BeforeClass
81
    public static void initializePolytomousKeyTest() {
82
        logger.setLevel(Level.INFO);
83
        CDMServer.getInstance().setKeepServerRunning(true);
84
    }
85

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

    
93

    
94

    
95
    @Test
96
    public void recursiveLoadSubGraphDepth1Test() {
97

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

    
104

    
105
        // checking to make sure the root object is in the session cache
106
        Assert.assertSame(pkey1.getRoot(), cacher.getFromCache(pkey1.getRoot()));
107

    
108
    }
109

    
110

    
111
    @Test
112
    public void recursiveLoadSubGraphDepth2Test() {
113

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

    
127
    }
128

    
129
    /**
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
132
     */
133
    @Test
134
    public void lazyLoadRecursiveTest() {
135

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

    
143
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
144

    
145
    }
146

    
147

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

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

    
161
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
162

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

    
170

    
171
        Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
172
        Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
173
        Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
174

    
175
    }
176

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

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

    
191
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
192

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

    
198

    
199
        Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
200
        Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
201
        Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
202
    }
203

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

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

    
218
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
219

    
220
        // checking that the root is not null and
221
        // that it exists in the cache and
222
        // that both the original object and the
223
        // cached object are the same
224
        Assert.assertNotNull(pkey1.getRoot().getStatement());
225
        Assert.assertNotNull(cacher.getFromCache(pkey1.getRoot().getStatement()));
226

    
227
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
228
    	// polytomous key
229
    	// 	|- root : polytomous key node
230
        PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
231

    
232

    
233

    
234
        Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement()));
235
        Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
236
        Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
237
    }
238

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

    
246

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

    
251
    	PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
252

    
253

    
254
        // lazy initialising root.statement
255
        KeyStatement st = CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class);
256
        st.getLabel();
257

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

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

    
272
    	PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
273

    
274

    
275
    	Assert.assertSame(pkey2.getRoot().getStatement(), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
276
    	Assert.assertSame(st, pkey2.getRoot().getStatement());
277
    	Assert.assertSame(cacher.getFromCache(st), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
278
    }
279

    
280

    
281

    
282

    
283

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

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

    
294

    
295
    	Assert.assertNotNull(rootPKNode);
296
    	Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
297

    
298
    	PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
299

    
300

    
301
    	Assert.assertNotNull(childOfRootPKNode);
302
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
303

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

    
310
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
311

    
312

    
313
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
314
    	Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
315
    }
316

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

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

    
330
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
331

    
332

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

    
336

    
337
    	Assert.assertNotNull(rootPKNode);
338
    	Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
339

    
340
    	PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
341

    
342

    
343
    	Assert.assertNotNull(childOfRootPKNode);
344
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
345

    
346
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
347
    	Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
348
    }
349

    
350

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

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

    
364
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
365

    
366

    
367
    	PolytomousKeyNode childOfRootPKNode = pkey.getRoot().getChildAt(1);
368

    
369
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
370
    	Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
371

    
372
    }
373

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

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

    
391
    	Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
392
        Language hindi = Language.getLanguageFromUuid(UUID.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
393

    
394

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

    
401
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH3_INIT_STRATEGY),PolytomousKey.class);
402

    
403

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

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

    
429

    
430

    
431
}
(1-1/2)