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
.application
.CdmApplicationState
;
28 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyNodeService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
35 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
36 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
37 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
38 import eu
.etaxonomy
.cdm
.model
.common
.Credit
;
39 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
40 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
41 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
42 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
43 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
44 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
45 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
46 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
47 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
48 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
49 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
51 import eu
.etaxonomy
.taxeditor
.httpinvoker
.RemotingSessionAwareTest
;
59 public class CdmEntitySessionAwareTest
extends RemotingSessionAwareTest
{
61 private static final Logger logger
= Logger
.getLogger(CdmEntitySessionAwareTest
.class);
63 UUID polytomousKeyUuid
= UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
64 UUID subKeyUuid
= UUID
.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01");
65 //UUID polytomousKeyUuid = UUID.fromString("bab66772-2c83-428a-bb6d-655d12ac6097");
66 UUID taxon1Uuid
= UUID
.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
67 UUID personUuid
= UUID
.fromString("945d08f2-eb92-45b6-9252-6275ea6d338b");
71 IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
72 IPolytomousKeyNodeService polytomousKeyNodeService
= getRemoteApplicationController().getPolytomousKeyNodeService();
73 ICommonService commonService
= getRemoteApplicationController().getCommonService();
74 ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
75 IVocabularyService vocabularyService
= getRemoteApplicationController().getVocabularyService();
76 ITermService termService
= getRemoteApplicationController().getTermService();
77 IUserService userService
= getRemoteApplicationController().getUserService();
79 //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
84 public static void initializePolytomousKeyTest() {
89 public void readAllPolytomousKeys() {
90 List
<PolytomousKey
> pKeys
= polytomousKeyService
.list(PolytomousKey
.class, null, null, null, null);
91 Iterator
<PolytomousKey
> pKeysItr
= pKeys
.iterator();
92 Assert
.assertEquals(pKeysItr
.next().getUuid(),UUID
.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
93 Assert
.assertEquals(pKeysItr
.next().getUuid(),UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
98 //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
99 public void readPolytmousKeyData() {
100 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
101 Set
<Taxon
> taxonomicScope
= pKey
.getTaxonomicScope();
102 Iterator
<Taxon
> tsItr
= taxonomicScope
.iterator();
103 Taxon taxon
= tsItr
.next();
105 Assert
.assertEquals("Achillea sec. Cyprus", taxon
.getTitleCache());
106 Assert
.assertEquals(tsItr
.hasNext(), false);
108 List
<Credit
> credits
= pKey
.getCredits();
109 AgentBase agent
= credits
.get(0).getAgent();
110 Assert
.assertEquals(agent
.getId(),4809);
111 Assert
.assertEquals(agent
.getTitleCache(),"R. A. Graham");
112 Assert
.assertEquals(credits
.get(0).getText(),"Credits Text Test");
114 Set
<Extension
> exts
= pKey
.getExtensions();
115 Iterator
<Extension
> extItr
= exts
.iterator();
116 Extension ext
= extItr
.next();
117 Assert
.assertEquals(ext
.getValue(), "http://test.com");
119 Set
<Rights
> rights
= pKey
.getRights();
120 Iterator
<Rights
> rightsItr
= rights
.iterator();
121 Rights right
= rightsItr
.next();
122 Assert
.assertEquals(right
.getText(),"Rights Text Test");
124 Set
<IdentifiableSource
> sources
= pKey
.getSources();
125 Iterator
<IdentifiableSource
> sourcesItr
= sources
.iterator();
126 IdentifiableSource source
= sourcesItr
.next();
127 Assert
.assertEquals(source
.getId(), 23710);
128 source
= sourcesItr
.next();
129 Assert
.assertEquals(source
.getId(), 23711);
131 // TO DO : Added tests for Annotations , Markers
137 public void readPolytomousKeyDataFromNodes() {
138 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
139 PolytomousKeyNode rootNode
= pKey
.getRoot();
142 Assert
.assertEquals(rootNode
.getId(), 2750);
144 Assert
.assertEquals(rootNode
.getChildAt(0).getId(), 2751);
145 Assert
.assertEquals(rootNode
.getChildAt(0).getParent().getId(), rootNode
.getId());
146 Assert
.assertEquals(rootNode
.getKey().getId(), pKey
.getId());
147 Integer sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(0), "sortIndex");
148 Assert
.assertEquals(sortIndex
, new Integer(0));
149 String statement
= "Capitula without ligulate ray-florets; leaves entire or subentire";
150 Assert
.assertEquals(rootNode
.getChildAt(0).getStatement().getLabelText(Language
.ENGLISH()), statement
);
152 Assert
.assertEquals(rootNode
.getChildAt(1).getId(), 2753);
153 Assert
.assertEquals(rootNode
.getChildAt(1).getParent().getId(), rootNode
.getId());
154 Assert
.assertEquals(rootNode
.getChildAt(1).getKey().getId(), pKey
.getId());
155 sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(1), "sortIndex");
156 Assert
.assertEquals(sortIndex
, new Integer(1));
157 statement
= "Capitula with ligulate ray-florets; leaves pinnatisect";
158 Assert
.assertEquals(rootNode
.getChildAt(1).getStatement().getLabelText(Language
.ENGLISH()), statement
);
160 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getId(), 2754);
161 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getParent().getId(), rootNode
.getChildAt(1).getId());
162 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getKey().getId(), pKey
.getId());
163 sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(1).getChildAt(0), "sortIndex");
164 Assert
.assertEquals(sortIndex
, new Integer(0));
165 statement
= "Ray-florets yellow";
166 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getStatement().getLabelText(Language
.ENGLISH()), statement
);
167 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
171 public void addGreatGrandChildPolytomousKeyNode() {
173 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
174 PolytomousKeyNode rootChildNode
= pKey
.getRoot().getChildAt(0);
175 PolytomousKeyNode grandChildNode
= PolytomousKeyNode
.NewInstance();
176 rootChildNode
.addChild(grandChildNode
);
178 grandChildNode
= polytomousKeyNodeService
.merge(grandChildNode
);
180 PolytomousKeyNode greatGrandChildNode
= PolytomousKeyNode
.NewInstance();
181 grandChildNode
.addChild(greatGrandChildNode
);
183 Assert
.assertFalse(pKey
.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
187 public void savePolytomousKeyNodeData() {
188 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
190 PolytomousKeyNode pkeynode
= pkey
.getRoot();
191 String newQuestion
= "New Question";
192 String newStatement
= "New Statement";
194 Assert
.assertEquals("Question 1",pkeynode
.getQuestion().getLabel().get(Language
.ENGLISH()).getText());
195 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
197 node
.setQuestion(null);
198 node
.setStatement(null);
200 //FIXME:Remoting Add tests for feature after fixing problem
202 //Feature feature = pkeynode.getFeature();
203 //Assert.assertEquals(feature.getTitleCache(),"Systematics");
204 //pkeynode.setFeature(null);
206 Assert
.assertEquals(pkeynode
.getChildAt(0).getModifyingText().get(Language
.ENGLISH()).getText(),"Modifying Text 1a");
207 String modifyingText
= "Modifying Text 1a updated";
209 //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
211 Assert
.assertEquals(pkeynode
.getChildAt(0).getSubkey().getId(),751);
212 Assert
.assertEquals("Asphodeline", pkeynode
.getChildAt(0).getSubkey().getTitleCache());
213 Assert
.assertNull(pkeynode
.getChildAt(1).getTaxon());
214 Taxon taxon
= CdmBase
.deproxy(taxonService
.find(taxon1Uuid
),Taxon
.class);
215 pkeynode
.getChildAt(1).setTaxon(taxon
);
217 polytomousKeyService
.merge(pkey
);
219 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
222 pkeynode
= pkey
.getRoot();
223 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
224 Assert
.assertNull(node
.getQuestion());
225 node
.setQuestion(KeyStatement
.NewInstance(Language
.ENGLISH(),newQuestion
));
226 Assert
.assertNull(node
.getStatement());
227 node
.setStatement(KeyStatement
.NewInstance(Language
.ENGLISH(),newStatement
));
230 Assert
.assertEquals(pkeynode
.getChildAt(1).getTaxon(), taxon
);
232 polytomousKeyService
.merge(pkey
);
234 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
236 pkeynode
= pkey
.getRoot();
237 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
238 Assert
.assertNotNull(node
.getQuestion());
239 Map
<Language
, LanguageString
> label
= node
.getQuestion().getLabel();
240 Assert
.assertEquals(newQuestion
, label
.get(Language
.ENGLISH()).getText());
241 Assert
.assertNotNull(node
.getStatement());
242 Assert
.assertEquals(newStatement
, node
.getStatement().getLabel(Language
.ENGLISH()).getText());
244 //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
250 public void savePolytomousKeyNodeDataWithSameSubKey() {
252 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
255 PolytomousKeyNode pkeynode
= pkey
.getRoot();
257 PolytomousKey subkey1
= CdmBase
.deproxy(pkeynode
.getChildAt(0).getSubkey(), PolytomousKey
.class);
258 String subkey1title
= subkey1
.getTitleCache();
259 subkey1
.setTitleCache(subkey1title
+ "test", true);
262 PolytomousKey subkey2
= CdmBase
.deproxy(pkeynode
.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey
.class);
263 String subkey2title
= subkey2
.getTitleCache();
264 subkey2
.setTitleCache(subkey2title
+ "test", true);
266 Assert
.assertSame(subkey1
, subkey2
);
268 polytomousKeyService
.merge(pkey
);
272 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
274 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
277 PolytomousKeyNode pkeynode
= pkey
.getRoot();
279 PersistentCollection children
= (PersistentCollection
) pkeynode
.getChildren();
280 PolytomousKeyNode childNode0
= (PolytomousKeyNode
)commonService
.get(pkeynode
.getUuid(), "children", 0);
281 PolytomousKey subkey1
= CdmBase
.deproxy(childNode0
.getSubkey(),PolytomousKey
.class);
282 String subkey1title
= subkey1
.getTitleCache();
283 subkey1
.setTitleCache(subkey1title
+ "test", true);
285 PolytomousKeyNode childNode1
= (PolytomousKeyNode
)commonService
.get(pkeynode
.getUuid(), "children", 1);
286 PolytomousKey subkey2
= CdmBase
.deproxy(childNode1
.getSubkey(),PolytomousKey
.class);
287 String subkey2title
= subkey2
.getTitleCache();
288 subkey2
.setTitleCache(subkey2title
+ "test", true);
290 Assert
.assertNotSame(childNode0
, childNode1
);
292 Assert
.assertSame(subkey1
, subkey2
);
294 polytomousKeyService
.merge(pkey
);
300 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
302 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
303 PolytomousKeyNode pkeynode
= pkey
.getRoot();
305 Map
<Language
, LanguageString
> label1
= pkeynode
.getQuestion().getLabel();
309 Map
<Language
, LanguageString
> label2
= pkeynode
.getChildAt(0).getStatement().getLabel();
313 polytomousKeyService
.merge(pkey
);
317 public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
318 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
321 PolytomousKeyNode rootNode
= pKey
.getRoot();
322 PolytomousKeyNode child
= rootNode
.getChildAt(0);
323 PolytomousKey subKey
= child
.getSubkey();
325 polytomousKeyService
.delete(subKey
);
327 // since subKey was not initialized before the delete, an attempt
328 // to initialize it after will lead to an NPE
330 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
331 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
332 } catch(NullPointerException npe
) {
335 // retrieving subkey shows its null
336 subKey
= CdmBase
.deproxy(polytomousKeyService
.find(subKeyUuid
),PolytomousKey
.class);
337 Assert
.assertNull(subKey
);
339 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
342 rootNode
= pKey
.getRoot();
343 child
= rootNode
.getChildAt(0);
344 subKey
= child
.getSubkey();
345 // subkey will not be null, because the delete functionality of the subkey
346 // does not currently delete it from a polytomous key node
347 Assert
.assertNotNull(subKey
);
348 // after reloading the object graph, accessing subKey should throw a null pointer exception
350 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
351 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
352 } catch(NullPointerException npe
) {
360 public void deleteSubKeyInPolytomousNode() {
361 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
364 PolytomousKeyNode rootNode
= pKey
.getRoot();
365 List
<PolytomousKeyNode
> children
= rootNode
.getChildren();
366 PolytomousKeyNode child
= rootNode
.getChildAt(0);
367 polytomousKeyNodeService
.delete(child
.getUuid(), true);
369 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
371 rootNode
= pKey
.getRoot();
372 children
= rootNode
.getChildren();
373 Assert
.assertFalse(children
.contains(child
));
377 public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
378 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
381 PolytomousKeyNode rootNode
= pKey
.getRoot();
382 PolytomousKeyNode child
= rootNode
.getChildAt(0);
383 PolytomousKey subKey
= child
.getSubkey();
384 // this call initializes the subkey
387 polytomousKeyService
.delete(subKey
);
389 // since subKey was initialized before the delete, it will be
390 // accesible even after the delete. This behaviour is similar
391 // to hibernate session delete where the deleted object is
392 // made transient, but not deleted from the object graph
394 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
395 Assert
.assertNotNull(subKey
);
399 public void saveNewTermVocabulary() {
400 TermVocabulary termVocabulary
=
401 TermVocabulary
.NewInstance(TermType
.Feature
,
407 List
<TermVocabulary
<DefinedTermBase
>> vocabularies
= vocabularyService
.findByTermType(TermType
.Feature
);
409 for(TermVocabulary vocab
: vocabularies
) {
410 vocab
.getTermsOrderedByLabels(Language
.ENGLISH());
413 termVocabulary
= vocabularyService
.merge(termVocabulary
);
414 vocabularies
.add(termVocabulary
);
415 termVocabulary
.setLabel("Test");
416 vocabularyService
.merge(new ArrayList
<TermVocabulary
>(vocabularies
));
418 vocabularyService
.delete(termVocabulary
);
422 @Ignore // should be enabled once resolution of #5066 is merged into cdmlib main branch
424 public void saveNewTerm() {
425 UUID vocNameFeatureUuid
= UUID
.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");
426 UUID vocFeatureUuid
= UUID
.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
427 DefinedTermBase newTerm
= TermType
.Feature
.getEmptyDefinedTermBase();
428 newTerm
.setLabel("CreateTest");
432 Assert
.assertNotNull(newTerm
);
433 TermVocabulary vocNameFeature
= vocabularyService
.find(vocNameFeatureUuid
);
434 TermVocabulary vocFeature
= vocabularyService
.find(vocFeatureUuid
);
436 List
<TermVocabulary
> vocs
= new ArrayList
<TermVocabulary
>();
438 vocs
.add(vocNameFeature
);
439 vocs
.add(vocFeature
);
441 vocNameFeature
.addTerm(newTerm
);
443 vocs
= vocabularyService
.merge(vocs
);
444 for(TermVocabulary voc
: vocs
) {
445 if(voc
.getUuid().equals(vocNameFeatureUuid
)) {
446 vocNameFeature
= voc
;
450 Assert
.assertTrue(vocNameFeature
.getTerms().contains(newTerm
));
452 for(Object obj
: vocNameFeature
.getTerms()) {
453 DefinedTermBase dtb
= (DefinedTermBase
)obj
;
454 if("CreateTest".equals(dtb
.getLabel())) {
456 Assert
.assertNotNull(dtb
.getCreatedBy());
457 Assert
.assertNotNull(dtb
.getCreated());
459 Assert
.assertNull(dtb
.getCreatedBy());
462 newTerm
.setLabel("UpdateTest");
463 newTerm
= termService
.merge(newTerm
);
464 Assert
.assertNotNull(newTerm
.getUpdatedBy());
465 Assert
.assertNotNull(newTerm
.getUpdated());
467 Assert
.assertNull(vocNameFeature
.getCreatedBy());
468 Assert
.assertNull(vocFeature
.getCreatedBy());
470 if(termService
.find(newTerm
.getUuid()) != null) {
471 termService
.delete(newTerm
.getUuid());
476 @Ignore // activate after fixing #5138
478 public void updatePerson() {
479 Person person
= (Person
) CdmApplicationState
.getCurrentAppConfig().getAgentService().load(personUuid
);
480 // Note : at this point the contact field in Person (AgentBase) is initialized even though no
481 // data related to the Contact class exists in the database.
482 person
.setFirstname("Me");
483 CdmApplicationState
.getCurrentAppConfig().getAgentService().merge(person
);
487 @Ignore // activate after fixing #5138
489 public void createPerson() {
491 Person person
= Person
.NewInstance();
492 //person.setTitleCache("New Person", true);
493 person
= (Person
) CdmApplicationState
.getCurrentAppConfig().getAgentService().merge(person
);
494 //Note : at this point the contact field in Person (AgentBase) is set to null which is
495 // different behaviour as compared to a load call on an existing Person entity.
496 // The fact that the contact is null will lead to the all-delete-orphan error.
497 person
= (Person
) CdmApplicationState
.getCurrentAppConfig().getAgentService().load(person
.getUuid());
498 person
.setFirstname("Some");
499 CdmApplicationState
.getCurrentAppConfig().getAgentService().merge(person
);