Project

General

Profile

Download (15.4 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.taxeditor.session;
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.hibernate.collection.spi.PersistentCollection;
21
import org.junit.Assert;
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.agent.AgentBase;
31
import eu.etaxonomy.cdm.model.common.CdmBase;
32
import eu.etaxonomy.cdm.model.common.Credit;
33
import eu.etaxonomy.cdm.model.common.Extension;
34
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
35
import eu.etaxonomy.cdm.model.common.Language;
36
import eu.etaxonomy.cdm.model.common.LanguageString;
37
import eu.etaxonomy.cdm.model.description.KeyStatement;
38
import eu.etaxonomy.cdm.model.description.PolytomousKey;
39
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
40
import eu.etaxonomy.cdm.model.media.Rights;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
43
import eu.etaxonomy.taxeditor.remoting.cache.EntityCacherDebugResult;
44
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
45

    
46
/**
47
 * @author cmathew
48
 * @date 7 Oct 2014
49
 *
50
 */
51
@DataSet
52
public class CdmEntitySessionAwareTest extends RemotingSessionAwareTest {
53

    
54
    private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.class);
55

    
56
    UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
57
    UUID subKeyUuid = UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01");
58
    //UUID polytomousKeyUuid = UUID.fromString("bab66772-2c83-428a-bb6d-655d12ac6097");
59
    UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
60

    
61

    
62
    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
63
    IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
64
    ICommonService commonService = getRemoteApplicationController().getCommonService();
65
    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
66

    
67
    Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
68

    
69

    
70

    
71
    @BeforeClass
72
    public static void initializePolytomousKeyTest() {
73
    }
74

    
75

    
76
    @Test
77
    public void readAllPolytomousKeys() {
78
        List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
79
        Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
80
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
81
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
82
    }
83

    
84

    
85
    @Test
86
    //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
87
    public void readPolytmousKeyData() {
88
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
89
        Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
90
        Iterator<Taxon> tsItr = taxonomicScope.iterator();
91
        Taxon taxon = tsItr.next();
92

    
93
        Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
94
        Assert.assertEquals(tsItr.hasNext(), false);
95

    
96
        List<Credit> credits = pKey.getCredits();
97
        AgentBase agent = credits.get(0).getAgent();
98
        Assert.assertEquals(agent.getId(),4809);
99
        Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
100
        Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
101

    
102
        Set<Extension> exts = pKey.getExtensions();
103
        Iterator<Extension> extItr = exts.iterator();
104
        Extension ext = extItr.next();
105
        Assert.assertEquals(ext.getValue(), "http://test.com");
106

    
107
        Set<Rights> rights = pKey.getRights();
108
        Iterator<Rights> rightsItr = rights.iterator();
109
        Rights right = rightsItr.next();
110
        Assert.assertEquals(right.getText(),"Rights Text Test");
111

    
112
        Set<IdentifiableSource> sources = pKey.getSources();
113
        Iterator<IdentifiableSource> sourcesItr = sources.iterator();
114
        IdentifiableSource source = sourcesItr.next();
115
        Assert.assertEquals(source.getId(), 23710);
116
        source = sourcesItr.next();
117
        Assert.assertEquals(source.getId(), 23711);
118

    
119
        // TO DO : Added tests for Annotations , Markers
120
    }
121

    
122
    @Test
123
    public void readPolytomousKeyDataFromNodes() {
124
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
125
        PolytomousKeyNode rootNode = pKey.getRoot();
126

    
127

    
128
        Assert.assertEquals(rootNode.getId(), 2750);
129

    
130
        Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
131
        Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
132
        Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
133
        Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
134
        Assert.assertEquals(sortIndex, new Integer(0));
135
        String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
136
        Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(english), statement);
137

    
138
        Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
139
        Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
140
        Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
141
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
142
        Assert.assertEquals(sortIndex, new Integer(1));
143
        statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
144
        Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(english), statement);
145

    
146
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
147
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
148
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
149
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
150
        Assert.assertEquals(sortIndex, new Integer(0));
151
        statement = "Ray-florets yellow";
152
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(english), statement);
153
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
154
    }
155

    
156
    @Test
157
    public void savePolytomousKeyNodeData() {
158
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
159

    
160
        PolytomousKeyNode pkeynode = pkey.getRoot();
161
        String newQuestion = "New Question";
162
        String newStatement = "New Statement";
163

    
164
        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(english).getText());
165
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
166

    
167
            node.setQuestion(null);
168
            node.setStatement(null);
169
        }
170
        //FIXME:Remoting Add tests for feature after fixing problem
171

    
172
        //Feature feature = pkeynode.getFeature();
173
        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
174
        //pkeynode.setFeature(null);
175

    
176
        Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(english).getText(),"Modifying Text 1a");
177
        String modifyingText = "Modifying Text 1a updated";
178

    
179
        //pkeynode.getChildAt(0).putModifyingText(english, modifyingText);
180

    
181
        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
182
        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
183
        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
184
        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
185
        pkeynode.getChildAt(1).setTaxon(taxon);
186

    
187
        EntityCacherDebugResult debugResult = new EntityCacherDebugResult(cacher,  Arrays.asList(pkey));
188

    
189
        polytomousKeyService.merge(pkey);
190

    
191
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
192
        sessionOwner = new MockSessionOwner();
193

    
194

    
195
        pkeynode = pkey.getRoot();
196
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
197
            Assert.assertNull(node.getQuestion());
198
            node.setQuestion(KeyStatement.NewInstance(english,newQuestion));
199
            Assert.assertNull(node.getStatement());
200
            node.setStatement(KeyStatement.NewInstance(english,newStatement));
201
        }
202

    
203

    
204
        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
205

    
206
        polytomousKeyService.merge(pkey);
207

    
208
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
209
        sessionOwner = new MockSessionOwner();
210

    
211

    
212
        pkeynode = pkey.getRoot();
213
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
214
            Assert.assertNotNull(node.getQuestion());
215
            Map<Language, LanguageString> label = node.getQuestion().getLabel();
216
            Assert.assertEquals(newQuestion, label.get(english).getText());
217
            Assert.assertNotNull(node.getStatement());
218
            Assert.assertEquals(newStatement, node.getStatement().getLabel(english).getText());
219
        }
220
        //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
221

    
222
    }
223

    
224
    @Test
225
    public void savePolytomousKeyNodeDataWithSameSubKey() {
226

    
227
        ICdmEntitySessionEnabled sessionOwner = new MockSessionOwner();
228
        cdmEntitySessionManager.bind(sessionOwner);
229
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
230

    
231

    
232
        PolytomousKeyNode pkeynode = pkey.getRoot();
233

    
234
        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
235
        String subkey1title = subkey1.getTitleCache();
236
        subkey1.setTitleCache(subkey1title + "test", true);
237

    
238

    
239
        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
240
        String subkey2title = subkey2.getTitleCache();
241
        subkey2.setTitleCache(subkey2title + "test", true);
242

    
243
        Assert.assertSame(subkey1, subkey2);
244

    
245
        polytomousKeyService.merge(pkey);
246
    }
247

    
248
    @Test
249
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
250

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

    
253

    
254
        PolytomousKeyNode pkeynode = pkey.getRoot();
255

    
256
        PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
257
        PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 0);
258
        PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
259
        String subkey1title = subkey1.getTitleCache();
260
        subkey1.setTitleCache(subkey1title + "test", true);
261

    
262
        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 1);
263
        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
264
        String subkey2title = subkey2.getTitleCache();
265
        subkey2.setTitleCache(subkey2title + "test", true);
266

    
267
        Assert.assertNotSame(childNode0, childNode1);
268

    
269
        Assert.assertSame(subkey1, subkey2);
270

    
271
        polytomousKeyService.merge(pkey);
272
    }
273

    
274

    
275
    @Test
276
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
277
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
278
        PolytomousKeyNode pkeynode = pkey.getRoot();
279

    
280
        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
281
        label1.size();
282

    
283

    
284
        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
285
        label2.size();
286

    
287

    
288
        polytomousKeyService.merge(pkey);
289
    }
290

    
291
    @Test
292
    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
293
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
294

    
295

    
296
        PolytomousKeyNode rootNode = pKey.getRoot();
297
        PolytomousKeyNode child = rootNode.getChildAt(0);
298
        PolytomousKey subKey = child.getSubkey();
299

    
300
        polytomousKeyService.delete(subKey);
301

    
302
        // since subKey was not initialized before the delete, an attempt
303
        // to initialize it after will lead to an NPE
304
        try {
305
            PolytomousKeyNode subKeyRootNode = subKey.getRoot();
306
            Assert.fail("A NullPointerException indicating null root node should be thrown here");
307
        } catch(NullPointerException npe) {
308

    
309
        }
310
        // retrieving subkey shows its null
311
        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
312
        Assert.assertNull(subKey);
313

    
314
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
315

    
316

    
317
        rootNode = pKey.getRoot();
318
        child = rootNode.getChildAt(0);
319
        subKey = child.getSubkey();
320
        // subkey will not be null, because the delete functionality of the subkey
321
        // does not currently delete it from a polytomous key node
322
        Assert.assertNotNull(subKey);
323
        // after reloading the object graph, accessing subKey should throw a null pointer exception
324
        try {
325
            PolytomousKeyNode subKeyRootNode = subKey.getRoot();
326
            Assert.fail("A NullPointerException indicating null root node should be thrown here");
327
        } catch(NullPointerException npe) {
328

    
329
        }
330

    
331
    }
332

    
333
    @Test
334
    public void deleteSubKeyInPolytomousNode() {
335
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
336

    
337

    
338
        PolytomousKeyNode rootNode = pKey.getRoot();
339
        List<PolytomousKeyNode> children = rootNode.getChildren();
340
        PolytomousKeyNode child = rootNode.getChildAt(0);
341
        polytomousKeyNodeService.delete(child.getUuid(), true);
342

    
343
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
344

    
345
        rootNode = pKey.getRoot();
346
        children = rootNode.getChildren();
347
        Assert.assertFalse(children.contains(child));
348
    }
349

    
350
    @Test
351
    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
352
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
353

    
354

    
355
        PolytomousKeyNode rootNode = pKey.getRoot();
356
        PolytomousKeyNode child = rootNode.getChildAt(0);
357
        PolytomousKey subKey = child.getSubkey();
358
        // this call initializes the subkey
359
        subKey.getRoot();
360

    
361
        polytomousKeyService.delete(subKey);
362

    
363
        // since subKey was initialized before the delete, it will be
364
        // accesible even after the delete. This behaviour is similar
365
        // to hibernate session delete where the deleted object is
366
        // made transient, but not deleted from the object graph
367

    
368
        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
369
        Assert.assertNotNull(subKey);
370

    
371
    }
372

    
373

    
374
}
(1-1/3)