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
.Iterator
;
13 import java
.util
.List
;
16 import java
.util
.UUID
;
18 import org
.apache
.log4j
.Level
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.hibernate
.collection
.spi
.PersistentCollection
;
21 import org
.junit
.Assert
;
22 import org
.junit
.Before
;
23 import org
.junit
.BeforeClass
;
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
.model
.agent
.AgentBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Credit
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
35 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
36 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
37 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
38 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
39 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
40 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
41 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
42 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
44 import eu
.etaxonomy
.taxeditor
.httpinvoker
.BaseRemotingTest
;
45 import eu
.etaxonomy
.taxeditor
.httpinvoker
.CDMServer
;
46 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmTransientEntityCacher
;
47 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
48 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
49 import eu
.etaxonomy
.taxeditor
.session
.MockSessionOwner
;
57 public class PolytomousKeyTest
extends BaseRemotingTest
{
59 private static final Logger logger
= Logger
.getLogger(PolytomousKeyTest
.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();
73 private CdmTransientEntityCacher cacher
;
74 private ICdmEntitySessionEnabled sessionOwner
;
76 private ICdmEntitySession cdmEntitySession
;
78 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
83 public static void initializePolytomousKeyTest() {
84 logger
.setLevel(Level
.INFO
);
85 CDMServer
.getInstance().setKeepServerRunning(true);
86 initializeController("default",
90 NomenclaturalCode
.ICNAFP
,
96 public void initializeSession() {
97 sessionOwner
= new MockSessionOwner();
98 cdmEntitySession
= cdmEntitySessionManager
.newSession(sessionOwner
, true);
99 cacher
= getCacher(sessionOwner
);
103 public void readAllPolytomousKeys() {
104 List
<PolytomousKey
> pKeys
= polytomousKeyService
.list(PolytomousKey
.class, null, null, null, null);
105 Iterator
<PolytomousKey
> pKeysItr
= pKeys
.iterator();
106 Assert
.assertEquals(pKeysItr
.next().getUuid(),UUID
.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
107 Assert
.assertEquals(pKeysItr
.next().getUuid(),UUID
.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
112 //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
113 public void readPolytmousKeyData() {
114 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
115 Set
<Taxon
> taxonomicScope
= pKey
.getTaxonomicScope();
116 Iterator
<Taxon
> tsItr
= taxonomicScope
.iterator();
117 Taxon taxon
= tsItr
.next();
119 Assert
.assertEquals("Achillea sec. Cyprus", taxon
.getTitleCache());
120 Assert
.assertEquals(tsItr
.hasNext(), false);
122 List
<Credit
> credits
= pKey
.getCredits();
123 AgentBase agent
= credits
.get(0).getAgent();
124 Assert
.assertEquals(agent
.getId(),4809);
125 Assert
.assertEquals(agent
.getTitleCache(),"R. A. Graham");
126 Assert
.assertEquals(credits
.get(0).getText(),"Credits Text Test");
128 Set
<Extension
> exts
= pKey
.getExtensions();
129 Iterator
<Extension
> extItr
= exts
.iterator();
130 Extension ext
= extItr
.next();
131 Assert
.assertEquals(ext
.getValue(), "http://test.com");
133 Set
<Rights
> rights
= pKey
.getRights();
134 Iterator
<Rights
> rightsItr
= rights
.iterator();
135 Rights right
= rightsItr
.next();
136 Assert
.assertEquals(right
.getText(),"Rights Text Test");
138 Set
<IdentifiableSource
> sources
= pKey
.getSources();
139 Iterator
<IdentifiableSource
> sourcesItr
= sources
.iterator();
140 IdentifiableSource source
= sourcesItr
.next();
141 Assert
.assertEquals(source
.getId(), 23710);
142 source
= sourcesItr
.next();
143 Assert
.assertEquals(source
.getId(), 23711);
145 // TO DO : Added tests for Annotations , Markers
149 public void readPolytomousKeyDataFromNodes() {
150 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
151 PolytomousKeyNode rootNode
= pKey
.getRoot();
154 Assert
.assertEquals(rootNode
.getId(), 2750);
156 Assert
.assertEquals(rootNode
.getChildAt(0).getId(), 2751);
157 Assert
.assertEquals(rootNode
.getChildAt(0).getParent().getId(), rootNode
.getId());
158 Assert
.assertEquals(rootNode
.getKey().getId(), pKey
.getId());
159 Integer sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(0), "sortIndex");
160 Assert
.assertEquals(sortIndex
, new Integer(0));
161 String statement
= "Capitula without ligulate ray-florets; leaves entire or subentire";
162 Assert
.assertEquals(rootNode
.getChildAt(0).getStatement().getLabelText(english
), statement
);
164 Assert
.assertEquals(rootNode
.getChildAt(1).getId(), 2753);
165 Assert
.assertEquals(rootNode
.getChildAt(1).getParent().getId(), rootNode
.getId());
166 Assert
.assertEquals(rootNode
.getChildAt(1).getKey().getId(), pKey
.getId());
167 sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(1), "sortIndex");
168 Assert
.assertEquals(sortIndex
, new Integer(1));
169 statement
= "Capitula with ligulate ray-florets; leaves pinnatisect";
170 Assert
.assertEquals(rootNode
.getChildAt(1).getStatement().getLabelText(english
), statement
);
172 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getId(), 2754);
173 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getParent().getId(), rootNode
.getChildAt(1).getId());
174 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getKey().getId(), pKey
.getId());
175 sortIndex
= (Integer
)getFieldValueViaReflection(rootNode
.getChildAt(1).getChildAt(0), "sortIndex");
176 Assert
.assertEquals(sortIndex
, new Integer(0));
177 statement
= "Ray-florets yellow";
178 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getStatement().getLabelText(english
), statement
);
179 Assert
.assertEquals(rootNode
.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
183 public void savePolytomousKeyNodeData() {
184 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
185 //pkey = cdmEntitySession.load(pkey);
186 PolytomousKeyNode pkeynode
= pkey
.getRoot();
187 String newQuestion
= "New Question";
188 String newStatement
= "New Statement";
190 Assert
.assertEquals("Question 1",pkeynode
.getQuestion().getLabel().get(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(english
).getText(),"Modifying Text 1a");
203 String modifyingText
= "Modifying Text 1a updated";
205 //pkeynode.getChildAt(0).putModifyingText(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);
216 sessionOwner
= new MockSessionOwner();
217 pkey
= cdmEntitySession
.load(pkey
);
219 pkeynode
= pkey
.getRoot();
220 for(PolytomousKeyNode node
: pkeynode
.getChildren()) {
221 Assert
.assertNull(node
.getQuestion());
222 node
.setQuestion(KeyStatement
.NewInstance(english
,newQuestion
));
223 Assert
.assertNull(node
.getStatement());
224 node
.setStatement(KeyStatement
.NewInstance(english
,newStatement
));
228 Assert
.assertEquals(pkeynode
.getChildAt(1).getTaxon(), taxon
);
230 polytomousKeyService
.merge(pkey
);
232 pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
233 sessionOwner
= new MockSessionOwner();
234 pkey
= cdmEntitySession
.load(pkey
);
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(english
).getText());
241 Assert
.assertNotNull(node
.getStatement());
242 Assert
.assertEquals(newStatement
, node
.getStatement().getLabel(english
).getText());
244 //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
249 public void savePolytomousKeyNodeDataWithSameSubKey() {
251 ICdmEntitySessionEnabled sessionOwner
= new MockSessionOwner();
252 cdmEntitySessionManager
.bind(sessionOwner
);
253 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
254 pkey
= cdmEntitySession
.load(pkey
);
256 PolytomousKeyNode pkeynode
= pkey
.getRoot();
258 PolytomousKey subkey1
= CdmBase
.deproxy(pkeynode
.getChildAt(0).getSubkey(), PolytomousKey
.class);
259 String subkey1title
= subkey1
.getTitleCache();
260 subkey1
.setTitleCache(subkey1title
+ "test", true);
263 PolytomousKey subkey2
= CdmBase
.deproxy(pkeynode
.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey
.class);
264 String subkey2title
= subkey2
.getTitleCache();
265 subkey2
.setTitleCache(subkey2title
+ "test", true);
267 Assert
.assertSame(subkey1
, subkey2
);
269 polytomousKeyService
.merge(pkey
);
273 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
275 PolytomousKey pkey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
276 pkey
= cdmEntitySession
.load(pkey
);
278 PolytomousKeyNode pkeynode
= pkey
.getRoot();
280 PersistentCollection children
= (PersistentCollection
) pkeynode
.getChildren();
281 PolytomousKeyNode childNode0
= (PolytomousKeyNode
)commonService
.get(children
, 0);
282 PolytomousKey subkey1
= CdmBase
.deproxy(childNode0
.getSubkey(),PolytomousKey
.class);
283 String subkey1title
= subkey1
.getTitleCache();
284 subkey1
.setTitleCache(subkey1title
+ "test", true);
286 PolytomousKeyNode childNode1
= (PolytomousKeyNode
)commonService
.get(children
, 1);
287 PolytomousKey subkey2
= CdmBase
.deproxy(childNode1
.getSubkey(),PolytomousKey
.class);
288 String subkey2title
= subkey2
.getTitleCache();
289 subkey2
.setTitleCache(subkey2title
+ "test", true);
291 Assert
.assertNotSame(childNode0
, childNode1
);
293 Assert
.assertSame(subkey1
, subkey2
);
295 polytomousKeyService
.merge(pkey
);
300 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);
318 pKey
= cdmEntitySession
.load(pKey
);
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);
339 pKey
= cdmEntitySession
.load(pKey
);
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
) {
358 public void deleteSubKeyInPolytomousNode() {
359 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
360 pKey
= cdmEntitySession
.load(pKey
);
362 PolytomousKeyNode rootNode
= pKey
.getRoot();
363 List
<PolytomousKeyNode
> children
= rootNode
.getChildren();
364 PolytomousKeyNode child
= rootNode
.getChildAt(0);
365 polytomousKeyNodeService
.delete(child
, true, true);
367 pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
369 rootNode
= pKey
.getRoot();
370 children
= rootNode
.getChildren();
371 Assert
.assertFalse(children
.contains(child
));
375 public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
376 PolytomousKey pKey
= CdmBase
.deproxy(polytomousKeyService
.find(polytomousKeyUuid
),PolytomousKey
.class);
377 pKey
= cdmEntitySession
.load(pKey
);
379 PolytomousKeyNode rootNode
= pKey
.getRoot();
380 PolytomousKeyNode child
= rootNode
.getChildAt(0);
381 PolytomousKey subKey
= child
.getSubkey();
382 // this call initializes the subkey
385 polytomousKeyService
.delete(subKey
);
387 // since subKey was initialized before the delete, it will be
388 // accesible even after the delete. This behaviour is similar
389 // to hibernate session delete where the deleted object is
390 // made transient, but not deleted from the object graph
392 PolytomousKeyNode subKeyRootNode
= subKey
.getRoot();
393 Assert
.assertNotNull(subKey
);