Project

General

Profile

Download (19.1 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

    
131

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

    
137

    
138
        Assert.assertEquals(rootNode.getId(), 2750);
139

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

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

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

    
166
    @Test
167
    public void addGrandChildPolytomousKeyNode() {
168
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
169
        PolytomousKeyNode rootNode = pKey.getRoot();
170
        PolytomousKeyNode childNode = PolytomousKeyNode.NewInstance();
171
        rootNode.addChild(childNode);
172

    
173
        polytomousKeyService.merge(pKey);
174

    
175
        PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();
176
        childNode.addChild(grandChildNode);
177

    
178
        polytomousKeyService.merge(pKey);
179
    }
180

    
181
    @Test
182
    public void savePolytomousKeyNodeData() {
183
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
184

    
185
        PolytomousKeyNode pkeynode = pkey.getRoot();
186
        String newQuestion = "New Question";
187
        String newStatement = "New Statement";
188

    
189
        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());
190
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
191

    
192
            node.setQuestion(null);
193
            node.setStatement(null);
194
        }
195
        //FIXME:Remoting Add tests for feature after fixing problem
196

    
197
        //Feature feature = pkeynode.getFeature();
198
        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
199
        //pkeynode.setFeature(null);
200

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

    
204
        //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
205

    
206
        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
207
        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
208
        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
209
        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
210
        pkeynode.getChildAt(1).setTaxon(taxon);
211

    
212
        polytomousKeyService.merge(pkey);
213

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

    
216

    
217
        pkeynode = pkey.getRoot();
218
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
219
            Assert.assertNull(node.getQuestion());
220
            node.setQuestion(KeyStatement.NewInstance(Language.ENGLISH(),newQuestion));
221
            Assert.assertNull(node.getStatement());
222
            node.setStatement(KeyStatement.NewInstance(Language.ENGLISH(),newStatement));
223
        }
224

    
225
        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
226

    
227
        polytomousKeyService.merge(pkey);
228

    
229
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
230

    
231
        pkeynode = pkey.getRoot();
232
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
233
            Assert.assertNotNull(node.getQuestion());
234
            Map<Language, LanguageString> label = node.getQuestion().getLabel();
235
            Assert.assertEquals(newQuestion, label.get(Language.ENGLISH()).getText());
236
            Assert.assertNotNull(node.getStatement());
237
            Assert.assertEquals(newStatement, node.getStatement().getLabel(Language.ENGLISH()).getText());
238
        }
239
        //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
240

    
241
    }
242

    
243

    
244
    @Test
245
    public void savePolytomousKeyNodeDataWithSameSubKey() {
246

    
247
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
248

    
249

    
250
        PolytomousKeyNode pkeynode = pkey.getRoot();
251

    
252
        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
253
        String subkey1title = subkey1.getTitleCache();
254
        subkey1.setTitleCache(subkey1title + "test", true);
255

    
256

    
257
        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
258
        String subkey2title = subkey2.getTitleCache();
259
        subkey2.setTitleCache(subkey2title + "test", true);
260

    
261
        Assert.assertSame(subkey1, subkey2);
262

    
263
        polytomousKeyService.merge(pkey);
264
    }
265

    
266
    @Test
267
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
268

    
269
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
270

    
271

    
272
        PolytomousKeyNode pkeynode = pkey.getRoot();
273

    
274
        PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
275
        PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 0);
276
        PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
277
        String subkey1title = subkey1.getTitleCache();
278
        subkey1.setTitleCache(subkey1title + "test", true);
279

    
280
        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 1);
281
        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
282
        String subkey2title = subkey2.getTitleCache();
283
        subkey2.setTitleCache(subkey2title + "test", true);
284

    
285
        Assert.assertNotSame(childNode0, childNode1);
286

    
287
        Assert.assertSame(subkey1, subkey2);
288

    
289
        polytomousKeyService.merge(pkey);
290
    }
291

    
292

    
293

    
294
    @Test
295
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
296

    
297
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
298
        PolytomousKeyNode pkeynode = pkey.getRoot();
299

    
300
        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
301
        label1.size();
302

    
303

    
304
        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
305
        label2.size();
306

    
307

    
308
        polytomousKeyService.merge(pkey);
309
    }
310

    
311
    @Test
312
    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
313
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
314

    
315

    
316
        PolytomousKeyNode rootNode = pKey.getRoot();
317
        PolytomousKeyNode child = rootNode.getChildAt(0);
318
        PolytomousKey subKey = child.getSubkey();
319

    
320
        polytomousKeyService.delete(subKey);
321

    
322
        // since subKey was not initialized before the delete, an attempt
323
        // to initialize it after will lead to an NPE
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
        // retrieving subkey shows its null
331
        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
332
        Assert.assertNull(subKey);
333

    
334
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
335

    
336

    
337
        rootNode = pKey.getRoot();
338
        child = rootNode.getChildAt(0);
339
        subKey = child.getSubkey();
340
        // subkey will not be null, because the delete functionality of the subkey
341
        // does not currently delete it from a polytomous key node
342
        Assert.assertNotNull(subKey);
343
        // after reloading the object graph, accessing subKey should throw a null pointer exception
344
        try {
345
            PolytomousKeyNode subKeyRootNode = subKey.getRoot();
346
            Assert.fail("A NullPointerException indicating null root node should be thrown here");
347
        } catch(NullPointerException npe) {
348

    
349
        }
350

    
351
    }
352

    
353

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

    
358

    
359
        PolytomousKeyNode rootNode = pKey.getRoot();
360
        List<PolytomousKeyNode> children = rootNode.getChildren();
361
        PolytomousKeyNode child = rootNode.getChildAt(0);
362
        polytomousKeyNodeService.delete(child.getUuid(), true);
363

    
364
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
365

    
366
        rootNode = pKey.getRoot();
367
        children = rootNode.getChildren();
368
        Assert.assertFalse(children.contains(child));
369
    }
370

    
371
    @Test
372
    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
373
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
374

    
375

    
376
        PolytomousKeyNode rootNode = pKey.getRoot();
377
        PolytomousKeyNode child = rootNode.getChildAt(0);
378
        PolytomousKey subKey = child.getSubkey();
379
        // this call initializes the subkey
380
        subKey.getRoot();
381

    
382
        polytomousKeyService.delete(subKey);
383

    
384
        // since subKey was initialized before the delete, it will be
385
        // accesible even after the delete. This behaviour is similar
386
        // to hibernate session delete where the deleted object is
387
        // made transient, but not deleted from the object graph
388

    
389
        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
390
        Assert.assertNotNull(subKey);
391
    }
392

    
393
    @Test
394
    public void saveNewTermVocabulary() {
395
        TermVocabulary termVocabulary =
396
                TermVocabulary.NewInstance(TermType.Feature,
397
                        null,
398
                        "Untitled",
399
                        null,
400
                        null);
401
        try {
402
            List<TermVocabulary<DefinedTermBase>> vocabularies = vocabularyService.findByTermType(TermType.Feature);
403

    
404
            for(TermVocabulary vocab : vocabularies) {
405
                vocab.getTermsOrderedByLabels(Language.ENGLISH());
406
            }
407

    
408
            termVocabulary = vocabularyService.merge(termVocabulary);
409
            vocabularies.add(termVocabulary);
410
            termVocabulary.setLabel("Test");
411
            vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));
412
        } finally {
413
            vocabularyService.delete(termVocabulary);
414
        }
415
    }
416

    
417
    @Ignore // should be enabled once resolution of #5066 is merged into cdmlib main branch
418
    @Test
419
    public void saveNewTerm() {
420
        UUID vocNameFeatureUuid = UUID.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");
421
        UUID vocFeatureUuid = UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
422
        DefinedTermBase newTerm = TermType.Feature.getEmptyDefinedTermBase();
423
        newTerm.setLabel("CreateTest");
424

    
425
        try {
426

    
427
            Assert.assertNotNull(newTerm);
428
            TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);
429
            TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);
430

    
431
            List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();
432

    
433
            vocs.add(vocNameFeature);
434
            vocs.add(vocFeature);
435

    
436
            vocNameFeature.addTerm(newTerm);
437

    
438
            vocs = vocabularyService.merge(vocs);
439
            for(TermVocabulary voc : vocs) {
440
                if(voc.getUuid().equals(vocNameFeatureUuid)) {
441
                    vocNameFeature = voc;
442
                }
443
            }
444

    
445
            Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));
446

    
447
            for(Object obj : vocNameFeature.getTerms()) {
448
                DefinedTermBase dtb = (DefinedTermBase)obj;
449
                if("CreateTest".equals(dtb.getLabel())) {
450
                    newTerm = dtb;
451
                    Assert.assertNotNull(dtb.getCreatedBy());
452
                    Assert.assertNotNull(dtb.getCreated());
453
                } else {
454
                    Assert.assertNull(dtb.getCreatedBy());
455
                }
456
            }
457
            newTerm.setLabel("UpdateTest");
458
            newTerm = termService.merge(newTerm);
459
            Assert.assertNotNull(newTerm.getUpdatedBy());
460
            Assert.assertNotNull(newTerm.getUpdated());
461

    
462
            Assert.assertNull(vocNameFeature.getCreatedBy());
463
            Assert.assertNull(vocFeature.getCreatedBy());
464
        } finally {
465
            if(termService.find(newTerm.getUuid()) != null) {
466
                termService.delete(newTerm.getUuid());
467
            }
468
        }
469
    }
470
}
(1-1/3)