Merge branch 'master' into abcd
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / service / TaxonNodeServiceImplTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.cdm.api.service;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertTrue;
18
19 import java.io.FileNotFoundException;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.UUID;
25
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.unitils.dbunit.annotation.DataSet;
30 import org.unitils.spring.annotation.SpringBeanByType;
31
32 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
33 import eu.etaxonomy.cdm.model.common.CdmBase;
34 import eu.etaxonomy.cdm.model.description.PolytomousKey;
35 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
36 import eu.etaxonomy.cdm.model.name.BotanicalName;
37 import eu.etaxonomy.cdm.model.name.NameRelationshipType;
38 import eu.etaxonomy.cdm.model.name.Rank;
39 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
40 import eu.etaxonomy.cdm.model.reference.Reference;
41 import eu.etaxonomy.cdm.model.taxon.Classification;
42 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
43 import eu.etaxonomy.cdm.model.taxon.Synonym;
44 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
45 import eu.etaxonomy.cdm.model.taxon.Taxon;
46 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
47 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
48 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
49 import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
50
51 /**
52 * @author n.hoffmann
53 * @created Dec 16, 2010
54 */
55 //@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml")
56 public class TaxonNodeServiceImplTest extends CdmTransactionalIntegrationTest{
57
58
59 @SpringBeanByType
60 private ITaxonNodeService taxonNodeService;
61
62 @SpringBeanByType
63 private IClassificationService classificationService;
64
65 @SpringBeanByType
66 private IReferenceService referenceService;
67
68 @SpringBeanByType
69 private ITermService termService;
70
71 @SpringBeanByType
72 private INameService nameService;
73
74 @SpringBeanByType
75 private ITaxonService taxonService;
76
77 @SpringBeanByType
78 private IPolytomousKeyService polKeyService;
79
80 @SpringBeanByType
81 private IPolytomousKeyNodeService polKeyNodeService;
82
83
84 private static final UUID t1Uuid = UUID.fromString("55c3e41a-c629-40e6-aa6a-ff274ac6ddb1");
85 private static final UUID t2Uuid = UUID.fromString("2659a7e0-ff35-4ee4-8493-b453756ab955");
86 private static final UUID classificationUuid = UUID.fromString("6c2bc8d9-ee62-4222-be89-4a8e31770878");
87 private static final UUID classification2Uuid = UUID.fromString("43d67247-936f-42a3-a739-bbcde372e334");
88 private static final UUID referenceUuid = UUID.fromString("de7d1205-291f-45d9-9059-ca83fc7ade14");
89 private static final UUID node1Uuid= UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
90 private static final UUID node2Uuid = UUID.fromString("2d41f0c2-b785-4f73-a436-cc2d5e93cc5b");
91
92 private static final UUID node4Uuid = UUID.fromString("fdaec4bd-c78e-44df-ae87-28f18110968c");
93 private static final UUID node5Uuid = UUID.fromString("c4d5170a-7967-4dac-ab76-ae2019eefde5");
94 private static final UUID node6Uuid = UUID.fromString("b419ba5e-9c8b-449c-ad86-7abfca9a7340");
95 private static final UUID rootNodeUuid = UUID.fromString("324a1a77-689c-44be-8e65-347d835f4111");
96
97
98 private Taxon t1;
99 private Taxon t2;
100 // private Synonym s1;
101 private SynonymRelationshipType synonymRelationshipType;
102 private Reference<?> reference;
103 private String referenceDetail;
104 private Classification classification;
105 private TaxonNode node1;
106 private TaxonNode node2;
107
108 /**
109 * @throws java.lang.Exception
110 */
111 @Before
112 public void setUp() throws Exception {
113 }
114
115 /**
116 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonNodeServiceImpl#makeTaxonNodeASynonymOfAnotherTaxonNode(eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType, eu.etaxonomy.cdm.model.reference.Reference, java.lang.String)}.
117 */
118 @Test
119 @DataSet
120 public final void testMakeTaxonNodeASynonymOfAnotherTaxonNode() {
121 classification = classificationService.load(classificationUuid);
122 node1 = taxonNodeService.load(node1Uuid);
123 node2 = taxonNodeService.load(node2Uuid);
124 reference = referenceService.load(referenceUuid);
125 // synonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
126 synonymRelationshipType = CdmBase.deproxy(termService.load(SynonymRelationshipType.uuidHomotypicSynonymOf), SynonymRelationshipType.class) ;
127 referenceDetail = "test";
128
129 //
130 //TODO
131
132 // printDataSet(System.err, new String [] {"TaxonNode"});
133
134 // descriptions
135 t1 = node1.getTaxon();
136 PolytomousKey polKey = PolytomousKey.NewInstance();
137 PolytomousKeyNode keyNode = PolytomousKeyNode.NewInstance("", "", t1, null);
138 keyNode.setKey(polKey);
139 polKeyNodeService.save(keyNode);
140 polKeyService.save(polKey);
141
142 //nameRelations
143
144 t1.getName().addRelationshipFromName(BotanicalName.NewInstance(Rank.SPECIES()), NameRelationshipType.ALTERNATIVE_NAME(), null );
145
146 //taxonRelations
147 t1.addTaxonRelation(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), TaxonRelationshipType.CONGRUENT_OR_EXCLUDES(), null, null);
148 Synonym synonym = Synonym.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null);
149 UUID uuidSynonym = taxonService.save(synonym).getUuid();
150
151 t1.addHomotypicSynonym(synonym, null, null);
152 UUID uuidT1 = taxonService.saveOrUpdate(t1);
153 t1 = null;
154 t1 =(Taxon) taxonService.load(uuidT1);
155 t1 = (Taxon)HibernateProxyHelper.deproxy(t1);
156 TaxonNameBase nameT1 = t1.getName();
157 UUID t1UUID = t1.getUuid();
158 t2 = node2.getTaxon();
159 assertEquals(2, t1.getDescriptions().size());
160 Assert.assertTrue(t2.getSynonyms().isEmpty());
161 Assert.assertTrue(t2.getDescriptions().size() == 0);
162 assertEquals(2,t1.getSynonyms().size());
163 UUID synUUID = null;
164 DeleteResult result;
165 //Taxon can't be deleted because of the polytomous key node
166
167 result = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode(node1, node2, synonymRelationshipType, reference, referenceDetail);
168 if (result.isError() || result.isAbort()){
169 Assert.fail();
170 }
171 commitAndStartNewTransaction(new String[]{"TaxonNode"});
172 t1 = (Taxon)taxonService.find(t1Uuid);
173 assertNotNull(t1);//because of the polytomous key node
174 node1 = taxonNodeService.load(node1Uuid);
175 assertNull(node1);
176
177
178
179
180 Set<CdmBase> updatedObjects = result.getUpdatedObjects();
181 Iterator<CdmBase> it = updatedObjects.iterator();
182 Taxon taxon;
183 if (it.hasNext()) {
184 CdmBase updatedObject = it.next();
185 if(updatedObject.isInstanceOf(Taxon.class)){
186 taxon = HibernateProxyHelper.deproxy(updatedObject, Taxon.class);
187 Set<Synonym> syns = taxon.getSynonyms();
188 assertNotNull(syns);
189 assertEquals(3,syns.size());
190
191 Set<TaxonNameBase> typifiedNames =taxon.getHomotypicGroup().getTypifiedNames();
192 assertEquals(typifiedNames.size(),3);
193 assertTrue(taxon.getHomotypicGroup().equals( nameT1.getHomotypicalGroup()));
194
195 assertEquals(taxon, t2);
196
197 } else{
198 Assert.fail();
199 }
200
201
202 }
203
204
205
206 }
207
208 /**
209 * Test method for {@link eu.etaxonomy.cdm.api.service.TaxonNodeServiceImpl#makeTaxonNodeASynonymOfAnotherTaxonNode(eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.taxon.TaxonNode, eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType, eu.etaxonomy.cdm.model.reference.Reference, java.lang.String)}.
210 */
211 @Test
212 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
213 public final void testMakeTaxonNodeAHeterotypicSynonymOfAnotherTaxonNode() {
214 classification = classificationService.load(classificationUuid);
215 node1 = taxonNodeService.load(node1Uuid);
216 node2 = taxonNodeService.load(node2Uuid);
217 reference = referenceService.load(referenceUuid);
218 // synonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
219 synonymRelationshipType = CdmBase.deproxy(termService.load(SynonymRelationshipType.uuidHeterotypicSynonymOf), SynonymRelationshipType.class) ;
220 referenceDetail = "test";
221
222 //
223 //TODO
224
225 // printDataSet(System.err, new String [] {"TaxonNode"});
226
227 // descriptions
228 t1 = node1.getTaxon();
229 PolytomousKey polKey = PolytomousKey.NewInstance();
230 PolytomousKeyNode keyNode = PolytomousKeyNode.NewInstance("", "", t1, null);
231 keyNode.setKey(polKey);
232 polKeyNodeService.save(keyNode);
233 polKeyService.save(polKey);
234
235 //nameRelations
236
237 t1.getName().addRelationshipFromName(BotanicalName.NewInstance(Rank.SPECIES()), NameRelationshipType.ALTERNATIVE_NAME(), null );
238 TaxonNameBase name1 = t1.getName();
239 UUID name1UUID = name1.getUuid();
240 //taxonRelations
241 t1.addTaxonRelation(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), TaxonRelationshipType.CONGRUENT_OR_EXCLUDES(), null, null);
242 Synonym synonym = Synonym.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null);
243 UUID uuidSynonym = taxonService.save(synonym).getUuid();
244 t1.addHomotypicSynonym(synonym, null, null);
245 TaxonNameBase nameT1 = t1.getName();
246 UUID t1UUID = t1.getUuid();
247 t2 = node2.getTaxon();
248 assertEquals(2, t1.getDescriptions().size());
249 Assert.assertTrue(t2.getSynonyms().isEmpty());
250 Assert.assertTrue(t2.getDescriptions().size() == 0);
251 assertEquals(2,t1.getSynonyms().size());
252 UUID synUUID = null;
253 DeleteResult result;
254 result = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode(node1, node2, synonymRelationshipType, reference, referenceDetail);
255
256 if (!result.getUpdatedObjects().iterator().hasNext()){
257 Assert.fail();
258 }
259 Taxon newAcceptedTaxon = (Taxon)result.getUpdatedObjects().iterator().next();
260 assertNotNull(taxonService.find(t1Uuid));
261 assertNull(taxonNodeService.find(node1Uuid));
262
263
264 synonym = (Synonym)taxonService.find(uuidSynonym);
265
266 assertNotNull(synonym);
267 keyNode.setTaxon(null);
268 polKeyNodeService.saveOrUpdate(keyNode);
269 HibernateProxyHelper.deproxy(t2);
270 HibernateProxyHelper.deproxy(t2.getHomotypicGroup());
271 HibernateProxyHelper.deproxy(t2.getName());
272 // syn = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode(node1, node2, synonymRelationshipType, reference, referenceDetail);
273 // if (syn == null){
274 // Assert.fail();
275 // }
276 // synUUID = syn.getUuid();
277
278
279 termService.saveOrUpdate(synonymRelationshipType);
280 Assert.assertFalse(t2.getSynonyms().isEmpty());
281 assertEquals(3,t2.getSynonyms().size());
282 assertEquals(2, t2.getDescriptions().size());
283
284 result = taxonService.deleteTaxon(t1.getUuid(), null, null);
285 if (result.isAbort() || result.isError()){
286 Assert.fail();
287 }
288 assertNull(taxonService.find(t1Uuid));
289 assertNull(taxonNodeService.find(node1Uuid));
290 name1 = nameService.find(name1UUID);
291 synonym = (Synonym)taxonService.find(uuidSynonym);
292 assertNotNull(name1);
293 assertEquals(1, name1.getTaxonBases().size());
294 assertNotNull(synonym);
295
296 Synonym syn =(Synonym) name1.getTaxonBases().iterator().next();
297
298 assertEquals(syn.getName().getHomotypicalGroup(), synonym.getName().getHomotypicalGroup());
299 assertFalse(newAcceptedTaxon.getHomotypicGroup().equals( syn.getName().getHomotypicalGroup()));
300
301 assertEquals(newAcceptedTaxon, t2);
302 TaxonNameBase name = syn.getName();
303 assertEquals(name, nameT1);
304 }
305
306
307 @Test
308 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonNodeServiceImplTest-indexing.xml")
309 public final void testIndexCreateNode() {
310 Taxon taxon = Taxon.NewInstance(null, null);
311 classification = classificationService.load(classificationUuid);
312 node2 = taxonNodeService.load(node2Uuid);
313 String oldTreeIndex = node2.treeIndex();
314
315 TaxonNode newNode = node2.addChildTaxon(taxon, null, null);
316 taxonNodeService.saveOrUpdate(node2);
317 commitAndStartNewTransaction(new String[]{"TaxonNode"});
318 newNode = taxonNodeService.load(newNode.getUuid());
319 Assert.assertEquals("", oldTreeIndex + newNode.getId() + "#", newNode.treeIndex());
320 }
321
322
323 @Test
324 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonNodeServiceImplTest-indexing.xml")
325 public final void testIndexMoveNode() {
326 //in classification
327 classification = classificationService.load(classificationUuid);
328 node1 = taxonNodeService.load(node1Uuid);
329 node2 = taxonNodeService.load(node2Uuid);
330 node2.addChildNode(node1, null, null);
331 taxonNodeService.saveOrUpdate(node1);
332 commitAndStartNewTransaction(new String[]{"TaxonNode"});
333 TaxonNode node6 = taxonNodeService.load(node6Uuid);
334 Assert.assertEquals("Node6 treeindex is not correct", node2.treeIndex() + "2#4#6#", node6.treeIndex());
335
336 //root of new classification
337 Classification classification2 = classificationService.load(classification2Uuid);
338 node1 = taxonNodeService.load(node1Uuid);
339 classification2.setRootNode(HibernateProxyHelper.deproxy(classification2.getRootNode(),TaxonNode.class));
340 classification2.addChildNode(node1, null, null);
341 taxonNodeService.saveOrUpdate(node1);
342 commitAndStartNewTransaction(new String[]{"TaxonNode"});
343 node1 = taxonNodeService.load(node1Uuid);
344 Assert.assertEquals("Node1 treeindex is not correct", "#t2#8#2#", node1.treeIndex());
345 node6 = taxonNodeService.load(node6Uuid);
346 Assert.assertEquals("Node6 treeindex is not correct", "#t2#8#2#4#6#", node6.treeIndex());
347
348 //into new classification
349 node2 = taxonNodeService.load(node2Uuid);
350 TaxonNode node5 = taxonNodeService.load(node5Uuid);
351 node5.addChildNode(node2, null, null);
352 taxonNodeService.saveOrUpdate(node5);
353 commitAndStartNewTransaction(new String[]{"TaxonNode"});
354 node2 = taxonNodeService.load(node2Uuid);
355 Assert.assertEquals("Node3 treeindex is not correct", "#t2#8#2#5#3#", node2.treeIndex());
356
357 }
358
359 @Test
360 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonNodeServiceImplTest-indexing.xml")
361 public final void testIndexDeleteNode() {
362 commitAndStartNewTransaction(new String[]{"TaxonNode"});
363 node1 = taxonNodeService.load(node1Uuid);
364 TaxonNode node4 = taxonNodeService.load(node4Uuid);
365 String treeIndex = node1.treeIndex();
366 TaxonNode node6 = taxonNodeService.load(node6Uuid);
367 treeIndex= node6.treeIndex();
368
369 HibernateProxyHelper.deproxy(node1, TaxonNode.class);
370 node1.deleteChildNode(node4, false);
371 TaxonNode node5 = taxonNodeService.load(node5Uuid);
372 treeIndex = node5.treeIndex();
373
374 node6 = taxonNodeService.load(node6Uuid);
375
376 treeIndex = node6.treeIndex();
377 Taxon newTaxon= Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null);
378 UUID taxonNewUuid = taxonService.save(newTaxon).getUuid();
379
380 node5.addChildTaxon(newTaxon, null, null);
381 String node5TreeIndex =node5.treeIndex();
382 taxonNodeService.saveOrUpdate(node5);
383
384 commitAndStartNewTransaction(new String[]{"TaxonNode"});
385 node5 = taxonNodeService.load(node5Uuid);
386 List<TaxonNode> children = node5.getChildNodes();
387 TaxonNode node = children.get(0);
388 int id = node.getId();
389 Assert.assertEquals("Node6 treeindex is not correct", "#t1#1#2#6#", treeIndex);
390 Assert.assertEquals("new node treeindex is not correct", node5TreeIndex + id +"#", node.treeIndex());
391 }
392
393
394 @Test
395 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
396 public final void testDeleteNode(){
397 classification = classificationService.load(classificationUuid);
398 node1 = taxonNodeService.load(node1Uuid);
399 node2 = taxonNodeService.load(rootNodeUuid);
400 node1 = (TaxonNode)HibernateProxyHelper.deproxy(node1);
401
402 TaxonNode newNode = node1.addChildTaxon(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), null, null);
403 UUID uuidNewNode = taxonNodeService.save(newNode).getUuid();
404 newNode = taxonNodeService.load(uuidNewNode);
405 UUID taxUUID = newNode.getTaxon().getUuid();
406 UUID nameUUID = newNode.getTaxon().getName().getUuid();
407
408 DeleteResult result = taxonNodeService.deleteTaxonNode(node1, null);
409 if (!result.isOk()){
410 Assert.fail();
411 }
412 newNode = taxonNodeService.load(uuidNewNode);
413 node1 = taxonNodeService.load(node1Uuid);
414 assertNull(newNode);
415 assertNull(node1);
416
417 t1 = (Taxon) taxonService.load(t1Uuid);
418 assertNull(t1);
419 Taxon newTaxon = (Taxon)taxonService.load(taxUUID);
420 assertNull(newTaxon);
421 BotanicalName name = (BotanicalName)nameService.load(nameUUID);
422 assertNull(name);
423
424
425 }
426 @Test
427 @DataSet
428 public final void testDeleteNodes(){
429 classification = classificationService.load(classificationUuid);
430 node1 = taxonNodeService.load(node1Uuid);
431 node2 = taxonNodeService.load(rootNodeUuid);
432 node1 = (TaxonNode)HibernateProxyHelper.deproxy(node1);
433 node2 = (TaxonNode)HibernateProxyHelper.deproxy(node2);
434 TaxonNode newNode = node1.addChildTaxon(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), null, null);
435 UUID uuidNewNode = taxonNodeService.save(newNode).getUuid();
436 Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
437 treeNodes.add(node1);
438 treeNodes.add(node2);
439
440 DeleteResult result = taxonNodeService.deleteTaxonNodes(treeNodes, null);
441
442
443 newNode = taxonNodeService.load(uuidNewNode);
444 node1 = taxonNodeService.load(node1Uuid);
445 assertNull(newNode);
446 assertNull(node1);
447 taxonService.getSession().flush();
448 t1 = (Taxon) taxonService.load(t1Uuid);
449 assertNull(t1);
450 t2 = (Taxon) taxonService.load(t2Uuid);
451 assertNull(t2);
452
453
454 }
455 @Test
456 @DataSet
457 public void testMoveTaxonNode(){
458
459 }
460
461 /* (non-Javadoc)
462 * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
463 */
464 @Override
465 public void createTestDataSet() throws FileNotFoundException {
466 // TODO Auto-generated method stub
467
468 }
469
470
471 }