Project

General

Profile

Download (42.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.api.service;
11

    
12
import static org.junit.Assert.assertEquals;
13
import static org.junit.Assert.assertFalse;
14
import static org.junit.Assert.assertNotNull;
15
import static org.junit.Assert.assertNull;
16
import static org.junit.Assert.assertTrue;
17

    
18
import java.io.FileNotFoundException;
19
import java.util.ArrayList;
20
import java.util.Collections;
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.IBotanicalName;
37
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
38
import eu.etaxonomy.cdm.model.name.Rank;
39
import eu.etaxonomy.cdm.model.name.TaxonName;
40
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
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.SynonymType;
45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
47
import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
48
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
49
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
50
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
51
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
52
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
53

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

    
61

    
62
	@SpringBeanByType
63
	private ITaxonNodeService taxonNodeService;
64

    
65
	@SpringBeanByType
66
	private IClassificationService classificationService;
67

    
68
	@SpringBeanByType
69
	private IReferenceService referenceService;
70

    
71
	@SpringBeanByType
72
	private ITermService termService;
73

    
74
	@SpringBeanByType
75
    private INameService nameService;
76

    
77
	@SpringBeanByType
78
	private ITaxonService taxonService;
79

    
80
	@SpringBeanByType
81
	private IPolytomousKeyService polKeyService;
82

    
83
	@SpringBeanByType
84
	private IPolytomousKeyNodeService polKeyNodeService;
85

    
86

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

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

    
100

    
101
	private Taxon t1;
102
	private Taxon t2;
103
	private Taxon t4;
104
//	private Synonym s1;
105
	private SynonymType synonymType;
106
	private Reference reference;
107
	private String referenceDetail;
108
	private Classification classification;
109
	private TaxonNode node1;
110
	private TaxonNode node2;
111

    
112
    private TaxonNode node4;
113

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

    
121
	/**
122
	 * 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.SynonymType, eu.etaxonomy.cdm.model.reference.Reference, java.lang.String)}.
123
	 */
124
	@Test
125
	@DataSet
126
	public final void testMakeTaxonNodeASynonymOfAnotherTaxonNode() {
127
		classification = classificationService.load(classificationUuid);
128
		node1 = taxonNodeService.load(node1Uuid);
129
		node2 = taxonNodeService.load(node2Uuid);
130
		node4 = taxonNodeService.load(node4Uuid);
131
		reference = referenceService.load(referenceUuid);
132
//		synonymType = SynonymType.HOMOTYPIC_SYNONYM_OF();
133
		synonymType = CdmBase.deproxy(termService.load(SynonymType.uuidHomotypicSynonymOf), SynonymType.class) ;
134
		referenceDetail = "test";
135

    
136
		//
137
		//TODO
138

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

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

    
149
		//nameRelations
150

    
151
		t1.getName().addRelationshipFromName(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), NameRelationshipType.ALTERNATIVE_NAME(), null );
152

    
153
		//taxonRelations
154
		t1.addTaxonRelation(Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null), TaxonRelationshipType.CONGRUENT_OR_EXCLUDES(), null, null);
155
		Synonym synonym = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
156
		UUID uuidSynonym = taxonService.save(synonym).getUuid();
157

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

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

    
184
		//Taxon can't be deleted because of the polytomous key node
185
		result = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode(node1, node2, synonymType, 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
		Set<CdmBase> updatedObjects = result.getUpdatedObjects();
196
		Iterator<CdmBase> it = updatedObjects.iterator();
197
		Taxon taxon;
198
		while (it.hasNext()) {
199
			CdmBase updatedObject = it.next();
200
			if(updatedObject.isInstanceOf(Taxon.class)){
201
				taxon = HibernateProxyHelper.deproxy(updatedObject, Taxon.class);
202
				Set<Synonym> syns =  taxon.getSynonyms();
203
				assertNotNull(syns);
204
				if (taxon.equals(t2)){
205
				    assertEquals(4,syns.size());
206
				    Set<TaxonName> typifiedNames =taxon.getHomotypicGroup().getTypifiedNames();
207
	                assertEquals(typifiedNames.size(),4);
208
	                assertTrue(taxon.getHomotypicGroup().equals( nameT1.getHomotypicalGroup()));
209

    
210
	                assertEquals(taxon, t2);
211
				}
212

    
213
			}
214

    
215
		}
216
	}
217

    
218
	/**
219
	 * 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.SynonymType, eu.etaxonomy.cdm.model.reference.Reference, java.lang.String)}.
220
	 */
221
	@Test
222
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
223
	public final void testMakeTaxonNodeAHeterotypicSynonymOfAnotherTaxonNode() {
224

    
225
	    //create data
226
	    classification = classificationService.load(classificationUuid);
227
		node1 = taxonNodeService.load(node1Uuid);
228
		node2 = taxonNodeService.load(node2Uuid);
229
		reference = referenceService.load(referenceUuid);
230
		synonymType = CdmBase.deproxy(termService.load(SynonymType.uuidHeterotypicSynonymOf), SynonymType.class) ;
231
		referenceDetail = "test";
232

    
233
		// descriptions
234
		t1 = node1.getTaxon();
235
		PolytomousKey polKey = PolytomousKey.NewInstance();
236
		PolytomousKeyNode keyNode = PolytomousKeyNode.NewInstance("", "", t1, null);
237
		keyNode.setKey(polKey);
238
		polKeyNodeService.save(keyNode);
239
		polKeyService.save(polKey);
240

    
241
		//nameRelations
242
		t1.getName().addRelationshipFromName(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), NameRelationshipType.ALTERNATIVE_NAME(), null );
243
		TaxonName name1 = t1.getName();
244
		UUID name1UUID = name1.getUuid();
245
		//taxonRelations
246
		t1.addTaxonRelation(Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null), TaxonRelationshipType.CONGRUENT_OR_EXCLUDES(), null, null);
247
		Synonym t1HomotypSynonym = Synonym.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
248

    
249
		t1.addHomotypicSynonym(t1HomotypSynonym);
250
		TaxonName nameT1 = t1.getName();
251
		t2 = node2.getTaxon();
252
		assertEquals("taxon 1 must have 2 descriptions", 2, t1.getDescriptions().size());
253
		assertEquals("taxon 1 must have 2 synonyms", 2, t1.getSynonyms().size());
254
		Assert.assertTrue("taxon 2 must have no synonyms", t2.getSynonyms().isEmpty());
255
		Assert.assertTrue("taxon 2 must have no descriptions", t2.getDescriptions().size() == 0);
256

    
257
		//save
258
		UUID uuidSynonym = taxonService.save(t1HomotypSynonym).getUuid();
259

    
260
		//do it
261
		DeleteResult result = taxonNodeService.makeTaxonNodeASynonymOfAnotherTaxonNode
262
		        (node1, node2, synonymType, reference, referenceDetail);
263

    
264
		//post conditions
265
		if (!result.getUpdatedObjects().iterator().hasNext()){
266
			Assert.fail("Some updates must have taken place");
267
		}
268
		assertEquals(3,result.getUpdatedObjects().size());
269
		assertNotNull("Old taxon should not have been deleted as it is referenced by key node", taxonService.find(t1Uuid));
270
		assertNull("Old taxon node should not exist anymore", taxonNodeService.find(node1Uuid));
271

    
272
		t1HomotypSynonym = (Synonym)taxonService.find(uuidSynonym);
273
		assertNotNull(t1HomotypSynonym);
274

    
275
		keyNode.setTaxon(null);
276
		polKeyNodeService.saveOrUpdate(keyNode);
277
		t2 =HibernateProxyHelper.deproxy(t2);
278
		HibernateProxyHelper.deproxy(t2.getHomotypicGroup());
279
		t2.setName(HibernateProxyHelper.deproxy(t2.getName()));
280

    
281
		termService.saveOrUpdate(synonymType);
282
		assertFalse("taxon 2 must have a synonym now", t2.getSynonyms().isEmpty());
283
		assertEquals("taxon 2 must have 3 synonyms now, the old taxon 1 and it's 2 synonyms", 3, t2.getSynonyms().size());
284
		assertEquals("taxon 2 must have 2 descriptions now, taken form taxon 1", 2, t2.getDescriptions().size());
285

    
286
		result = taxonService.deleteTaxon(t1.getUuid(), null, null);
287
		if (result.isAbort() || result.isError()){
288
			Assert.fail();
289
		}
290
		assertNull(taxonService.find(t1Uuid));
291
		assertNull(taxonNodeService.find(node1Uuid));
292
		name1 = nameService.find(name1UUID);
293
		assertNotNull("taxon name 1 should still exist", name1);
294
		assertEquals("... but being used for the new synonym only as taxon 1 is deleted", 1, name1.getTaxonBases().size());
295
		t1HomotypSynonym = (Synonym)taxonService.find(uuidSynonym);
296
		assertNotNull(t1HomotypSynonym);
297

    
298
		Synonym newSynonym =(Synonym) name1.getTaxonBases().iterator().next();
299

    
300
		Taxon newAcceptedTaxon = CdmBase.deproxy(taxonService.find(t2.getUuid()), Taxon.class);
301
		assertEquals("The new synonym (old accepted taxon) and it's homotypic synonym should still be homotypic", newSynonym.getHomotypicGroup(), t1HomotypSynonym.getName().getHomotypicalGroup());
302
		assertFalse("The new accepted taxon must not be homotypic to ", newAcceptedTaxon.getHomotypicGroup().equals(newSynonym.getName().getHomotypicalGroup()));
303

    
304
		assertEquals("The new accepted taxon is taxon 2", newAcceptedTaxon, t2);
305
		assertEquals("The new synonyms name must be the same as the old accepted taxon's name", newSynonym.getName(), nameT1);
306
	}
307

    
308

    
309
	@Test
310
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonNodeServiceImplTest-indexing.xml")
311
	public final void testIndexCreateNode() {
312
		Taxon taxon = Taxon.NewInstance(null, null);
313
		classification = classificationService.load(classificationUuid);
314
		node2 = taxonNodeService.load(node2Uuid);
315
		String oldTreeIndex = node2.treeIndex();
316

    
317
		TaxonNode newNode = node2.addChildTaxon(taxon, null, null);
318
		taxonNodeService.saveOrUpdate(newNode);
319
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
320
		newNode = taxonNodeService.load(newNode.getUuid());
321
		Assert.assertEquals("", oldTreeIndex + newNode.getId() + "#", newNode.treeIndex());
322
	}
323

    
324

    
325
	@Test
326
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonNodeServiceImplTest-indexing.xml")
327
	public final void testIndexMoveNode() {
328
		//in classification
329
		classification = classificationService.load(classificationUuid);
330
		node1 = taxonNodeService.load(node1Uuid);
331
		node2 = taxonNodeService.load(node2Uuid);
332
		node2.addChildNode(node1, null, null);
333
		taxonNodeService.saveOrUpdate(node1);
334
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
335
		TaxonNode node6 = taxonNodeService.load(node6Uuid);
336
		Assert.assertEquals("Node6 treeindex is not correct", node2.treeIndex() + "2#4#6#", node6.treeIndex());
337

    
338
		//root of new classification
339
		Classification classification2 = classificationService.load(classification2Uuid);
340
		node1 = taxonNodeService.load(node1Uuid);
341
		classification2.setRootNode(HibernateProxyHelper.deproxy(classification2.getRootNode(),TaxonNode.class));
342
		classification2.addChildNode(node1, null, null);
343
		taxonNodeService.saveOrUpdate(node1);
344
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
345
		node1 = taxonNodeService.load(node1Uuid);
346
		Assert.assertEquals("Node1 treeindex is not correct", "#t2#8#2#", node1.treeIndex());
347
		node6 = taxonNodeService.load(node6Uuid);
348
		Assert.assertEquals("Node6 treeindex is not correct", "#t2#8#2#4#6#", node6.treeIndex());
349

    
350
		//into new classification
351
		node2 = taxonNodeService.load(node2Uuid);
352
		TaxonNode node5 = taxonNodeService.load(node5Uuid);
353
		node2 =node5.addChildNode(node2, null, null);
354
		taxonNodeService.saveOrUpdate(node2);
355
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
356
		node2 = taxonNodeService.load(node2Uuid);
357
		Assert.assertEquals("Node3 treeindex is not correct", "#t2#8#2#5#3#", node2.treeIndex());
358

    
359
}
360

    
361
	@Test
362
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonNodeServiceImplTest-indexing.xml")
363
	public final void testIndexDeleteNode() {
364
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
365
		node1 = taxonNodeService.load(node1Uuid);
366
		TaxonNode node4 = taxonNodeService.load(node4Uuid);
367
		String treeIndex = node1.treeIndex();
368
		TaxonNode node6 = taxonNodeService.load(node6Uuid);
369
		treeIndex= node6.treeIndex();
370

    
371
		HibernateProxyHelper.deproxy(node1, TaxonNode.class);
372
		node1.deleteChildNode(node4, false);
373
		TaxonNode node5 = taxonNodeService.load(node5Uuid);
374
		treeIndex = node5.treeIndex();
375

    
376
		node6 = taxonNodeService.load(node6Uuid);
377

    
378
		treeIndex = node6.treeIndex();
379
		Taxon newTaxon= Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
380
		UUID taxonNewUuid = taxonService.save(newTaxon).getUuid();
381

    
382
		node5.addChildTaxon(newTaxon, null, null);
383
		String node5TreeIndex =node5.treeIndex();
384
		taxonNodeService.saveOrUpdate(node5);
385

    
386
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
387
		node5 = taxonNodeService.load(node5Uuid);
388
		List<TaxonNode> children =  node5.getChildNodes();
389
		TaxonNode node = children.get(0);
390
		int id = node.getId();
391
		Assert.assertEquals("Node6 treeindex is not correct", "#t1#1#2#6#", treeIndex);
392
		Assert.assertEquals("new node treeindex is not correct", node5TreeIndex + id +"#", node.treeIndex());
393
	}
394

    
395

    
396
	@Test
397
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
398
	public final void testDeleteNode(){
399
		classification = classificationService.load(classificationUuid);
400
		node1 = taxonNodeService.load(node1Uuid);
401
		node2 = taxonNodeService.load(rootNodeUuid);
402
		node1 = HibernateProxyHelper.deproxy(node1);
403

    
404
		TaxonNode newNode = node1.addChildTaxon(Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null), null, null);
405
		UUID uuidNewNode = taxonNodeService.save(newNode).getUuid();
406
		newNode = taxonNodeService.load(uuidNewNode);
407
		UUID taxUUID = newNode.getTaxon().getUuid();
408
		UUID nameUUID = newNode.getTaxon().getName().getUuid();
409

    
410
		DeleteResult result = taxonNodeService.deleteTaxonNode(node1, null);
411
		if (!result.isOk()){
412
			Assert.fail();
413
		}
414
		newNode = taxonNodeService.load(uuidNewNode);
415
		node1 = taxonNodeService.load(node1Uuid);
416
		assertNull(newNode);
417
		assertNull(node1);
418

    
419
		t1 = (Taxon) taxonService.load(t1Uuid);
420
		assertNull(t1);
421
		Taxon newTaxon = (Taxon)taxonService.load(taxUUID);
422
		assertNull(newTaxon);
423
		IBotanicalName name = nameService.load(nameUUID);
424
		assertNull(name);
425

    
426

    
427
	}
428

    
429
	@Test
430
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
431

    
432
    public final void testDeleteNodeWithReusedTaxon(){
433
        classification = classificationService.load(classificationUuid);
434
        node1 = taxonNodeService.load(node1Uuid);
435
        node2 = taxonNodeService.load(rootNodeUuid);
436
        node1 = HibernateProxyHelper.deproxy(node1);
437

    
438

    
439
        Classification classification2 = Classification.NewInstance("Classification2");
440
        TaxonNode nodeClassification2 =classification2.addChildTaxon(node1.getTaxon(), null, null);
441
        assertEquals(node1.getTaxon().getUuid(), t1Uuid);
442
        classificationService.save(classification2);
443
        List<TaxonNode> nodesOfClassification2 = taxonNodeService.listAllNodesForClassification(classification2, null, null);
444
        UUID nodeUUID = nodesOfClassification2.get(0).getUuid();
445
        assertEquals(nodeUUID, nodeClassification2.getUuid());
446
        Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()),  null);
447
        taxonService.save(newTaxon);
448
        TaxonNode newNode = node1.addChildTaxon(newTaxon,null, null);
449
        UUID uuidNewNode = taxonNodeService.save(newNode).getUuid();
450
        newNode = taxonNodeService.load(uuidNewNode);
451
        UUID taxUUID = newNode.getTaxon().getUuid();
452
        UUID nameUUID = newNode.getTaxon().getName().getUuid();
453

    
454
        DeleteResult result = taxonNodeService.deleteTaxonNode(node1, null);
455
        if (!result.isOk()){
456
            Assert.fail();
457
        }
458
        //taxonService.getSession().flush();
459
        newNode = taxonNodeService.load(uuidNewNode);
460
        node1 = taxonNodeService.load(node1Uuid);
461
        assertNull(newNode);
462
        assertNull(node1);
463
        assertNotNull(taxonNodeService.load(nodeUUID));
464

    
465
        t1 = (Taxon) taxonService.load(t1Uuid);
466
        assertNotNull(t1);
467
        newTaxon = (Taxon)taxonService.load(taxUUID);
468
        assertNull(newTaxon);
469
        IBotanicalName name = nameService.load(nameUUID);
470
        assertNull(name);
471

    
472

    
473
    }
474

    
475

    
476

    
477
	@Test
478
	@DataSet
479
	public final void testDeleteNodes(){
480
		classification = classificationService.load(classificationUuid);
481
		node1 = taxonNodeService.load(node1Uuid);
482
		node2 = taxonNodeService.load(rootNodeUuid);
483
		node1 = HibernateProxyHelper.deproxy(node1);
484
		node2 = HibernateProxyHelper.deproxy(node2);
485
		TaxonNode newNode = node1.addChildTaxon(Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null), null, null);
486
		UUID uuidNewNode = taxonNodeService.save(newNode).getUuid();
487
		List<TaxonNode> treeNodes = new ArrayList<TaxonNode>();
488
		treeNodes.add(node1);
489
		treeNodes.add(node2);
490

    
491
		DeleteResult result = taxonNodeService.deleteTaxonNodes(treeNodes, null);
492

    
493

    
494
		newNode = taxonNodeService.load(uuidNewNode);
495
		node1 = taxonNodeService.load(node1Uuid);
496
		assertNull(newNode);
497
		assertNull(node1);
498
		//taxonService.getSession().flush();
499
		t1 = (Taxon) taxonService.load(t1Uuid);
500
		assertNull(t1);
501
		t2 = (Taxon) taxonService.load(t2Uuid);
502
		assertNull(t2);
503

    
504

    
505
	}
506
	@Test
507
	@DataSet
508
	public void testMoveTaxonNode(){
509
	    classification = classificationService.load(classificationUuid);
510
	  //  Set<TaxonNode>  nodes = classification.getAllNodes();
511
	    List<TaxonNode>  nodes = classification.getChildNodes();
512
	    System.out.println(nodes.size());
513
	    classification.addChildTaxon(Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null), nodes.size(), null, null);
514
	   nodes =  classification.getChildNodes();
515
	    System.out.println(nodes.size());
516

    
517
	}
518

    
519
    @Test
520
    public void testCompareNaturalOrder() {
521
    	/*
522
    	 * Classification
523
    	 *  * Abies
524
    	 *  `- Abies alba
525
    	 *   - Abies balsamea
526
    	 *  * Pinus
527
    	 *  `- Pinus pampa
528
    	 */
529
    	Classification classification = Classification.NewInstance("Classification");
530
    	IBotanicalName abiesName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
531
    	abiesName.setGenusOrUninomial("Abies");
532
    	Taxon abies = Taxon.NewInstance(abiesName, null);
533
    	IBotanicalName abiesAlbaName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
534
    	abiesAlbaName.setGenusOrUninomial("Abies");
535
    	abiesAlbaName.setSpecificEpithet("alba");
536
    	Taxon abiesAlba = Taxon.NewInstance(abiesAlbaName, null);
537
    	IBotanicalName pinusName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
538
    	pinusName.setGenusOrUninomial("Pinus");
539
    	Taxon pinus = Taxon.NewInstance(pinusName, null);
540
    	IBotanicalName pinusPampaName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
541
    	pinusPampaName.setGenusOrUninomial("Pinus");
542
    	pinusPampaName.setSpecificEpithet("pampa");
543
    	Taxon pinusPampa = Taxon.NewInstance(pinusPampaName, null);
544

    
545
        IBotanicalName abiesBalsameaName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
546
        abiesBalsameaName.setGenusOrUninomial("Abies");
547
        abiesBalsameaName.setSpecificEpithet("balsamea");
548
        Taxon abiesBalsamea = Taxon.NewInstance(abiesBalsameaName, null);
549

    
550
        List<TaxonNode> nodes = new ArrayList<TaxonNode>();
551
    	nodes.add(classification.addChildTaxon(abies, null, null));
552
    	TaxonNode abiesAlbaNode = classification.addParentChild(abies, abiesAlba, null, null);
553
    	TaxonNode balsameaNode = classification.addParentChild(abies, abiesBalsamea, null, null);
554
    	nodes.add(balsameaNode);
555
    	nodes.add(abiesAlbaNode);
556
    	nodes.add(classification.addChildTaxon(pinus, null, null));
557
    	nodes.add(classification.addParentChild(pinus, pinusPampa, null, null));
558
    	classificationService.saveClassification(classification);
559
    	//this.taxonNodeService.save(nodes);
560
    	TaxonNaturalComparator comparator = new TaxonNaturalComparator();
561
    	List<TaxonNode> allNodes = new ArrayList<>(classification.getAllNodes());
562
    	Collections.sort(allNodes, comparator);
563

    
564
    	Assert.assertEquals(allNodes.get(0).getTaxon(), abies );
565
    	Assert.assertEquals(allNodes.get(2).getTaxon(), abiesBalsamea );
566
    	Assert.assertEquals(allNodes.get(1).getTaxon(), abiesAlba );
567

    
568
    	taxonNodeService.moveTaxonNode(balsameaNode, abiesAlbaNode,1);
569
    	classification = classificationService.load(classification.getUuid());
570

    
571
    	allNodes = new ArrayList<>(classification.getAllNodes());
572
        Collections.sort(allNodes, comparator);
573

    
574
        Assert.assertEquals(allNodes.get(0).getTaxon(), abies );
575
        Assert.assertEquals(allNodes.get(1).getTaxon(), abiesBalsamea );
576
        Assert.assertEquals(allNodes.get(2).getTaxon(), abiesAlba );
577

    
578
    }
579

    
580
    @Test
581
    @DataSet(loadStrategy = CleanSweepInsertLoadStrategy.class, value = "TaxonNodeServiceImplTest.testGetUuidAndTitleCacheHierarchy.xml")
582
    public void testGetUuidAndTitleCacheHierarchy(){
583
        UUID classificationUuid = UUID.fromString("029b4c07-5903-4dcf-87e8-406ed0e0285f");
584
        UUID abiesUuid = UUID.fromString("f8306fd3-9825-41bf-94aa-a7b5790b553e");
585
        UUID abiesAlbaUuid = UUID.fromString("c70f76e5-2dcb-41c5-ae6f-d756e0a0fae0");
586
        UUID abiesAlbaSubBrotaUuid = UUID.fromString("06d58161-7707-44b5-b720-6c0eb916b37c");
587
        UUID abiesPalmaUuid = UUID.fromString("6dfd30dd-e589-493a-b66a-19c4cb374f92");
588
        UUID pinusUuid = UUID.fromString("5d8e8341-f5e9-4616-96cf-f0351dda42f4");
589
//        /*
590
//         * Checklist
591
//         *  - Abies
592
//         *   - Abies alba
593
//         *    - Abieas alba subs. brota
594
//         *   - Abies palma
595
//         *  -Pinus
596
//         */
597
//        Classification checklist = Classification.NewInstance("Checklist");
598
//        checklist.setUuid(classificationUuid);
599
//
600
//        BotanicalName abiesName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
601
//        abiesName.setGenusOrUninomial("Abies");
602
//        Taxon abies = Taxon.NewInstance(abiesName, null);
603
//
604
//        BotanicalName abiesAlbaName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
605
//        abiesAlbaName.setGenusOrUninomial("Abies");
606
//        abiesAlbaName.setSpecificEpithet("alba");
607
//        Taxon abiesAlba = Taxon.NewInstance(abiesAlbaName, null);
608
//
609
//        BotanicalName abiesAlbaSubBrotaName = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
610
//        abiesAlbaSubBrotaName.setGenusOrUninomial("Abies");
611
//        abiesAlbaSubBrotaName.setSpecificEpithet("alba");
612
//        abiesAlbaSubBrotaName.setInfraSpecificEpithet("brota");
613
//        Taxon abiesAlbaSubBrota = Taxon.NewInstance(abiesAlbaSubBrotaName, null);
614
//
615
//        BotanicalName abiesPalmaName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
616
//        abiesPalmaName.setGenusOrUninomial("Abies");
617
//        abiesPalmaName.setSpecificEpithet("palma");
618
//        Taxon abiesPalma = Taxon.NewInstance(abiesPalmaName, null);
619
//
620
//        BotanicalName pinusName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
621
//        pinusName.setGenusOrUninomial("Pinus");
622
//        Taxon pinus = Taxon.NewInstance(pinusName, null);
623
//
624
//        checklist.addParentChild(null, abies, null, null);
625
//        checklist.addParentChild(abies, abiesAlba, null, null);
626
//        checklist.addParentChild(abiesAlba, abiesAlbaSubBrota, null, null);
627
//        checklist.addParentChild(abies, abiesPalma, null, null);
628
//        checklist.addParentChild(null, pinus, null, null);
629
//
630
//
631
//        setComplete();
632
//        endTransaction();
633
//
634
//        String fileNameAppendix = "testGetUuidAndTitleCacheHierarchy";
635
//
636
//        writeDbUnitDataSetFile(new String[] {
637
//            "TAXONBASE", "TAXONNAME",
638
//            "TAXONRELATIONSHIP",
639
//            "HOMOTYPICALGROUP",
640
//            "CLASSIFICATION", "TAXONNODE",
641
//            "HIBERNATE_SEQUENCES" // IMPORTANT!!!
642
//            },
643
//            fileNameAppendix );
644
        Classification classification = classificationService.load(classificationUuid);
645

    
646
        List<TaxonNode> expectedChildTaxonNodes = classification.getChildNodes();
647
        List<UuidAndTitleCache<TaxonNode>> childNodesUuidAndTitleCache = taxonNodeService.listChildNodesAsUuidAndTitleCache(classification.getRootNode());
648
        assertNotNull("child UuidAndTitleCache list is null", childNodesUuidAndTitleCache);
649

    
650
        compareChildren(expectedChildTaxonNodes, childNodesUuidAndTitleCache);
651

    
652
        //test taxon parent of sub species
653
        Taxon abiesAlbaSubBrota = HibernateProxyHelper.deproxy(taxonService.load(abiesAlbaSubBrotaUuid), Taxon.class);
654
        TaxonNode abiesAlbaSubBrotaNode = abiesAlbaSubBrota.getTaxonNodes().iterator().next();
655
        TaxonNode expectedTaxonParent = HibernateProxyHelper.deproxy(abiesAlbaSubBrotaNode.getParent(), TaxonNode.class);
656
        UuidAndTitleCache<TaxonNode> taxonParent = taxonNodeService.getParentUuidAndTitleCache(abiesAlbaSubBrotaNode);
657
        assertEquals("Taxon Nodes do not match. ", expectedTaxonParent.getUuid(), taxonParent.getUuid());
658
        assertEquals("Taxon Nodes do not match. ", (Integer)expectedTaxonParent.getId(), taxonParent.getId());
659
        assertEquals("Taxon Nodes do not match. ", expectedTaxonParent.getTaxon().getTitleCache(), taxonParent.getTitleCache());
660
        assertEquals("Taxon Nodes do not match. ", expectedTaxonParent, taxonNodeService.load(taxonParent.getUuid()));
661

    
662
        //test classification parent
663
        Taxon abies = HibernateProxyHelper.deproxy(taxonService.load(abiesUuid), Taxon.class);
664
        TaxonNode abiesNode = abies.getTaxonNodes().iterator().next();
665
        TaxonNode expectedClassificationParent = HibernateProxyHelper.deproxy(abiesNode.getParent(), TaxonNode.class);
666
        UuidAndTitleCache<TaxonNode> classificationParent= taxonNodeService.getParentUuidAndTitleCache(abiesNode);
667
        assertEquals("Taxon Nodes do not match. ", expectedClassificationParent.getUuid(), classificationParent.getUuid());
668
        assertEquals("Taxon Nodes do not match. ", (Integer)expectedClassificationParent.getId(), classificationParent.getId());
669
        assertEquals("Taxon Nodes do not match. ", expectedClassificationParent.getClassification().getTitleCache(), classificationParent.getTitleCache());
670
        assertEquals("Taxon Nodes do not match. ", expectedClassificationParent, taxonNodeService.load(classificationParent.getUuid()));
671
    }
672

    
673
    private void compareChildren(List<TaxonNode> expectedChildTaxonNodes, List<UuidAndTitleCache<TaxonNode>> childNodesUuidAndTitleCache){
674
        assertEquals("Number of children does not match", expectedChildTaxonNodes.size(), childNodesUuidAndTitleCache.size());
675
        UuidAndTitleCache<TaxonNode> foundMatch = null;
676
        for (TaxonNode taxonNode : expectedChildTaxonNodes) {
677
            foundMatch = null;
678
            for (UuidAndTitleCache<TaxonNode> uuidAndTitleCache : childNodesUuidAndTitleCache) {
679
                if(uuidAndTitleCache.getUuid().equals(taxonNode.getUuid())){
680
                    String titleCache = taxonNode.getTaxon().getTitleCache();
681
                    if(uuidAndTitleCache.getTitleCache().equals(titleCache)){
682
                        foundMatch = uuidAndTitleCache;
683
                        break;
684
                    }
685
                }
686
            }
687
            assertTrue(String.format("no matching UuidAndTitleCache found for child %s", taxonNode), foundMatch!=null);
688
            compareChildren(taxonNode.getChildNodes(), taxonNodeService.listChildNodesAsUuidAndTitleCache(foundMatch));
689
        }
690
    }
691

    
692
    private UuidAndTitleCache<TaxonNode> findMatchingUuidAndTitleCache(List<UuidAndTitleCache<TaxonNode>> childNodesUuidAndTitleCache,
693
            UuidAndTitleCache<TaxonNode> foundMatch, TaxonNode taxonNode) {
694
        for (UuidAndTitleCache<TaxonNode> uuidAndTitleCache : childNodesUuidAndTitleCache) {
695
            if(uuidAndTitleCache.getUuid().equals(taxonNode.getUuid())){
696
                String titleCache = taxonNode.getTaxon().getTitleCache();
697
                if(uuidAndTitleCache.getTitleCache().equals(titleCache)){
698
                    foundMatch = uuidAndTitleCache;
699
                    break;
700
                }
701
            }
702
        }
703
        return foundMatch;
704
    }
705

    
706
    @Test
707
    @DataSet("TaxonNodeServiceImplTest.testSetSecundumForSubtree.xml")
708
    public void testSetSecundumForSubtree(){
709
        UUID subTreeUuid = UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
710
//        UUID taxon1uuid = UUID.fromString("55c3e41a-c629-40e6-aa6a-ff274ac6ddb1");
711
//        UUID taxon5uuid = UUID.fromString("d0b99fee-a783-4dda-b8a2-8960703cfcc2");
712
        Reference newSec = referenceService.find(UUID.fromString("1d3fb074-d7ba-47e4-be94-b4cb1a99afa7"));
713

    
714
        //assert current state
715
        Assert.assertNotNull(newSec);
716
        Assert.assertNull(taxonService.find(1).getSec());
717
        Assert.assertNull(taxonService.find(2).getSec());
718
        Assert.assertNull(taxonService.find(3).getSec());
719
        Assert.assertNull(taxonService.find(4).getSec());
720
        TaxonBase<?> taxon5 = taxonService.find(5);
721
        Assert.assertNotNull(taxon5.getSec());
722
        Assert.assertNotEquals(newSec, taxon5.getSec());
723
        Assert.assertNotNull(taxon5.getSecMicroReference());
724

    
725
        //set secundum
726
//        SetSecundumForSubtreeConfigurator config = new SetSecundumForSubtreeConfigurator(subTreeUuid);
727
//        config.setNewSecundum(newSec);
728
        taxonNodeService.setSecundumForSubtree(subTreeUuid,  newSec, true, true, true, true, true, true, null);
729

    
730
        commitAndStartNewTransaction(new String[]{"TaxonBase","TaxonBase_AUD"});
731
        Assert.assertEquals(newSec, taxonService.find(1).getSec());
732
        Assert.assertNull(taxonService.find(2).getSec());
733
        Assert.assertEquals(newSec, taxonService.find(3).getSec());
734
        Assert.assertNull(taxonService.find(4).getSec());
735
        taxon5 = taxonService.find(5);
736
        Assert.assertEquals(newSec, taxon5.getSec());
737
        Assert.assertNull(taxon5.getSecMicroReference());
738
    }
739

    
740
    @Test
741
    @DataSet("TaxonNodeServiceImplTest.testSetSecundumForSubtree.xml")
742
    public void testSetSecundumForSubtreeNoOverwrite(){
743
        UUID subTreeUuid = UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
744
//        UUID taxon1uuid = UUID.fromString("55c3e41a-c629-40e6-aa6a-ff274ac6ddb1");
745
//        UUID taxon5uuid = UUID.fromString("d0b99fee-a783-4dda-b8a2-8960703cfcc2");
746
        Reference newSec = referenceService.find(UUID.fromString("1d3fb074-d7ba-47e4-be94-b4cb1a99afa7"));
747

    
748
        //assert current state
749
        Assert.assertNotNull(newSec);
750
        Assert.assertNull(taxonService.find(1).getSec());
751
        Assert.assertNull(taxonService.find(2).getSec());
752
        Assert.assertNull(taxonService.find(3).getSec());
753
        Assert.assertNull(taxonService.find(4).getSec());
754
        TaxonBase<?> taxon5 = taxonService.find(5);
755
        Assert.assertNotNull(taxon5.getSec());
756
        Assert.assertNotEquals(newSec, taxon5.getSec());
757
        Assert.assertNotNull(taxon5.getSecMicroReference());
758

    
759
        //set secundum
760
//        SetSecundumForSubtreeConfigurator config = new SetSecundumForSubtreeConfigurator(subTreeUuid, newSec, null);
761
//        config.setOverwriteExistingAccepted(false);
762
//        config.setOverwriteExistingSynonyms(false);
763
        taxonNodeService.setSecundumForSubtree(subTreeUuid,  newSec, true, true, false, false, true, true, null);
764

    
765
        commitAndStartNewTransaction(new String[]{"TaxonBase","TaxonBase_AUD"});
766
        Assert.assertEquals(newSec, taxonService.find(1).getSec());
767
        Assert.assertNull(taxonService.find(2).getSec());
768
        Assert.assertEquals(newSec, taxonService.find(3).getSec());
769
        Assert.assertNull(taxonService.find(4).getSec());
770
        Reference oldTaxon5Sec = taxon5.getSec();
771
        taxon5 = taxonService.find(5);
772
        Assert.assertEquals(oldTaxon5Sec, taxon5.getSec());
773
        Assert.assertNotEquals(newSec, taxon5.getSec());
774
        Assert.assertNotNull(taxon5.getSecMicroReference());
775
    }
776

    
777
    @Test
778
    @DataSet("TaxonNodeServiceImplTest.testSetSecundumForSubtree.xml")
779
    public void testSetSecundumForSubtreeOnlyAccepted(){
780
        UUID subTreeUuid = UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
781
        Reference newSec = referenceService.find(UUID.fromString("1d3fb074-d7ba-47e4-be94-b4cb1a99afa7"));
782

    
783
        //assert current state
784
        Assert.assertNotNull(newSec);
785
        Assert.assertNull(taxonService.find(1).getSec());
786
        Assert.assertNull(taxonService.find(2).getSec());
787
        Assert.assertNull(taxonService.find(3).getSec());
788
        Assert.assertNull(taxonService.find(4).getSec());
789
        TaxonBase<?> taxon5 = taxonService.find(5);
790
        Assert.assertNotNull(taxon5.getSec());
791
        Assert.assertNotEquals(newSec, taxon5.getSec());
792
        Assert.assertNotNull(taxon5.getSecMicroReference());
793

    
794
        //set secundum
795
//        SetSecundumForSubtreeConfigurator config = new SetSecundumForSubtreeConfigurator(subTreeUuid, newSec, null);
796
//        config.setIncludeSynonyms(false);
797
        taxonNodeService.setSecundumForSubtree(subTreeUuid,  newSec, true, false, true, true, true, true, null);
798

    
799
        commitAndStartNewTransaction(new String[]{"TaxonBase","TaxonBase_AUD"});
800
        Assert.assertEquals(newSec, taxonService.find(1).getSec());
801
        Assert.assertNull(taxonService.find(2).getSec());
802
        Assert.assertNull(taxonService.find(3).getSec());
803
        Assert.assertNull(taxonService.find(4).getSec());
804
        taxon5 = taxonService.find(5);
805
        Assert.assertEquals(newSec, taxon5.getSec());
806
        Assert.assertNull(taxon5.getSecMicroReference());
807
    }
808

    
809
    @Test
810
    @DataSet("TaxonNodeServiceImplTest.testSetSecundumForSubtree.xml")
811
    public void testSetSecundumForSubtreeOnlySynonyms(){
812
        UUID subTreeUuid = UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
813
        Reference newSec = referenceService.find(UUID.fromString("1d3fb074-d7ba-47e4-be94-b4cb1a99afa7"));
814

    
815
        //assert current state
816
        Assert.assertNotNull(newSec);
817
        Assert.assertNull(taxonService.find(1).getSec());
818
        Assert.assertNull(taxonService.find(2).getSec());
819
        Assert.assertNull(taxonService.find(3).getSec());
820
        Assert.assertNull(taxonService.find(4).getSec());
821
        TaxonBase<?> taxon5 = taxonService.find(5);
822
        Assert.assertNotNull(taxon5.getSec());
823
        Assert.assertNotEquals(newSec, taxon5.getSec());
824
        Assert.assertNotNull(taxon5.getSecMicroReference());
825

    
826
        //set secundum
827
//        SetSecundumForSubtreeConfigurator config = new SetSecundumForSubtreeConfigurator(subTreeUuid, newSec, null);
828
//        config.setIncludeAcceptedTaxa(false);
829
        taxonNodeService.setSecundumForSubtree(subTreeUuid,  newSec, false, true, true, true, true, true, null);
830

    
831
        commitAndStartNewTransaction(new String[]{"TaxonBase","TaxonBase_AUD"});
832
        Assert.assertNull(taxonService.find(1).getSec());
833
        Assert.assertNull(taxonService.find(2).getSec());
834
        Assert.assertEquals("Synonym should be updated", newSec, taxonService.find(3).getSec());
835
        Assert.assertNull(taxonService.find(4).getSec());
836
        Reference oldTaxon5Sec = taxon5.getSec();
837
        taxon5 = taxonService.find(5);
838
        Assert.assertEquals(oldTaxon5Sec, taxon5.getSec());
839
        Assert.assertNotEquals(newSec, taxon5.getSec());
840
        Assert.assertNotNull(taxon5.getSecMicroReference());
841
    }
842

    
843
    @Test
844
    @DataSet("TaxonNodeServiceImplTest.testSetSecundumForSubtree.xml")
845
    public void testSetSecundumForSubtreeNoShared(){
846
        UUID subTreeUuid = UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
847
        Reference newSec = referenceService.find(UUID.fromString("1d3fb074-d7ba-47e4-be94-b4cb1a99afa7"));
848

    
849
        //assert current state
850
        Assert.assertNotNull(newSec);
851
        Assert.assertNull(taxonService.find(1).getSec());
852
        Assert.assertNull(taxonService.find(2).getSec());
853
        Assert.assertNull(taxonService.find(3).getSec());
854
        Assert.assertNull(taxonService.find(4).getSec());
855
        TaxonBase<?> taxon5 = taxonService.find(5);
856
        Assert.assertNotNull(taxon5.getSec());
857
        Assert.assertNotEquals(newSec, taxon5.getSec());
858
        Assert.assertNotNull(taxon5.getSecMicroReference());
859

    
860
        //set secundum
861
     //   SetSecundumForSubtreeConfigurator config = new SetSecundumForSubtreeConfigurator(subTreeUuid, newSec, null);
862
   //     config.setIncludeSharedTaxa(false);
863
        taxonNodeService.setSecundumForSubtree(subTreeUuid, newSec, true, true, true, true, false, true, null);
864

    
865
        commitAndStartNewTransaction(new String[]{"TaxonBase","TaxonBase_AUD"});
866
        Assert.assertNull("Shared taxon must not be set", taxonService.find(1).getSec());
867
        Assert.assertNull(taxonService.find(2).getSec());
868
        Assert.assertNull("Synonym of shared taxon must not be set", taxonService.find(3).getSec());
869
        Assert.assertNull(taxonService.find(4).getSec());
870
        taxon5 = taxonService.find(5);
871
        Assert.assertEquals(newSec, taxon5.getSec());
872
        Assert.assertNull(taxon5.getSecMicroReference());
873
    }
874

    
875

    
876
    @Override
877
//    @Test
878
    public void createTestDataSet() throws FileNotFoundException {
879
        UUID classificationUuid = UUID.fromString("029b4c07-5903-4dcf-87e8-406ed0e0285f");
880
        UUID abiesUuid = UUID.fromString("f8306fd3-9825-41bf-94aa-a7b5790b553e");
881
        UUID abiesAlbaUuid = UUID.fromString("c70f76e5-2dcb-41c5-ae6f-d756e0a0fae0");
882
        UUID abiesAlbaSubBrotaUuid = UUID.fromString("06d58161-7707-44b5-b720-6c0eb916b37c");
883
        UUID abiesPalmaUuid = UUID.fromString("6dfd30dd-e589-493a-b66a-19c4cb374f92");
884
        UUID pinusUuid = UUID.fromString("5d8e8341-f5e9-4616-96cf-f0351dda42f4");
885

    
886
        /*
887
         * Checklist
888
         *  - Abies
889
         *   - Abies alba
890
         *    - Abieas alba subs. brota
891
         *   - Abies palma
892
         *  -Pinus
893
         */
894
        Classification checklist = Classification.NewInstance("Checklist");
895
        checklist.setUuid(classificationUuid);
896

    
897
        IBotanicalName abiesName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
898
        abiesName.setGenusOrUninomial("Abies");
899
        Taxon abies = Taxon.NewInstance(abiesName, null);
900
        abies.setUuid(abiesUuid);
901

    
902
        IBotanicalName abiesAlbaName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
903
        abiesAlbaName.setGenusOrUninomial("Abies");
904
        abiesAlbaName.setSpecificEpithet("alba");
905
        Taxon abiesAlba = Taxon.NewInstance(abiesAlbaName, null);
906
        abiesAlba.setUuid(abiesAlbaUuid);
907

    
908
        IBotanicalName abiesAlbaSubBrotaName = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
909
        abiesAlbaSubBrotaName.setGenusOrUninomial("Abies");
910
        abiesAlbaSubBrotaName.setSpecificEpithet("alba");
911
        abiesAlbaSubBrotaName.setInfraSpecificEpithet("brota");
912
        Taxon abiesAlbaSubBrota = Taxon.NewInstance(abiesAlbaSubBrotaName, null);
913
        abiesAlbaSubBrota.setUuid(abiesAlbaSubBrotaUuid);
914

    
915
        IBotanicalName abiesPalmaName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
916
        abiesPalmaName.setGenusOrUninomial("Abies");
917
        abiesPalmaName.setSpecificEpithet("palma");
918
        Taxon abiesPalma = Taxon.NewInstance(abiesPalmaName, null);
919
        abiesPalma.setUuid(abiesPalmaUuid);
920

    
921
        IBotanicalName pinusName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
922
        pinusName.setGenusOrUninomial("Pinus");
923
        Taxon pinus = Taxon.NewInstance(pinusName, null);
924
        pinus.setUuid(pinusUuid);
925

    
926
        checklist.addChildTaxon(abies, null, null);
927
        checklist.addParentChild(abies, abiesAlba, null, null);
928
        checklist.addParentChild(abiesAlba, abiesAlbaSubBrota, null, null);
929
        checklist.addParentChild(abies, abiesPalma, null, null);
930
        checklist.addChildTaxon(pinus, null, null);
931

    
932
        taxonService.saveOrUpdate(abies);
933
        taxonService.saveOrUpdate(abiesAlba);
934
        taxonService.saveOrUpdate(abiesAlbaSubBrota);
935
        taxonService.saveOrUpdate(abiesPalma);
936
        taxonService.saveOrUpdate(pinus);
937
        classificationService.saveOrUpdate(checklist);
938

    
939

    
940
        setComplete();
941
        endTransaction();
942

    
943
        String fileNameAppendix = "testGetUuidAndTitleCacheHierarchy";
944

    
945
        writeDbUnitDataSetFile(new String[] {
946
            "TAXONBASE", "TAXONNAME",
947
            "TAXONRELATIONSHIP",
948
            "HOMOTYPICALGROUP",
949
            "CLASSIFICATION", "TAXONNODE",
950
            "LANGUAGESTRING",
951
            "HIBERNATE_SEQUENCES" // IMPORTANT!!!
952
            },
953
            fileNameAppendix );
954
    }
955

    
956

    
957
}
(24-24/34)