Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2015 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
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.
8
 */
9
package eu.etaxonomy.taxeditor.operation;
10

    
11
import java.util.ArrayList;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Set;
15
import java.util.UUID;
16

    
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;
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.TaxonName;
34
import eu.etaxonomy.cdm.model.taxon.Classification;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
38
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TreeNodeDropAdapterE4.MovingType;
39
import eu.etaxonomy.taxeditor.navigation.navigator.operation.ChangeAcceptedTaxonToSynonymOperation;
40
import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteTaxonNodeOperation;
41
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveFactualDataOperation;
42
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
43

    
44
/**
45
 * @author cmathew
46
 * @date 9 Mar 2015
47
 */
48
@DataSet
49
public class TaxonNavigatorTest extends BaseOperationTest {
50

    
51
    @SuppressWarnings("unused")
52
	private static final Logger logger = Logger.getLogger(TaxonNameEditorTest.class);
53

    
54
    ITaxonNodeService taxonNodeService = getRemoteApplicationController().getTaxonNodeService();
55
    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
56
    IClassificationService classificationService = getRemoteApplicationController().getClassificationService();
57

    
58
    Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
59

    
60

    
61
    @Test
62
    public void testChangeAcceptedTaxonToSynonym() throws ExecutionException {
63

    
64
        UUID oldTaxonNodeNodeUuid = UUID.fromString("b85b5b78-6760-409f-ac91-bb89e95ff2a1");
65
        UUID newAcceptedTaxonNodeUuid = UUID.fromString("6ad8e9e2-f5f6-41ad-aa30-f62a903650db");
66

    
67
        TaxonNode oldTaxonNode = taxonNodeService.load(oldTaxonNodeNodeUuid);
68
        TaxonName name = oldTaxonNode.getTaxon().getName();
69

    
70

    
71
        TaxonNode newAcceptedTaxonNode = taxonNodeService.load(newAcceptedTaxonNodeUuid);
72
        int countTargetSynonyms = newAcceptedTaxonNode.getTaxon().getSynonyms().size();
73
        sessionOwner.addExpectedUpdatedObject(oldTaxonNode.getParent());
74
        sessionOwner.addExpectedUpdatedObject(newAcceptedTaxonNode);
75

    
76
        operation =  new ChangeAcceptedTaxonToSynonymOperation(sessionOwner,
77
                false,
78
                oldTaxonNode.getUuid(),
79
                newAcceptedTaxonNode.getUuid(), null, null, true);
80
        operation.execute(monitor, info);
81
        newAcceptedTaxonNode = taxonNodeService.load(newAcceptedTaxonNodeUuid);
82
        oldTaxonNode = taxonNodeService.load(oldTaxonNodeNodeUuid);
83
        Assert.assertNull(oldTaxonNode);
84
        Assert.assertEquals(countTargetSynonyms + 1,newAcceptedTaxonNode.getTaxon().getSynonyms().size());
85
        Assert.assertEquals(name, newAcceptedTaxonNode.getTaxon().getSynonyms().iterator().next().getName());
86

    
87

    
88
    }
89

    
90
    @Test
91
    @Ignore
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
        MovingType moveToParentNode = MovingType.CHILD;
97

    
98
        TaxonNode taxonNodeToMove = taxonNodeService.load(taxonNodeToMoveUuid);
99
        TaxonNode oldParent = taxonNodeToMove.getParent();
100
        Set<UUID>uuids = new HashSet<>();
101
        uuids.add(taxonNodeToMoveUuid);
102
       // sessionOwner.addExpectedUpdatedObject(oldParent);
103
        int childCount = oldParent.getCountChildren();
104
        TaxonNode newParentTreeNode = taxonNodeService.load(newParentTreeNodeUuid);
105
       // sessionOwner.addExpectedUpdatedObject(newParentTreeNode);
106
        operation = new MoveTaxonOperation(sessionOwner,
107
                false,
108
                uuids,
109
                newParentTreeNode.getUuid(),
110
                moveToParentNode);
111
        operation.execute(monitor, info);
112
        //TODO: fix this, because move taxon is now a longrunning task
113
//        Assert.assertEquals(childCount-1, oldParent.getCountChildren());
114
//        Assert.assertTrue(!oldParent.getChildNodes().contains(taxonNodeToMove));
115
//        Assert.assertTrue(newParentTreeNode.getChildNodes().contains(taxonNodeToMove));
116
    }
117

    
118
    @Test
119
    public void testMoveFactualData() throws ExecutionException {
120
        UUID sourceTaxonUuid = UUID.fromString("e40854d7-143f-4054-b229-6ed4cedb4bff");
121
        UUID targetTaxonUuid = UUID.fromString("b8402dc4-5050-4882-a147-01b71e0e47d6");
122

    
123
        Taxon sourceTaxon = CdmBase.deproxy(taxonService.load(sourceTaxonUuid), Taxon.class);
124
        Set<TaxonDescription> sourceDescriptions = new HashSet<>(sourceTaxon.getDescriptions());
125
        sessionOwner.addExpectedUpdatedObject(sourceTaxon);
126

    
127
        Taxon targetTaxon = CdmBase.deproxy(taxonService.load(targetTaxonUuid), Taxon.class);
128
        int countTargetDescriptions = targetTaxon.getDescriptions().size();
129
        sessionOwner.addExpectedUpdatedObject(targetTaxon);
130

    
131
        operation = new MoveFactualDataOperation(sessionOwner,
132
                false,
133
                sourceTaxonUuid,
134
                targetTaxonUuid, true);
135
        operation.execute(monitor, info);
136
        Assert.assertEquals(0, sourceTaxon.getDescriptions().size());
137
        Assert.assertEquals(sourceDescriptions.size() + countTargetDescriptions, targetTaxon.getDescriptions().size());
138
        Assert.assertTrue(targetTaxon.getDescriptions().containsAll(sourceDescriptions));
139
    }
140

    
141
    @Test
142
    public void testDeleteTaxonNodeWithDeleteChildren() throws ExecutionException {
143
        UUID taxonNodeUuid = UUID.fromString("2f05d429-632d-4230-b9cb-70299360b470");
144
        TaxonNode taxonNode = taxonNodeService.load(taxonNodeUuid);
145
        List<TaxonNode> childNodes = taxonNode.getChildNodes();
146
        List<UUID> childNodeUuids = new ArrayList<UUID>();
147
        for(TaxonNode childNode : childNodes) {
148
            childNodeUuids.add(childNode.getUuid());
149
        }
150
        Assert.assertTrue(taxonNode.getCountChildren() > 0);
151

    
152
        Set<TaxonNodeDto> nodes = new HashSet<>();
153
        nodes.add(new TaxonNodeDto(taxonNode));
154

    
155
        TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
156
        configNodes.setChildHandling(ChildHandling.DELETE);
157
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
158
        config.setTaxonNodeConfig(configNodes);
159

    
160
        sessionOwner.addExpectedUpdatedObject(taxonNode.getParent());
161

    
162
        operation = new DeleteTaxonNodeOperation(sessionOwner,
163
                false,
164
                nodes,
165
                null,
166
                config);
167
        operation.execute(monitor, info);
168
        TaxonNode reloadedNode = taxonNodeService.load(taxonNode.getUuid());
169
        Assert.assertNull(reloadedNode);
170
        for(UUID childNodeUuid : childNodeUuids) {
171
            TaxonNode reloadedChildNode = taxonNodeService.load(childNodeUuid);
172
            Assert.assertNull(reloadedChildNode);
173
        }
174
    }
175

    
176
    @Test
177
    public void testDeleteTaxonNodes() throws ExecutionException {
178
        UUID taxonNode1Uuid = UUID.fromString("99f03b56-67cd-4e01-9ceb-2362d48f9d07");
179
        UUID taxonNode2Uuid = UUID.fromString("91698cec-615f-4472-9002-feda1a6acded");
180
        UUID taxonNode3Uuid = UUID.fromString("4fe03763-b966-4361-8334-352f6f777588");
181

    
182
        TaxonNode taxonNode1 = taxonNodeService.load(taxonNode1Uuid);
183
        TaxonNode taxonNode2 = taxonNodeService.load(taxonNode2Uuid);
184
        TaxonNode taxonNode3 = taxonNodeService.load(taxonNode3Uuid);
185

    
186
        Set<TaxonNodeDto> nodes = new HashSet<>();
187
        nodes.add(new TaxonNodeDto(taxonNode1));
188
        nodes.add(new TaxonNodeDto(taxonNode2));
189
        nodes.add(new TaxonNodeDto(taxonNode3));
190

    
191
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
192

    
193

    
194
        sessionOwner.addExpectedUpdatedObject(taxonNode1.getParent());
195
        sessionOwner.addExpectedUpdatedObject(taxonNode2.getParent());
196
        sessionOwner.addExpectedUpdatedObject(taxonNode3.getParent());
197
        operation = new DeleteTaxonNodeOperation(sessionOwner,
198
                false,
199
                nodes,
200
                null,
201
                config);
202
        operation.execute(monitor, info);
203
        TaxonNode reloadedNode1 = taxonNodeService.load(taxonNode1.getUuid());
204
        TaxonNode reloadedNode2 = taxonNodeService.load(taxonNode2.getUuid());
205
        TaxonNode reloadedNode3 = taxonNodeService.load(taxonNode3.getUuid());
206
        Assert.assertNull(reloadedNode1);
207
        Assert.assertNull(reloadedNode2);
208
        Assert.assertNull(reloadedNode3);
209

    
210
    }
211

    
212
    @Test
213
    public void testDeleteTaxonNodeWithMovingChildrenToParentNode() throws ExecutionException {
214
        UUID taxonNodeUuid = UUID.fromString("2f05d429-632d-4230-b9cb-70299360b470");
215
        TaxonNode taxonNode = taxonNodeService.load(taxonNodeUuid);
216
        TaxonNode parentTaxonNode = taxonNode.getParent();
217
        List<TaxonNode> childNodes = taxonNode.getChildNodes();
218
        List<UUID> childNodeUuids = new ArrayList<UUID>();
219
        for(TaxonNode childNode : childNodes) {
220
            childNodeUuids.add(childNode.getUuid());
221
        }
222
        Assert.assertTrue(taxonNode.getCountChildren() > 0);
223

    
224
        Set<TaxonNodeDto> nodes = new HashSet<>();
225
        nodes.add(new TaxonNodeDto(taxonNode));
226

    
227
        TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
228
        configNodes.setChildHandling(ChildHandling.MOVE_TO_PARENT);
229
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
230
        config.setTaxonNodeConfig(configNodes);
231

    
232
        sessionOwner.addExpectedUpdatedObject(taxonNode.getParent());
233
        operation = new DeleteTaxonNodeOperation(sessionOwner,
234
                false,
235
                nodes,
236
                null,
237
                config);
238
        operation.execute(monitor, info);
239
        TaxonNode reloadedNode = taxonNodeService.load(taxonNode.getUuid());
240
        Assert.assertNull(reloadedNode);
241
        List<TaxonNode> parentChildNodes = parentTaxonNode.getChildNodes();
242
        List<UUID> parentChildNodeUuids = new ArrayList<UUID>();
243
        for(TaxonNode parentChildNode : parentChildNodes) {
244
            parentChildNodeUuids.add(parentChildNode.getUuid());
245
        }
246
        parentChildNodeUuids.containsAll(childNodeUuids);
247

    
248
    }
249

    
250
    @Test
251
    @DataSet("TaxonNavigatorTest.testDeleteClassificationWithDeleteChildren.xml")
252
    public void testDeleteClassificationWithDeleteChildren() throws ExecutionException {
253
        UUID classificationUuid = UUID.fromString("0c2b5d25-7b15-4401-8b51-dd4be0ee5cab");
254
        Classification classification = classificationService.load(classificationUuid);
255
        TaxonNode rootNode = classification.getRootNode();
256
        Set<TaxonNodeDto> nodes = new HashSet<>();
257
        nodes.add(new TaxonNodeDto(classification.getRootNode()));
258

    
259
        TaxonNodeDeletionConfigurator configNodes = new TaxonNodeDeletionConfigurator();
260
        configNodes.setChildHandling(ChildHandling.DELETE);
261
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
262
        config.setTaxonNodeConfig(configNodes);
263

    
264
        operation = new DeleteTaxonNodeOperation(sessionOwner,
265
                false,
266
                nodes,
267
                null,
268
                config);
269
        operation.execute(monitor, info);
270

    
271
        Classification reloadedClassification = classificationService.load(classification.getUuid());
272
        Assert.assertNull(reloadedClassification);
273
        TaxonNode reloadedRootNode = taxonNodeService.load(rootNode.getUuid());
274
        Assert.assertNull(reloadedRootNode);
275

    
276
    }
277

    
278

    
279
}
(5-5/5)