Fix remaining test erros for collection call changes from #5031
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / cdm / model / 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.cdm.model;
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.Ignore;
22 import org.junit.Test;
23 import org.unitils.dbunit.annotation.DataSet;
24
25 import eu.etaxonomy.cdm.api.service.IClassificationService;
26 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
27 import eu.etaxonomy.cdm.api.service.ITaxonService;
28 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
29 import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
30 import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator.ChildHandling;
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32 import eu.etaxonomy.cdm.model.common.Language;
33 import eu.etaxonomy.cdm.model.description.TaxonDescription;
34 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
35 import eu.etaxonomy.cdm.model.taxon.Classification;
36 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
37 import eu.etaxonomy.cdm.model.taxon.Taxon;
38 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
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 import eu.etaxonomy.taxeditor.operations.BaseOperationTest;
44
45 /**
46 * @author cmathew
47 * @date 9 Mar 2015
48 *
49 */
50 @DataSet
51 public class TaxonNavigatorTest extends BaseOperationTest {
52
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 boolean moveToParentNode = true;
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 @Ignore // waiting for #5019 to be fixed
175 @Test
176 public void testDeleteTaxonNodes() throws ExecutionException {
177 UUID taxonNode1Uuid = UUID.fromString("99f03b56-67cd-4e01-9ceb-2362d48f9d07");
178 UUID taxonNode2Uuid = UUID.fromString("91698cec-615f-4472-9002-feda1a6acded");
179 UUID taxonNode3Uuid = UUID.fromString("4fe03763-b966-4361-8334-352f6f777588");
180
181 TaxonNode taxonNode1 = taxonNodeService.load(taxonNode1Uuid);
182 TaxonNode taxonNode2 = taxonNodeService.load(taxonNode2Uuid);
183 TaxonNode taxonNode3 = taxonNodeService.load(taxonNode3Uuid);
184
185 Set<ITaxonTreeNode> nodes = new HashSet<ITaxonTreeNode>();
186 nodes.add(taxonNode1);
187 nodes.add(taxonNode2);
188 nodes.add(taxonNode3);
189
190 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
191
192
193 sessionOwner.addExpectedUpdatedObject(taxonNode1.getParent());
194 sessionOwner.addExpectedUpdatedObject(taxonNode2.getParent());
195 sessionOwner.addExpectedUpdatedObject(taxonNode3.getParent());
196 operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
197 false,
198 nodes,
199 config);
200 operation.execute(monitor, info);
201 TaxonNode reloadedNode1 = taxonNodeService.load(taxonNode1.getUuid());
202 TaxonNode reloadedNode2 = taxonNodeService.load(taxonNode2.getUuid());
203 TaxonNode reloadedNode3 = taxonNodeService.load(taxonNode3.getUuid());
204 Assert.assertNull(reloadedNode1);
205 Assert.assertNull(reloadedNode2);
206 Assert.assertNull(reloadedNode3);
207
208 }
209
210 @Test
211 public void testDeleteTaxonNodeWithMovingChildrenToParentNode() throws ExecutionException {
212 UUID taxonNodeUuid = UUID.fromString("2f05d429-632d-4230-b9cb-70299360b470");
213 TaxonNode taxonNode = taxonNodeService.load(taxonNodeUuid);
214 TaxonNode parentTaxonNode = taxonNode.getParent();
215 List<TaxonNode> childNodes = taxonNode.getChildNodes();
216 List<UUID> childNodeUuids = new ArrayList<UUID>();
217 for(TaxonNode childNode : childNodes) {
218 childNodeUuids.add(childNode.getUuid());
219 }
220 Assert.assertTrue(taxonNode.getCountChildren() > 0);
221
222 Set<ITaxonTreeNode> nodes = new HashSet<ITaxonTreeNode>();
223 nodes.add(taxonNode);
224
225 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
226 configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
227 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
228 config.setTaxonNodeConfig(configNodes);
229
230 sessionOwner.addExpectedUpdatedObject(taxonNode.getParent());
231 operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
232 false,
233 nodes,
234 config);
235 operation.execute(monitor, info);
236 TaxonNode reloadedNode = taxonNodeService.load(taxonNode.getUuid());
237 Assert.assertNull(reloadedNode);
238 List<TaxonNode> parentChildNodes = parentTaxonNode.getChildNodes();
239 List<UUID> parentChildNodeUuids = new ArrayList<UUID>();
240 for(TaxonNode parentChildNode : parentChildNodes) {
241 parentChildNodeUuids.add(parentChildNode.getUuid());
242 }
243 parentChildNodeUuids.containsAll(childNodeUuids);
244
245 }
246
247 @Test
248 @DataSet("TaxonNavigatorTest.testDeleteClassificationWithDeleteChildren.xml")
249 public void testDeleteClassificationWithDeleteChildren() throws ExecutionException {
250 UUID classificationUuid = UUID.fromString("0c2b5d25-7b15-4401-8b51-dd4be0ee5cab");
251 Classification classification = classificationService.load(classificationUuid);
252 TaxonNode rootNode = classification.getRootNode();
253 Set<ITaxonTreeNode> nodes = new HashSet<ITaxonTreeNode>();
254 nodes.add(classification);
255
256 TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
257 configNodes.setChildHandling(ChildHandling.DELETE);
258 TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
259 config.setTaxonNodeConfig(configNodes);
260
261 operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
262 false,
263 nodes,
264 config);
265 operation.execute(monitor, info);
266
267 Classification reloadedClassification = classificationService.load(classification.getUuid());
268 Assert.assertNull(reloadedClassification);
269 TaxonNode reloadedRootNode = taxonNodeService.load(rootNode.getUuid());
270 Assert.assertNull(reloadedRootNode);
271
272 }
273
274
275 }