Project

General

Profile

Download (18.7 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.ISessionEventListener;
41
import eu.etaxonomy.taxeditor.session.MockSessionOwner;
42

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

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

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

    
58

    
59
    private IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
60
    private IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
61
    private ICommonService commonService = getRemoteApplicationController().getCommonService();
62
    private ITaxonService taxonService = getRemoteApplicationController().getTaxonService();    
63
    
64
    
65
	private CdmTransientEntityCacher cacher;
66
    private ISessionEventListener sessionOwner;
67
       
68
    private static final List<String> PKEY_DEPTH1_INIT_STRATEGY = Arrays.asList(new String[] {
69
			});
70
    
71
    private static final List<String> PKEY_DEPTH2_INIT_STRATEGY = Arrays.asList(new String[] {
72
    		"root"});
73
    
74
    private static final List<String> PKEY_DEPTH3_INIT_STRATEGY = Arrays.asList(new String[] {    		
75
    		"root.statement"});
76
    
77

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

    
85
    @Before
86
    public void initializeSession() {
87
    	sessionOwner = new MockSessionOwner();    
88
    	cdmEntitySessionManager.bind(sessionOwner);
89
    	cacher = getCacher(sessionOwner);
90
    }    
91

    
92

    
93
    
94
    @Test
95
    public void recursiveLoadSubGraphDepth1Test() {
96

    
97
    	// this call will load into the session cache the graph 
98
    	// polytomous key  
99
    	// 	|- root : polytomous key node 
100
    	// in a recursive call    	
101
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
102
        pkey1 = cdmEntitySessionManager.load(sessionOwner, pkey1);
103
        
104
        // checking to make sure the root object is in the session cache
105
        Assert.assertSame(pkey1.getRoot(), cacher.getFromCache(pkey1.getRoot()));
106
        
107
    }
108

    
109
    
110
    @Test
111
    public void recursiveLoadSubGraphDepth2Test() {
112

    
113
    	// this call will load into the session cache the graph 
114
    	// polytomous key  
115
    	// 	|- root : polytomous key node 
116
    	//		|- question : KeyStatement
117
    	//		|- statement : KeyStatement    	
118
    	// in a recursive call    	
119
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
120
        pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
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
        pkey1 = cdmEntitySessionManager.load(sessionOwner, pkey1);
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
        pkey1 = cdmEntitySessionManager.load(sessionOwner, pkey1);
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
        pkey2 = cdmEntitySessionManager.load(sessionOwner, pkey2);
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
        pkey1 = cdmEntitySessionManager.load(sessionOwner, pkey1);
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
        pkey2 = cdmEntitySessionManager.load(sessionOwner, pkey2);
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
        pkey1 = cdmEntitySessionManager.load(sessionOwner, pkey1);
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
        pkey2 = cdmEntitySessionManager.load(sessionOwner, pkey2);
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
        pkey1 = cdmEntitySessionManager.load(sessionOwner, pkey1);
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
    	pkey2 = cdmEntitySessionManager.load(sessionOwner, pkey2);
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
    	rootPKNode = cdmEntitySessionManager.load(sessionOwner, rootPKNode);
294

    
295
    	Assert.assertNotNull(rootPKNode);
296
    	Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
297
    	
298
    	PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
299
    	childOfRootPKNode = cdmEntitySessionManager.load(sessionOwner, childOfRootPKNode);
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
    	pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
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
    	pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
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
    	rootPKNode = cdmEntitySessionManager.load(sessionOwner, rootPKNode);
336

    
337
    	Assert.assertNotNull(rootPKNode);
338
    	Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
339
    	
340
    	PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
341
    	childOfRootPKNode = cdmEntitySessionManager.load(sessionOwner, childOfRootPKNode);
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
    	pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
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
    	pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
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)