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
.agent
.TeamOrPersonBase
;
38 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
39 import eu
.etaxonomy
.cdm
.model
.common
.Credit
;
40 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
41 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
42 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
43 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
44 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
45 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
46 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
47 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
48 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
49 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
50 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
52 import eu
.etaxonomy
.taxeditor
.httpinvoker
.RemotingSessionAwareTest
;
60 public class CdmEntitySessionAwareTest
extends RemotingSessionAwareTest
{
62 private static final Logger logger
= Logger
.getLogger(CdmEntitySessionAwareTest
.class);
64 UUID polytomousKeyUuid
= UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
65 UUID subKeyUuid
= UUID
.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01");
66 //UUID polytomousKeyUuid = UUID.fromString("bab66772-2c83-428a-bb6d-655d12ac6097");
67 UUID taxon1Uuid
= UUID
.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
70 IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
71 IPolytomousKeyNodeService polytomousKeyNodeService
= getRemoteApplicationController().getPolytomousKeyNodeService();
72 ICommonService commonService
= getRemoteApplicationController().getCommonService();
73 ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
74 IVocabularyService vocabularyService
= getRemoteApplicationController().getVocabularyService();
75 ITermService termService
= getRemoteApplicationController().getTermService();
76 IUserService userService
= getRemoteApplicationController().getUserService();
78 //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
83 public static void initializePolytomousKeyTest() {
88 public void readAllPolytomousKeys() {
89 List
<PolytomousKey
> pKeys
= polytomousKeyService
.list(PolytomousKey
.class, null, null, null, null);
90 Iterator
<PolytomousKey
> pKeysItr
= pKeys
.iterator();
91 Assert
.assertEquals(pKeysItr
.next().getUuid(),UUID
.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
92 Assert
.assertEquals(pKeysItr
.next().getUuid(),UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
97 //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
98 public void readPolytmousKeyData() {
99 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
100 Set
<Taxon
> taxonomicScope
= pKey
.getTaxonomicScope();
101 Iterator
<Taxon
> tsItr
= taxonomicScope
.iterator();
102 Taxon taxon
= tsItr
.next();
104 Assert
.assertEquals("Achillea sec. Cyprus", taxon
.getTitleCache());
105 Assert
.assertEquals(tsItr
.hasNext(), false);
107 List
<Credit
> credits
= pKey
.getCredits();
108 AgentBase agent
= credits
.get(0).getAgent();
109 Assert
.assertEquals(agent
.getId(),4809);
110 Assert
.assertEquals(agent
.getTitleCache(),"R. A. Graham");
111 Assert
.assertEquals(credits
.get(0).getText(),"Credits Text Test");
113 Set
<Extension
> exts
= pKey
.getExtensions();
114 Iterator
<Extension
> extItr
= exts
.iterator();
115 Extension ext
= extItr
.next();
116 Assert
.assertEquals(ext
.getValue(), "http://test.com");
118 Set
<Rights
> rights
= pKey
.getRights();
119 Iterator
<Rights
> rightsItr
= rights
.iterator();
120 Rights right
= rightsItr
.next();
121 Assert
.assertEquals(right
.getText(),"Rights Text Test");
123 Set
<IdentifiableSource
> sources
= pKey
.getSources();
124 Iterator
<IdentifiableSource
> sourcesItr
= sources
.iterator();
125 IdentifiableSource source
= sourcesItr
.next();
126 Assert
.assertEquals(source
.getId(), 23710);
127 source
= sourcesItr
.next();
128 Assert
.assertEquals(source
.getId(), 23711);
130 // TO DO : Added tests for Annotations , Markers
136 public void readPolytomousKeyDataFromNodes() {
137 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
138 PolytomousKeyNode rootNode
= pKey
.getRoot();
141 Assert
.assertEquals(rootNode
.getId(), 2750);
143 Assert
.assertEquals(rootNode
.getChildAt(0).getId(), 2751);
144 Assert
.assertEquals(rootNode
.getChildAt(0).getParent().getId(), rootNode
.getId());
145 Assert
.assertEquals(rootNode
.getKey().getId(), pKey
.getId());
146 Integer sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(0), "sortIndex");
147 Assert
.assertEquals(sortIndex
, new Integer(0));
148 String statement
= "Capitula without ligulate ray-florets; leaves entire or subentire";
149 Assert
.assertEquals(rootNode
.getChildAt(0).getStatement().getLabelText(Language
.ENGLISH()), statement
);
151 Assert
.assertEquals(rootNode
.getChildAt(1).getId(), 2753);
152 Assert
.assertEquals(rootNode
.getChildAt(1).getParent().getId(), rootNode
.getId());
153 Assert
.assertEquals(rootNode
.getChildAt(1).getKey().getId(), pKey
.getId());
154 sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(1), "sortIndex");
155 Assert
.assertEquals(sortIndex
, new Integer(1));
156 statement
= "Capitula with ligulate ray-florets; leaves pinnatisect";
157 Assert
.assertEquals(rootNode
.getChildAt(1).getStatement().getLabelText(Language
.ENGLISH()), statement
);
159 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getId(), 2754);
160 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getParent().getId(), rootNode
.getChildAt(1).getId());
161 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getKey().getId(), pKey
.getId());
162 sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(1).getChildAt(0), "sortIndex");
163 Assert
.assertEquals(sortIndex
, new Integer(0));
164 statement
= "Ray-florets yellow";
165 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getStatement().getLabelText(Language
.ENGLISH()), statement
);
166 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
170 public void addGreatGrandChildPolytomousKeyNode() {
172 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
173 PolytomousKeyNode rootChildNode
= pKey
.getRoot().getChildAt(0);
174 PolytomousKeyNode grandChildNode
= PolytomousKeyNode
.NewInstance();
175 rootChildNode
.addChild(grandChildNode
);
177 grandChildNode
= polytomousKeyNodeService
.merge(grandChildNode
);
179 PolytomousKeyNode greatGrandChildNode
= PolytomousKeyNode
.NewInstance();
180 grandChildNode
.addChild(greatGrandChildNode
);
182 Assert
.assertFalse(pKey
.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
186 public void savePolytomousKeyNodeData() {
187 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
189 PolytomousKeyNode pkeynode
= pkey
.getRoot();
190 String newQuestion
= "New Question";
191 String newStatement
= "New Statement";
193 Assert
.assertEquals("Question 1",pkeynode
.getQuestion().getLabel().get(Language
.ENGLISH()).getText());
194 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
196 node
.setQuestion(null);
197 node
.setStatement(null);
199 //FIXME:Remoting Add tests for feature after fixing problem
201 //Feature feature = pkeynode.getFeature();
202 //Assert.assertEquals(feature.getTitleCache(),"Systematics");
203 //pkeynode.setFeature(null);
205 Assert
.assertEquals(pkeynode
.getChildAt(0).getModifyingText().get(Language
.ENGLISH()).getText(),"Modifying Text 1a");
206 String modifyingText
= "Modifying Text 1a updated";
208 //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
210 Assert
.assertEquals(pkeynode
.getChildAt(0).getSubkey().getId(),751);
211 Assert
.assertEquals("Asphodeline", pkeynode
.getChildAt(0).getSubkey().getTitleCache());
212 Assert
.assertNull(pkeynode
.getChildAt(1).getTaxon());
213 Taxon taxon
= CdmBase
.deproxy(taxonService
.find(taxon1Uuid
),Taxon
.class);
214 pkeynode
.getChildAt(1).setTaxon(taxon
);
216 polytomousKeyService
.merge(pkey
);
218 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
221 pkeynode
= pkey
.getRoot();
222 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
223 Assert
.assertNull(node
.getQuestion());
224 node
.setQuestion(KeyStatement
.NewInstance(Language
.ENGLISH(),newQuestion
));
225 Assert
.assertNull(node
.getStatement());
226 node
.setStatement(KeyStatement
.NewInstance(Language
.ENGLISH(),newStatement
));
229 Assert
.assertEquals(pkeynode
.getChildAt(1).getTaxon(), taxon
);
231 polytomousKeyService
.merge(pkey
);
233 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
235 pkeynode
= pkey
.getRoot();
236 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
237 Assert
.assertNotNull(node
.getQuestion());
238 Map
<Language
, LanguageString
> label
= node
.getQuestion().getLabel();
239 Assert
.assertEquals(newQuestion
, label
.get(Language
.ENGLISH()).getText());
240 Assert
.assertNotNull(node
.getStatement());
241 Assert
.assertEquals(newStatement
, node
.getStatement().getLabel(Language
.ENGLISH()).getText());
243 //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
249 public void savePolytomousKeyNodeDataWithSameSubKey() {
251 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
254 PolytomousKeyNode pkeynode
= pkey
.getRoot();
256 PolytomousKey subkey1
= CdmBase
.deproxy(pkeynode
.getChildAt(0).getSubkey(), PolytomousKey
.class);
257 String subkey1title
= subkey1
.getTitleCache();
258 subkey1
.setTitleCache(subkey1title
+ "test", true);
261 PolytomousKey subkey2
= CdmBase
.deproxy(pkeynode
.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey
.class);
262 String subkey2title
= subkey2
.getTitleCache();
263 subkey2
.setTitleCache(subkey2title
+ "test", true);
265 Assert
.assertSame(subkey1
, subkey2
);
267 polytomousKeyService
.merge(pkey
);
271 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
273 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
276 PolytomousKeyNode pkeynode
= pkey
.getRoot();
278 PersistentCollection children
= (PersistentCollection
) pkeynode
.getChildren();
279 PolytomousKeyNode childNode0
= (PolytomousKeyNode
)commonService
.get(pkeynode
.getUuid(), "children", 0);
280 PolytomousKey subkey1
= CdmBase
.deproxy(childNode0
.getSubkey(),PolytomousKey
.class);
281 String subkey1title
= subkey1
.getTitleCache();
282 subkey1
.setTitleCache(subkey1title
+ "test", true);
284 PolytomousKeyNode childNode1
= (PolytomousKeyNode
)commonService
.get(pkeynode
.getUuid(), "children", 1);
285 PolytomousKey subkey2
= CdmBase
.deproxy(childNode1
.getSubkey(),PolytomousKey
.class);
286 String subkey2title
= subkey2
.getTitleCache();
287 subkey2
.setTitleCache(subkey2title
+ "test", true);
289 Assert
.assertNotSame(childNode0
, childNode1
);
291 Assert
.assertSame(subkey1
, subkey2
);
293 polytomousKeyService
.merge(pkey
);
299 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
301 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
302 PolytomousKeyNode pkeynode
= pkey
.getRoot();
304 Map
<Language
, LanguageString
> label1
= pkeynode
.getQuestion().getLabel();
308 Map
<Language
, LanguageString
> label2
= pkeynode
.getChildAt(0).getStatement().getLabel();
312 polytomousKeyService
.merge(pkey
);
316 public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
317 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
320 PolytomousKeyNode rootNode
= pKey
.getRoot();
321 PolytomousKeyNode child
= rootNode
.getChildAt(0);
322 PolytomousKey subKey
= child
.getSubkey();
324 polytomousKeyService
.delete(subKey
);
326 // since subKey was not initialized before the delete, an attempt
327 // to initialize it after will lead to an NPE
329 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
330 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
331 } catch(NullPointerException npe
) {
334 // retrieving subkey shows its null
335 subKey
= CdmBase
.deproxy(polytomousKeyService
.find(subKeyUuid
),PolytomousKey
.class);
336 Assert
.assertNull(subKey
);
338 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
341 rootNode
= pKey
.getRoot();
342 child
= rootNode
.getChildAt(0);
343 subKey
= child
.getSubkey();
344 // subkey will not be null, because the delete functionality of the subkey
345 // does not currently delete it from a polytomous key node
346 Assert
.assertNotNull(subKey
);
347 // after reloading the object graph, accessing subKey should throw a null pointer exception
349 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
350 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
351 } catch(NullPointerException npe
) {
359 public void deleteSubKeyInPolytomousNode() {
360 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
363 PolytomousKeyNode rootNode
= pKey
.getRoot();
364 List
<PolytomousKeyNode
> children
= rootNode
.getChildren();
365 PolytomousKeyNode child
= rootNode
.getChildAt(0);
366 polytomousKeyNodeService
.delete(child
.getUuid(), true);
368 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
370 rootNode
= pKey
.getRoot();
371 children
= rootNode
.getChildren();
372 Assert
.assertFalse(children
.contains(child
));
376 public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
377 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
380 PolytomousKeyNode rootNode
= pKey
.getRoot();
381 PolytomousKeyNode child
= rootNode
.getChildAt(0);
382 PolytomousKey subKey
= child
.getSubkey();
383 // this call initializes the subkey
386 polytomousKeyService
.delete(subKey
);
388 // since subKey was initialized before the delete, it will be
389 // accesible even after the delete. This behaviour is similar
390 // to hibernate session delete where the deleted object is
391 // made transient, but not deleted from the object graph
393 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
394 Assert
.assertNotNull(subKey
);
398 public void saveNewTermVocabulary() {
399 TermVocabulary termVocabulary
=
400 TermVocabulary
.NewInstance(TermType
.Feature
,
406 List
<TermVocabulary
<DefinedTermBase
>> vocabularies
= vocabularyService
.findByTermType(TermType
.Feature
);
408 for(TermVocabulary vocab
: vocabularies
) {
409 vocab
.getTermsOrderedByLabels(Language
.ENGLISH());
412 termVocabulary
= vocabularyService
.merge(termVocabulary
);
413 vocabularies
.add(termVocabulary
);
414 termVocabulary
.setLabel("Test");
415 vocabularyService
.merge(new ArrayList
<TermVocabulary
>(vocabularies
));
417 vocabularyService
.delete(termVocabulary
);
421 @Ignore // should be enabled once resolution of #5066 is merged into cdmlib main branch
423 public void saveNewTerm() {
424 UUID vocNameFeatureUuid
= UUID
.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");
425 UUID vocFeatureUuid
= UUID
.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
426 DefinedTermBase newTerm
= TermType
.Feature
.getEmptyDefinedTermBase();
427 newTerm
.setLabel("CreateTest");
431 Assert
.assertNotNull(newTerm
);
432 TermVocabulary vocNameFeature
= vocabularyService
.find(vocNameFeatureUuid
);
433 TermVocabulary vocFeature
= vocabularyService
.find(vocFeatureUuid
);
435 List
<TermVocabulary
> vocs
= new ArrayList
<TermVocabulary
>();
437 vocs
.add(vocNameFeature
);
438 vocs
.add(vocFeature
);
440 vocNameFeature
.addTerm(newTerm
);
442 vocs
= vocabularyService
.merge(vocs
);
443 for(TermVocabulary voc
: vocs
) {
444 if(voc
.getUuid().equals(vocNameFeatureUuid
)) {
445 vocNameFeature
= voc
;
449 Assert
.assertTrue(vocNameFeature
.getTerms().contains(newTerm
));
451 for(Object obj
: vocNameFeature
.getTerms()) {
452 DefinedTermBase dtb
= (DefinedTermBase
)obj
;
453 if("CreateTest".equals(dtb
.getLabel())) {
455 Assert
.assertNotNull(dtb
.getCreatedBy());
456 Assert
.assertNotNull(dtb
.getCreated());
458 Assert
.assertNull(dtb
.getCreatedBy());
461 newTerm
.setLabel("UpdateTest");
462 newTerm
= termService
.merge(newTerm
);
463 Assert
.assertNotNull(newTerm
.getUpdatedBy());
464 Assert
.assertNotNull(newTerm
.getUpdated());
466 Assert
.assertNull(vocNameFeature
.getCreatedBy());
467 Assert
.assertNull(vocFeature
.getCreatedBy());
469 if(termService
.find(newTerm
.getUuid()) != null) {
470 termService
.delete(newTerm
.getUuid());
475 @Ignore // activate after fixing #5138
477 public void savePerson() {
478 TeamOrPersonBase teamOrPerson
= Person
.NewInstance();
479 teamOrPerson
.setTitleCache("New Person", true);
480 teamOrPerson
= (TeamOrPersonBase
)CdmApplicationState
.getCurrentAppConfig().getAgentService().merge(teamOrPerson
);
481 //CdmApplicationState.getCurrentAppConfig().getAgentService().saveOrUpdate(teamOrPerson);
483 Person person
= (Person
)teamOrPerson
;
484 // person.setFirstname("Some");
485 // CdmApplicationState.getCurrentAppConfig().getAgentService().merge(teamOrPerson);
487 person
= (Person
) CdmApplicationState
.getCurrentAppConfig().getAgentService().find(person
.getUuid());
488 person
.setFirstname("Some");
489 CdmApplicationState
.getCurrentAppConfig().getAgentService().merge(teamOrPerson
);