Project

General

Profile

Download (23 KB) Statistics
| Branch: | Tag: | Revision:
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.ArrayList;
21
import java.util.Collections;
22
import java.util.Iterator;
23
import java.util.List;
24
import java.util.Set;
25
import java.util.UUID;
26

    
27
import org.junit.Assert;
28
import org.junit.Before;
29
import org.junit.Test;
30
import org.unitils.dbunit.annotation.DataSet;
31
import org.unitils.spring.annotation.SpringBeanByType;
32

    
33
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.description.PolytomousKey;
36
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
37
import eu.etaxonomy.cdm.model.name.BotanicalName;
38
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
39
import eu.etaxonomy.cdm.model.name.Rank;
40
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
41
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.model.taxon.Classification;
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.TaxonNaturalComparator;
47
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
48
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
49
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
50
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
51

    
52
/**
53
 * @author n.hoffmann
54
 * @created Dec 16, 2010
55
 */
56
//@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml")
57
public class TaxonNodeServiceImplTest extends CdmTransactionalIntegrationTest{
58

    
59

    
60
	@SpringBeanByType
61
	private ITaxonNodeService taxonNodeService;
62

    
63
	@SpringBeanByType
64
	private IClassificationService classificationService;
65

    
66
	@SpringBeanByType
67
	private IReferenceService referenceService;
68

    
69
	@SpringBeanByType
70
	private ITermService termService;
71

    
72
	@SpringBeanByType
73
    private INameService nameService;
74

    
75
	@SpringBeanByType
76
	private ITaxonService taxonService;
77

    
78
	@SpringBeanByType
79
	private IPolytomousKeyService polKeyService;
80

    
81
	@SpringBeanByType
82
	private IPolytomousKeyNodeService polKeyNodeService;
83

    
84

    
85
	private static final UUID t1Uuid = UUID.fromString("55c3e41a-c629-40e6-aa6a-ff274ac6ddb1");
86
	private static final UUID t2Uuid = UUID.fromString("2659a7e0-ff35-4ee4-8493-b453756ab955");
87
	private static final UUID classificationUuid = UUID.fromString("6c2bc8d9-ee62-4222-be89-4a8e31770878");
88
	private static final UUID classification2Uuid = UUID.fromString("43d67247-936f-42a3-a739-bbcde372e334");
89
	private static final UUID referenceUuid = UUID.fromString("de7d1205-291f-45d9-9059-ca83fc7ade14");
90
	private static final UUID node1Uuid= UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
91
	private static final UUID node2Uuid = UUID.fromString("2d41f0c2-b785-4f73-a436-cc2d5e93cc5b");
92

    
93
	private static final UUID node4Uuid = UUID.fromString("2fbf7bf5-22dd-4c1a-84e4-c8c93d1f0342");
94
	private static final UUID node5Uuid = UUID.fromString("c4d5170a-7967-4dac-ab76-ae2019eefde5");
95
	private static final UUID node6Uuid = UUID.fromString("b419ba5e-9c8b-449c-ad86-7abfca9a7340");
96
	private static final UUID rootNodeUuid = UUID.fromString("324a1a77-689c-44be-8e65-347d835f4111");
97

    
98

    
99
	private Taxon t1;
100
	private Taxon t2;
101
	private Taxon t4;
102
//	private Synonym s1;
103
	private SynonymRelationshipType synonymRelationshipType;
104
	private Reference reference;
105
	private String referenceDetail;
106
	private Classification classification;
107
	private TaxonNode node1;
108
	private TaxonNode node2;
109

    
110
    private TaxonNode node4;
111

    
112
	/**
113
	 * @throws java.lang.Exception
114
	 */
115
	@Before
116
	public void setUp() throws Exception {
117
	}
118

    
119
	/**
120
	 * 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)}.
121
	 */
122
	@Test
123
	@DataSet
124
	public final void testMakeTaxonNodeASynonymOfAnotherTaxonNode() {
125
		classification = classificationService.load(classificationUuid);
126
		node1 = taxonNodeService.load(node1Uuid);
127
		node2 = taxonNodeService.load(node2Uuid);
128
		node4 = taxonNodeService.load(node4Uuid);
129
		reference = referenceService.load(referenceUuid);
130
//		synonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
131
		synonymRelationshipType = CdmBase.deproxy(termService.load(SynonymRelationshipType.uuidHomotypicSynonymOf), SynonymRelationshipType.class) ;
132
		referenceDetail = "test";
133

    
134
		//
135
		//TODO
136

    
137
//		printDataSet(System.err, new String [] {"TaxonNode"});
138

    
139
		// descriptions
140
		t1 = node1.getTaxon();
141
		PolytomousKey polKey = PolytomousKey.NewInstance();
142
		PolytomousKeyNode keyNode = PolytomousKeyNode.NewInstance("", "", t1, null);
143
		keyNode.setKey(polKey);
144
		polKeyNodeService.save(keyNode);
145
		polKeyService.save(polKey);
146

    
147
		//nameRelations
148

    
149
		t1.getName().addRelationshipFromName(BotanicalName.NewInstance(Rank.SPECIES()), NameRelationshipType.ALTERNATIVE_NAME(), null );
150

    
151
		//taxonRelations
152
		t1.addTaxonRelation(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), TaxonRelationshipType.CONGRUENT_OR_EXCLUDES(), null, null);
153
		Synonym synonym = Synonym.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null);
154
		UUID uuidSynonym = taxonService.save(synonym).getUuid();
155

    
156
		t1.addHomotypicSynonym(synonym, null, null);
157
		UUID uuidT1 = taxonService.saveOrUpdate(t1);
158
		t1 = null;
159
		t1 =(Taxon) taxonService.load(uuidT1);
160
		t1 = HibernateProxyHelper.deproxy(t1);
161
		TaxonNameBase nameT1 = t1.getName();
162
		UUID t1UUID = t1.getUuid();
163
		t2 = node2.getTaxon();
164
		assertEquals(2, t1.getDescriptions().size());
165
		Assert.assertTrue(t2.getSynonyms().isEmpty());
166
		Assert.assertTrue(t2.getDescriptions().size() == 0);
167
		assertEquals(2,t1.getSynonyms().size());
168
		UUID synUUID = null;
169
		DeleteResult result;
170

    
171
		t4 = node4.getTaxon();
172
        UUID uuidT4 = t4.getUuid();
173
        t4 = (Taxon) taxonService.load(uuidT4);
174
        TaxonNameBase name4 = nameService.load(t4.getName().getUuid());
175
        result = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode(node4, node2, synonymRelationshipType, reference, referenceDetail);
176
        if (result.isError() || result.isAbort()){
177
            Assert.fail();
178
        }
179
        t4 = (Taxon)taxonService.find(uuidT4);
180
        assertNull(t4);
181

    
182

    
183
		//Taxon can't be deleted because of the polytomous key node
184

    
185
		result = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode(node1, node2, synonymRelationshipType, reference, referenceDetail);
186
		if (result.isError() || result.isAbort()){
187
			Assert.fail();
188
		}
189
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
190
		t1 = (Taxon)taxonService.find(t1Uuid);
191
		assertNotNull(t1);//because of the polytomous key node
192
		node1 = taxonNodeService.load(node1Uuid);
193
		assertNull(node1);
194

    
195

    
196

    
197

    
198
		Set<CdmBase> updatedObjects = result.getUpdatedObjects();
199
		Iterator<CdmBase> it = updatedObjects.iterator();
200
		Taxon taxon;
201
		if (it.hasNext()) {
202
			CdmBase updatedObject = it.next();
203
			if(updatedObject.isInstanceOf(Taxon.class)){
204
				taxon = HibernateProxyHelper.deproxy(updatedObject, Taxon.class);
205
				Set<Synonym> syns =  taxon.getSynonyms();
206
				assertNotNull(syns);
207
				assertEquals(4,syns.size());
208

    
209
				Set<TaxonNameBase> typifiedNames =taxon.getHomotypicGroup().getTypifiedNames();
210
				assertEquals(typifiedNames.size(),4);
211
				assertTrue(taxon.getHomotypicGroup().equals( nameT1.getHomotypicalGroup()));
212

    
213
				assertEquals(taxon, t2);
214

    
215
			} else{
216
				Assert.fail();
217
			}
218

    
219

    
220
		}
221

    
222

    
223

    
224
	}
225

    
226
	/**
227
	 * 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)}.
228
	 */
229
	@Test
230
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
231
	public final void testMakeTaxonNodeAHeterotypicSynonymOfAnotherTaxonNode() {
232
		classification = classificationService.load(classificationUuid);
233
		node1 = taxonNodeService.load(node1Uuid);
234
		node2 = taxonNodeService.load(node2Uuid);
235
		reference = referenceService.load(referenceUuid);
236
//		synonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
237
		synonymRelationshipType = CdmBase.deproxy(termService.load(SynonymRelationshipType.uuidHeterotypicSynonymOf), SynonymRelationshipType.class) ;
238
		referenceDetail = "test";
239

    
240
		//
241
		//TODO
242

    
243
//		printDataSet(System.err, new String [] {"TaxonNode"});
244

    
245
		// descriptions
246
		t1 = node1.getTaxon();
247
		PolytomousKey polKey = PolytomousKey.NewInstance();
248
		PolytomousKeyNode keyNode = PolytomousKeyNode.NewInstance("", "", t1, null);
249
		keyNode.setKey(polKey);
250
		polKeyNodeService.save(keyNode);
251
		polKeyService.save(polKey);
252

    
253
		//nameRelations
254

    
255
		t1.getName().addRelationshipFromName(BotanicalName.NewInstance(Rank.SPECIES()), NameRelationshipType.ALTERNATIVE_NAME(), null );
256
		TaxonNameBase name1 = t1.getName();
257
		UUID name1UUID = name1.getUuid();
258
		//taxonRelations
259
		t1.addTaxonRelation(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), TaxonRelationshipType.CONGRUENT_OR_EXCLUDES(), null, null);
260
		Synonym synonym = Synonym.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null);
261
		UUID uuidSynonym = taxonService.save(synonym).getUuid();
262
		t1.addHomotypicSynonym(synonym, null, null);
263
		TaxonNameBase nameT1 = t1.getName();
264
		UUID t1UUID = t1.getUuid();
265
		t2 = node2.getTaxon();
266
		assertEquals(2, t1.getDescriptions().size());
267
		Assert.assertTrue(t2.getSynonyms().isEmpty());
268
		Assert.assertTrue(t2.getDescriptions().size() == 0);
269
		assertEquals(2,t1.getSynonyms().size());
270
		UUID synUUID = null;
271
		DeleteResult result;
272
		result = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode(node1, node2, synonymRelationshipType, reference, referenceDetail);
273

    
274
		if (!result.getUpdatedObjects().iterator().hasNext()){
275
			Assert.fail();
276
		}
277
		Taxon newAcceptedTaxon = (Taxon)result.getUpdatedObjects().iterator().next();
278
		assertNotNull(taxonService.find(t1Uuid));
279
		assertNull(taxonNodeService.find(node1Uuid));
280

    
281

    
282
		synonym = (Synonym)taxonService.find(uuidSynonym);
283

    
284
		assertNotNull(synonym);
285
		keyNode.setTaxon(null);
286
		polKeyNodeService.saveOrUpdate(keyNode);
287
		HibernateProxyHelper.deproxy(t2);
288
		HibernateProxyHelper.deproxy(t2.getHomotypicGroup());
289
		HibernateProxyHelper.deproxy(t2.getName());
290
//		syn = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode(node1, node2, synonymRelationshipType, reference, referenceDetail);
291
//		if (syn == null){
292
//			Assert.fail();
293
//		}
294
//		synUUID = syn.getUuid();
295

    
296

    
297
		termService.saveOrUpdate(synonymRelationshipType);
298
		Assert.assertFalse(t2.getSynonyms().isEmpty());
299
		assertEquals(3,t2.getSynonyms().size());
300
		assertEquals(2, t2.getDescriptions().size());
301

    
302
		result = taxonService.deleteTaxon(t1.getUuid(), null, null);
303
		if (result.isAbort() || result.isError()){
304
			Assert.fail();
305
		}
306
		assertNull(taxonService.find(t1Uuid));
307
		assertNull(taxonNodeService.find(node1Uuid));
308
		name1 = nameService.find(name1UUID);
309
		synonym = (Synonym)taxonService.find(uuidSynonym);
310
		assertNotNull(name1);
311
		assertEquals(1, name1.getTaxonBases().size());
312
		assertNotNull(synonym);
313

    
314
		Synonym syn =(Synonym) name1.getTaxonBases().iterator().next();
315

    
316
		assertEquals(syn.getName().getHomotypicalGroup(), synonym.getName().getHomotypicalGroup());
317
		assertFalse(newAcceptedTaxon.getHomotypicGroup().equals( syn.getName().getHomotypicalGroup()));
318

    
319
		assertEquals(newAcceptedTaxon, t2);
320
		TaxonNameBase name = syn.getName();
321
		assertEquals(name, nameT1);
322
	}
323

    
324

    
325
	@Test
326
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonNodeServiceImplTest-indexing.xml")
327
	public final void testIndexCreateNode() {
328
		Taxon taxon = Taxon.NewInstance(null, null);
329
		classification = classificationService.load(classificationUuid);
330
		node2 = taxonNodeService.load(node2Uuid);
331
		String oldTreeIndex = node2.treeIndex();
332

    
333
		TaxonNode newNode = node2.addChildTaxon(taxon, null, null);
334
		taxonNodeService.saveOrUpdate(node2);
335
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
336
		newNode = taxonNodeService.load(newNode.getUuid());
337
		Assert.assertEquals("", oldTreeIndex + newNode.getId() + "#", newNode.treeIndex());
338
	}
339

    
340

    
341
	@Test
342
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonNodeServiceImplTest-indexing.xml")
343
	public final void testIndexMoveNode() {
344
		//in classification
345
		classification = classificationService.load(classificationUuid);
346
		node1 = taxonNodeService.load(node1Uuid);
347
		node2 = taxonNodeService.load(node2Uuid);
348
		node2.addChildNode(node1, null, null);
349
		taxonNodeService.saveOrUpdate(node1);
350
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
351
		TaxonNode node6 = taxonNodeService.load(node6Uuid);
352
		Assert.assertEquals("Node6 treeindex is not correct", node2.treeIndex() + "2#4#6#", node6.treeIndex());
353

    
354
		//root of new classification
355
		Classification classification2 = classificationService.load(classification2Uuid);
356
		node1 = taxonNodeService.load(node1Uuid);
357
		classification2.setRootNode(HibernateProxyHelper.deproxy(classification2.getRootNode(),TaxonNode.class));
358
		classification2.addChildNode(node1, null, null);
359
		taxonNodeService.saveOrUpdate(node1);
360
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
361
		node1 = taxonNodeService.load(node1Uuid);
362
		Assert.assertEquals("Node1 treeindex is not correct", "#t2#8#2#", node1.treeIndex());
363
		node6 = taxonNodeService.load(node6Uuid);
364
		Assert.assertEquals("Node6 treeindex is not correct", "#t2#8#2#4#6#", node6.treeIndex());
365

    
366
		//into new classification
367
		node2 = taxonNodeService.load(node2Uuid);
368
		TaxonNode node5 = taxonNodeService.load(node5Uuid);
369
		node5.addChildNode(node2, null, null);
370
		taxonNodeService.saveOrUpdate(node5);
371
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
372
		node2 = taxonNodeService.load(node2Uuid);
373
		Assert.assertEquals("Node3 treeindex is not correct", "#t2#8#2#5#3#", node2.treeIndex());
374

    
375
}
376

    
377
	@Test
378
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonNodeServiceImplTest-indexing.xml")
379
	public final void testIndexDeleteNode() {
380
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
381
		node1 = taxonNodeService.load(node1Uuid);
382
		TaxonNode node4 = taxonNodeService.load(node4Uuid);
383
		String treeIndex = node1.treeIndex();
384
		TaxonNode node6 = taxonNodeService.load(node6Uuid);
385
		treeIndex= node6.treeIndex();
386

    
387
		HibernateProxyHelper.deproxy(node1, TaxonNode.class);
388
		node1.deleteChildNode(node4, false);
389
		TaxonNode node5 = taxonNodeService.load(node5Uuid);
390
		treeIndex = node5.treeIndex();
391

    
392
		node6 = taxonNodeService.load(node6Uuid);
393

    
394
		treeIndex = node6.treeIndex();
395
		Taxon newTaxon= Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null);
396
		UUID taxonNewUuid = taxonService.save(newTaxon).getUuid();
397

    
398
		node5.addChildTaxon(newTaxon, null, null);
399
		String node5TreeIndex =node5.treeIndex();
400
		taxonNodeService.saveOrUpdate(node5);
401

    
402
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
403
		node5 = taxonNodeService.load(node5Uuid);
404
		List<TaxonNode> children =  node5.getChildNodes();
405
		TaxonNode node = children.get(0);
406
		int id = node.getId();
407
		Assert.assertEquals("Node6 treeindex is not correct", "#t1#1#2#6#", treeIndex);
408
		Assert.assertEquals("new node treeindex is not correct", node5TreeIndex + id +"#", node.treeIndex());
409
	}
410

    
411

    
412
	@Test
413
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
414
	public final void testDeleteNode(){
415
		classification = classificationService.load(classificationUuid);
416
		node1 = taxonNodeService.load(node1Uuid);
417
		node2 = taxonNodeService.load(rootNodeUuid);
418
		node1 = HibernateProxyHelper.deproxy(node1);
419

    
420
		TaxonNode newNode = node1.addChildTaxon(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), null, null);
421
		UUID uuidNewNode = taxonNodeService.save(newNode).getUuid();
422
		newNode = taxonNodeService.load(uuidNewNode);
423
		UUID taxUUID = newNode.getTaxon().getUuid();
424
		UUID nameUUID = newNode.getTaxon().getName().getUuid();
425

    
426
		DeleteResult result = taxonNodeService.deleteTaxonNode(node1, null);
427
		if (!result.isOk()){
428
			Assert.fail();
429
		}
430
		newNode = taxonNodeService.load(uuidNewNode);
431
		node1 = taxonNodeService.load(node1Uuid);
432
		assertNull(newNode);
433
		assertNull(node1);
434

    
435
		t1 = (Taxon) taxonService.load(t1Uuid);
436
		assertNull(t1);
437
		Taxon newTaxon = (Taxon)taxonService.load(taxUUID);
438
		assertNull(newTaxon);
439
		BotanicalName name = (BotanicalName)nameService.load(nameUUID);
440
		assertNull(name);
441

    
442

    
443
	}
444

    
445
	@Test
446
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
447

    
448
    public final void testDeleteNodeWithReusedTaxon(){
449
        classification = classificationService.load(classificationUuid);
450
        node1 = taxonNodeService.load(node1Uuid);
451
        node2 = taxonNodeService.load(rootNodeUuid);
452
        node1 = HibernateProxyHelper.deproxy(node1);
453

    
454

    
455
        Classification classification2 = Classification.NewInstance("Classification2");
456
        TaxonNode nodeClassification2 =classification2.addChildTaxon(node1.getTaxon(), null, null);
457
        assertEquals(node1.getTaxon().getUuid(), t1Uuid);
458
        classificationService.save(classification2);
459
        List<TaxonNode> nodesOfClassification2 = taxonNodeService.listAllNodesForClassification(classification2, null, null);
460
        UUID nodeUUID = nodesOfClassification2.get(0).getUuid();
461
        assertEquals(nodeUUID, nodeClassification2.getUuid());
462
        Taxon newTaxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()),  null);
463
        taxonService.save(newTaxon);
464
        TaxonNode newNode = node1.addChildTaxon(newTaxon,null, null);
465
        UUID uuidNewNode = taxonNodeService.save(newNode).getUuid();
466
        newNode = taxonNodeService.load(uuidNewNode);
467
        UUID taxUUID = newNode.getTaxon().getUuid();
468
        UUID nameUUID = newNode.getTaxon().getName().getUuid();
469

    
470
        DeleteResult result = taxonNodeService.deleteTaxonNode(node1, null);
471
        if (!result.isOk()){
472
            Assert.fail();
473
        }
474
        taxonService.getSession().flush();
475
        newNode = taxonNodeService.load(uuidNewNode);
476
        node1 = taxonNodeService.load(node1Uuid);
477
        assertNull(newNode);
478
        assertNull(node1);
479
        assertNotNull(taxonNodeService.load(nodeUUID));
480

    
481
        t1 = (Taxon) taxonService.load(t1Uuid);
482
        assertNotNull(t1);
483
        newTaxon = (Taxon)taxonService.load(taxUUID);
484
        assertNull(newTaxon);
485
        BotanicalName name = (BotanicalName)nameService.load(nameUUID);
486
        assertNull(name);
487

    
488

    
489
    }
490

    
491

    
492

    
493
	@Test
494
	@DataSet
495
	public final void testDeleteNodes(){
496
		classification = classificationService.load(classificationUuid);
497
		node1 = taxonNodeService.load(node1Uuid);
498
		node2 = taxonNodeService.load(rootNodeUuid);
499
		node1 = HibernateProxyHelper.deproxy(node1);
500
		node2 = HibernateProxyHelper.deproxy(node2);
501
		TaxonNode newNode = node1.addChildTaxon(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), null, null);
502
		UUID uuidNewNode = taxonNodeService.save(newNode).getUuid();
503
		List<TaxonNode> treeNodes = new ArrayList<TaxonNode>();
504
		treeNodes.add(node1);
505
		treeNodes.add(node2);
506

    
507
		DeleteResult result = taxonNodeService.deleteTaxonNodes(treeNodes, null);
508

    
509

    
510
		newNode = taxonNodeService.load(uuidNewNode);
511
		node1 = taxonNodeService.load(node1Uuid);
512
		assertNull(newNode);
513
		assertNull(node1);
514
		taxonService.getSession().flush();
515
		t1 = (Taxon) taxonService.load(t1Uuid);
516
		assertNull(t1);
517
		t2 = (Taxon) taxonService.load(t2Uuid);
518
		assertNull(t2);
519

    
520

    
521
	}
522
	@Test
523
	@DataSet
524
	public void testMoveTaxonNode(){
525
	    classification = classificationService.load(classificationUuid);
526
	  //  Set<TaxonNode>  nodes = classification.getAllNodes();
527
	    List<TaxonNode>  nodes = classification.getChildNodes();
528
	    System.out.println(nodes.size());
529
	    classification.addChildTaxon(Taxon.NewInstance(BotanicalName.NewInstance(Rank.SPECIES()), null), nodes.size(), null, null);
530
	   nodes =  classification.getChildNodes();
531
	    System.out.println(nodes.size());
532

    
533
	}
534

    
535
    @Test
536
    public void testCompareNaturalOrder() {
537
    	/*
538
    	 * Classification
539
    	 *  * Abies
540
    	 *  `- Abies alba
541
    	 *   - Abies balsamea
542
    	 *  * Pinus
543
    	 *  `- Pinus pampa
544
    	 */
545
    	Classification classification = Classification.NewInstance("Classification");
546
    	BotanicalName abiesName = BotanicalName.NewInstance(Rank.GENUS());
547
    	abiesName.setGenusOrUninomial("Abies");
548
    	Taxon abies = Taxon.NewInstance(abiesName, null);
549
    	BotanicalName abiesAlbaName = BotanicalName.NewInstance(Rank.SPECIES());
550
    	abiesAlbaName.setGenusOrUninomial("Abies");
551
    	abiesAlbaName.setSpecificEpithet("alba");
552
    	Taxon abiesAlba = Taxon.NewInstance(abiesAlbaName, null);
553
    	BotanicalName pinusName = BotanicalName.NewInstance(Rank.GENUS());
554
    	pinusName.setGenusOrUninomial("Pinus");
555
    	Taxon pinus = Taxon.NewInstance(pinusName, null);
556
    	BotanicalName pinusPampaName = BotanicalName.NewInstance(Rank.SPECIES());
557
    	pinusPampaName.setGenusOrUninomial("Pinus");
558
    	pinusPampaName.setSpecificEpithet("pampa");
559
    	Taxon pinusPampa = Taxon.NewInstance(pinusPampaName, null);
560

    
561
        BotanicalName abiesBalsameaName = BotanicalName.NewInstance(Rank.SPECIES());
562
        abiesBalsameaName.setGenusOrUninomial("Abies");
563
        abiesBalsameaName.setSpecificEpithet("balsamea");
564
        Taxon abiesBalsamea = Taxon.NewInstance(abiesBalsameaName, null);
565

    
566

    
567
    	classification.addChildTaxon(abies, null, null);
568
    	TaxonNode abiesAlbaNode = classification.addParentChild(abies, abiesAlba, null, null);
569
    	TaxonNode balsameaNode = classification.addParentChild(abies, abiesBalsamea, null, null);
570
    	classification.addChildTaxon(pinus, null, null);
571
    	classification.addParentChild(pinus, pinusPampa, null, null);
572
    	classificationService.save(classification);
573

    
574
    	TaxonNaturalComparator comparator = new TaxonNaturalComparator();
575
    	List<TaxonNode> allNodes = new ArrayList<>(classification.getAllNodes());
576
    	Collections.sort(allNodes, comparator);
577
    	System.out.println("---Before sorting---");
578
    	for (TaxonNode node: allNodes){
579
    	    System.out.println(node.getTaxon().getTitleCache());
580
    	}
581
    	Assert.assertEquals(allNodes.get(0).getTaxon(), abies );
582
    	Assert.assertEquals(allNodes.get(2).getTaxon(), abiesBalsamea );
583
    	Assert.assertEquals(allNodes.get(1).getTaxon(), abiesAlba );
584

    
585
    	taxonNodeService.moveTaxonNode(balsameaNode, abiesAlbaNode,1);
586
    	classification = classificationService.load(classification.getUuid());
587

    
588
       allNodes = new ArrayList<>(classification.getAllNodes());
589
        Collections.sort(allNodes, comparator);
590
        System.out.println("\n---After sorting---");
591
        for (TaxonNode node: allNodes){
592
            System.out.println(node.getTaxon().getTitleCache());
593
        }
594
        Assert.assertEquals(allNodes.get(0).getTaxon(), abies );
595
        Assert.assertEquals(allNodes.get(1).getTaxon(), abiesBalsamea );
596
        Assert.assertEquals(allNodes.get(2).getTaxon(), abiesAlba );
597

    
598
    }
599

    
600

    
601
    @Override
602
    public void createTestDataSet() throws FileNotFoundException {}
603

    
604

    
605
}
(23-23/32)