#3948 first step, if a taxon is used in a polytomous key, the taxonNode will be delet...
[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.assertNotNull;
15 import static org.junit.Assert.assertNull;
16
17 import java.util.HashSet;
18 import java.util.Set;
19 import java.util.UUID;
20
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.unitils.dbunit.annotation.DataSet;
25 import org.unitils.spring.annotation.SpringApplicationContext;
26 import org.unitils.spring.annotation.SpringBeanByType;
27
28 import eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException;
29 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.description.PolytomousKey;
32 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
33 import eu.etaxonomy.cdm.model.name.BotanicalName;
34 import eu.etaxonomy.cdm.model.name.Rank;
35 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
36 import eu.etaxonomy.cdm.model.reference.Reference;
37 import eu.etaxonomy.cdm.model.taxon.Classification;
38 import eu.etaxonomy.cdm.model.taxon.Synonym;
39 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
40 import eu.etaxonomy.cdm.model.taxon.Taxon;
41 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
42 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
43 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
44
45 /**
46 * @author n.hoffmann
47 * @created Dec 16, 2010
48 */
49 @SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml")
50 public class TaxonNodeServiceImplTest extends CdmTransactionalIntegrationTest{
51
52
53 @SpringBeanByType
54 private ITaxonNodeService taxonNodeService;
55
56 @SpringBeanByType
57 private IClassificationService classificationService;
58
59 @SpringBeanByType
60 private IReferenceService referenceService;
61
62 @SpringBeanByType
63 private ITermService termService;
64
65 @SpringBeanByType
66 private ITaxonService taxonService;
67
68 @SpringBeanByType
69 private IPolytomousKeyService polKeyService;
70
71 @SpringBeanByType
72 private IPolytomousKeyNodeService polKeyNodeService;
73
74
75 private static final UUID t1Uuid = UUID.fromString("55c3e41a-c629-40e6-aa6a-ff274ac6ddb1");
76 private static final UUID t2Uuid = UUID.fromString("2659a7e0-ff35-4ee4-8493-b453756ab955");
77 private static final UUID classificationUuid = UUID.fromString("6c2bc8d9-ee62-4222-be89-4a8e31770878");
78 private static final UUID classification2Uuid = UUID.fromString("43d67247-936f-42a3-a739-bbcde372e334");
79 private static final UUID referenceUuid = UUID.fromString("de7d1205-291f-45d9-9059-ca83fc7ade14");
80 private static final UUID node1Uuid= UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
81 private static final UUID node2Uuid = UUID.fromString("2d41f0c2-b785-4f73-a436-cc2d5e93cc5b");
82 // private static final UUID node4Uuid = UUID.fromString("fdaec4bd-c78e-44df-ae87-28f18110968c");
83 private static final UUID node5Uuid = UUID.fromString("c4d5170a-7967-4dac-ab76-ae2019eefde5");
84 private static final UUID node6Uuid = UUID.fromString("b419ba5e-9c8b-449c-ad86-7abfca9a7340");
85 private static final UUID rootNodeUuid = UUID.fromString("324a1a77-689c-44be-8e65-347d835f4111");
86
87 private Taxon t1;
88 private Taxon t2;
89 // private Synonym s1;
90 private SynonymRelationshipType synonymRelationshipType;
91 private Reference<?> reference;
92 private String referenceDetail;
93 private Classification classification;
94 private TaxonNode node1;
95 private TaxonNode node2;
96
97 /**
98 * @throws java.lang.Exception
99 */
100 @Before
101 public void setUp() throws Exception {
102 }
103
104 /**
105 * 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)}.
106 */
107 @Test
108 @DataSet
109 public final void testMakeTaxonNodeASynonymOfAnotherTaxonNode() {
110 classification = classificationService.load(classificationUuid);
111 node1 = taxonNodeService.load(node1Uuid);
112 node2 = taxonNodeService.load(node2Uuid);
113 reference = referenceService.load(referenceUuid);
114 // synonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
115 synonymRelationshipType = CdmBase.deproxy(termService.load(SynonymRelationshipType.uuidHomotypicSynonymOf), SynonymRelationshipType.class) ;
116 referenceDetail = "test";
117
118 //
119 //TODO
120
121 // printDataSet(System.err, new String [] {"TaxonNode"});
122
123 // descriptions
124 t1 = node1.getTaxon();
125 PolytomousKey polKey = PolytomousKey.NewInstance();
126 PolytomousKeyNode keyNode = PolytomousKeyNode.NewInstance("", "", t1, null);
127 keyNode.setKey(polKey);
128 polKeyNodeService.save(keyNode);
129 polKeyService.save(polKey);
130
131 TaxonNameBase nameT1 = t1.getName();
132 UUID t1UUID = t1.getUuid();
133 t2 = node2.getTaxon();
134 assertEquals(2, t1.getDescriptions().size());
135 Assert.assertTrue(t2.getSynonyms().isEmpty());
136 Assert.assertTrue(t2.getDescriptions().size() == 0);
137 assertEquals(1,t1.getSynonyms().size());
138 UUID synUUID = null;
139 Synonym syn;
140 try {
141 syn = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode(node1, node2, synonymRelationshipType, reference, referenceDetail);
142 synUUID = syn.getUuid();
143 } catch (DataChangeNoRollbackException e) {
144 logger.debug(e.getMessage());
145 Assert.fail();
146 }
147 termService.saveOrUpdate(synonymRelationshipType);
148 Assert.assertFalse(t2.getSynonyms().isEmpty());
149 assertEquals(2,t2.getSynonyms().size());
150 assertEquals(2, t2.getDescriptions().size());
151
152
153 assertNotNull(taxonService.find(t1Uuid));
154 assertNull(taxonNodeService.find(node1Uuid));
155 syn = (Synonym)taxonService.find(synUUID);
156 assertNotNull(syn);
157 Taxon tax = syn.getAcceptedTaxa().iterator().next();
158 assertEquals(tax, t2);
159 TaxonNameBase name = syn.getName();
160 assertEquals(name, nameT1);
161 }
162
163 @Test
164 @DataSet(value="TaxonNodeServiceImplTest-indexing.xml")
165 public final void testIndexCreateNode() {
166 Taxon taxon = Taxon.NewInstance(null, null);
167 classification = classificationService.load(classificationUuid);
168 node2 = taxonNodeService.load(node2Uuid);
169 String oldTreeIndex = node2.treeIndex();
170
171 TaxonNode newNode = node2.addChildTaxon(taxon, null, null);
172 taxonNodeService.saveOrUpdate(node2);
173 commitAndStartNewTransaction(new String[]{"TaxonNode"});
174 newNode = taxonNodeService.load(newNode.getUuid());
175 Assert.assertEquals("", oldTreeIndex + newNode.getId() + "#", newNode.treeIndex());
176 }
177
178
179 @Test
180 @DataSet(value="TaxonNodeServiceImplTest-indexing.xml")
181 public final void testIndexMoveNode() {
182 //in classification
183 classification = classificationService.load(classificationUuid);
184 node1 = taxonNodeService.load(node1Uuid);
185 node2 = taxonNodeService.load(node2Uuid);
186 node2.addChildNode(node1, null, null);
187 taxonNodeService.saveOrUpdate(node1);
188 commitAndStartNewTransaction(new String[]{"TaxonNode"});
189 TaxonNode node6 = taxonNodeService.load(node6Uuid);
190 Assert.assertEquals("Node6 treeindex is not correct", node2.treeIndex() + "2#4#6#", node6.treeIndex());
191
192 //root of new classification
193 Classification classification2 = classificationService.load(classification2Uuid);
194 node1 = taxonNodeService.load(node1Uuid);
195 classification2.addChildNode(node1, null, null);
196 taxonNodeService.saveOrUpdate(node1);
197 commitAndStartNewTransaction(new String[]{"TaxonNode"});
198 node1 = taxonNodeService.load(node1Uuid);
199 Assert.assertEquals("Node1 treeindex is not correct", "#t2#2#", node1.treeIndex());
200 node6 = taxonNodeService.load(node6Uuid);
201 Assert.assertEquals("Node6 treeindex is not correct", "#t2#2#4#6#", node6.treeIndex());
202
203 //into new classification
204 node2 = taxonNodeService.load(node2Uuid);
205 TaxonNode node5 = taxonNodeService.load(node5Uuid);
206 node5.addChildNode(node2, null, null);
207 taxonNodeService.saveOrUpdate(node5);
208 commitAndStartNewTransaction(new String[]{"TaxonNode"});
209 node2 = taxonNodeService.load(node2Uuid);
210 Assert.assertEquals("Node3 treeindex is not correct", "#t2#2#5#3#", node2.treeIndex());
211
212 }
213
214 @Test //here we may have a test for testing delete of a node and attaching the children
215 //to its parents, however this depends on the way delete is implemented and therefore needs
216 //to wait until this is finally done
217 public final void testIndexDeleteNode() {
218 // node2 = taxonNodeService.load(node2Uuid);
219 // node2.getParent().deleteChildNode(node2);
220 //
221 // node5.addChildNode(node3, null, null);
222 // taxonNodeService.saveOrUpdate(node5);
223 // commitAndStartNewTransaction(new String[]{"TaxonNode"});
224 // node3 = taxonNodeService.load(node3Uuid);
225 // Assert.assertEquals("Node3 treeindex is not correct", "#t2#2#5#3#", node3.getTreeIndex());
226 }
227
228
229 @Test
230 @DataSet
231 public final void testDeleteNode(){
232 classification = classificationService.load(classificationUuid);
233 node1 = taxonNodeService.load(node1Uuid);
234 node2 = taxonNodeService.load(rootNodeUuid);
235 node1 = (TaxonNode)HibernateProxyHelper.deproxy(node1);
236
237 TaxonNode newNode = node1.addChildTaxon(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), null, null);
238 UUID uuidNewNode = taxonNodeService.save(newNode);
239 newNode = taxonNodeService.load(uuidNewNode);
240 UUID taxUUID = newNode.getTaxon().getUuid();
241 try {
242 taxonNodeService.deleteTaxonNode(node1, null);
243 } catch (DataChangeNoRollbackException e) {
244 Assert.fail();
245 }
246 newNode = taxonNodeService.load(uuidNewNode);
247 node1 = taxonNodeService.load(node1Uuid);
248 assertNull(newNode);
249 assertNull(node1);
250
251 t1 = (Taxon) taxonService.load(t1Uuid);
252 assertNull(t1);
253 Taxon newTaxon = (Taxon)taxonService.load(taxUUID);
254 assertNull(newTaxon);
255
256
257 }
258 @Test
259 @DataSet
260 public final void testDeleteNodes(){
261 classification = classificationService.load(classificationUuid);
262 node1 = taxonNodeService.load(node1Uuid);
263 node2 = taxonNodeService.load(rootNodeUuid);
264 node1 = (TaxonNode)HibernateProxyHelper.deproxy(node1);
265 node2 = (TaxonNode)HibernateProxyHelper.deproxy(node2);
266 TaxonNode newNode = node1.addChildTaxon(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), null, null);
267 UUID uuidNewNode = taxonNodeService.save(newNode);
268 Set<ITaxonTreeNode> treeNodes = new HashSet<ITaxonTreeNode>();
269 treeNodes.add(node1);
270 treeNodes.add(node2);
271 try {
272 taxonNodeService.deleteTaxonNodes(treeNodes, null);
273 } catch (DataChangeNoRollbackException e) {
274 Assert.fail();
275 }
276 newNode = taxonNodeService.load(uuidNewNode);
277 node1 = taxonNodeService.load(node1Uuid);
278 assertNull(newNode);
279 assertNull(node1);
280 taxonService.getSession().flush();
281 t1 = (Taxon) taxonService.load(t1Uuid);
282 assertNull(t1);
283 t2 = (Taxon) taxonService.load(t2Uuid);
284 assertNull(t2);
285
286
287 }
288
289
290 }