Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / taxeditor / operation / TaxonNavigatorTest.java
1 /**
2 * Copyright (C) 2015 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 package eu.etaxonomy.taxeditor.operation;
10
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.unitils.dbunit.annotation.DataSet;
22
23 import eu.etaxonomy.cdm.api.service.IClassificationService;
24 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
25 import eu.etaxonomy.cdm.api.service.ITaxonService;
26 import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
27 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
28 import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.model.common.Language;
31 import eu.etaxonomy.cdm.model.description.TaxonDescription;
32 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
33 import eu.etaxonomy.cdm.model.taxon.Classification;
34 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
35 import eu.etaxonomy.cdm.model.taxon.Taxon;
36 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37 import eu.etaxonomy.taxeditor.navigation.navigator.TreeNodeDropAdapter.MovingType;
38 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingChangeAcceptedTaxonToSynonymOperation;
39 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingDeleteTaxonNodeOperation;
40 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveFactualDataOperation;
41 import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
42
43 /**
44 * @author cmathew
45 * @date 9 Mar 2015
46 *
47 */
48 @DataSet
49 public class TaxonNavigatorTest extends BaseOperationTest {
50
51 @SuppressWarnings("unused")
52 private static final Logger logger = Logger.getLogger(TaxonNameEditorTest.class);
53
54 ITaxonNodeService taxonNodeService = getRemoteApplicationController().getTaxonNodeService();
55 ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
56 IClassificationService classificationService = getRemoteApplicationController().getClassificationService();
57
58 Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
59
60
61
62 @Test
63 public void testChangeAcceptedTaxonToSynonym() throws ExecutionException {
64
65
66 UUID oldTaxonNodeNodeUuid = UUID.fromString("b85b5b78-6760-409f-ac91-bb89e95ff2a1");
67 UUID newAcceptedTaxonNodeUuid = UUID.fromString("6ad8e9e2-f5f6-41ad-aa30-f62a903650db");
68
69 TaxonNode oldTaxonNode = taxonNodeService.load(oldTaxonNodeNodeUuid);
70 TaxonNameBase name = oldTaxonNode.getTaxon().getName();
71
72
73 TaxonNode newAcceptedTaxonNode = taxonNodeService.load(newAcceptedTaxonNodeUuid);
74 int countTargetSynonyms = newAcceptedTaxonNode.getTaxon().getSynonyms().size();
75 sessionOwner.addExpectedUpdatedObject(oldTaxonNode.getParent());
76 sessionOwner.addExpectedUpdatedObject(newAcceptedTaxonNode);
77
78 operation = new RemotingChangeAcceptedTaxonToSynonymOperation(sessionOwner,
79 false,
80 oldTaxonNode.getUuid(),
81 newAcceptedTaxonNode.getUuid());
82 operation.execute(monitor, info);
83 newAcceptedTaxonNode = taxonNodeService.load(newAcceptedTaxonNodeUuid);
84 oldTaxonNode = taxonNodeService.load(oldTaxonNodeNodeUuid);
85 Assert.assertNull(oldTaxonNode);
86 Assert.assertEquals(countTargetSynonyms + 1,newAcceptedTaxonNode.getTaxon().getSynonyms().size());
87 Assert.assertEquals(name, newAcceptedTaxonNode.getTaxon().getSynonyms().iterator().next().getName());
88
89
90 }
91
92 @Test
93 public void testMoveTaxon() throws ExecutionException {
94
95 UUID taxonNodeToMoveUuid = UUID.fromString("b8439f51-6b96-445a-b401-7a836ba1cf58");
96 UUID newParentTreeNodeUuid = UUID.fromString("2f05d429-632d-4230-b9cb-70299360b470");
97 MovingType moveToParentNode = MovingType.CHILD;
98
99 TaxonNode taxonNodeToMove = taxonNodeService.load(taxonNodeToMoveUuid);
100 TaxonNode oldParent = taxonNodeToMove.getParent();
101 sessionOwner.addExpectedUpdatedObject(oldParent);
102 int childCount = oldParent.getCountChildren();
103 TaxonNode newParentTreeNode = taxonNodeService.load(newParentTreeNodeUuid);
104 sessionOwner.addExpectedUpdatedObject(newParentTreeNode);
105 operation = new RemotingMoveTaxonOperation(sessionOwner,
106 false,
107 taxonNodeToMove.getUuid(),
108 newParentTreeNode.getUuid(),
109 moveToParentNode);
110 operation.execute(monitor, info);
111 Assert.assertEquals(childCount-1, oldParent.getCountChildren());
112 Assert.assertTrue(!oldParent.getChildNodes().contains(taxonNodeToMove));
113 Assert.assertTrue(newParentTreeNode.getChildNodes().contains(taxonNodeToMove));
114 }
115
116 @Test
117 public void testMoveFactualData() throws ExecutionException {
118 UUID sourceTaxonUuid = UUID.fromString("e40854d7-143f-4054-b229-6ed4cedb4bff");
119 UUID targetTaxonUuid = UUID.fromString("b8402dc4-5050-4882-a147-01b71e0e47d6");
120
121 Taxon sourceTaxon = CdmBase.deproxy(taxonService.load(sourceTaxonUuid), Taxon.class);
122 Set<TaxonDescription> sourceDescriptions = new HashSet(sourceTaxon.getDescriptions());
123 sessionOwner.addExpectedUpdatedObject(sourceTaxon);
124
125 Taxon targetTaxon = CdmBase.deproxy(taxonService.load(targetTaxonUuid), Taxon.class);
126 int countTargetDescriptions = targetTaxon.getDescriptions().size();
127 sessionOwner.addExpectedUpdatedObject(targetTaxon);
128
129 operation = new RemotingMoveFactualDataOperation(sessionOwner,
130 false,
131 sourceTaxonUuid,
132 targetTaxonUuid);
133 operation.execute(monitor, info);
134 Assert.assertEquals(0, sourceTaxon.getDescriptions().size());
135 Assert.assertEquals(sourceDescriptions.size() + countTargetDescriptions, targetTaxon.getDescriptions().size());
136 Assert.assertTrue(targetTaxon.getDescriptions().containsAll(sourceDescriptions));
137 }
138
139 @Test
140 public void testDeleteTaxonNodeWithDeleteChildren() throws ExecutionException {
141 UUID taxonNodeUuid = UUID.fromString("2f05d429-632d-4230-b9cb-70299360b470");
142 TaxonNode taxonNode = taxonNodeService.load(taxonNodeUuid);
143 List<TaxonNode> childNodes = taxonNode.getChildNodes();
144 List<UUID> childNodeUuids = new ArrayList<UUID>();
145 for(TaxonNode childNode : childNodes) {
146 childNodeUuids.add(childNode.getUuid());
147 }
148 Assert.assertTrue(taxonNode.getCountChildren() > 0);
149
150 Set<ITaxonTreeNode> nodes = new HashSet<ITaxonTreeNode>();
151 nodes.add(taxonNode);
152
153 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
154 configNodes.setChildHandling(ChildHandling.DELETE);
155 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
156 config.setTaxonNodeConfig(configNodes);
157
158 sessionOwner.addExpectedUpdatedObject(taxonNode.getParent());
159
160 operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
161 false,
162 nodes,
163 config);
164 operation.execute(monitor, info);
165 TaxonNode reloadedNode = taxonNodeService.load(taxonNode.getUuid());
166 Assert.assertNull(reloadedNode);
167 for(UUID childNodeUuid : childNodeUuids) {
168 TaxonNode reloadedChildNode = taxonNodeService.load(childNodeUuid);
169 Assert.assertNull(reloadedChildNode);
170 }
171 }
172
173 @Test
174 public void testDeleteTaxonNodes() throws ExecutionException {
175 UUID taxonNode1Uuid = UUID.fromString("99f03b56-67cd-4e01-9ceb-2362d48f9d07");
176 UUID taxonNode2Uuid = UUID.fromString("91698cec-615f-4472-9002-feda1a6acded");
177 UUID taxonNode3Uuid = UUID.fromString("4fe03763-b966-4361-8334-352f6f777588");
178
179 TaxonNode taxonNode1 = taxonNodeService.load(taxonNode1Uuid);
180 TaxonNode taxonNode2 = taxonNodeService.load(taxonNode2Uuid);
181 TaxonNode taxonNode3 = taxonNodeService.load(taxonNode3Uuid);
182
183 Set<ITaxonTreeNode> nodes = new HashSet<ITaxonTreeNode>();
184 nodes.add(taxonNode1);
185 nodes.add(taxonNode2);
186 nodes.add(taxonNode3);
187
188 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
189
190
191 sessionOwner.addExpectedUpdatedObject(taxonNode1.getParent());
192 sessionOwner.addExpectedUpdatedObject(taxonNode2.getParent());
193 sessionOwner.addExpectedUpdatedObject(taxonNode3.getParent());
194 operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
195 false,
196 nodes,
197 config);
198 operation.execute(monitor, info);
199 TaxonNode reloadedNode1 = taxonNodeService.load(taxonNode1.getUuid());
200 TaxonNode reloadedNode2 = taxonNodeService.load(taxonNode2.getUuid());
201 TaxonNode reloadedNode3 = taxonNodeService.load(taxonNode3.getUuid());
202 Assert.assertNull(reloadedNode1);
203 Assert.assertNull(reloadedNode2);
204 Assert.assertNull(reloadedNode3);
205
206 }
207
208 @Test
209 public void testDeleteTaxonNodeWithMovingChildrenToParentNode() throws ExecutionException {
210 UUID taxonNodeUuid = UUID.fromString("2f05d429-632d-4230-b9cb-70299360b470");
211 TaxonNode taxonNode = taxonNodeService.load(taxonNodeUuid);
212 TaxonNode parentTaxonNode = taxonNode.getParent();
213 List<TaxonNode> childNodes = taxonNode.getChildNodes();
214 List<UUID> childNodeUuids = new ArrayList<UUID>();
215 for(TaxonNode childNode : childNodes) {
216 childNodeUuids.add(childNode.getUuid());
217 }
218 Assert.assertTrue(taxonNode.getCountChildren() > 0);
219
220 Set<ITaxonTreeNode> nodes = new HashSet<ITaxonTreeNode>();
221 nodes.add(taxonNode);
222
223 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
224 configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
225 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
226 config.setTaxonNodeConfig(configNodes);
227
228 sessionOwner.addExpectedUpdatedObject(taxonNode.getParent());
229 operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
230 false,
231 nodes,
232 config);
233 operation.execute(monitor, info);
234 TaxonNode reloadedNode = taxonNodeService.load(taxonNode.getUuid());
235 Assert.assertNull(reloadedNode);
236 List<TaxonNode> parentChildNodes = parentTaxonNode.getChildNodes();
237 List<UUID> parentChildNodeUuids = new ArrayList<UUID>();
238 for(TaxonNode parentChildNode : parentChildNodes) {
239 parentChildNodeUuids.add(parentChildNode.getUuid());
240 }
241 parentChildNodeUuids.containsAll(childNodeUuids);
242
243 }
244
245 @Test
246 @DataSet("TaxonNavigatorTest.testDeleteClassificationWithDeleteChildren.xml")
247 public void testDeleteClassificationWithDeleteChildren() throws ExecutionException {
248 UUID classificationUuid = UUID.fromString("0c2b5d25-7b15-4401-8b51-dd4be0ee5cab");
249 Classification classification = classificationService.load(classificationUuid);
250 TaxonNode rootNode = classification.getRootNode();
251 Set<ITaxonTreeNode> nodes = new HashSet<ITaxonTreeNode>();
252 nodes.add(classification);
253
254 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
255 configNodes.setChildHandling(ChildHandling.DELETE);
256 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
257 config.setTaxonNodeConfig(configNodes);
258
259 operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
260 false,
261 nodes,
262 config);
263 operation.execute(monitor, info);
264
265 Classification reloadedClassification = classificationService.load(classification.getUuid());
266 Assert.assertNull(reloadedClassification);
267 TaxonNode reloadedRootNode = taxonNodeService.load(rootNode.getUuid());
268 Assert.assertNull(reloadedRootNode);
269
270 }
271
272
273 }