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