Project

General

Profile

Download (7.2 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.assertNotNull;
13
import static org.junit.Assert.assertNull;
14

    
15
import java.io.FileNotFoundException;
16
import java.util.UUID;
17

    
18
import org.junit.Assert;
19
import org.junit.Before;
20
import org.junit.Test;
21
import org.unitils.dbunit.annotation.DataSet;
22
import org.unitils.spring.annotation.SpringBeanByType;
23

    
24
import eu.etaxonomy.cdm.api.service.config.FeatureNodeDeletionConfigurator;
25
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
26
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
27
import eu.etaxonomy.cdm.model.common.ITreeNode;
28
import eu.etaxonomy.cdm.model.description.Feature;
29
import eu.etaxonomy.cdm.model.description.FeatureNode;
30
import eu.etaxonomy.cdm.model.description.FeatureTree;
31
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
32

    
33
/**
34
 * @author a.mueller
35
 * @since 2013-09-28
36
 */
37
public class FeatureNodeServiceImplTest extends CdmTransactionalIntegrationTest{
38

    
39
	private static final String sep = ITreeNode.separator;
40
	private static final String pref = ITreeNode.treePrefix;
41

    
42
	@SpringBeanByType
43
	private IFeatureNodeService featureNodeService;
44

    
45
	@SpringBeanByType
46
	private IFeatureTreeService featureTreeService;
47

    
48
	@SpringBeanByType
49
	private ITermService termService;
50

    
51

    
52
//	private static final UUID t2Uuid = UUID.fromString("55c3e41a-c629-40e6-aa6a-ff274ac6ddb1");
53
//	private static final UUID t3Uuid = UUID.fromString("2659a7e0-ff35-4ee4-8493-b453756ab955");
54
	private static final UUID featureTreeUuid = UUID.fromString("6c2bc8d9-ee62-4222-be89-4a8e31770878");
55
	private static final UUID featureTree2Uuid = UUID.fromString("43d67247-936f-42a3-a739-bbcde372e334");
56
	private static final UUID node2Uuid= UUID.fromString("484a1a77-689c-44be-8e65-347d835f47e8");
57
	private static final UUID node3Uuid = UUID.fromString("2d41f0c2-b785-4f73-a436-cc2d5e93cc5b");
58
	private static final UUID node4Uuid = UUID.fromString("fdaec4bd-c78e-44df-ae87-28f18110968c");
59
	private static final UUID node5Uuid = UUID.fromString("c4d5170a-7967-4dac-ab76-ae2019eefde5");
60
	private static final UUID node6Uuid = UUID.fromString("b419ba5e-9c8b-449c-ad86-7abfca9a7340");
61

    
62
//	private Synonym s1;
63
	private FeatureNode node3;
64
	private FeatureNode node2;
65

    
66
	/**
67
	 * @throws java.lang.Exception
68
	 */
69
	@Before
70
	public void setUp() throws Exception {
71
	}
72

    
73
	@Test
74
	public final void testIndexCreatRoot() {
75
		FeatureTree featureTree = FeatureTree.NewInstance();
76
		featureTreeService.save(featureTree);
77

    
78
		Feature feature = (Feature)termService.find(914);
79
		FeatureNode newNode = FeatureNode.NewInstance(feature);
80
		featureTree.getRoot().addChild(newNode);
81

    
82
		featureTreeService.save(featureTree);
83

    
84
		featureNodeService.saveOrUpdate(newNode);
85

    
86
		commitAndStartNewTransaction(new String[]{"FeatureNode"});
87
		newNode = featureNodeService.load(newNode.getUuid());
88
		Assert.assertEquals("", sep + pref+featureTree.getId()+sep + featureTree.getRoot().getId()+ sep  + newNode.getId() + sep, newNode.treeIndex());
89
	}
90

    
91

    
92
	@Test
93
	@DataSet(value="FeatureNodeServiceImplTest-indexing.xml")
94
	public final void testIndexCreateNode() {
95
		Feature feature = (Feature)termService.find(914);
96

    
97
		node2 = featureNodeService.load(node2Uuid);
98
		String oldTreeIndex = node2.treeIndex();
99

    
100
		FeatureNode newNode = FeatureNode.NewInstance(feature);
101
		node2.addChild(newNode);
102
		featureNodeService.saveOrUpdate(node2);
103

    
104
		commitAndStartNewTransaction(new String[]{"FeatureNode"});
105
		newNode = featureNodeService.load(newNode.getUuid());
106
		Assert.assertEquals("", oldTreeIndex + newNode.getId() + sep, newNode.treeIndex());
107
	}
108

    
109

    
110
	@Test
111
	@DataSet(value="FeatureNodeServiceImplTest-indexing.xml")
112
	public final void testIndexMoveNode() {
113
		//in feature tree
114
		FeatureTree featureTree = featureTreeService.load(featureTreeUuid);
115
		node2 = featureNodeService.load(node2Uuid);
116
		node3 = featureNodeService.load(node3Uuid);
117
		node3.addChild(node2);
118
		featureNodeService.saveOrUpdate(node2);
119
		commitAndStartNewTransaction(new String[]{"FeatureNode"});
120
		FeatureNode node6 = featureNodeService.load(node6Uuid);
121
		Assert.assertEquals("Node6 treeindex is not correct", node3.treeIndex() + "2#4#6#", node6.treeIndex());
122

    
123
		//root of new feature tree
124
		FeatureTree featureTree2 = featureTreeService.load(featureTree2Uuid);
125
		node2 = featureNodeService.load(node2Uuid);
126
		featureTree2.getRoot().addChild(node2);
127
		featureNodeService.saveOrUpdate(node2);
128
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
129
		node2 = featureNodeService.load(node2Uuid);
130
		Assert.assertEquals("Node2 treeindex is not correct", "#t2#7#2#", node2.treeIndex());
131
		node6 = featureNodeService.load(node6Uuid);
132
		Assert.assertEquals("Node6 treeindex is not correct", "#t2#7#2#4#6#", node6.treeIndex());
133

    
134
		//into new classification
135
		node3 = featureNodeService.load(node3Uuid);
136
		FeatureNode node5 = featureNodeService.load(node5Uuid);
137
		node5.addChild(node3);
138
		featureNodeService.saveOrUpdate(node5);
139
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
140
		node3 = featureNodeService.load(node3Uuid);
141
		Assert.assertEquals("Node3 treeindex is not correct", "#t2#7#2#5#3#", node3.treeIndex());
142

    
143
	}
144

    
145
	@Test  //here we may have a test for testing delete of a node and attaching the children
146
	//to its parents, however this depends on the way delete is implemented and therefore needs
147
	//to wait until this is finally done
148
	@DataSet(value="FeatureNodeServiceImplTest-indexing.xml")
149
	public final void testIndexDeleteNode() {
150
		node2 = featureNodeService.load(node2Uuid);
151
		FeatureNode root = node2.getParent();
152
		FeatureNodeDeletionConfigurator config = new FeatureNodeDeletionConfigurator();
153
		config.setDeleteElement(false);
154
        config.setChildHandling(ChildHandling.MOVE_TO_PARENT);
155
        DeleteResult result = featureNodeService.deleteFeatureNode(node2Uuid, config);
156
        commitAndStartNewTransaction(new String[]{"TaxonNode"});
157
        FeatureTree tree1 = featureTreeService.load(featureTreeUuid);
158
        assertNotNull(tree1);
159
        node2 = featureNodeService.load(node2Uuid);
160
        assertNull(node2);
161
        node3 = featureNodeService.load(node3Uuid);
162
        assertNotNull(node3);
163
        FeatureNode node4 = featureNodeService.load(node4Uuid);
164
        assertNotNull(node4);
165
		config.setDeleteElement(false);
166
		config.setChildHandling(ChildHandling.DELETE);
167
		result = featureNodeService.deleteFeatureNode(node4Uuid, config);
168
		commitAndStartNewTransaction(new String[]{"TaxonNode"});
169
		tree1 = featureTreeService.load(featureTreeUuid);
170
		node4 = featureNodeService.load(node4Uuid);
171
        assertNull(node4);
172
        FeatureNode node6 = featureNodeService.load(node6Uuid);
173
        assertNull(node6);
174

    
175
		HibernateProxyHelper.deproxy(tree1, FeatureTree.class);
176
		FeatureNode rootNode = HibernateProxyHelper.deproxy(tree1.getRoot(), FeatureNode.class);
177
		assertNotNull(tree1);
178
		featureTreeService.delete(tree1.getUuid());
179
		commitAndStartNewTransaction(/*new String[]{"TaxonNode"}*/);
180
		tree1 = featureTreeService.load(featureTreeUuid);
181
		assertNull(tree1);
182

    
183
	}
184

    
185
    @Override
186
    public void createTestDataSet() throws FileNotFoundException {}
187
}
(8-8/37)