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 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
);
173 polytomousKeyService
.merge(pKey
);
175 PolytomousKeyNode grandChildNode
= PolytomousKeyNode
.NewInstance();
176 childNode
.addChild(grandChildNode
);
178 polytomousKeyService
.merge(pKey
);
182 public void savePolytomousKeyNodeData() {
183 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
185 PolytomousKeyNode pkeynode
= pkey
.getRoot();
186 String newQuestion
= "New Question";
187 String newStatement
= "New Statement";
189 Assert
.assertEquals("Question 1",pkeynode
.getQuestion().getLabel().get(Language
.ENGLISH()).getText());
190 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
192 node
.setQuestion(null);
193 node
.setStatement(null);
195 //FIXME:Remoting Add tests for feature after fixing problem
197 //Feature feature = pkeynode.getFeature();
198 //Assert.assertEquals(feature.getTitleCache(),"Systematics");
199 //pkeynode.setFeature(null);
201 Assert
.assertEquals(pkeynode
.getChildAt(0).getModifyingText().get(Language
.ENGLISH()).getText(),"Modifying Text 1a");
202 String modifyingText
= "Modifying Text 1a updated";
204 //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
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
);
212 polytomousKeyService
.merge(pkey
);
214 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
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
));
225 Assert
.assertEquals(pkeynode
.getChildAt(1).getTaxon(), taxon
);
227 polytomousKeyService
.merge(pkey
);
229 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
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());
239 //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
245 public void savePolytomousKeyNodeDataWithSameSubKey() {
247 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
250 PolytomousKeyNode pkeynode
= pkey
.getRoot();
252 PolytomousKey subkey1
= CdmBase
.deproxy(pkeynode
.getChildAt(0).getSubkey(), PolytomousKey
.class);
253 String subkey1title
= subkey1
.getTitleCache();
254 subkey1
.setTitleCache(subkey1title
+ "test", true);
257 PolytomousKey subkey2
= CdmBase
.deproxy(pkeynode
.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey
.class);
258 String subkey2title
= subkey2
.getTitleCache();
259 subkey2
.setTitleCache(subkey2title
+ "test", true);
261 Assert
.assertSame(subkey1
, subkey2
);
263 polytomousKeyService
.merge(pkey
);
267 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
269 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
272 PolytomousKeyNode pkeynode
= pkey
.getRoot();
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);
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);
285 Assert
.assertNotSame(childNode0
, childNode1
);
287 Assert
.assertSame(subkey1
, subkey2
);
289 polytomousKeyService
.merge(pkey
);
295 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
297 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
298 PolytomousKeyNode pkeynode
= pkey
.getRoot();
300 Map
<Language
, LanguageString
> label1
= pkeynode
.getQuestion().getLabel();
304 Map
<Language
, LanguageString
> label2
= pkeynode
.getChildAt(0).getStatement().getLabel();
308 polytomousKeyService
.merge(pkey
);
312 public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
313 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
316 PolytomousKeyNode rootNode
= pKey
.getRoot();
317 PolytomousKeyNode child
= rootNode
.getChildAt(0);
318 PolytomousKey subKey
= child
.getSubkey();
320 polytomousKeyService
.delete(subKey
);
322 // since subKey was not initialized before the delete, an attempt
323 // to initialize it after will lead to an NPE
325 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
326 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
327 } catch(NullPointerException npe
) {
330 // retrieving subkey shows its null
331 subKey
= CdmBase
.deproxy(polytomousKeyService
.find(subKeyUuid
),PolytomousKey
.class);
332 Assert
.assertNull(subKey
);
334 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
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
345 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
346 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
347 } catch(NullPointerException npe
) {
355 public void deleteSubKeyInPolytomousNode() {
356 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
359 PolytomousKeyNode rootNode
= pKey
.getRoot();
360 List
<PolytomousKeyNode
> children
= rootNode
.getChildren();
361 PolytomousKeyNode child
= rootNode
.getChildAt(0);
362 polytomousKeyNodeService
.delete(child
.getUuid(), true);
364 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
366 rootNode
= pKey
.getRoot();
367 children
= rootNode
.getChildren();
368 Assert
.assertFalse(children
.contains(child
));
372 public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
373 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
376 PolytomousKeyNode rootNode
= pKey
.getRoot();
377 PolytomousKeyNode child
= rootNode
.getChildAt(0);
378 PolytomousKey subKey
= child
.getSubkey();
379 // this call initializes the subkey
382 polytomousKeyService
.delete(subKey
);
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
389 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
390 Assert
.assertNotNull(subKey
);
394 public void saveNewTermVocabulary() {
395 TermVocabulary termVocabulary
=
396 TermVocabulary
.NewInstance(TermType
.Feature
,
402 List
<TermVocabulary
<DefinedTermBase
>> vocabularies
= vocabularyService
.findByTermType(TermType
.Feature
);
404 for(TermVocabulary vocab
: vocabularies
) {
405 vocab
.getTermsOrderedByLabels(Language
.ENGLISH());
408 termVocabulary
= vocabularyService
.merge(termVocabulary
);
409 vocabularies
.add(termVocabulary
);
410 termVocabulary
.setLabel("Test");
411 vocabularyService
.merge(new ArrayList
<TermVocabulary
>(vocabularies
));
413 vocabularyService
.delete(termVocabulary
);
417 @Ignore // should be enabled once resolution of #5066 is merged into cdmlib main branch
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");
427 Assert
.assertNotNull(newTerm
);
428 TermVocabulary vocNameFeature
= vocabularyService
.find(vocNameFeatureUuid
);
429 TermVocabulary vocFeature
= vocabularyService
.find(vocFeatureUuid
);
431 List
<TermVocabulary
> vocs
= new ArrayList
<TermVocabulary
>();
433 vocs
.add(vocNameFeature
);
434 vocs
.add(vocFeature
);
436 vocNameFeature
.addTerm(newTerm
);
438 vocs
= vocabularyService
.merge(vocs
);
439 for(TermVocabulary voc
: vocs
) {
440 if(voc
.getUuid().equals(vocNameFeatureUuid
)) {
441 vocNameFeature
= voc
;
445 Assert
.assertTrue(vocNameFeature
.getTerms().contains(newTerm
));
447 for(Object obj
: vocNameFeature
.getTerms()) {
448 DefinedTermBase dtb
= (DefinedTermBase
)obj
;
449 if("CreateTest".equals(dtb
.getLabel())) {
451 Assert
.assertNotNull(dtb
.getCreatedBy());
452 Assert
.assertNotNull(dtb
.getCreated());
454 Assert
.assertNull(dtb
.getCreatedBy());
457 newTerm
.setLabel("UpdateTest");
458 newTerm
= termService
.merge(newTerm
);
459 Assert
.assertNotNull(newTerm
.getUpdatedBy());
460 Assert
.assertNotNull(newTerm
.getUpdated());
462 Assert
.assertNull(vocNameFeature
.getCreatedBy());
463 Assert
.assertNull(vocFeature
.getCreatedBy());
465 if(termService
.find(newTerm
.getUuid()) != null) {
466 termService
.delete(newTerm
.getUuid());