3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
16 import java
.util
.UUID
;
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
;
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
;
51 public class TaxonNavigatorTest
extends BaseOperationTest
{
53 private static final Logger logger
= Logger
.getLogger(TaxonNameEditorTest
.class);
55 ITaxonNodeService taxonNodeService
= getRemoteApplicationController().getTaxonNodeService();
56 ITaxonService taxonService
= getRemoteApplicationController().getTaxonService();
57 IClassificationService classificationService
= getRemoteApplicationController().getClassificationService();
59 Language english
= Language
.getLanguageFromUuid(Language
.uuidEnglish
);
64 public void testChangeAcceptedTaxonToSynonym() throws ExecutionException
{
67 UUID oldTaxonNodeNodeUuid
= UUID
.fromString("b85b5b78-6760-409f-ac91-bb89e95ff2a1");
68 UUID newAcceptedTaxonNodeUuid
= UUID
.fromString("6ad8e9e2-f5f6-41ad-aa30-f62a903650db");
70 TaxonNode oldTaxonNode
= taxonNodeService
.load(oldTaxonNodeNodeUuid
);
71 TaxonNameBase name
= oldTaxonNode
.getTaxon().getName();
74 TaxonNode newAcceptedTaxonNode
= taxonNodeService
.load(newAcceptedTaxonNodeUuid
);
75 int countTargetSynonyms
= newAcceptedTaxonNode
.getTaxon().getSynonyms().size();
76 sessionOwner
.addExpectedUpdatedObject(oldTaxonNode
.getParent());
77 sessionOwner
.addExpectedUpdatedObject(newAcceptedTaxonNode
);
79 operation
= new RemotingChangeAcceptedTaxonToSynonymOperation(sessionOwner
,
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());
94 public void testMoveTaxon() throws ExecutionException
{
96 UUID taxonNodeToMoveUuid
= UUID
.fromString("b8439f51-6b96-445a-b401-7a836ba1cf58");
97 UUID newParentTreeNodeUuid
= UUID
.fromString("2f05d429-632d-4230-b9cb-70299360b470");
98 boolean moveToParentNode
= true;
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
,
108 taxonNodeToMove
.getUuid(),
109 newParentTreeNode
.getUuid(),
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
));
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");
122 Taxon sourceTaxon
= CdmBase
.deproxy(taxonService
.load(sourceTaxonUuid
), Taxon
.class);
123 Set
<TaxonDescription
> sourceDescriptions
= new HashSet(sourceTaxon
.getDescriptions());
124 sessionOwner
.addExpectedUpdatedObject(sourceTaxon
);
126 Taxon targetTaxon
= CdmBase
.deproxy(taxonService
.load(targetTaxonUuid
), Taxon
.class);
127 int countTargetDescriptions
= targetTaxon
.getDescriptions().size();
128 sessionOwner
.addExpectedUpdatedObject(targetTaxon
);
130 operation
= new RemotingMoveFactualDataOperation(sessionOwner
,
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
));
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());
149 Assert
.assertTrue(taxonNode
.getCountChildren() > 0);
151 Set
<ITaxonTreeNode
> nodes
= new HashSet
<ITaxonTreeNode
>();
152 nodes
.add(taxonNode
);
154 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
155 configNodes
.setChildHandling(ChildHandling
.DELETE
);
156 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
157 config
.setTaxonNodeConfig(configNodes
);
159 sessionOwner
.addExpectedUpdatedObject(taxonNode
.getParent());
161 operation
= new RemotingDeleteTaxonNodeOperation(sessionOwner
,
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
);
174 @Ignore // waiting for #5019 to be fixed
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");
181 TaxonNode taxonNode1
= taxonNodeService
.load(taxonNode1Uuid
);
182 TaxonNode taxonNode2
= taxonNodeService
.load(taxonNode2Uuid
);
183 TaxonNode taxonNode3
= taxonNodeService
.load(taxonNode3Uuid
);
185 Set
<ITaxonTreeNode
> nodes
= new HashSet
<ITaxonTreeNode
>();
186 nodes
.add(taxonNode1
);
187 nodes
.add(taxonNode2
);
188 nodes
.add(taxonNode3
);
190 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
193 sessionOwner
.addExpectedUpdatedObject(taxonNode1
.getParent());
194 sessionOwner
.addExpectedUpdatedObject(taxonNode2
.getParent());
195 sessionOwner
.addExpectedUpdatedObject(taxonNode3
.getParent());
196 operation
= new RemotingDeleteTaxonNodeOperation(sessionOwner
,
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
);
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());
220 Assert
.assertTrue(taxonNode
.getCountChildren() > 0);
222 Set
<ITaxonTreeNode
> nodes
= new HashSet
<ITaxonTreeNode
>();
223 nodes
.add(taxonNode
);
225 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
226 configNodes
.setChildHandling(ChildHandling
.MOVE_TO_PARENT
);
227 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
228 config
.setTaxonNodeConfig(configNodes
);
230 sessionOwner
.addExpectedUpdatedObject(taxonNode
.getParent());
231 operation
= new RemotingDeleteTaxonNodeOperation(sessionOwner
,
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());
243 parentChildNodeUuids
.containsAll(childNodeUuids
);
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
);
256 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
257 configNodes
.setChildHandling(ChildHandling
.DELETE
);
258 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
259 config
.setTaxonNodeConfig(configNodes
);
261 operation
= new RemotingDeleteTaxonNodeOperation(sessionOwner
,
265 operation
.execute(monitor
, info
);
267 Classification reloadedClassification
= classificationService
.load(classification
.getUuid());
268 Assert
.assertNull(reloadedClassification
);
269 TaxonNode reloadedRootNode
= taxonNodeService
.load(rootNode
.getUuid());
270 Assert
.assertNull(reloadedRootNode
);