3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.session
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Iterator
;
14 import java
.util
.List
;
17 import java
.util
.UUID
;
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
;
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
;
57 public class CdmEntitySessionAwareTest
extends RemotingSessionAwareTest
{
59 private static final Logger logger
= Logger
.getLogger(CdmEntitySessionAwareTest
.class);
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");
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();
75 //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
80 public static void initializePolytomousKeyTest() {
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"));
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();
101 Assert
.assertEquals("Achillea sec. Cyprus", taxon
.getTitleCache());
102 Assert
.assertEquals(tsItr
.hasNext(), false);
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");
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");
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");
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);
127 // TO DO : Added tests for Annotations , Markers
133 public void readPolytomousKeyDataFromNodes() {
134 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
135 PolytomousKeyNode rootNode
= pKey
.getRoot();
138 Assert
.assertEquals(rootNode
.getId(), 2750);
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
);
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
);
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");
167 public void addGreatGrandChildPolytomousKeyNode() {
169 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
170 PolytomousKeyNode rootChildNode
= pKey
.getRoot().getChildAt(0);
171 PolytomousKeyNode grandChildNode
= PolytomousKeyNode
.NewInstance();
172 rootChildNode
.addChild(grandChildNode
);
174 grandChildNode
= polytomousKeyNodeService
.merge(grandChildNode
);
176 PolytomousKeyNode greatGrandChildNode
= PolytomousKeyNode
.NewInstance();
177 grandChildNode
.addChild(greatGrandChildNode
);
179 Assert
.assertFalse(pKey
.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
183 public void savePolytomousKeyNodeData() {
184 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
186 PolytomousKeyNode pkeynode
= pkey
.getRoot();
187 String newQuestion
= "New Question";
188 String newStatement
= "New Statement";
190 Assert
.assertEquals("Question 1",pkeynode
.getQuestion().getLabel().get(Language
.ENGLISH()).getText());
191 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
193 node
.setQuestion(null);
194 node
.setStatement(null);
196 //FIXME:Remoting Add tests for feature after fixing problem
198 //Feature feature = pkeynode.getFeature();
199 //Assert.assertEquals(feature.getTitleCache(),"Systematics");
200 //pkeynode.setFeature(null);
202 Assert
.assertEquals(pkeynode
.getChildAt(0).getModifyingText().get(Language
.ENGLISH()).getText(),"Modifying Text 1a");
203 String modifyingText
= "Modifying Text 1a updated";
205 //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
207 Assert
.assertEquals(pkeynode
.getChildAt(0).getSubkey().getId(),751);
208 Assert
.assertEquals("Asphodeline", pkeynode
.getChildAt(0).getSubkey().getTitleCache());
209 Assert
.assertNull(pkeynode
.getChildAt(1).getTaxon());
210 Taxon taxon
= CdmBase
.deproxy(taxonService
.find(taxon1Uuid
),Taxon
.class);
211 pkeynode
.getChildAt(1).setTaxon(taxon
);
213 polytomousKeyService
.merge(pkey
);
215 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
218 pkeynode
= pkey
.getRoot();
219 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
220 Assert
.assertNull(node
.getQuestion());
221 node
.setQuestion(KeyStatement
.NewInstance(Language
.ENGLISH(),newQuestion
));
222 Assert
.assertNull(node
.getStatement());
223 node
.setStatement(KeyStatement
.NewInstance(Language
.ENGLISH(),newStatement
));
226 Assert
.assertEquals(pkeynode
.getChildAt(1).getTaxon(), taxon
);
228 polytomousKeyService
.merge(pkey
);
230 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
232 pkeynode
= pkey
.getRoot();
233 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
234 Assert
.assertNotNull(node
.getQuestion());
235 Map
<Language
, LanguageString
> label
= node
.getQuestion().getLabel();
236 Assert
.assertEquals(newQuestion
, label
.get(Language
.ENGLISH()).getText());
237 Assert
.assertNotNull(node
.getStatement());
238 Assert
.assertEquals(newStatement
, node
.getStatement().getLabel(Language
.ENGLISH()).getText());
240 //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
246 public void savePolytomousKeyNodeDataWithSameSubKey() {
248 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
251 PolytomousKeyNode pkeynode
= pkey
.getRoot();
253 PolytomousKey subkey1
= CdmBase
.deproxy(pkeynode
.getChildAt(0).getSubkey(), PolytomousKey
.class);
254 String subkey1title
= subkey1
.getTitleCache();
255 subkey1
.setTitleCache(subkey1title
+ "test", true);
258 PolytomousKey subkey2
= CdmBase
.deproxy(pkeynode
.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey
.class);
259 String subkey2title
= subkey2
.getTitleCache();
260 subkey2
.setTitleCache(subkey2title
+ "test", true);
262 Assert
.assertSame(subkey1
, subkey2
);
264 polytomousKeyService
.merge(pkey
);
268 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
270 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
273 PolytomousKeyNode pkeynode
= pkey
.getRoot();
275 PersistentCollection children
= (PersistentCollection
) pkeynode
.getChildren();
276 PolytomousKeyNode childNode0
= (PolytomousKeyNode
)commonService
.get(pkeynode
.getUuid(), "children", 0);
277 PolytomousKey subkey1
= CdmBase
.deproxy(childNode0
.getSubkey(),PolytomousKey
.class);
278 String subkey1title
= subkey1
.getTitleCache();
279 subkey1
.setTitleCache(subkey1title
+ "test", true);
281 PolytomousKeyNode childNode1
= (PolytomousKeyNode
)commonService
.get(pkeynode
.getUuid(), "children", 1);
282 PolytomousKey subkey2
= CdmBase
.deproxy(childNode1
.getSubkey(),PolytomousKey
.class);
283 String subkey2title
= subkey2
.getTitleCache();
284 subkey2
.setTitleCache(subkey2title
+ "test", true);
286 Assert
.assertNotSame(childNode0
, childNode1
);
288 Assert
.assertSame(subkey1
, subkey2
);
290 polytomousKeyService
.merge(pkey
);
296 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
298 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
299 PolytomousKeyNode pkeynode
= pkey
.getRoot();
301 Map
<Language
, LanguageString
> label1
= pkeynode
.getQuestion().getLabel();
305 Map
<Language
, LanguageString
> label2
= pkeynode
.getChildAt(0).getStatement().getLabel();
309 polytomousKeyService
.merge(pkey
);
313 public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
314 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
317 PolytomousKeyNode rootNode
= pKey
.getRoot();
318 PolytomousKeyNode child
= rootNode
.getChildAt(0);
319 PolytomousKey subKey
= child
.getSubkey();
321 polytomousKeyService
.delete(subKey
);
323 // since subKey was not initialized before the delete, an attempt
324 // to initialize it after will lead to an NPE
326 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
327 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
328 } catch(NullPointerException npe
) {
331 // retrieving subkey shows its null
332 subKey
= CdmBase
.deproxy(polytomousKeyService
.find(subKeyUuid
),PolytomousKey
.class);
333 Assert
.assertNull(subKey
);
335 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
338 rootNode
= pKey
.getRoot();
339 child
= rootNode
.getChildAt(0);
340 subKey
= child
.getSubkey();
341 // subkey will not be null, because the delete functionality of the subkey
342 // does not currently delete it from a polytomous key node
343 Assert
.assertNotNull(subKey
);
344 // after reloading the object graph, accessing subKey should throw a null pointer exception
346 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
347 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
348 } catch(NullPointerException npe
) {
356 public void deleteSubKeyInPolytomousNode() {
357 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
360 PolytomousKeyNode rootNode
= pKey
.getRoot();
361 List
<PolytomousKeyNode
> children
= rootNode
.getChildren();
362 PolytomousKeyNode child
= rootNode
.getChildAt(0);
363 polytomousKeyNodeService
.delete(child
.getUuid(), true);
365 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
367 rootNode
= pKey
.getRoot();
368 children
= rootNode
.getChildren();
369 Assert
.assertFalse(children
.contains(child
));
373 public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
374 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
377 PolytomousKeyNode rootNode
= pKey
.getRoot();
378 PolytomousKeyNode child
= rootNode
.getChildAt(0);
379 PolytomousKey subKey
= child
.getSubkey();
380 // this call initializes the subkey
383 polytomousKeyService
.delete(subKey
);
385 // since subKey was initialized before the delete, it will be
386 // accesible even after the delete. This behaviour is similar
387 // to hibernate session delete where the deleted object is
388 // made transient, but not deleted from the object graph
390 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
391 Assert
.assertNotNull(subKey
);
395 public void saveNewTermVocabulary() {
396 TermVocabulary termVocabulary
=
397 TermVocabulary
.NewInstance(TermType
.Feature
,
403 List
<TermVocabulary
<DefinedTermBase
>> vocabularies
= vocabularyService
.findByTermType(TermType
.Feature
);
405 for(TermVocabulary vocab
: vocabularies
) {
406 vocab
.getTermsOrderedByLabels(Language
.ENGLISH());
409 termVocabulary
= vocabularyService
.merge(termVocabulary
);
410 vocabularies
.add(termVocabulary
);
411 termVocabulary
.setLabel("Test");
412 vocabularyService
.merge(new ArrayList
<TermVocabulary
>(vocabularies
));
414 vocabularyService
.delete(termVocabulary
);
418 @Ignore // should be enabled once resolution of #5066 is merged into cdmlib main branch
420 public void saveNewTerm() {
421 UUID vocNameFeatureUuid
= UUID
.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");
422 UUID vocFeatureUuid
= UUID
.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
423 DefinedTermBase newTerm
= TermType
.Feature
.getEmptyDefinedTermBase();
424 newTerm
.setLabel("CreateTest");
428 Assert
.assertNotNull(newTerm
);
429 TermVocabulary vocNameFeature
= vocabularyService
.find(vocNameFeatureUuid
);
430 TermVocabulary vocFeature
= vocabularyService
.find(vocFeatureUuid
);
432 List
<TermVocabulary
> vocs
= new ArrayList
<TermVocabulary
>();
434 vocs
.add(vocNameFeature
);
435 vocs
.add(vocFeature
);
437 vocNameFeature
.addTerm(newTerm
);
439 vocs
= vocabularyService
.merge(vocs
);
440 for(TermVocabulary voc
: vocs
) {
441 if(voc
.getUuid().equals(vocNameFeatureUuid
)) {
442 vocNameFeature
= voc
;
446 Assert
.assertTrue(vocNameFeature
.getTerms().contains(newTerm
));
448 for(Object obj
: vocNameFeature
.getTerms()) {
449 DefinedTermBase dtb
= (DefinedTermBase
)obj
;
450 if("CreateTest".equals(dtb
.getLabel())) {
452 Assert
.assertNotNull(dtb
.getCreatedBy());
453 Assert
.assertNotNull(dtb
.getCreated());
455 Assert
.assertNull(dtb
.getCreatedBy());
458 newTerm
.setLabel("UpdateTest");
459 newTerm
= termService
.merge(newTerm
);
460 Assert
.assertNotNull(newTerm
.getUpdatedBy());
461 Assert
.assertNotNull(newTerm
.getUpdated());
463 Assert
.assertNull(vocNameFeature
.getCreatedBy());
464 Assert
.assertNull(vocFeature
.getCreatedBy());
466 if(termService
.find(newTerm
.getUuid()) != null) {
467 termService
.delete(newTerm
.getUuid());