Project

General

Profile

Download (12.5 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.logging.log4j.LogManager;
18
import org.apache.logging.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.NodeDeletionConfigurator.ChildHandling;
29
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
30
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
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.metadata.SecReferenceHandlingEnum;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.taxon.Classification;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
40
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TreeNodeDropAdapterE4.MovingType;
41
import eu.etaxonomy.taxeditor.navigation.navigator.operation.ChangeAcceptedTaxonToSynonymOperation;
42
import eu.etaxonomy.taxeditor.navigation.navigator.operation.DeleteTaxonNodeOperation;
43
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveFactualDataOperation;
44
import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
45

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

    
53
    @SuppressWarnings("unused")
54
	private static final Logger logger = LogManager.getLogger();
55

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

    
60
    Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
61

    
62

    
63
    @Test
64
    public void testChangeAcceptedTaxonToSynonym() throws ExecutionException {
65

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

    
69
        TaxonNode oldTaxonNode = taxonNodeService.load(oldTaxonNodeNodeUuid);
70
        TaxonName name = oldTaxonNode.getTaxon().getName();
71

    
72

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

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

    
89

    
90
    }
91

    
92
    @Test
93
    @Ignore
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
        MovingType moveToParentNode = MovingType.CHILD;
99

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

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

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

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

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

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

    
154
        Set<TaxonNodeDto> nodes = new HashSet<>();
155
        nodes.add(new TaxonNodeDto(taxonNode));
156

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

    
162
        sessionOwner.addExpectedUpdatedObject(taxonNode.getParent());
163

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

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

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

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

    
193
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
194

    
195

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

    
212
    }
213

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

    
226
        Set<TaxonNodeDto> nodes = new HashSet<>();
227
        nodes.add(new TaxonNodeDto(taxonNode));
228

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

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

    
250
    }
251

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

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

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

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

    
278
    }
279

    
280

    
281
}
(5-5/5)