CdmTransientEntityCacher, EntityCacherDebugResult : moved debug methods to debug...
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / cdm / model / PolytomousKeyTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.model;
11
12 import java.util.Arrays;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import org.apache.log4j.Level;
20 import org.apache.log4j.Logger;
21 import org.hibernate.collection.spi.PersistentCollection;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.unitils.dbunit.annotation.DataSet;
27
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.model.agent.AgentBase;
33 import eu.etaxonomy.cdm.model.common.CdmBase;
34 import eu.etaxonomy.cdm.model.common.Credit;
35 import eu.etaxonomy.cdm.model.common.Extension;
36 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
37 import eu.etaxonomy.cdm.model.common.Language;
38 import eu.etaxonomy.cdm.model.common.LanguageString;
39 import eu.etaxonomy.cdm.model.description.KeyStatement;
40 import eu.etaxonomy.cdm.model.description.PolytomousKey;
41 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
42 import eu.etaxonomy.cdm.model.media.Rights;
43 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
44 import eu.etaxonomy.cdm.model.taxon.Taxon;
45 import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
46 import eu.etaxonomy.taxeditor.httpinvoker.CDMServer;
47 import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
48 import eu.etaxonomy.taxeditor.remoting.cache.EntityCacherDebugResult;
49 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
50 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
51 import eu.etaxonomy.taxeditor.session.MockSessionOwner;
52
53 /**
54 * @author cmathew
55 * @date 7 Oct 2014
56 *
57 */
58 @DataSet
59 public class PolytomousKeyTest extends BaseRemotingTest {
60
61 private static final Logger logger = Logger.getLogger(PolytomousKeyTest.class);
62
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
68
69 IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
70 IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
71 ICommonService commonService = getRemoteApplicationController().getCommonService();
72 ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
73
74
75 private CdmTransientEntityCacher cacher;
76 private ICdmEntitySessionEnabled sessionOwner;
77
78 private ICdmEntitySession cdmEntitySession;
79
80 Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
81
82
83
84 @BeforeClass
85 public static void initializePolytomousKeyTest() {
86 logger.setLevel(Level.INFO);
87 CDMServer.getInstance().setKeepServerRunning(true);
88 initializeController("default",
89 "localhost",
90 8080,
91 "",
92 NomenclaturalCode.ICNAFP,
93 "admin",
94 "00000");
95 }
96
97 @Before
98 public void initializeSession() {
99 sessionOwner = new MockSessionOwner();
100 cdmEntitySession = cdmEntitySessionManager.newSession(sessionOwner, true);
101 cacher = getCacher(sessionOwner);
102 }
103
104 @Test
105 public void readAllPolytomousKeys() {
106 List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
107 Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
108 Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
109 Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
110 }
111
112
113 @Test
114 //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
115 public void readPolytmousKeyData() {
116 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
117 Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
118 Iterator<Taxon> tsItr = taxonomicScope.iterator();
119 Taxon taxon = tsItr.next();
120
121 Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
122 Assert.assertEquals(tsItr.hasNext(), false);
123
124 List<Credit> credits = pKey.getCredits();
125 AgentBase agent = credits.get(0).getAgent();
126 Assert.assertEquals(agent.getId(),4809);
127 Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
128 Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
129
130 Set<Extension> exts = pKey.getExtensions();
131 Iterator<Extension> extItr = exts.iterator();
132 Extension ext = extItr.next();
133 Assert.assertEquals(ext.getValue(), "http://test.com");
134
135 Set<Rights> rights = pKey.getRights();
136 Iterator<Rights> rightsItr = rights.iterator();
137 Rights right = rightsItr.next();
138 Assert.assertEquals(right.getText(),"Rights Text Test");
139
140 Set<IdentifiableSource> sources = pKey.getSources();
141 Iterator<IdentifiableSource> sourcesItr = sources.iterator();
142 IdentifiableSource source = sourcesItr.next();
143 Assert.assertEquals(source.getId(), 23710);
144 source = sourcesItr.next();
145 Assert.assertEquals(source.getId(), 23711);
146
147 // TO DO : Added tests for Annotations , Markers
148 }
149
150 @Test
151 public void readPolytomousKeyDataFromNodes() {
152 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
153 PolytomousKeyNode rootNode = pKey.getRoot();
154
155
156 Assert.assertEquals(rootNode.getId(), 2750);
157
158 Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
159 Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
160 Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
161 Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
162 Assert.assertEquals(sortIndex, new Integer(0));
163 String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
164 Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(english), statement);
165
166 Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
167 Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
168 Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
169 sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
170 Assert.assertEquals(sortIndex, new Integer(1));
171 statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
172 Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(english), statement);
173
174 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
175 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
176 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
177 sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
178 Assert.assertEquals(sortIndex, new Integer(0));
179 statement = "Ray-florets yellow";
180 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(english), statement);
181 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
182 }
183
184 @Test
185 public void savePolytomousKeyNodeData() {
186 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
187
188 PolytomousKeyNode pkeynode = pkey.getRoot();
189 String newQuestion = "New Question";
190 String newStatement = "New Statement";
191
192 Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(english).getText());
193 for(PolytomousKeyNode node : pkeynode.getChildren()) {
194
195 node.setQuestion(null);
196 node.setStatement(null);
197 }
198 //FIXME:Remoting Add tests for feature after fixing problem
199
200 //Feature feature = pkeynode.getFeature();
201 //Assert.assertEquals(feature.getTitleCache(),"Systematics");
202 //pkeynode.setFeature(null);
203
204 Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(english).getText(),"Modifying Text 1a");
205 String modifyingText = "Modifying Text 1a updated";
206
207 //pkeynode.getChildAt(0).putModifyingText(english, modifyingText);
208
209 Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
210 Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
211 Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
212 Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
213 pkeynode.getChildAt(1).setTaxon(taxon);
214
215 EntityCacherDebugResult debugResult = new EntityCacherDebugResult(cacher, Arrays.asList(pkey));
216
217 polytomousKeyService.merge(pkey);
218
219 pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
220 sessionOwner = new MockSessionOwner();
221
222
223 pkeynode = pkey.getRoot();
224 for(PolytomousKeyNode node : pkeynode.getChildren()) {
225 Assert.assertNull(node.getQuestion());
226 node.setQuestion(KeyStatement.NewInstance(english,newQuestion));
227 Assert.assertNull(node.getStatement());
228 node.setStatement(KeyStatement.NewInstance(english,newStatement));
229 }
230
231
232 Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
233
234 polytomousKeyService.merge(pkey);
235
236 pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
237 sessionOwner = new MockSessionOwner();
238
239
240 pkeynode = pkey.getRoot();
241 for(PolytomousKeyNode node : pkeynode.getChildren()) {
242 Assert.assertNotNull(node.getQuestion());
243 Map<Language, LanguageString> label = node.getQuestion().getLabel();
244 Assert.assertEquals(newQuestion, label.get(english).getText());
245 Assert.assertNotNull(node.getStatement());
246 Assert.assertEquals(newStatement, node.getStatement().getLabel(english).getText());
247 }
248 //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
249
250 }
251
252 @Test
253 public void savePolytomousKeyNodeDataWithSameSubKey() {
254
255 ICdmEntitySessionEnabled sessionOwner = new MockSessionOwner();
256 cdmEntitySessionManager.bind(sessionOwner);
257 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
258
259
260 PolytomousKeyNode pkeynode = pkey.getRoot();
261
262 PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
263 String subkey1title = subkey1.getTitleCache();
264 subkey1.setTitleCache(subkey1title + "test", true);
265
266
267 PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
268 String subkey2title = subkey2.getTitleCache();
269 subkey2.setTitleCache(subkey2title + "test", true);
270
271 Assert.assertSame(subkey1, subkey2);
272
273 polytomousKeyService.merge(pkey);
274 }
275
276 @Test
277 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
278
279 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
280
281
282 PolytomousKeyNode pkeynode = pkey.getRoot();
283
284 PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
285 PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(children, 0);
286 PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
287 String subkey1title = subkey1.getTitleCache();
288 subkey1.setTitleCache(subkey1title + "test", true);
289
290 PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(children, 1);
291 PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
292 String subkey2title = subkey2.getTitleCache();
293 subkey2.setTitleCache(subkey2title + "test", true);
294
295 Assert.assertNotSame(childNode0, childNode1);
296
297 Assert.assertSame(subkey1, subkey2);
298
299 polytomousKeyService.merge(pkey);
300 }
301
302
303 @Test
304 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
305 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
306 PolytomousKeyNode pkeynode = pkey.getRoot();
307
308 Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
309 label1.size();
310
311
312 Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
313 label2.size();
314
315
316 polytomousKeyService.merge(pkey);
317 }
318
319 @Test
320 public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
321 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
322
323
324 PolytomousKeyNode rootNode = pKey.getRoot();
325 PolytomousKeyNode child = rootNode.getChildAt(0);
326 PolytomousKey subKey = child.getSubkey();
327
328 polytomousKeyService.delete(subKey);
329
330 // since subKey was not initialized before the delete, an attempt
331 // to initialize it after will lead to an NPE
332 try {
333 PolytomousKeyNode subKeyRootNode = subKey.getRoot();
334 Assert.fail("A NullPointerException indicating null root node should be thrown here");
335 } catch(NullPointerException npe) {
336
337 }
338 // retrieving subkey shows its null
339 subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
340 Assert.assertNull(subKey);
341
342 pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
343
344
345 rootNode = pKey.getRoot();
346 child = rootNode.getChildAt(0);
347 subKey = child.getSubkey();
348 // subkey will not be null, because the delete functionality of the subkey
349 // does not currently delete it from a polytomous key node
350 Assert.assertNotNull(subKey);
351 // after reloading the object graph, accessing subKey should throw a null pointer exception
352 try {
353 PolytomousKeyNode subKeyRootNode = subKey.getRoot();
354 Assert.fail("A NullPointerException indicating null root node should be thrown here");
355 } catch(NullPointerException npe) {
356
357 }
358
359 }
360
361 @Test
362 public void deleteSubKeyInPolytomousNode() {
363 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
364
365
366 PolytomousKeyNode rootNode = pKey.getRoot();
367 List<PolytomousKeyNode> children = rootNode.getChildren();
368 PolytomousKeyNode child = rootNode.getChildAt(0);
369 polytomousKeyNodeService.delete(child.getUuid(), true);
370
371 pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
372
373 rootNode = pKey.getRoot();
374 children = rootNode.getChildren();
375 Assert.assertFalse(children.contains(child));
376 }
377
378 @Test
379 public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
380 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
381
382
383 PolytomousKeyNode rootNode = pKey.getRoot();
384 PolytomousKeyNode child = rootNode.getChildAt(0);
385 PolytomousKey subKey = child.getSubkey();
386 // this call initializes the subkey
387 subKey.getRoot();
388
389 polytomousKeyService.delete(subKey);
390
391 // since subKey was initialized before the delete, it will be
392 // accesible even after the delete. This behaviour is similar
393 // to hibernate session delete where the deleted object is
394 // made transient, but not deleted from the object graph
395
396 PolytomousKeyNode subKeyRootNode = subKey.getRoot();
397 Assert.assertNotNull(subKey);
398
399 }
400
401
402 }