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