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