Project

General

Profile

Download (12.2 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.TreeNodeDropAdapter.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
        sessionOwner.addExpectedUpdatedObject(oldParent);
102
        int childCount = oldParent.getCountChildren();
103
        TaxonNode newParentTreeNode = taxonNodeService.load(newParentTreeNodeUuid);
104
        sessionOwner.addExpectedUpdatedObject(newParentTreeNode);
105
        operation = new RemotingMoveTaxonOperation(sessionOwner,
106
                false,
107
                taxonNodeToMove.getUuid(),
108
                newParentTreeNode.getUuid(),
109
                moveToParentNode);
110
        operation.execute(monitor, info);
111
        Assert.assertEquals(childCount-1, oldParent.getCountChildren());
112
        Assert.assertTrue(!oldParent.getChildNodes().contains(taxonNodeToMove));
113
        Assert.assertTrue(newParentTreeNode.getChildNodes().contains(taxonNodeToMove));
114
    }
115

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

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

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

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

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

    
150
        Set<TaxonNodeDto> nodes = new HashSet();
151
        nodes.add(new TaxonNodeDto(taxonNode));
152

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

    
158
        sessionOwner.addExpectedUpdatedObject(taxonNode.getParent());
159

    
160
        operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
161
                false,
162
                nodes,
163
                null,
164
                config);
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);
171
        }
172
    }
173

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

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

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

    
189
        TaxonDeletionConfigurator config = new TaxonDeletionConfigurator();
190

    
191

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

    
208
    }
209

    
210
    @Test
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());
219
        }
220
        Assert.assertTrue(taxonNode.getCountChildren() > 0);
221

    
222
        Set<TaxonNodeDto> nodes = new HashSet();
223
        nodes.add(new TaxonNodeDto(taxonNode));
224

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

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

    
246
    }
247

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

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

    
262
        operation = new RemotingDeleteTaxonNodeOperation(sessionOwner,
263
                false,
264
                nodes,
265
                null,
266
                config);
267
        operation.execute(monitor, info);
268

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

    
274
    }
275

    
276

    
277
}
(5-5/5)