Project

General

Profile

Download (16.7 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2014 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.api.cache;
10

    
11
import java.util.Arrays;
12
import java.util.Iterator;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.apache.log4j.Logger;
19
import org.junit.Assert;
20
import org.junit.BeforeClass;
21
import org.junit.Test;
22
import org.unitils.dbunit.annotation.DataSet;
23

    
24
import eu.etaxonomy.cdm.api.service.ICommonService;
25
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
26
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
27
import eu.etaxonomy.cdm.api.service.ITaxonService;
28
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
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

    
37
/**
38
 * @author cmathew
39
 * @date 7 Oct 2014
40
 *
41
 */
42
@DataSet
43
public class CdmClientCachingTest extends RemotingSessionAwareTest {
44

    
45
    private static final Logger logger = Logger.getLogger(CdmClientCachingTest.class);
46

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

    
52

    
53
    private final IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
54
    private final IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
55
    private final ICommonService commonService = getRemoteApplicationController().getCommonService();
56
    private final ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
57

    
58

    
59
    private static final List<String> PKEY_DEPTH1_INIT_STRATEGY = Arrays.asList(new String[] {
60
			});
61

    
62
    private static final List<String> PKEY_DEPTH2_INIT_STRATEGY = Arrays.asList(new String[] {
63
    		"root"});
64

    
65
    private static final List<String> PKEY_DEPTH3_INIT_STRATEGY = Arrays.asList(new String[] {
66
    		"root.statement"});
67

    
68

    
69
    @BeforeClass
70
    public static void initializePolytomousKeyTest() {
71

    
72
    }
73

    
74

    
75

    
76

    
77
    @Test
78
    public void recursiveLoadSubGraphDepth1Test() {
79

    
80
    	// this call will load into the session cache the graph
81
    	// polytomous key
82
    	// 	|- root : polytomous key node
83
    	// in a recursive call
84
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
85

    
86
        // checking to make sure the root object is in the session cache
87
        Assert.assertSame(CdmBase.deproxy(pkey1.getRoot(),PolytomousKeyNode.class), cacher.getFromCache(pkey1.getRoot()));
88

    
89
    }
90

    
91

    
92
    @Test
93
    public void recursiveLoadSubGraphDepth2Test() {
94

    
95
    	// this call will load into the session cache the graph
96
    	// polytomous key
97
    	// 	|- root : polytomous key node
98
    	//		|- question : KeyStatement
99
    	//		|- statement : KeyStatement
100
    	// in a recursive call
101
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
102
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
103
        // checking to make sure the root object is in the session cache
104
        Assert.assertSame(CdmBase.deproxy(pkey.getRoot(),PolytomousKeyNode.class), cacher.getFromCache(pkey.getRoot()));
105
        Assert.assertSame(CdmBase.deproxy(pkey.getRoot().getStatement(),KeyStatement.class), cacher.getFromCache(pkey.getRoot().getStatement()));
106
        Assert.assertSame(CdmBase.deproxy(pkey.getRoot().getQuestion(),KeyStatement.class), cacher.getFromCache(pkey.getRoot().getQuestion()));
107

    
108
    }
109

    
110
    /**
111
     * when : retrieving objects using recursive caching of object graphs with different depths
112
     * then : the objects in the sub-graph having the same persistence id should be the same
113
     */
114
    @Test
115
    public void lazyLoadRecursiveTest() {
116

    
117
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
118
    	// polytomous key
119
    	// 	|- root : polytomous key node
120
    	//		|- question : KeyStatement
121
    	//		|- statement : KeyStatement
122
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
123

    
124
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
125

    
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 differentSubGraphDepthTest1() {
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
    	// in a recursive call
140
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
141
        KeyStatement ks1 = CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class);
142

    
143
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
144

    
145
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
146
    	// polytomous key
147
    	// 	|- root : polytomous key node
148
    	//		|- question : KeyStatement
149
    	//		|- statement : KeyStatement
150
        PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH3_INIT_STRATEGY),PolytomousKey.class);
151

    
152
        KeyStatement ks2 = CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class);
153

    
154
        Assert.assertSame(ks2, cacher.getFromCache(ks2));
155
        Assert.assertSame(cacher.getFromCache(ks1), cacher.getFromCache(ks2));
156

    
157
    }
158

    
159
    /**
160
     * when : retrieving objects using recursive caching of object graphs with different depths
161
     * then : the objects in the sub-graph having the same persistence id should be the same
162
     */
163
    @Test
164
    public void differentSubGraphDepthTest2() {
165

    
166
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
167
    	// polytomous key
168
    	// 	|- root : polytomous key node
169
    	//		|- question : KeyStatement
170
    	//		|- statement : KeyStatement
171
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
172
        KeyStatement ks1 = pkey1.getRoot().getStatement();
173
        Assert.assertSame(ks1, pkey1.getRoot().getStatement());
174
        CdmTransientEntityCacher cacher = getCacher(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
        PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH1_INIT_STRATEGY),PolytomousKey.class);
180

    
181
        Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
182
        pkey2.getRoot().getStatement().getCreatedBy();
183
        Assert.assertSame(cacher.getFromCache(ks1), cacher.getFromCache(pkey2.getRoot().getStatement()));
184
    }
185

    
186
    /**
187
     * when : retrieving objects using (first) recursive load directly and (second) lazy loading in the same session
188
     * then : the objects in the sub-graph having the same persistence id should be the same
189
     */
190
    @Test
191
    public void recursiveLoadAndLazyLoadTest() {
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
    	//		|- question : KeyStatement
197
    	//		|- statement : KeyStatement
198
        PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
199

    
200

    
201
        CdmTransientEntityCacher cacher = getCacher(sessionOwner);
202

    
203
        // checking that the root is not null and
204
        // that it exists in the cache and
205
        // that both the original object and the
206
        // cached object are the same
207
        Assert.assertNotNull(CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class));
208
        Assert.assertNotNull(cacher.getFromCache(pkey1.getRoot().getStatement()));
209

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

    
215
        KeyStatement ks2 =  CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class);
216
        Assert.assertSame(ks2, cacher.getFromCache(pkey2.getRoot().getStatement()));
217

    
218
        Assert.assertSame(pkey1.getRoot().getStatement(), pkey2.getRoot().getStatement());
219
        Assert.assertSame(cacher.getFromCache(pkey1.getRoot().getStatement()), cacher.getFromCache(pkey2.getRoot().getStatement()));
220
    }
221

    
222
    /**
223
     * when : retrieving objects using (first) lazy loading  and (second) recursive load directly in the same session
224
     * then : the objects in the sub-graph having the same persistence id should be the same
225
     */
226
    @Test
227
    public void lazyLoadAndRecursiveLoadTest() {
228

    
229

    
230
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
231
    	// polytomous key
232
    	// 	|- root : polytomous key node
233

    
234
    	PolytomousKey pkey1 = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
235

    
236

    
237
        // lazy initialising root.statement
238
        KeyStatement st = CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class);
239
        st.getLabel();
240

    
241
        // checking that the root is not null and
242
        // that it exists in the cache and
243
        // that both the original object and the
244
        // cached object are the same
245
    	Assert.assertNotNull(pkey1.getRoot().getStatement());
246
    	Assert.assertSame(CdmBase.deproxy(pkey1.getRoot().getStatement(), KeyStatement.class),
247
    			cacher.getFromCache(pkey1.getRoot().getStatement(), KeyStatement.class));
248

    
249
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
250
    	// polytomous key
251
    	// 	|- root : polytomous key node
252
    	//		|- question : KeyStatement
253
    	//		|- statement : KeyStatement
254

    
255
    	PolytomousKey pkey2 = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
256

    
257

    
258
    	Assert.assertSame(CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
259
    	Assert.assertSame(st, CdmBase.deproxy(pkey2.getRoot().getStatement(), KeyStatement.class));
260
    	Assert.assertSame(cacher.getFromCache(st), cacher.getFromCache(pkey2.getRoot().getStatement(), KeyStatement.class));
261
    }
262

    
263

    
264

    
265

    
266

    
267
    /**
268
     * when : loading an object (first) and then (second) loading a graph the object is contained in, in the same session
269
     * then : the object should be the same
270
     */
271
    @Test
272
    public void subGraphObjectLoadTest1() {
273

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

    
277

    
278
    	Assert.assertNotNull(rootPKNode);
279
    	Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
280

    
281
    	PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
282

    
283

    
284
    	Assert.assertNotNull(childOfRootPKNode);
285
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
286

    
287
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
288
    	// polytomous key
289
    	// 	|- root : polytomous key node
290
    	//		|- question : KeyStatement
291
    	//		|- statement : KeyStatement
292

    
293
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
294

    
295

    
296
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
297
    	Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
298
    }
299

    
300
    /**
301
     * when : loading a graph (first) and then (second) loading an object contained in in the graph, in the same session
302
     * then : the object should be the same
303
     */
304
    @Test
305
    public void subGraphObjectLoadTest2() {
306

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

    
313
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
314

    
315

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

    
319

    
320
    	Assert.assertNotNull(rootPKNode);
321
    	Assert.assertSame(rootPKNode, cacher.getFromCache(rootPKNode));
322

    
323
    	PolytomousKeyNode childOfRootPKNode = CdmBase.deproxy(polytomousKeyNodeService.find(polytomousKeyNodeUuid2),PolytomousKeyNode.class);
324

    
325

    
326
    	Assert.assertNotNull(childOfRootPKNode);
327
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
328

    
329
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
330
    	Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
331
    }
332

    
333

    
334
    /**
335
     * when : loading objects from a collection
336
     * then : the object stored in the cache should be the same
337
     */
338
    @Test
339
    public void subGraphCollectionLoadTest() {
340

    
341
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
342
    	// polytomous key
343
    	// 	|- root : polytomous key node
344
    	//		|- question : KeyStatement
345
    	//		|- statement : KeyStatement
346

    
347
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH2_INIT_STRATEGY),PolytomousKey.class);
348

    
349

    
350
    	PolytomousKeyNode childOfRootPKNode = pkey.getRoot().getChildAt(1);
351

    
352
    	Assert.assertSame(childOfRootPKNode, cacher.getFromCache(childOfRootPKNode));
353
    	Assert.assertSame(pkey.getRoot().getChildAt(1), childOfRootPKNode);
354

    
355
    }
356

    
357
    /**
358
     * when : loading a non-lazy collection in a subgraph and loading the collection directly
359
     * then : the object stored in the cache should be the same as the object in the sub-graph collection and
360
     *        the object in the directly loaded collection
361
     */
362
    @Test
363
    public void nonLazyCollectionLoadTest() {
364
    	// need to find an example of this
365
    }
366

    
367
    /**
368
     * when : loading objects from a map
369
     * then : the object stored in the cache should be the same
370
     */
371
    @Test
372
    public void subGraphMapLoadTest() {
373

    
374
    	Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
375
        Language hindi = Language.getLanguageFromUuid(UUID.fromString("0a1d9d1d-135d-4575-b172-669b51673c39"));
376

    
377

    
378
    	// this call will load into the session cache the graph and update the objects in the sub-graph for a
379
    	// polytomous key
380
    	// 	|- root : polytomous key node
381
    	//		|- question : KeyStatement
382
    	//		|- statement : KeyStatement
383

    
384
    	PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.load(polytomousKeyUuid, PKEY_DEPTH3_INIT_STRATEGY),PolytomousKey.class);
385

    
386

    
387
    	Map<Language, LanguageString> labelMap = pkey.getRoot().getStatement().getLabel();
388
    	Set<Language> languages = labelMap.keySet();
389
    	Iterator<Language> languagesItr = languages.iterator();
390
    	while(languagesItr.hasNext()) {
391
    		Language lang = languagesItr.next();
392
    		if(lang.equals(english)) {
393
    			Assert.assertSame(lang, english);
394
    		}
395
    		if(lang.equals(hindi)) {
396
    			Assert.assertSame(lang, hindi);
397
    		}
398
    		Assert.assertSame(lang, cacher.getFromCache(lang));
399
    	}
400
    }
401

    
402
    /**
403
     * when : loading a non-lazy map in a subgraph and loading the map directly
404
     * then : the object stored in the cache should be the same as the object in the sub-graph map and
405
     *        the object in the directly loaded map
406
     */
407
    @Test
408
    public void nonLazyMapLoadTest() {
409
    	// need to find an example of this
410
    }
411

    
412

    
413

    
414
}
    (1-1/1)