#5247 Fix duplicate entity issue for new entities by using the new merge service...
[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.common.CdmBase;
38 import eu.etaxonomy.cdm.model.common.Credit;
39 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
40 import eu.etaxonomy.cdm.model.common.Extension;
41 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
42 import eu.etaxonomy.cdm.model.common.Language;
43 import eu.etaxonomy.cdm.model.common.LanguageString;
44 import eu.etaxonomy.cdm.model.common.TermType;
45 import eu.etaxonomy.cdm.model.common.TermVocabulary;
46 import eu.etaxonomy.cdm.model.description.KeyStatement;
47 import eu.etaxonomy.cdm.model.description.PolytomousKey;
48 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
49 import eu.etaxonomy.cdm.model.media.Rights;
50 import eu.etaxonomy.cdm.model.taxon.Taxon;
51 import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
52
53 /**
54 * @author cmathew
55 * @date 7 Oct 2014
56 *
57 */
58 @DataSet
59 public class CdmEntitySessionAwareTest extends RemotingSessionAwareTest {
60
61 private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.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 UUID personUuid = UUID.fromString("945d08f2-eb92-45b6-9252-6275ea6d338b");
68
69
70
71 IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
72 IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
73 ICommonService commonService = getRemoteApplicationController().getCommonService();
74 ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
75 IVocabularyService vocabularyService = getRemoteApplicationController().getVocabularyService();
76 ITermService termService = getRemoteApplicationController().getTermService();
77 IUserService userService = getRemoteApplicationController().getUserService();
78
79 //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
80
81
82
83 @BeforeClass
84 public static void initializePolytomousKeyTest() {
85 }
86
87
88 @Test
89 public void readAllPolytomousKeys() {
90 List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
91 Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
92 Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
93 Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
94 }
95
96
97 @Test
98 //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
99 public void readPolytmousKeyData() {
100 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
101 Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
102 Iterator<Taxon> tsItr = taxonomicScope.iterator();
103 Taxon taxon = tsItr.next();
104
105 Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
106 Assert.assertEquals(tsItr.hasNext(), false);
107
108 List<Credit> credits = pKey.getCredits();
109 AgentBase agent = credits.get(0).getAgent();
110 Assert.assertEquals(agent.getId(),4809);
111 Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
112 Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
113
114 Set<Extension> exts = pKey.getExtensions();
115 Iterator<Extension> extItr = exts.iterator();
116 Extension ext = extItr.next();
117 Assert.assertEquals(ext.getValue(), "http://test.com");
118
119 Set<Rights> rights = pKey.getRights();
120 Iterator<Rights> rightsItr = rights.iterator();
121 Rights right = rightsItr.next();
122 Assert.assertEquals(right.getText(),"Rights Text Test");
123
124 Set<IdentifiableSource> sources = pKey.getSources();
125 Iterator<IdentifiableSource> sourcesItr = sources.iterator();
126 IdentifiableSource source = sourcesItr.next();
127 Assert.assertEquals(source.getId(), 23710);
128 source = sourcesItr.next();
129 Assert.assertEquals(source.getId(), 23711);
130
131 // TO DO : Added tests for Annotations , Markers
132 }
133
134
135
136 @Test
137 public void readPolytomousKeyDataFromNodes() {
138 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
139 PolytomousKeyNode rootNode = pKey.getRoot();
140
141
142 Assert.assertEquals(rootNode.getId(), 2750);
143
144 Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
145 Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
146 Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
147 Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
148 Assert.assertEquals(sortIndex, new Integer(0));
149 String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
150 Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);
151
152 Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
153 Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
154 Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
155 sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
156 Assert.assertEquals(sortIndex, new Integer(1));
157 statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
158 Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(Language.ENGLISH()), statement);
159
160 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
161 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
162 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
163 sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
164 Assert.assertEquals(sortIndex, new Integer(0));
165 statement = "Ray-florets yellow";
166 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);
167 Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
168 }
169
170 @Test
171 public void addGrandChildPolytomousKeyNode() {
172
173 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
174 PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);
175 PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();
176 rootChildNode.addChild(grandChildNode);
177
178 pKey = polytomousKeyService.merge(pKey, true);
179 grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
180 Assert.assertTrue(0 != grandChildNode.getId());
181
182 grandChildNode.setStatement(KeyStatement.NewInstance("test"));
183 pKey = polytomousKeyService.merge(pKey, true);
184
185 grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
186 KeyStatement ks = grandChildNode.getStatement();
187 Assert.assertTrue(0 != ks.getId());
188
189 }
190
191 @Test
192 public void addGreatGrandChildPolytomousKeyNode() {
193
194 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
195 PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);
196 PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();
197 rootChildNode.addChild(grandChildNode);
198
199
200 grandChildNode = polytomousKeyNodeService.merge(grandChildNode);
201
202 PolytomousKeyNode greatGrandChildNode = PolytomousKeyNode.NewInstance();
203 grandChildNode.addChild(greatGrandChildNode);
204
205 Assert.assertFalse(pKey.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
206 }
207
208 @Test
209 public void savePolytomousKeyNodeData() {
210 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
211
212 PolytomousKeyNode pkeynode = pkey.getRoot();
213 String newQuestion = "New Question";
214 String newStatement = "New Statement";
215
216 Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());
217 for(PolytomousKeyNode node : pkeynode.getChildren()) {
218
219 node.setQuestion(null);
220 node.setStatement(null);
221 }
222 //FIXME:Remoting Add tests for feature after fixing problem
223
224 //Feature feature = pkeynode.getFeature();
225 //Assert.assertEquals(feature.getTitleCache(),"Systematics");
226 //pkeynode.setFeature(null);
227
228 Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(Language.ENGLISH()).getText(),"Modifying Text 1a");
229 String modifyingText = "Modifying Text 1a updated";
230
231 //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
232
233 Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
234 Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
235 Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
236 Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
237 pkeynode.getChildAt(1).setTaxon(taxon);
238
239 polytomousKeyService.merge(pkey);
240
241 pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
242
243
244 pkeynode = pkey.getRoot();
245 for(PolytomousKeyNode node : pkeynode.getChildren()) {
246 Assert.assertNull(node.getQuestion());
247 node.setQuestion(KeyStatement.NewInstance(Language.ENGLISH(),newQuestion));
248 Assert.assertNull(node.getStatement());
249 node.setStatement(KeyStatement.NewInstance(Language.ENGLISH(),newStatement));
250 }
251
252 Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
253
254 polytomousKeyService.merge(pkey);
255
256 pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
257
258 pkeynode = pkey.getRoot();
259 for(PolytomousKeyNode node : pkeynode.getChildren()) {
260 Assert.assertNotNull(node.getQuestion());
261 Map<Language, LanguageString> label = node.getQuestion().getLabel();
262 Assert.assertEquals(newQuestion, label.get(Language.ENGLISH()).getText());
263 Assert.assertNotNull(node.getStatement());
264 Assert.assertEquals(newStatement, node.getStatement().getLabel(Language.ENGLISH()).getText());
265 }
266 //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
267
268 }
269
270
271 @Test
272 public void savePolytomousKeyNodeDataWithSameSubKey() {
273
274 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
275
276
277 PolytomousKeyNode pkeynode = pkey.getRoot();
278
279 PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
280 String subkey1title = subkey1.getTitleCache();
281 subkey1.setTitleCache(subkey1title + "test", true);
282
283
284 PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
285 String subkey2title = subkey2.getTitleCache();
286 subkey2.setTitleCache(subkey2title + "test", true);
287
288 Assert.assertSame(subkey1, subkey2);
289
290 polytomousKeyService.merge(pkey);
291 }
292
293 @Test
294 public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
295
296 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
297
298
299 PolytomousKeyNode pkeynode = pkey.getRoot();
300
301 PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
302 PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 0);
303 PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
304 String subkey1title = subkey1.getTitleCache();
305 subkey1.setTitleCache(subkey1title + "test", true);
306
307 PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 1);
308 PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
309 String subkey2title = subkey2.getTitleCache();
310 subkey2.setTitleCache(subkey2title + "test", true);
311
312 Assert.assertNotSame(childNode0, childNode1);
313
314 Assert.assertSame(subkey1, subkey2);
315
316 polytomousKeyService.merge(pkey);
317 }
318
319
320
321 @Test
322 public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
323
324 PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
325 PolytomousKeyNode pkeynode = pkey.getRoot();
326
327 Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
328 label1.size();
329
330
331 Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
332 label2.size();
333
334
335 polytomousKeyService.merge(pkey);
336 }
337
338 @Test
339 public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
340 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
341
342
343 PolytomousKeyNode rootNode = pKey.getRoot();
344 PolytomousKeyNode child = rootNode.getChildAt(0);
345 PolytomousKey subKey = child.getSubkey();
346
347 polytomousKeyService.delete(subKey.getUuid());
348
349 // retrieving subkey shows its null
350 subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
351 Assert.assertNull(subKey);
352
353 pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
354
355
356 rootNode = pKey.getRoot();
357 child = rootNode.getChildAt(0);
358 subKey = child.getSubkey();
359 // subkey will not be null, because the delete functionality of the subkey
360 // does not currently delete it from a polytomous key node
361
362 // FIXME: With the new delete functionality this should be null, shouldn't it ?
363 Assert.assertNotNull(subKey);
364 }
365
366
367 @Test
368 public void deleteSubKeyInPolytomousNode() {
369 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
370
371
372 PolytomousKeyNode rootNode = pKey.getRoot();
373 List<PolytomousKeyNode> children = rootNode.getChildren();
374 PolytomousKeyNode child = rootNode.getChildAt(0);
375 polytomousKeyNodeService.delete(child.getUuid(), true);
376
377 pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
378
379 rootNode = pKey.getRoot();
380 children = rootNode.getChildren();
381 Assert.assertFalse(children.contains(child));
382 }
383
384 @Test
385 public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
386 PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
387
388
389 PolytomousKeyNode rootNode = pKey.getRoot();
390 PolytomousKeyNode child = rootNode.getChildAt(0);
391 PolytomousKey subKey = child.getSubkey();
392 // this call initializes the subkey
393 subKey.getRoot();
394
395 polytomousKeyService.delete(subKey);
396
397 // since subKey was initialized before the delete, it will be
398 // accesible even after the delete. This behaviour is similar
399 // to hibernate session delete where the deleted object is
400 // made transient, but not deleted from the object graph
401
402 PolytomousKeyNode subKeyRootNode = subKey.getRoot();
403 Assert.assertNotNull(subKey);
404 }
405
406 @Test
407 public void saveNewTermVocabulary() {
408 TermVocabulary termVocabulary =
409 TermVocabulary.NewInstance(TermType.Feature,
410 null,
411 "Untitled",
412 null,
413 null);
414 try {
415 List<TermVocabulary<DefinedTermBase>> vocabularies = vocabularyService.findByTermType(TermType.Feature);
416
417 for(TermVocabulary vocab : vocabularies) {
418 vocab.getTermsOrderedByLabels(Language.ENGLISH());
419 }
420
421 termVocabulary = vocabularyService.merge(termVocabulary);
422 vocabularies.add(termVocabulary);
423 termVocabulary.setLabel("Test");
424 vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));
425 } finally {
426 vocabularyService.delete(termVocabulary);
427 }
428 }
429
430 @Ignore // should be enabled once resolution of #5066 is merged into cdmlib main branch
431 @Test
432 public void saveNewTerm() {
433 UUID vocNameFeatureUuid = UUID.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");
434 UUID vocFeatureUuid = UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
435 DefinedTermBase newTerm = TermType.Feature.getEmptyDefinedTermBase();
436 newTerm.setLabel("CreateTest");
437
438 try {
439
440 Assert.assertNotNull(newTerm);
441 TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);
442 TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);
443
444 List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();
445
446 vocs.add(vocNameFeature);
447 vocs.add(vocFeature);
448
449 vocNameFeature.addTerm(newTerm);
450
451 vocs = vocabularyService.merge(vocs);
452 for(TermVocabulary voc : vocs) {
453 if(voc.getUuid().equals(vocNameFeatureUuid)) {
454 vocNameFeature = voc;
455 }
456 }
457
458 Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));
459
460 for(Object obj : vocNameFeature.getTerms()) {
461 DefinedTermBase dtb = (DefinedTermBase)obj;
462 if("CreateTest".equals(dtb.getLabel())) {
463 newTerm = dtb;
464 Assert.assertNotNull(dtb.getCreatedBy());
465 Assert.assertNotNull(dtb.getCreated());
466 } else {
467 Assert.assertNull(dtb.getCreatedBy());
468 }
469 }
470 newTerm.setLabel("UpdateTest");
471 newTerm = termService.merge(newTerm);
472 Assert.assertNotNull(newTerm.getUpdatedBy());
473 Assert.assertNotNull(newTerm.getUpdated());
474
475 Assert.assertNull(vocNameFeature.getCreatedBy());
476 Assert.assertNull(vocFeature.getCreatedBy());
477 } finally {
478 if(termService.find(newTerm.getUuid()) != null) {
479 termService.delete(newTerm.getUuid());
480 }
481 }
482 }
483
484 @Ignore // activate after fixing #5138
485 @Test
486 public void updatePerson() {
487 Person person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(personUuid);
488 // Note : at this point the contact field in Person (AgentBase) is initialized even though no
489 // data related to the Contact class exists in the database.
490 person.setFirstname("Me");
491 CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
492
493 }
494
495 @Ignore // activate after fixing #5138
496 @Test
497 public void createPerson() {
498
499 Person person = Person.NewInstance();
500 //person.setTitleCache("New Person", true);
501 person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
502 //Note : at this point the contact field in Person (AgentBase) is set to null which is
503 // different behaviour as compared to a load call on an existing Person entity.
504 // The fact that the contact is null will lead to the all-delete-orphan error.
505 person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(person.getUuid());
506 person.setFirstname("Some");
507 CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
508 }
509 }