Project

General

Profile

Download (18.6 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.ArrayList;
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.Ignore;
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.api.service.ITermService;
32
import eu.etaxonomy.cdm.api.service.IUserService;
33
import eu.etaxonomy.cdm.api.service.IVocabularyService;
34
import eu.etaxonomy.cdm.model.agent.AgentBase;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.common.Credit;
37
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
38
import eu.etaxonomy.cdm.model.common.Extension;
39
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
40
import eu.etaxonomy.cdm.model.common.Language;
41
import eu.etaxonomy.cdm.model.common.LanguageString;
42
import eu.etaxonomy.cdm.model.common.TermType;
43
import eu.etaxonomy.cdm.model.common.TermVocabulary;
44
import eu.etaxonomy.cdm.model.description.KeyStatement;
45
import eu.etaxonomy.cdm.model.description.PolytomousKey;
46
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
47
import eu.etaxonomy.cdm.model.media.Rights;
48
import eu.etaxonomy.cdm.model.taxon.Taxon;
49
import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
50

    
51
/**
52
 * @author cmathew
53
 * @date 7 Oct 2014
54
 *
55
 */
56
@DataSet
57
public class CdmEntitySessionAwareTest extends RemotingSessionAwareTest {
58

    
59
    private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.class);
60

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

    
66

    
67
    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
68
    IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
69
    ICommonService commonService = getRemoteApplicationController().getCommonService();
70
    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
71
    IVocabularyService vocabularyService = getRemoteApplicationController().getVocabularyService();
72
    ITermService termService = getRemoteApplicationController().getTermService();
73
    IUserService userService = getRemoteApplicationController().getUserService();
74

    
75
    //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
76

    
77

    
78

    
79
    @BeforeClass
80
    public static void initializePolytomousKeyTest() {
81
    }
82

    
83

    
84
    @Test
85
    public void readAllPolytomousKeys() {
86
        List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
87
        Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
88
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
89
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
90
    }
91

    
92

    
93
    @Test
94
    //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
95
    public void readPolytmousKeyData() {
96
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
97
        Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
98
        Iterator<Taxon> tsItr = taxonomicScope.iterator();
99
        Taxon taxon = tsItr.next();
100

    
101
        Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
102
        Assert.assertEquals(tsItr.hasNext(), false);
103

    
104
        List<Credit> credits = pKey.getCredits();
105
        AgentBase agent = credits.get(0).getAgent();
106
        Assert.assertEquals(agent.getId(),4809);
107
        Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
108
        Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
109

    
110
        Set<Extension> exts = pKey.getExtensions();
111
        Iterator<Extension> extItr = exts.iterator();
112
        Extension ext = extItr.next();
113
        Assert.assertEquals(ext.getValue(), "http://test.com");
114

    
115
        Set<Rights> rights = pKey.getRights();
116
        Iterator<Rights> rightsItr = rights.iterator();
117
        Rights right = rightsItr.next();
118
        Assert.assertEquals(right.getText(),"Rights Text Test");
119

    
120
        Set<IdentifiableSource> sources = pKey.getSources();
121
        Iterator<IdentifiableSource> sourcesItr = sources.iterator();
122
        IdentifiableSource source = sourcesItr.next();
123
        Assert.assertEquals(source.getId(), 23710);
124
        source = sourcesItr.next();
125
        Assert.assertEquals(source.getId(), 23711);
126

    
127
        // TO DO : Added tests for Annotations , Markers
128
    }
129

    
130
    @Test
131
    public void readPolytomousKeyDataFromNodes() {
132
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
133
        PolytomousKeyNode rootNode = pKey.getRoot();
134

    
135

    
136
        Assert.assertEquals(rootNode.getId(), 2750);
137

    
138
        Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
139
        Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
140
        Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
141
        Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
142
        Assert.assertEquals(sortIndex, new Integer(0));
143
        String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
144
        Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);
145

    
146
        Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
147
        Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
148
        Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
149
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
150
        Assert.assertEquals(sortIndex, new Integer(1));
151
        statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
152
        Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(Language.ENGLISH()), statement);
153

    
154
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
155
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
156
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
157
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
158
        Assert.assertEquals(sortIndex, new Integer(0));
159
        statement = "Ray-florets yellow";
160
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);
161
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
162
    }
163

    
164

    
165
    @Test
166
    public void savePolytomousKeyNodeData() {
167
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
168

    
169
        PolytomousKeyNode pkeynode = pkey.getRoot();
170
        String newQuestion = "New Question";
171
        String newStatement = "New Statement";
172

    
173
        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());
174
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
175

    
176
            node.setQuestion(null);
177
            node.setStatement(null);
178
        }
179
        //FIXME:Remoting Add tests for feature after fixing problem
180

    
181
        //Feature feature = pkeynode.getFeature();
182
        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
183
        //pkeynode.setFeature(null);
184

    
185
        Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(Language.ENGLISH()).getText(),"Modifying Text 1a");
186
        String modifyingText = "Modifying Text 1a updated";
187

    
188
        //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
189

    
190
        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
191
        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
192
        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
193
        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
194
        pkeynode.getChildAt(1).setTaxon(taxon);
195

    
196
        polytomousKeyService.merge(pkey);
197

    
198
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
199

    
200

    
201
        pkeynode = pkey.getRoot();
202
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
203
            Assert.assertNull(node.getQuestion());
204
            node.setQuestion(KeyStatement.NewInstance(Language.ENGLISH(),newQuestion));
205
            Assert.assertNull(node.getStatement());
206
            node.setStatement(KeyStatement.NewInstance(Language.ENGLISH(),newStatement));
207
        }
208

    
209
        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
210

    
211
        polytomousKeyService.merge(pkey);
212

    
213
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
214

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

    
225
    }
226

    
227

    
228
    @Test
229
    public void savePolytomousKeyNodeDataWithSameSubKey() {
230

    
231
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
232

    
233

    
234
        PolytomousKeyNode pkeynode = pkey.getRoot();
235

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

    
240

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

    
245
        Assert.assertSame(subkey1, subkey2);
246

    
247
        polytomousKeyService.merge(pkey);
248
    }
249

    
250
    @Test
251
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
252

    
253
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
254

    
255

    
256
        PolytomousKeyNode pkeynode = pkey.getRoot();
257

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

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

    
269
        Assert.assertNotSame(childNode0, childNode1);
270

    
271
        Assert.assertSame(subkey1, subkey2);
272

    
273
        polytomousKeyService.merge(pkey);
274
    }
275

    
276

    
277

    
278
    @Test
279
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
280

    
281
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
282
        PolytomousKeyNode pkeynode = pkey.getRoot();
283

    
284
        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
285
        label1.size();
286

    
287

    
288
        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
289
        label2.size();
290

    
291

    
292
        polytomousKeyService.merge(pkey);
293
    }
294

    
295
    @Test
296
    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
297
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
298

    
299

    
300
        PolytomousKeyNode rootNode = pKey.getRoot();
301
        PolytomousKeyNode child = rootNode.getChildAt(0);
302
        PolytomousKey subKey = child.getSubkey();
303

    
304
        polytomousKeyService.delete(subKey);
305

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

    
313
        }
314
        // retrieving subkey shows its null
315
        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
316
        Assert.assertNull(subKey);
317

    
318
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
319

    
320

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

    
333
        }
334

    
335
    }
336

    
337

    
338
    @Test
339
    public void deleteSubKeyInPolytomousNode() {
340
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
341

    
342

    
343
        PolytomousKeyNode rootNode = pKey.getRoot();
344
        List<PolytomousKeyNode> children = rootNode.getChildren();
345
        PolytomousKeyNode child = rootNode.getChildAt(0);
346
        polytomousKeyNodeService.delete(child.getUuid(), true);
347

    
348
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
349

    
350
        rootNode = pKey.getRoot();
351
        children = rootNode.getChildren();
352
        Assert.assertFalse(children.contains(child));
353
    }
354

    
355
    @Test
356
    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
357
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
358

    
359

    
360
        PolytomousKeyNode rootNode = pKey.getRoot();
361
        PolytomousKeyNode child = rootNode.getChildAt(0);
362
        PolytomousKey subKey = child.getSubkey();
363
        // this call initializes the subkey
364
        subKey.getRoot();
365

    
366
        polytomousKeyService.delete(subKey);
367

    
368
        // since subKey was initialized before the delete, it will be
369
        // accesible even after the delete. This behaviour is similar
370
        // to hibernate session delete where the deleted object is
371
        // made transient, but not deleted from the object graph
372

    
373
        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
374
        Assert.assertNotNull(subKey);
375
    }
376

    
377
    @Test
378
    public void saveNewTermVocabulary() {
379
        TermVocabulary termVocabulary =
380
                TermVocabulary.NewInstance(TermType.Feature,
381
                        null,
382
                        "Untitled",
383
                        null,
384
                        null);
385
        try {
386
            List<TermVocabulary<DefinedTermBase>> vocabularies = vocabularyService.findByTermType(TermType.Feature);
387

    
388
            for(TermVocabulary vocab : vocabularies) {
389
                vocab.getTermsOrderedByLabels(Language.ENGLISH());
390
            }
391

    
392
            termVocabulary = vocabularyService.merge(termVocabulary);
393
            vocabularies.add(termVocabulary);
394
            termVocabulary.setLabel("Test");
395
            vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));
396
        } finally {
397
            vocabularyService.delete(termVocabulary);
398
        }
399
    }
400

    
401
    @Ignore // should be enabled once resolution of #5066 is merged into cdmlib main branch
402
    @Test
403
    public void saveNewTerm() {
404
        UUID vocNameFeatureUuid = UUID.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");
405
        UUID vocFeatureUuid = UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
406
        DefinedTermBase newTerm = TermType.Feature.getEmptyDefinedTermBase();
407
        newTerm.setLabel("CreateTest");
408

    
409
        try {
410

    
411
            Assert.assertNotNull(newTerm);
412
            TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);
413
            TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);
414

    
415
            List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();
416

    
417
            vocs.add(vocNameFeature);
418
            vocs.add(vocFeature);
419

    
420
            vocNameFeature.addTerm(newTerm);
421

    
422
            vocs = vocabularyService.merge(vocs);
423
            for(TermVocabulary voc : vocs) {
424
                if(voc.getUuid().equals(vocNameFeatureUuid)) {
425
                    vocNameFeature = voc;
426
                }
427
            }
428

    
429
            Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));
430

    
431
            for(Object obj : vocNameFeature.getTerms()) {
432
                DefinedTermBase dtb = (DefinedTermBase)obj;
433
                if("CreateTest".equals(dtb.getLabel())) {
434
                    newTerm = dtb;
435
                    Assert.assertNotNull(dtb.getCreatedBy());
436
                    Assert.assertNotNull(dtb.getCreated());
437
                } else {
438
                    Assert.assertNull(dtb.getCreatedBy());
439
                }
440
            }
441
            newTerm.setLabel("UpdateTest");
442
            newTerm = termService.merge(newTerm);
443
            Assert.assertNotNull(newTerm.getUpdatedBy());
444
            Assert.assertNotNull(newTerm.getUpdated());
445

    
446
            Assert.assertNull(vocNameFeature.getCreatedBy());
447
            Assert.assertNull(vocFeature.getCreatedBy());
448
        } finally {
449
            if(termService.find(newTerm.getUuid()) != null) {
450
                termService.delete(newTerm.getUuid());
451
            }
452
        }
453
    }
454
}
(1-1/3)