Project

General

Profile

Download (15.4 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2014 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.cdm.model;
11

    
12
import java.util.Arrays;
13
import java.util.Iterator;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.apache.log4j.Logger;
20
import org.hibernate.collection.spi.PersistentCollection;
21
import org.junit.Assert;
22
import org.junit.BeforeClass;
23
import org.junit.Test;
24
import org.unitils.dbunit.annotation.DataSet;
25

    
26
import eu.etaxonomy.cdm.api.service.ICommonService;
27
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
28
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
29
import eu.etaxonomy.cdm.api.service.ITaxonService;
30
import eu.etaxonomy.cdm.model.agent.AgentBase;
31
import eu.etaxonomy.cdm.model.common.CdmBase;
32
import eu.etaxonomy.cdm.model.common.Credit;
33
import eu.etaxonomy.cdm.model.common.Extension;
34
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
35
import eu.etaxonomy.cdm.model.common.Language;
36
import eu.etaxonomy.cdm.model.common.LanguageString;
37
import eu.etaxonomy.cdm.model.description.KeyStatement;
38
import eu.etaxonomy.cdm.model.description.PolytomousKey;
39
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
40
import eu.etaxonomy.cdm.model.media.Rights;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
43
import eu.etaxonomy.taxeditor.remoting.cache.EntityCacherDebugResult;
44
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
45
import eu.etaxonomy.taxeditor.session.MockSessionOwner;
46

    
47
/**
48
 * @author cmathew
49
 * @date 7 Oct 2014
50
 *
51
 */
52
@DataSet
53
public class PolytomousKeyTest extends RemotingSessionAwareTest {
54

    
55
    private static final Logger logger = Logger.getLogger(PolytomousKeyTest.class);
56

    
57
    UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
58
    UUID subKeyUuid = UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01");
59
    //UUID polytomousKeyUuid = UUID.fromString("bab66772-2c83-428a-bb6d-655d12ac6097");
60
    UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
61

    
62

    
63
    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
64
    IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
65
    ICommonService commonService = getRemoteApplicationController().getCommonService();
66
    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
67

    
68
    Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
69

    
70

    
71

    
72
    @BeforeClass
73
    public static void initializePolytomousKeyTest() {
74
    }
75

    
76

    
77
    @Test
78
    public void readAllPolytomousKeys() {
79
        List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
80
        Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
81
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
82
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
83
    }
84

    
85

    
86
    @Test
87
    //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
88
    public void readPolytmousKeyData() {
89
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
90
        Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
91
        Iterator<Taxon> tsItr = taxonomicScope.iterator();
92
        Taxon taxon = tsItr.next();
93

    
94
        Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
95
        Assert.assertEquals(tsItr.hasNext(), false);
96

    
97
        List<Credit> credits = pKey.getCredits();
98
        AgentBase agent = credits.get(0).getAgent();
99
        Assert.assertEquals(agent.getId(),4809);
100
        Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
101
        Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
102

    
103
        Set<Extension> exts = pKey.getExtensions();
104
        Iterator<Extension> extItr = exts.iterator();
105
        Extension ext = extItr.next();
106
        Assert.assertEquals(ext.getValue(), "http://test.com");
107

    
108
        Set<Rights> rights = pKey.getRights();
109
        Iterator<Rights> rightsItr = rights.iterator();
110
        Rights right = rightsItr.next();
111
        Assert.assertEquals(right.getText(),"Rights Text Test");
112

    
113
        Set<IdentifiableSource> sources = pKey.getSources();
114
        Iterator<IdentifiableSource> sourcesItr = sources.iterator();
115
        IdentifiableSource source = sourcesItr.next();
116
        Assert.assertEquals(source.getId(), 23710);
117
        source = sourcesItr.next();
118
        Assert.assertEquals(source.getId(), 23711);
119

    
120
        // TO DO : Added tests for Annotations , Markers
121
    }
122

    
123
    @Test
124
    public void readPolytomousKeyDataFromNodes() {
125
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
126
        PolytomousKeyNode rootNode = pKey.getRoot();
127

    
128

    
129
        Assert.assertEquals(rootNode.getId(), 2750);
130

    
131
        Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
132
        Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
133
        Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
134
        Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
135
        Assert.assertEquals(sortIndex, new Integer(0));
136
        String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
137
        Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(english), statement);
138

    
139
        Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
140
        Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
141
        Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
142
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
143
        Assert.assertEquals(sortIndex, new Integer(1));
144
        statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
145
        Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(english), statement);
146

    
147
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
148
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
149
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
150
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
151
        Assert.assertEquals(sortIndex, new Integer(0));
152
        statement = "Ray-florets yellow";
153
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(english), statement);
154
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
155
    }
156

    
157
    @Test
158
    public void savePolytomousKeyNodeData() {
159
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
160

    
161
        PolytomousKeyNode pkeynode = pkey.getRoot();
162
        String newQuestion = "New Question";
163
        String newStatement = "New Statement";
164

    
165
        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(english).getText());
166
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
167

    
168
            node.setQuestion(null);
169
            node.setStatement(null);
170
        }
171
        //FIXME:Remoting Add tests for feature after fixing problem
172

    
173
        //Feature feature = pkeynode.getFeature();
174
        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
175
        //pkeynode.setFeature(null);
176

    
177
        Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(english).getText(),"Modifying Text 1a");
178
        String modifyingText = "Modifying Text 1a updated";
179

    
180
        //pkeynode.getChildAt(0).putModifyingText(english, modifyingText);
181

    
182
        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
183
        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
184
        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
185
        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
186
        pkeynode.getChildAt(1).setTaxon(taxon);
187

    
188
        EntityCacherDebugResult debugResult = new EntityCacherDebugResult(cacher,  Arrays.asList(pkey));
189

    
190
        polytomousKeyService.merge(pkey);
191

    
192
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
193
        sessionOwner = new MockSessionOwner();
194

    
195

    
196
        pkeynode = pkey.getRoot();
197
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
198
            Assert.assertNull(node.getQuestion());
199
            node.setQuestion(KeyStatement.NewInstance(english,newQuestion));
200
            Assert.assertNull(node.getStatement());
201
            node.setStatement(KeyStatement.NewInstance(english,newStatement));
202
        }
203

    
204

    
205
        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
206

    
207
        polytomousKeyService.merge(pkey);
208

    
209
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
210
        sessionOwner = new MockSessionOwner();
211

    
212

    
213
        pkeynode = pkey.getRoot();
214
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
215
            Assert.assertNotNull(node.getQuestion());
216
            Map<Language, LanguageString> label = node.getQuestion().getLabel();
217
            Assert.assertEquals(newQuestion, label.get(english).getText());
218
            Assert.assertNotNull(node.getStatement());
219
            Assert.assertEquals(newStatement, node.getStatement().getLabel(english).getText());
220
        }
221
        //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
222

    
223
    }
224

    
225
    @Test
226
    public void savePolytomousKeyNodeDataWithSameSubKey() {
227

    
228
        ICdmEntitySessionEnabled sessionOwner = new MockSessionOwner();
229
        cdmEntitySessionManager.bind(sessionOwner);
230
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
231

    
232

    
233
        PolytomousKeyNode pkeynode = pkey.getRoot();
234

    
235
        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
236
        String subkey1title = subkey1.getTitleCache();
237
        subkey1.setTitleCache(subkey1title + "test", true);
238

    
239

    
240
        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
241
        String subkey2title = subkey2.getTitleCache();
242
        subkey2.setTitleCache(subkey2title + "test", true);
243

    
244
        Assert.assertSame(subkey1, subkey2);
245

    
246
        polytomousKeyService.merge(pkey);
247
    }
248

    
249
    @Test
250
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
251

    
252
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
253

    
254

    
255
        PolytomousKeyNode pkeynode = pkey.getRoot();
256

    
257
        PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
258
        PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 0);
259
        PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
260
        String subkey1title = subkey1.getTitleCache();
261
        subkey1.setTitleCache(subkey1title + "test", true);
262

    
263
        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 1);
264
        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
265
        String subkey2title = subkey2.getTitleCache();
266
        subkey2.setTitleCache(subkey2title + "test", true);
267

    
268
        Assert.assertNotSame(childNode0, childNode1);
269

    
270
        Assert.assertSame(subkey1, subkey2);
271

    
272
        polytomousKeyService.merge(pkey);
273
    }
274

    
275

    
276
    @Test
277
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
278
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
279
        PolytomousKeyNode pkeynode = pkey.getRoot();
280

    
281
        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
282
        label1.size();
283

    
284

    
285
        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
286
        label2.size();
287

    
288

    
289
        polytomousKeyService.merge(pkey);
290
    }
291

    
292
    @Test
293
    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
294
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
295

    
296

    
297
        PolytomousKeyNode rootNode = pKey.getRoot();
298
        PolytomousKeyNode child = rootNode.getChildAt(0);
299
        PolytomousKey subKey = child.getSubkey();
300

    
301
        polytomousKeyService.delete(subKey);
302

    
303
        // since subKey was not initialized before the delete, an attempt
304
        // to initialize it after will lead to an NPE
305
        try {
306
            PolytomousKeyNode subKeyRootNode = subKey.getRoot();
307
            Assert.fail("A NullPointerException indicating null root node should be thrown here");
308
        } catch(NullPointerException npe) {
309

    
310
        }
311
        // retrieving subkey shows its null
312
        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
313
        Assert.assertNull(subKey);
314

    
315
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
316

    
317

    
318
        rootNode = pKey.getRoot();
319
        child = rootNode.getChildAt(0);
320
        subKey = child.getSubkey();
321
        // subkey will not be null, because the delete functionality of the subkey
322
        // does not currently delete it from a polytomous key node
323
        Assert.assertNotNull(subKey);
324
        // after reloading the object graph, accessing subKey should throw a null pointer exception
325
        try {
326
            PolytomousKeyNode subKeyRootNode = subKey.getRoot();
327
            Assert.fail("A NullPointerException indicating null root node should be thrown here");
328
        } catch(NullPointerException npe) {
329

    
330
        }
331

    
332
    }
333

    
334
    @Test
335
    public void deleteSubKeyInPolytomousNode() {
336
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
337

    
338

    
339
        PolytomousKeyNode rootNode = pKey.getRoot();
340
        List<PolytomousKeyNode> children = rootNode.getChildren();
341
        PolytomousKeyNode child = rootNode.getChildAt(0);
342
        polytomousKeyNodeService.delete(child.getUuid(), true);
343

    
344
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
345

    
346
        rootNode = pKey.getRoot();
347
        children = rootNode.getChildren();
348
        Assert.assertFalse(children.contains(child));
349
    }
350

    
351
    @Test
352
    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
353
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
354

    
355

    
356
        PolytomousKeyNode rootNode = pKey.getRoot();
357
        PolytomousKeyNode child = rootNode.getChildAt(0);
358
        PolytomousKey subKey = child.getSubkey();
359
        // this call initializes the subkey
360
        subKey.getRoot();
361

    
362
        polytomousKeyService.delete(subKey);
363

    
364
        // since subKey was initialized before the delete, it will be
365
        // accesible even after the delete. This behaviour is similar
366
        // to hibernate session delete where the deleted object is
367
        // made transient, but not deleted from the object graph
368

    
369
        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
370
        Assert.assertNotNull(subKey);
371

    
372
    }
373

    
374

    
375
}
(1-1/3)