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
.cdm
.model
;
12 import java
.util
.Arrays
;
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
.Test
;
24 import org
.unitils
.dbunit
.annotation
.DataSet
;
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
.model
.agent
.AgentBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.Credit
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
34 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
35 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
36 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
37 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
38 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
39 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
40 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
41 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
42 import eu
.etaxonomy
.taxeditor
.httpinvoker
.RemotingSessionAwareTest
;
43 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.EntityCacherDebugResult
;
44 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
45 import eu
.etaxonomy
.taxeditor
.session
.MockSessionOwner
;
53 public class PolytomousKeyTest
extends RemotingSessionAwareTest
{
55 private static final Logger logger
= Logger
.getLogger(PolytomousKeyTest
.class);
57 UUID polytomousKeyUuid
= UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
58 UUID subKeyUuid
= UUID
.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01");
59 //UUID polytomousKeyUuid = UUID.fromString("bab66772-2c83-428a-bb6d-655d12ac6097");
60 UUID taxon1Uuid
= UUID
.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
63 IPolytomousKeyService polytomousKeyService
= getRemoteApplicationController().getPolytomousKeyService();
64 IPolytomousKeyNodeService polytomousKeyNodeService
= getRemoteApplicationController().getPolytomousKeyNodeService();
65 ICommonService commonService
= getRemoteApplicationController().getCommonService();
66 ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
68 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
73 public static void initializePolytomousKeyTest() {
78 public void readAllPolytomousKeys() {
79 List
<PolytomousKey
> pKeys
= polytomousKeyService
.list(PolytomousKey
.class, null, null, null, null);
80 Iterator
<PolytomousKey
> pKeysItr
= pKeys
.iterator();
81 Assert
.assertEquals(pKeysItr
.next().getUuid(),UUID
.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
82 Assert
.assertEquals(pKeysItr
.next().getUuid(),UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
87 //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
88 public void readPolytmousKeyData() {
89 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
90 Set
<Taxon
> taxonomicScope
= pKey
.getTaxonomicScope();
91 Iterator
<Taxon
> tsItr
= taxonomicScope
.iterator();
92 Taxon taxon
= tsItr
.next();
94 Assert
.assertEquals("Achillea sec. Cyprus", taxon
.getTitleCache());
95 Assert
.assertEquals(tsItr
.hasNext(), false);
97 List
<Credit
> credits
= pKey
.getCredits();
98 AgentBase agent
= credits
.get(0).getAgent();
99 Assert
.assertEquals(agent
.getId(),4809);
100 Assert
.assertEquals(agent
.getTitleCache(),"R. A. Graham");
101 Assert
.assertEquals(credits
.get(0).getText(),"Credits Text Test");
103 Set
<Extension
> exts
= pKey
.getExtensions();
104 Iterator
<Extension
> extItr
= exts
.iterator();
105 Extension ext
= extItr
.next();
106 Assert
.assertEquals(ext
.getValue(), "http://test.com");
108 Set
<Rights
> rights
= pKey
.getRights();
109 Iterator
<Rights
> rightsItr
= rights
.iterator();
110 Rights right
= rightsItr
.next();
111 Assert
.assertEquals(right
.getText(),"Rights Text Test");
113 Set
<IdentifiableSource
> sources
= pKey
.getSources();
114 Iterator
<IdentifiableSource
> sourcesItr
= sources
.iterator();
115 IdentifiableSource source
= sourcesItr
.next();
116 Assert
.assertEquals(source
.getId(), 23710);
117 source
= sourcesItr
.next();
118 Assert
.assertEquals(source
.getId(), 23711);
120 // TO DO : Added tests for Annotations , Markers
124 public void readPolytomousKeyDataFromNodes() {
125 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
126 PolytomousKeyNode rootNode
= pKey
.getRoot();
129 Assert
.assertEquals(rootNode
.getId(), 2750);
131 Assert
.assertEquals(rootNode
.getChildAt(0).getId(), 2751);
132 Assert
.assertEquals(rootNode
.getChildAt(0).getParent().getId(), rootNode
.getId());
133 Assert
.assertEquals(rootNode
.getKey().getId(), pKey
.getId());
134 Integer sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(0), "sortIndex");
135 Assert
.assertEquals(sortIndex
, new Integer(0));
136 String statement
= "Capitula without ligulate ray-florets; leaves entire or subentire";
137 Assert
.assertEquals(rootNode
.getChildAt(0).getStatement().getLabelText(english
), statement
);
139 Assert
.assertEquals(rootNode
.getChildAt(1).getId(), 2753);
140 Assert
.assertEquals(rootNode
.getChildAt(1).getParent().getId(), rootNode
.getId());
141 Assert
.assertEquals(rootNode
.getChildAt(1).getKey().getId(), pKey
.getId());
142 sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(1), "sortIndex");
143 Assert
.assertEquals(sortIndex
, new Integer(1));
144 statement
= "Capitula with ligulate ray-florets; leaves pinnatisect";
145 Assert
.assertEquals(rootNode
.getChildAt(1).getStatement().getLabelText(english
), statement
);
147 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getId(), 2754);
148 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getParent().getId(), rootNode
.getChildAt(1).getId());
149 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getKey().getId(), pKey
.getId());
150 sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(1).getChildAt(0), "sortIndex");
151 Assert
.assertEquals(sortIndex
, new Integer(0));
152 statement
= "Ray-florets yellow";
153 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getStatement().getLabelText(english
), statement
);
154 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
158 public void savePolytomousKeyNodeData() {
159 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
161 PolytomousKeyNode pkeynode
= pkey
.getRoot();
162 String newQuestion
= "New Question";
163 String newStatement
= "New Statement";
165 Assert
.assertEquals("Question 1",pkeynode
.getQuestion().getLabel().get(english
).getText());
166 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
168 node
.setQuestion(null);
169 node
.setStatement(null);
171 //FIXME:Remoting Add tests for feature after fixing problem
173 //Feature feature = pkeynode.getFeature();
174 //Assert.assertEquals(feature.getTitleCache(),"Systematics");
175 //pkeynode.setFeature(null);
177 Assert
.assertEquals(pkeynode
.getChildAt(0).getModifyingText().get(english
).getText(),"Modifying Text 1a");
178 String modifyingText
= "Modifying Text 1a updated";
180 //pkeynode.getChildAt(0).putModifyingText(english, modifyingText);
182 Assert
.assertEquals(pkeynode
.getChildAt(0).getSubkey().getId(),751);
183 Assert
.assertEquals("Asphodeline", pkeynode
.getChildAt(0).getSubkey().getTitleCache());
184 Assert
.assertNull(pkeynode
.getChildAt(1).getTaxon());
185 Taxon taxon
= CdmBase
.deproxy(taxonService
.find(taxon1Uuid
),Taxon
.class);
186 pkeynode
.getChildAt(1).setTaxon(taxon
);
188 EntityCacherDebugResult debugResult
= new EntityCacherDebugResult(cacher
, Arrays
.asList(pkey
));
190 polytomousKeyService
.merge(pkey
);
192 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
193 sessionOwner
= new MockSessionOwner();
196 pkeynode
= pkey
.getRoot();
197 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
198 Assert
.assertNull(node
.getQuestion());
199 node
.setQuestion(KeyStatement
.NewInstance(english
,newQuestion
));
200 Assert
.assertNull(node
.getStatement());
201 node
.setStatement(KeyStatement
.NewInstance(english
,newStatement
));
205 Assert
.assertEquals(pkeynode
.getChildAt(1).getTaxon(), taxon
);
207 polytomousKeyService
.merge(pkey
);
209 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
210 sessionOwner
= new MockSessionOwner();
213 pkeynode
= pkey
.getRoot();
214 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
215 Assert
.assertNotNull(node
.getQuestion());
216 Map
<Language
, LanguageString
> label
= node
.getQuestion().getLabel();
217 Assert
.assertEquals(newQuestion
, label
.get(english
).getText());
218 Assert
.assertNotNull(node
.getStatement());
219 Assert
.assertEquals(newStatement
, node
.getStatement().getLabel(english
).getText());
221 //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
226 public void savePolytomousKeyNodeDataWithSameSubKey() {
228 ICdmEntitySessionEnabled sessionOwner
= new MockSessionOwner();
229 cdmEntitySessionManager
.bind(sessionOwner
);
230 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
233 PolytomousKeyNode pkeynode
= pkey
.getRoot();
235 PolytomousKey subkey1
= CdmBase
.deproxy(pkeynode
.getChildAt(0).getSubkey(), PolytomousKey
.class);
236 String subkey1title
= subkey1
.getTitleCache();
237 subkey1
.setTitleCache(subkey1title
+ "test", true);
240 PolytomousKey subkey2
= CdmBase
.deproxy(pkeynode
.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey
.class);
241 String subkey2title
= subkey2
.getTitleCache();
242 subkey2
.setTitleCache(subkey2title
+ "test", true);
244 Assert
.assertSame(subkey1
, subkey2
);
246 polytomousKeyService
.merge(pkey
);
250 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
252 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
255 PolytomousKeyNode pkeynode
= pkey
.getRoot();
257 PersistentCollection children
= (PersistentCollection
) pkeynode
.getChildren();
258 PolytomousKeyNode childNode0
= (PolytomousKeyNode
)commonService
.get(children
, 0);
259 PolytomousKey subkey1
= CdmBase
.deproxy(childNode0
.getSubkey(),PolytomousKey
.class);
260 String subkey1title
= subkey1
.getTitleCache();
261 subkey1
.setTitleCache(subkey1title
+ "test", true);
263 PolytomousKeyNode childNode1
= (PolytomousKeyNode
)commonService
.get(children
, 1);
264 PolytomousKey subkey2
= CdmBase
.deproxy(childNode1
.getSubkey(),PolytomousKey
.class);
265 String subkey2title
= subkey2
.getTitleCache();
266 subkey2
.setTitleCache(subkey2title
+ "test", true);
268 Assert
.assertNotSame(childNode0
, childNode1
);
270 Assert
.assertSame(subkey1
, subkey2
);
272 polytomousKeyService
.merge(pkey
);
277 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
278 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
279 PolytomousKeyNode pkeynode
= pkey
.getRoot();
281 Map
<Language
, LanguageString
> label1
= pkeynode
.getQuestion().getLabel();
285 Map
<Language
, LanguageString
> label2
= pkeynode
.getChildAt(0).getStatement().getLabel();
289 polytomousKeyService
.merge(pkey
);
293 public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
294 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
297 PolytomousKeyNode rootNode
= pKey
.getRoot();
298 PolytomousKeyNode child
= rootNode
.getChildAt(0);
299 PolytomousKey subKey
= child
.getSubkey();
301 polytomousKeyService
.delete(subKey
);
303 // since subKey was not initialized before the delete, an attempt
304 // to initialize it after will lead to an NPE
306 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
307 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
308 } catch(NullPointerException npe
) {
311 // retrieving subkey shows its null
312 subKey
= CdmBase
.deproxy(polytomousKeyService
.find(subKeyUuid
),PolytomousKey
.class);
313 Assert
.assertNull(subKey
);
315 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
318 rootNode
= pKey
.getRoot();
319 child
= rootNode
.getChildAt(0);
320 subKey
= child
.getSubkey();
321 // subkey will not be null, because the delete functionality of the subkey
322 // does not currently delete it from a polytomous key node
323 Assert
.assertNotNull(subKey
);
324 // after reloading the object graph, accessing subKey should throw a null pointer exception
326 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
327 Assert
.fail("A NullPointerException indicating null root node should be thrown here");
328 } catch(NullPointerException npe
) {
335 public void deleteSubKeyInPolytomousNode() {
336 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
339 PolytomousKeyNode rootNode
= pKey
.getRoot();
340 List
<PolytomousKeyNode
> children
= rootNode
.getChildren();
341 PolytomousKeyNode child
= rootNode
.getChildAt(0);
342 polytomousKeyNodeService
.delete(child
.getUuid(), true);
344 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
346 rootNode
= pKey
.getRoot();
347 children
= rootNode
.getChildren();
348 Assert
.assertFalse(children
.contains(child
));
352 public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
353 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
356 PolytomousKeyNode rootNode
= pKey
.getRoot();
357 PolytomousKeyNode child
= rootNode
.getChildAt(0);
358 PolytomousKey subKey
= child
.getSubkey();
359 // this call initializes the subkey
362 polytomousKeyService
.delete(subKey
);
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
369 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
370 Assert
.assertNotNull(subKey
);