#5138 Add test for cascade error
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / taxeditor / session / CdmEntitySessionAwareTest.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.taxeditor.session;
11
12 import java.util.ArrayList;
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.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;
26
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;
53
54 /**
55 * @author cmathew
56 * @date 7 Oct 2014
57 *
58 */
59 @DataSet
60 public class CdmEntitySessionAwareTest extends RemotingSessionAwareTest {
61
62 private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.class);
63
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");
68
69
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();
77
78 //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
79
80
81
82 @BeforeClass
83 public static void initializePolytomousKeyTest() {
84 }
85
86
87 @Test
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"));
93 }
94
95
96 @Test
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();
103
104 Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
105 Assert.assertEquals(tsItr.hasNext(), false);
106
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");
112
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");
117
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");
122
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);
129
130 // TO DO : Added tests for Annotations , Markers
131 }
132
133
134
135 @Test
136 public void readPolytomousKeyDataFromNodes() {
137 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
138 PolytomousKeyNode rootNode = pKey.getRoot();
139
140
141 Assert.assertEquals(rootNode.getId(), 2750);
142
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);
150
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);
158
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");
167 }
168
169 @Test
170 public void addGreatGrandChildPolytomousKeyNode() {
171
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);
176
177 grandChildNode = polytomousKeyNodeService.merge(grandChildNode);
178
179 PolytomousKeyNode greatGrandChildNode = PolytomousKeyNode.NewInstance();
180 grandChildNode.addChild(greatGrandChildNode);
181
182 Assert.assertFalse(pKey.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
183 }
184
185 @Test
186 public void savePolytomousKeyNodeData() {
187 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
188
189 PolytomousKeyNode pkeynode = pkey.getRoot();
190 String newQuestion = "New Question";
191 String newStatement = "New Statement";
192
193 Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());
194 for(PolytomousKeyNode node : pkeynode.getChildren()) {
195
196 node.setQuestion(null);
197 node.setStatement(null);
198 }
199 //FIXME:Remoting Add tests for feature after fixing problem
200
201 //Feature feature = pkeynode.getFeature();
202 //Assert.assertEquals(feature.getTitleCache(),"Systematics");
203 //pkeynode.setFeature(null);
204
205 Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(Language.ENGLISH()).getText(),"Modifying Text 1a");
206 String modifyingText = "Modifying Text 1a updated";
207
208 //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
209
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);
215
216 polytomousKeyService.merge(pkey);
217
218 pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
219
220
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));
227 }
228
229 Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
230
231 polytomousKeyService.merge(pkey);
232
233 pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
234
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());
242 }
243 //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
244
245 }
246
247
248 @Test
249 public void savePolytomousKeyNodeDataWithSameSubKey() {
250
251 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
252
253
254 PolytomousKeyNode pkeynode = pkey.getRoot();
255
256 PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
257 String subkey1title = subkey1.getTitleCache();
258 subkey1.setTitleCache(subkey1title + "test", true);
259
260
261 PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
262 String subkey2title = subkey2.getTitleCache();
263 subkey2.setTitleCache(subkey2title + "test", true);
264
265 Assert.assertSame(subkey1, subkey2);
266
267 polytomousKeyService.merge(pkey);
268 }
269
270 @Test
271 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
272
273 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
274
275
276 PolytomousKeyNode pkeynode = pkey.getRoot();
277
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);
283
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);
288
289 Assert.assertNotSame(childNode0, childNode1);
290
291 Assert.assertSame(subkey1, subkey2);
292
293 polytomousKeyService.merge(pkey);
294 }
295
296
297
298 @Test
299 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
300
301 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
302 PolytomousKeyNode pkeynode = pkey.getRoot();
303
304 Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
305 label1.size();
306
307
308 Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
309 label2.size();
310
311
312 polytomousKeyService.merge(pkey);
313 }
314
315 @Test
316 public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
317 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
318
319
320 PolytomousKeyNode rootNode = pKey.getRoot();
321 PolytomousKeyNode child = rootNode.getChildAt(0);
322 PolytomousKey subKey = child.getSubkey();
323
324 polytomousKeyService.delete(subKey);
325
326 // since subKey was not initialized before the delete, an attempt
327 // to initialize it after will lead to an NPE
328 try {
329 PolytomousKeyNode subKeyRootNode = subKey.getRoot();
330 Assert.fail("A NullPointerException indicating null root node should be thrown here");
331 } catch(NullPointerException npe) {
332
333 }
334 // retrieving subkey shows its null
335 subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
336 Assert.assertNull(subKey);
337
338 pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
339
340
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
348 try {
349 PolytomousKeyNode subKeyRootNode = subKey.getRoot();
350 Assert.fail("A NullPointerException indicating null root node should be thrown here");
351 } catch(NullPointerException npe) {
352
353 }
354
355 }
356
357
358 @Test
359 public void deleteSubKeyInPolytomousNode() {
360 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
361
362
363 PolytomousKeyNode rootNode = pKey.getRoot();
364 List<PolytomousKeyNode> children = rootNode.getChildren();
365 PolytomousKeyNode child = rootNode.getChildAt(0);
366 polytomousKeyNodeService.delete(child.getUuid(), true);
367
368 pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
369
370 rootNode = pKey.getRoot();
371 children = rootNode.getChildren();
372 Assert.assertFalse(children.contains(child));
373 }
374
375 @Test
376 public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
377 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
378
379
380 PolytomousKeyNode rootNode = pKey.getRoot();
381 PolytomousKeyNode child = rootNode.getChildAt(0);
382 PolytomousKey subKey = child.getSubkey();
383 // this call initializes the subkey
384 subKey.getRoot();
385
386 polytomousKeyService.delete(subKey);
387
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
392
393 PolytomousKeyNode subKeyRootNode = subKey.getRoot();
394 Assert.assertNotNull(subKey);
395 }
396
397 @Test
398 public void saveNewTermVocabulary() {
399 TermVocabulary termVocabulary =
400 TermVocabulary.NewInstance(TermType.Feature,
401 null,
402 "Untitled",
403 null,
404 null);
405 try {
406 List<TermVocabulary<DefinedTermBase>> vocabularies = vocabularyService.findByTermType(TermType.Feature);
407
408 for(TermVocabulary vocab : vocabularies) {
409 vocab.getTermsOrderedByLabels(Language.ENGLISH());
410 }
411
412 termVocabulary = vocabularyService.merge(termVocabulary);
413 vocabularies.add(termVocabulary);
414 termVocabulary.setLabel("Test");
415 vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));
416 } finally {
417 vocabularyService.delete(termVocabulary);
418 }
419 }
420
421 @Ignore // should be enabled once resolution of #5066 is merged into cdmlib main branch
422 @Test
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");
428
429 try {
430
431 Assert.assertNotNull(newTerm);
432 TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);
433 TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);
434
435 List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();
436
437 vocs.add(vocNameFeature);
438 vocs.add(vocFeature);
439
440 vocNameFeature.addTerm(newTerm);
441
442 vocs = vocabularyService.merge(vocs);
443 for(TermVocabulary voc : vocs) {
444 if(voc.getUuid().equals(vocNameFeatureUuid)) {
445 vocNameFeature = voc;
446 }
447 }
448
449 Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));
450
451 for(Object obj : vocNameFeature.getTerms()) {
452 DefinedTermBase dtb = (DefinedTermBase)obj;
453 if("CreateTest".equals(dtb.getLabel())) {
454 newTerm = dtb;
455 Assert.assertNotNull(dtb.getCreatedBy());
456 Assert.assertNotNull(dtb.getCreated());
457 } else {
458 Assert.assertNull(dtb.getCreatedBy());
459 }
460 }
461 newTerm.setLabel("UpdateTest");
462 newTerm = termService.merge(newTerm);
463 Assert.assertNotNull(newTerm.getUpdatedBy());
464 Assert.assertNotNull(newTerm.getUpdated());
465
466 Assert.assertNull(vocNameFeature.getCreatedBy());
467 Assert.assertNull(vocFeature.getCreatedBy());
468 } finally {
469 if(termService.find(newTerm.getUuid()) != null) {
470 termService.delete(newTerm.getUuid());
471 }
472 }
473 }
474
475 @Test
476 public void savePerson() {
477 TeamOrPersonBase teamOrPerson = Person.NewInstance();
478 teamOrPerson.setTitleCache("New Person", true);
479 teamOrPerson = (TeamOrPersonBase)CdmApplicationState.getCurrentAppConfig().getAgentService().merge(teamOrPerson);
480 //CdmApplicationState.getCurrentAppConfig().getAgentService().saveOrUpdate(teamOrPerson);
481
482 Person person = (Person)teamOrPerson;
483 // person.setFirstname("Some");
484 // CdmApplicationState.getCurrentAppConfig().getAgentService().merge(teamOrPerson);
485
486 person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().find(person.getUuid());
487 person.setFirstname("Some");
488 CdmApplicationState.getCurrentAppConfig().getAgentService().merge(teamOrPerson);
489 }
490 }