Project

General

Profile

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

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

    
57
    private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.class);
58

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

    
64

    
65
    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
66
    IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
67
    ICommonService commonService = getRemoteApplicationController().getCommonService();
68
    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
69
    IVocabularyService vocabularyService = getRemoteApplicationController().getVocabularyService();
70
    ITermService termService = getRemoteApplicationController().getTermService();
71
    //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
72

    
73

    
74

    
75
    @BeforeClass
76
    public static void initializePolytomousKeyTest() {
77
    }
78

    
79

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

    
88

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

    
97
        Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
98
        Assert.assertEquals(tsItr.hasNext(), false);
99

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

    
106
        Set<Extension> exts = pKey.getExtensions();
107
        Iterator<Extension> extItr = exts.iterator();
108
        Extension ext = extItr.next();
109
        Assert.assertEquals(ext.getValue(), "http://test.com");
110

    
111
        Set<Rights> rights = pKey.getRights();
112
        Iterator<Rights> rightsItr = rights.iterator();
113
        Rights right = rightsItr.next();
114
        Assert.assertEquals(right.getText(),"Rights Text Test");
115

    
116
        Set<IdentifiableSource> sources = pKey.getSources();
117
        Iterator<IdentifiableSource> sourcesItr = sources.iterator();
118
        IdentifiableSource source = sourcesItr.next();
119
        Assert.assertEquals(source.getId(), 23710);
120
        source = sourcesItr.next();
121
        Assert.assertEquals(source.getId(), 23711);
122

    
123
        // TO DO : Added tests for Annotations , Markers
124
    }
125

    
126
    @Test
127
    public void readPolytomousKeyDataFromNodes() {
128
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
129
        PolytomousKeyNode rootNode = pKey.getRoot();
130

    
131

    
132
        Assert.assertEquals(rootNode.getId(), 2750);
133

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

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

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

    
160

    
161
    @Test
162
    public void savePolytomousKeyNodeData() {
163
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
164

    
165
        PolytomousKeyNode pkeynode = pkey.getRoot();
166
        String newQuestion = "New Question";
167
        String newStatement = "New Statement";
168

    
169
        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());
170
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
171

    
172
            node.setQuestion(null);
173
            node.setStatement(null);
174
        }
175
        //FIXME:Remoting Add tests for feature after fixing problem
176

    
177
        //Feature feature = pkeynode.getFeature();
178
        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
179
        //pkeynode.setFeature(null);
180

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

    
184
        //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
185

    
186
        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
187
        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
188
        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
189
        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
190
        pkeynode.getChildAt(1).setTaxon(taxon);
191

    
192
        polytomousKeyService.merge(pkey);
193

    
194
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
195

    
196

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

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

    
207
        polytomousKeyService.merge(pkey);
208

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

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

    
221
    }
222

    
223

    
224
    @Test
225
    public void savePolytomousKeyNodeDataWithSameSubKey() {
226

    
227
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
228

    
229

    
230
        PolytomousKeyNode pkeynode = pkey.getRoot();
231

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

    
236

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

    
241
        Assert.assertSame(subkey1, subkey2);
242

    
243
        polytomousKeyService.merge(pkey);
244
    }
245

    
246
    @Test
247
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
248

    
249
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
250

    
251

    
252
        PolytomousKeyNode pkeynode = pkey.getRoot();
253

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

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

    
265
        Assert.assertNotSame(childNode0, childNode1);
266

    
267
        Assert.assertSame(subkey1, subkey2);
268

    
269
        polytomousKeyService.merge(pkey);
270
    }
271

    
272

    
273

    
274
    @Test
275
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
276

    
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

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

    
338

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

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

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

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

    
355

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

    
362
        polytomousKeyService.delete(subKey);
363

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

    
369
        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
370
        Assert.assertNotNull(subKey);
371
    }
372

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

    
384
            for(TermVocabulary vocab : vocabularies) {
385
                vocab.getTermsOrderedByLabels(Language.ENGLISH());
386
            }
387

    
388
            termVocabulary = vocabularyService.merge(termVocabulary);
389
            vocabularies.add(termVocabulary);
390
            termVocabulary.setLabel("Test");
391
            vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));
392
        } finally {
393
            vocabularyService.delete(termVocabulary);
394
        }
395
    }
396

    
397
    @Test
398
    public void saveNewTerm() {
399
        UUID vocNameFeatureUuid = UUID.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");
400
        UUID vocFeatureUuid = UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
401
        DefinedTermBase newTerm = TermType.Feature.getEmptyDefinedTermBase();
402
        newTerm.setLabel("CreateTest");
403
        try {
404
            Assert.assertNotNull(newTerm);
405
            TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);
406
            TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);
407

    
408
            List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();
409

    
410
            vocs.add(vocNameFeature);
411
            vocs.add(vocFeature);
412

    
413
            vocNameFeature.addTerm(newTerm);
414

    
415
            vocs = vocabularyService.merge(vocs);
416
            for(TermVocabulary voc : vocs) {
417
                if(voc.getUuid().equals(vocNameFeatureUuid)) {
418
                    vocNameFeature = voc;
419
                }
420
            }
421

    
422
            Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));
423

    
424
            for(Object obj : vocNameFeature.getTerms()) {
425
                DefinedTermBase dtb = (DefinedTermBase)obj;
426
                if("CreateTest".equals(dtb.getLabel())) {
427
                    newTerm = dtb;
428
                    Assert.assertNotNull(dtb.getCreatedBy());
429
                    Assert.assertNotNull(dtb.getCreated());
430
                }
431
            }
432
            newTerm.setLabel("UpdateTest");
433
            newTerm = termService.merge(newTerm);
434
            Assert.assertNotNull(newTerm.getUpdatedBy());
435
            Assert.assertNotNull(newTerm.getUpdated());
436
        } finally {
437
            termService.delete(newTerm.getUuid());
438
        }
439
    }
440
}
(1-1/3)