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