Project

General

Profile

Download (47.6 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.api.service.config.SecundumForSubtreeConfigurator;
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.IBotanicalName;
38
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
39
import eu.etaxonomy.cdm.model.name.Rank;
40
import eu.etaxonomy.cdm.model.name.TaxonName;
41
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
42
import eu.etaxonomy.cdm.model.reference.Reference;
43
import eu.etaxonomy.cdm.model.taxon.Classification;
44
import eu.etaxonomy.cdm.model.taxon.Synonym;
45
import eu.etaxonomy.cdm.model.taxon.SynonymType;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
48
import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
49
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
50
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
51
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
52
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
53
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
54

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

    
62

    
63
	@SpringBeanByType
64
	private ITaxonNodeService taxonNodeService;
65

    
66
	@SpringBeanByType
67
	private IClassificationService classificationService;
68

    
69
	@SpringBeanByType
70
	private IReferenceService referenceService;
71

    
72
	@SpringBeanByType
73
	private ITermService termService;
74

    
75
	@SpringBeanByType
76
    private INameService nameService;
77

    
78
	@SpringBeanByType
79
	private ITaxonService taxonService;
80

    
81
	@SpringBeanByType
82
	private IPolytomousKeyService polKeyService;
83

    
84
	@SpringBeanByType
85
	private IPolytomousKeyNodeService polKeyNodeService;
86

    
87

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

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

    
101

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

    
113
    private TaxonNode node4;
114

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

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

    
137
		//
138
		//TODO
139

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

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

    
150
		//nameRelations
151

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

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

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

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

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

    
196
		Set<CdmBase> updatedObjects = result.getUpdatedObjects();
197
		Iterator<CdmBase> it = updatedObjects.iterator();
198
		Taxon taxon;
199
		while (it.hasNext()) {
200
			CdmBase updatedObject = it.next();
201
			if(updatedObject.isInstanceOf(Taxon.class)){
202
				taxon = HibernateProxyHelper.deproxy(updatedObject, Taxon.class);
203
				Set<Synonym> syns =  taxon.getSynonyms();
204
				assertNotNull(syns);
205
				if (taxon.equals(t2)){
206
				    assertEquals(4,syns.size());
207
				    Set<TaxonName> typifiedNames =taxon.getHomotypicGroup().getTypifiedNames();
208
	                assertEquals(typifiedNames.size(),4);
209
	                assertTrue(taxon.getHomotypicGroup().equals( nameT1.getHomotypicalGroup()));
210

    
211
	                assertEquals(taxon, t2);
212
				}
213

    
214
			}
215

    
216
		}
217
	}
218

    
219
	/**
220
	 * 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)}.
221
	 */
222
	@Test
223
	@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class)
224
	public final void testMakeTaxonNodeAHeterotypicSynonymOfAnotherTaxonNode() {
225

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
309

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

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

    
325

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

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

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

    
360
}
361

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

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

    
377
		node6 = taxonNodeService.load(node6Uuid);
378

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

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

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

    
396

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

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

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

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

    
427

    
428
	}
429

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

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

    
439

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

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

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

    
473

    
474
    }
475

    
476

    
477

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

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

    
494

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

    
505

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

    
518
	}
519

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

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

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

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

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

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

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

    
579
    }
580

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

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

    
651
        compareChildren(expectedChildTaxonNodes, childNodesUuidAndTitleCache);
652

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

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

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

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

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

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

    
726
        //set secundum
727
        SecundumForSubtreeConfigurator config = new SecundumForSubtreeConfigurator(subTreeUuid);
728
        config.setNewSecundum(newSec);
729
        taxonNodeService.setSecundumForSubtree(config);
730

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
876
    @Test
877
    @DataSet("TaxonNodeServiceImplTest.testSetSecundumForSubtree.xml")
878
    public void testSetPublishForSubtree(){
879
        UUID subTreeUuid = UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
880

    
881
        //assert current state
882
        Assert.assertTrue(taxonService.find(1).isPublish());
883
        Assert.assertTrue(taxonService.find(2).isPublish());
884
        Assert.assertTrue(taxonService.find(3).isPublish());
885
        Assert.assertTrue(taxonService.find(4).isPublish());
886
        Assert.assertTrue(taxonService.find(5).isPublish());
887

    
888
        //set publish
889
//        SecundumForSubtreeConfigurator config = new SetPublishForSubtreeConfigurator(subTreeUuid);
890
//        config.setNewSecundum(newSec);
891
        boolean publish = false;
892
        taxonNodeService.setPublishForSubtree(subTreeUuid,  publish, true, true, true, null);
893

    
894
        commitAndStartNewTransaction(new String[]{});
895
        Assert.assertEquals(publish, taxonService.find(1).isPublish());
896
        Assert.assertEquals(true, taxonService.find(2).isPublish());
897
        Assert.assertEquals(publish, taxonService.find(3).isPublish());
898
        Assert.assertEquals(true, taxonService.find(4).isPublish());
899
        Assert.assertEquals(publish, taxonService.find(5).isPublish());
900
    }
901

    
902
    @Test
903
    @DataSet("TaxonNodeServiceImplTest.testSetSecundumForSubtree.xml")
904
    public void testSetPublishForSubtreeOnlyAccepted(){
905
        UUID subTreeUuid = UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
906

    
907
        //assert current state
908
        Assert.assertTrue(taxonService.find(1).isPublish());
909
        Assert.assertTrue(taxonService.find(2).isPublish());
910
        Assert.assertTrue(taxonService.find(3).isPublish());
911
        Assert.assertTrue(taxonService.find(4).isPublish());
912
        Assert.assertTrue(taxonService.find(5).isPublish());
913

    
914
        //set secundum
915
//        SecundumForSubtreeConfigurator config = new SetPublishForSubtreeConfigurator(subTreeUuid, newSec, null);
916
//        config.setIncludeSynonyms(false);
917
        boolean publish = false;
918
        taxonNodeService.setPublishForSubtree(subTreeUuid,  publish, true, false, true, null);
919

    
920
        commitAndStartNewTransaction();
921
        Assert.assertEquals(publish, taxonService.find(1).isPublish());
922
        Assert.assertEquals(true, taxonService.find(2).isPublish());
923
        Assert.assertEquals(true, taxonService.find(3).isPublish());
924
        Assert.assertEquals(true, taxonService.find(4).isPublish());
925
        Assert.assertEquals(publish, taxonService.find(5).isPublish());
926
    }
927

    
928
    @Test
929
    @DataSet("TaxonNodeServiceImplTest.testSetSecundumForSubtree.xml")
930
    public void testSetPublishForSubtreeOnlySynonyms(){
931
        UUID subTreeUuid = UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
932

    
933
        //assert current state
934
        Assert.assertTrue(taxonService.find(1).isPublish());
935
        Assert.assertTrue(taxonService.find(2).isPublish());
936
        Assert.assertTrue(taxonService.find(3).isPublish());
937
        Assert.assertTrue(taxonService.find(4).isPublish());
938
        Assert.assertTrue(taxonService.find(5).isPublish());
939

    
940
        //set secundum
941
//        SecundumForSubtreeConfigurator config = new SetPublishForSubtreeConfigurator(subTreeUuid, newSec, null);
942
//        config.setIncludeAcceptedTaxa(false);
943
        boolean publish = false;
944
        taxonNodeService.setPublishForSubtree(subTreeUuid,  publish, false, true, true, null);
945

    
946
        commitAndStartNewTransaction(new String[]{});
947
        Assert.assertEquals(true, taxonService.find(1).isPublish());
948
        Assert.assertEquals(true, taxonService.find(2).isPublish());
949
        Assert.assertEquals(publish, taxonService.find(3).isPublish());
950
        Assert.assertEquals(true, taxonService.find(4).isPublish());
951
        Assert.assertEquals(true, taxonService.find(5).isPublish());
952
    }
953

    
954

    
955
    @Test
956
    @DataSet("TaxonNodeServiceImplTest.testSetSecundumForSubtree.xml")
957
    public void testSetPublishForSubtreeNoShared(){
958
        UUID subTreeUuid = UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
959

    
960
        //assert current state
961
        Assert.assertTrue(taxonService.find(1).isPublish());
962
        Assert.assertTrue(taxonService.find(2).isPublish());
963
        Assert.assertTrue(taxonService.find(3).isPublish());
964
        Assert.assertTrue(taxonService.find(4).isPublish());
965
        Assert.assertTrue(taxonService.find(5).isPublish());
966

    
967
        //set secundum
968
     //   SecundumForSubtreeConfigurator config = new SetPublishForSubtreeConfigurator(subTreeUuid, newSec, null);
969
   //     config.setIncludeSharedTaxa(false);
970
        boolean publish = false;
971
        taxonNodeService.setPublishForSubtree(subTreeUuid, publish, true, true, false, null);
972

    
973
        commitAndStartNewTransaction(new String[]{});
974
        Assert.assertEquals("Shared taxon must not be set", true, taxonService.find(1).isPublish());
975
        Assert.assertEquals(true, taxonService.find(2).isPublish());
976
        Assert.assertEquals("Synonym of shared taxon must not be set", true, taxonService.find(3).isPublish());
977
        Assert.assertEquals(true, taxonService.find(4).isPublish());
978
        Assert.assertEquals(publish, taxonService.find(5).isPublish());
979
    }
980

    
981

    
982
    @Override
983
//    @Test
984
    public void createTestDataSet() throws FileNotFoundException {
985
        UUID classificationUuid = UUID.fromString("029b4c07-5903-4dcf-87e8-406ed0e0285f");
986
        UUID abiesUuid = UUID.fromString("f8306fd3-9825-41bf-94aa-a7b5790b553e");
987
        UUID abiesAlbaUuid = UUID.fromString("c70f76e5-2dcb-41c5-ae6f-d756e0a0fae0");
988
        UUID abiesAlbaSubBrotaUuid = UUID.fromString("06d58161-7707-44b5-b720-6c0eb916b37c");
989
        UUID abiesPalmaUuid = UUID.fromString("6dfd30dd-e589-493a-b66a-19c4cb374f92");
990
        UUID pinusUuid = UUID.fromString("5d8e8341-f5e9-4616-96cf-f0351dda42f4");
991

    
992
        /*
993
         * Checklist
994
         *  - Abies
995
         *   - Abies alba
996
         *    - Abieas alba subs. brota
997
         *   - Abies palma
998
         *  -Pinus
999
         */
1000
        Classification checklist = Classification.NewInstance("Checklist");
1001
        checklist.setUuid(classificationUuid);
1002

    
1003
        IBotanicalName abiesName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
1004
        abiesName.setGenusOrUninomial("Abies");
1005
        Taxon abies = Taxon.NewInstance(abiesName, null);
1006
        abies.setUuid(abiesUuid);
1007

    
1008
        IBotanicalName abiesAlbaName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
1009
        abiesAlbaName.setGenusOrUninomial("Abies");
1010
        abiesAlbaName.setSpecificEpithet("alba");
1011
        Taxon abiesAlba = Taxon.NewInstance(abiesAlbaName, null);
1012
        abiesAlba.setUuid(abiesAlbaUuid);
1013

    
1014
        IBotanicalName abiesAlbaSubBrotaName = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
1015
        abiesAlbaSubBrotaName.setGenusOrUninomial("Abies");
1016
        abiesAlbaSubBrotaName.setSpecificEpithet("alba");
1017
        abiesAlbaSubBrotaName.setInfraSpecificEpithet("brota");
1018
        Taxon abiesAlbaSubBrota = Taxon.NewInstance(abiesAlbaSubBrotaName, null);
1019
        abiesAlbaSubBrota.setUuid(abiesAlbaSubBrotaUuid);
1020

    
1021
        IBotanicalName abiesPalmaName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
1022
        abiesPalmaName.setGenusOrUninomial("Abies");
1023
        abiesPalmaName.setSpecificEpithet("palma");
1024
        Taxon abiesPalma = Taxon.NewInstance(abiesPalmaName, null);
1025
        abiesPalma.setUuid(abiesPalmaUuid);
1026

    
1027
        IBotanicalName pinusName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
1028
        pinusName.setGenusOrUninomial("Pinus");
1029
        Taxon pinus = Taxon.NewInstance(pinusName, null);
1030
        pinus.setUuid(pinusUuid);
1031

    
1032
        checklist.addChildTaxon(abies, null, null);
1033
        checklist.addParentChild(abies, abiesAlba, null, null);
1034
        checklist.addParentChild(abiesAlba, abiesAlbaSubBrota, null, null);
1035
        checklist.addParentChild(abies, abiesPalma, null, null);
1036
        checklist.addChildTaxon(pinus, null, null);
1037

    
1038
        taxonService.saveOrUpdate(abies);
1039
        taxonService.saveOrUpdate(abiesAlba);
1040
        taxonService.saveOrUpdate(abiesAlbaSubBrota);
1041
        taxonService.saveOrUpdate(abiesPalma);
1042
        taxonService.saveOrUpdate(pinus);
1043
        classificationService.saveOrUpdate(checklist);
1044

    
1045

    
1046
        setComplete();
1047
        endTransaction();
1048

    
1049
        String fileNameAppendix = "testGetUuidAndTitleCacheHierarchy";
1050

    
1051
        writeDbUnitDataSetFile(new String[] {
1052
            "TAXONBASE", "TAXONNAME",
1053
            "TAXONRELATIONSHIP",
1054
            "HOMOTYPICALGROUP",
1055
            "CLASSIFICATION", "TAXONNODE",
1056
            "LANGUAGESTRING",
1057
            "HIBERNATE_SEQUENCES" // IMPORTANT!!!
1058
            },
1059
            fileNameAppendix );
1060
    }
1061

    
1062

    
1063
}
(24-24/34)