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.Test;
21
import org.unitils.dbunit.annotation.DataSet;
22

    
23
import eu.etaxonomy.cdm.api.service.IClassificationService;
24
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
25
import eu.etaxonomy.cdm.api.service.ITaxonService;
26
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
27
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
28
import eu.etaxonomy.cdm.api.service.config.TaxonNodeDeletionConfigurator;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.common.Language;
31
import eu.etaxonomy.cdm.model.description.TaxonDescription;
32
import eu.etaxonomy.cdm.model.name.TaxonName;
33
import eu.etaxonomy.cdm.model.taxon.Classification;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
37
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TreeNodeDropAdapterE4.MovingType;
38
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingChangeAcceptedTaxonToSynonymOperation;
39
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingDeleteTaxonNodeOperation;
40
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveFactualDataOperation;
41
import eu.etaxonomy.taxeditor.navigation.navigator.operation.RemotingMoveTaxonOperation;
42

    
43
/**
44
 * @author cmathew
45
 * @date 9 Mar 2015
46
 *
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

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

    
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 RemotingChangeAcceptedTaxonToSynonymOperation(sessionOwner,
79
                false,
80
                oldTaxonNode.getUuid(),
81
                newAcceptedTaxonNode.getUuid());
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
    public void testMoveTaxon() throws ExecutionException {
94

    
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;
98

    
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 RemotingMoveTaxonOperation(sessionOwner,
108
                false,
109
                uuids,
110
                newParentTreeNode.getUuid(),
111
                moveToParentNode);
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));
117
    }
118

    
119
    @Test
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");
123

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

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

    
132
        operation = new RemotingMoveFactualDataOperation(sessionOwner,
133
                false,
134
                sourceTaxonUuid,
135
                targetTaxonUuid);
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));
140
    }
141

    
142
    @Test
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());
150
        }
151
        Assert.assertTrue(taxonNode.getCountChildren() > 0);
152

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

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

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

    
163
        operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
164
                false,
165
                nodes,
166
                null,
167
                config);
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);
174
        }
175
    }
176

    
177
    @Test
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");
182

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

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

    
192
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
193

    
194

    
195
        sessionOwner.addExpectedUpdatedObject(taxonNode1.getParent());
196
        sessionOwner.addExpectedUpdatedObject(taxonNode2.getParent());
197
        sessionOwner.addExpectedUpdatedObject(taxonNode3.getParent());
198
        operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
199
                false,
200
                nodes,
201
                null,
202
                config);
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);
210

    
211
    }
212

    
213
    @Test
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());
222
        }
223
        Assert.assertTrue(taxonNode.getCountChildren() > 0);
224

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

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

    
233
        sessionOwner.addExpectedUpdatedObject(taxonNode.getParent());
234
        operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
235
                false,
236
                nodes,
237
                null,
238
                config);
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());
246
        }
247
        parentChildNodeUuids.containsAll(childNodeUuids);
248

    
249
    }
250

    
251
    @Test
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()));
259

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

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

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

    
277
    }
278

    
279

    
280
}
(5-5/5)