2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.operation
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashSet
;
13 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.core
.commands
.ExecutionException
;
19 import org
.junit
.Assert
;
20 import org
.junit
.Ignore
;
21 import org
.junit
.Test
;
22 import org
.unitils
.dbunit
.annotation
.DataSet
;
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
.metadata
.SecReferenceHandlingEnum
;
34 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
38 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDto
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.e4
.TreeNodeDropAdapterE4
.MovingType
;
40 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.ChangeAcceptedTaxonToSynonymOperation
;
41 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.DeleteTaxonNodeOperation
;
42 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.MoveFactualDataOperation
;
43 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.MoveTaxonOperation
;
50 public class TaxonNavigatorTest
extends BaseOperationTest
{
52 @SuppressWarnings("unused")
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
);
63 public void testChangeAcceptedTaxonToSynonym() throws ExecutionException
{
65 UUID oldTaxonNodeNodeUuid
= UUID
.fromString("b85b5b78-6760-409f-ac91-bb89e95ff2a1");
66 UUID newAcceptedTaxonNodeUuid
= UUID
.fromString("6ad8e9e2-f5f6-41ad-aa30-f62a903650db");
68 TaxonNode oldTaxonNode
= taxonNodeService
.load(oldTaxonNodeNodeUuid
);
69 TaxonName name
= oldTaxonNode
.getTaxon().getName();
72 TaxonNode newAcceptedTaxonNode
= taxonNodeService
.load(newAcceptedTaxonNodeUuid
);
73 int countTargetSynonyms
= newAcceptedTaxonNode
.getTaxon().getSynonyms().size();
74 sessionOwner
.addExpectedUpdatedObject(oldTaxonNode
.getParent());
75 sessionOwner
.addExpectedUpdatedObject(newAcceptedTaxonNode
);
77 operation
= new ChangeAcceptedTaxonToSynonymOperation(sessionOwner
,
79 oldTaxonNode
.getUuid(),
80 newAcceptedTaxonNode
.getUuid(), null, SecReferenceHandlingEnum
.KeepOrWarn
, true);
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());
93 public void testMoveTaxon() throws ExecutionException
{
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
;
99 TaxonNode taxonNodeToMove
= taxonNodeService
.load(taxonNodeToMoveUuid
);
100 TaxonNode oldParent
= taxonNodeToMove
.getParent();
101 Set
<UUID
>uuids
= new HashSet
<>();
102 uuids
.add(taxonNodeToMoveUuid
);
103 // sessionOwner.addExpectedUpdatedObject(oldParent);
104 int childCount
= oldParent
.getCountChildren();
105 TaxonNode newParentTreeNode
= taxonNodeService
.load(newParentTreeNodeUuid
);
106 // sessionOwner.addExpectedUpdatedObject(newParentTreeNode);
107 operation
= new MoveTaxonOperation(sessionOwner
,
110 newParentTreeNode
.getUuid(),
111 moveToParentNode
, SecReferenceHandlingEnum
.KeepOrWarn
, null);
112 operation
.execute(monitor
, info
);
113 //TODO: fix this, because move taxon is now a longrunning task
114 // Assert.assertEquals(childCount-1, oldParent.getCountChildren());
115 // Assert.assertTrue(!oldParent.getChildNodes().contains(taxonNodeToMove));
116 // Assert.assertTrue(newParentTreeNode.getChildNodes().contains(taxonNodeToMove));
120 public void testMoveFactualData() throws ExecutionException
{
121 UUID sourceTaxonUuid
= UUID
.fromString("e40854d7-143f-4054-b229-6ed4cedb4bff");
122 UUID targetTaxonUuid
= UUID
.fromString("b8402dc4-5050-4882-a147-01b71e0e47d6");
124 Taxon sourceTaxon
= CdmBase
.deproxy(taxonService
.load(sourceTaxonUuid
), Taxon
.class);
125 Set
<TaxonDescription
> sourceDescriptions
= new HashSet
<>(sourceTaxon
.getDescriptions());
126 sessionOwner
.addExpectedUpdatedObject(sourceTaxon
);
128 Taxon targetTaxon
= CdmBase
.deproxy(taxonService
.load(targetTaxonUuid
), Taxon
.class);
129 int countTargetDescriptions
= targetTaxon
.getDescriptions().size();
130 sessionOwner
.addExpectedUpdatedObject(targetTaxon
);
132 operation
= new MoveFactualDataOperation(sessionOwner
,
135 targetTaxonUuid
, true);
136 operation
.execute(monitor
, info
);
137 Assert
.assertEquals(0, sourceTaxon
.getDescriptions().size());
138 Assert
.assertEquals(sourceDescriptions
.size() + countTargetDescriptions
, targetTaxon
.getDescriptions().size());
139 Assert
.assertTrue(targetTaxon
.getDescriptions().containsAll(sourceDescriptions
));
143 public void testDeleteTaxonNodeWithDeleteChildren() throws ExecutionException
{
144 UUID taxonNodeUuid
= UUID
.fromString("2f05d429-632d-4230-b9cb-70299360b470");
145 TaxonNode taxonNode
= taxonNodeService
.load(taxonNodeUuid
);
146 List
<TaxonNode
> childNodes
= taxonNode
.getChildNodes();
147 List
<UUID
> childNodeUuids
= new ArrayList
<UUID
>();
148 for(TaxonNode childNode
: childNodes
) {
149 childNodeUuids
.add(childNode
.getUuid());
151 Assert
.assertTrue(taxonNode
.getCountChildren() > 0);
153 Set
<TaxonNodeDto
> nodes
= new HashSet
<>();
154 nodes
.add(new TaxonNodeDto(taxonNode
));
156 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
157 configNodes
.setChildHandling(ChildHandling
.DELETE
);
158 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
159 config
.setTaxonNodeConfig(configNodes
);
161 sessionOwner
.addExpectedUpdatedObject(taxonNode
.getParent());
163 operation
= new DeleteTaxonNodeOperation(sessionOwner
,
168 operation
.execute(monitor
, info
);
169 TaxonNode reloadedNode
= taxonNodeService
.load(taxonNode
.getUuid());
170 Assert
.assertNull(reloadedNode
);
171 for(UUID childNodeUuid
: childNodeUuids
) {
172 TaxonNode reloadedChildNode
= taxonNodeService
.load(childNodeUuid
);
173 Assert
.assertNull(reloadedChildNode
);
178 public void testDeleteTaxonNodes() throws ExecutionException
{
179 UUID taxonNode1Uuid
= UUID
.fromString("99f03b56-67cd-4e01-9ceb-2362d48f9d07");
180 UUID taxonNode2Uuid
= UUID
.fromString("91698cec-615f-4472-9002-feda1a6acded");
181 UUID taxonNode3Uuid
= UUID
.fromString("4fe03763-b966-4361-8334-352f6f777588");
183 TaxonNode taxonNode1
= taxonNodeService
.load(taxonNode1Uuid
);
184 TaxonNode taxonNode2
= taxonNodeService
.load(taxonNode2Uuid
);
185 TaxonNode taxonNode3
= taxonNodeService
.load(taxonNode3Uuid
);
187 Set
<TaxonNodeDto
> nodes
= new HashSet
<>();
188 nodes
.add(new TaxonNodeDto(taxonNode1
));
189 nodes
.add(new TaxonNodeDto(taxonNode2
));
190 nodes
.add(new TaxonNodeDto(taxonNode3
));
192 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
195 sessionOwner
.addExpectedUpdatedObject(taxonNode1
.getParent());
196 sessionOwner
.addExpectedUpdatedObject(taxonNode2
.getParent());
197 sessionOwner
.addExpectedUpdatedObject(taxonNode3
.getParent());
198 operation
= new DeleteTaxonNodeOperation(sessionOwner
,
203 operation
.execute(monitor
, info
);
204 TaxonNode reloadedNode1
= taxonNodeService
.load(taxonNode1
.getUuid());
205 TaxonNode reloadedNode2
= taxonNodeService
.load(taxonNode2
.getUuid());
206 TaxonNode reloadedNode3
= taxonNodeService
.load(taxonNode3
.getUuid());
207 Assert
.assertNull(reloadedNode1
);
208 Assert
.assertNull(reloadedNode2
);
209 Assert
.assertNull(reloadedNode3
);
214 public void testDeleteTaxonNodeWithMovingChildrenToParentNode() throws ExecutionException
{
215 UUID taxonNodeUuid
= UUID
.fromString("2f05d429-632d-4230-b9cb-70299360b470");
216 TaxonNode taxonNode
= taxonNodeService
.load(taxonNodeUuid
);
217 TaxonNode parentTaxonNode
= taxonNode
.getParent();
218 List
<TaxonNode
> childNodes
= taxonNode
.getChildNodes();
219 List
<UUID
> childNodeUuids
= new ArrayList
<UUID
>();
220 for(TaxonNode childNode
: childNodes
) {
221 childNodeUuids
.add(childNode
.getUuid());
223 Assert
.assertTrue(taxonNode
.getCountChildren() > 0);
225 Set
<TaxonNodeDto
> nodes
= new HashSet
<>();
226 nodes
.add(new TaxonNodeDto(taxonNode
));
228 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
229 configNodes
.setChildHandling(ChildHandling
.MOVE_TO_PARENT
);
230 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
231 config
.setTaxonNodeConfig(configNodes
);
233 sessionOwner
.addExpectedUpdatedObject(taxonNode
.getParent());
234 operation
= new DeleteTaxonNodeOperation(sessionOwner
,
239 operation
.execute(monitor
, info
);
240 TaxonNode reloadedNode
= taxonNodeService
.load(taxonNode
.getUuid());
241 Assert
.assertNull(reloadedNode
);
242 List
<TaxonNode
> parentChildNodes
= parentTaxonNode
.getChildNodes();
243 List
<UUID
> parentChildNodeUuids
= new ArrayList
<UUID
>();
244 for(TaxonNode parentChildNode
: parentChildNodes
) {
245 parentChildNodeUuids
.add(parentChildNode
.getUuid());
247 parentChildNodeUuids
.containsAll(childNodeUuids
);
252 @DataSet("TaxonNavigatorTest.testDeleteClassificationWithDeleteChildren.xml")
253 public void testDeleteClassificationWithDeleteChildren() throws ExecutionException
{
254 UUID classificationUuid
= UUID
.fromString("0c2b5d25-7b15-4401-8b51-dd4be0ee5cab");
255 Classification classification
= classificationService
.load(classificationUuid
);
256 TaxonNode rootNode
= classification
.getRootNode();
257 Set
<TaxonNodeDto
> nodes
= new HashSet
<>();
258 nodes
.add(new TaxonNodeDto(classification
.getRootNode()));
260 TaxonNodeDeletionConfigurator configNodes
= new TaxonNodeDeletionConfigurator();
261 configNodes
.setChildHandling(ChildHandling
.DELETE
);
262 TaxonDeletionConfigurator config
= new TaxonDeletionConfigurator();
263 config
.setTaxonNodeConfig(configNodes
);
265 operation
= new DeleteTaxonNodeOperation(sessionOwner
,
270 operation
.execute(monitor
, info
);
272 Classification reloadedClassification
= classificationService
.load(classification
.getUuid());
273 Assert
.assertNull(reloadedClassification
);
274 TaxonNode reloadedRootNode
= taxonNodeService
.load(rootNode
.getUuid());
275 Assert
.assertNull(reloadedRootNode
);