Project

General

Profile

Download (16.5 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.Iterator;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16
import java.util.UUID;
17

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

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

    
51
/**
52
 * @author cmathew
53
 * @date 7 Oct 2014
54
 *
55
 */
56
@DataSet
57
public class PolytomousKeyTest extends BaseRemotingTest {
58

    
59
    private static final Logger logger = Logger.getLogger(PolytomousKeyTest.class);
60

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

    
66

    
67
    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
68
    IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
69
    ICommonService commonService = getRemoteApplicationController().getCommonService();
70
    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
71

    
72

    
73
    private CdmTransientEntityCacher cacher;
74
    private ICdmEntitySessionEnabled sessionOwner;
75

    
76
    private ICdmEntitySession cdmEntitySession;
77

    
78
    Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
79

    
80

    
81

    
82
    @BeforeClass
83
    public static void initializePolytomousKeyTest() {
84
        logger.setLevel(Level.INFO);
85
        CDMServer.getInstance().setKeepServerRunning(true);
86
        initializeController("default",
87
                "localhost",
88
                8080,
89
                "",
90
                NomenclaturalCode.ICNAFP,
91
                "admin",
92
                "00000");
93
    }
94

    
95
    @Before
96
    public void initializeSession() {
97
        sessionOwner = new MockSessionOwner();
98
        cdmEntitySession = cdmEntitySessionManager.newSession(sessionOwner, true);
99
        cacher = getCacher(sessionOwner);
100
    }
101

    
102
    @Test
103
    public void readAllPolytomousKeys() {
104
        List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
105
        Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
106
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
107
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
108
    }
109

    
110

    
111
    @Test
112
    //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
113
    public void readPolytmousKeyData() {
114
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
115
        Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
116
        Iterator<Taxon> tsItr = taxonomicScope.iterator();
117
        Taxon taxon = tsItr.next();
118

    
119
        Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
120
        Assert.assertEquals(tsItr.hasNext(), false);
121

    
122
        List<Credit> credits = pKey.getCredits();
123
        AgentBase agent = credits.get(0).getAgent();
124
        Assert.assertEquals(agent.getId(),4809);
125
        Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
126
        Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
127

    
128
        Set<Extension> exts = pKey.getExtensions();
129
        Iterator<Extension> extItr = exts.iterator();
130
        Extension ext = extItr.next();
131
        Assert.assertEquals(ext.getValue(), "http://test.com");
132

    
133
        Set<Rights> rights = pKey.getRights();
134
        Iterator<Rights> rightsItr = rights.iterator();
135
        Rights right = rightsItr.next();
136
        Assert.assertEquals(right.getText(),"Rights Text Test");
137

    
138
        Set<IdentifiableSource> sources = pKey.getSources();
139
        Iterator<IdentifiableSource> sourcesItr = sources.iterator();
140
        IdentifiableSource source = sourcesItr.next();
141
        Assert.assertEquals(source.getId(), 23710);
142
        source = sourcesItr.next();
143
        Assert.assertEquals(source.getId(), 23711);
144

    
145
        // TO DO : Added tests for Annotations , Markers
146
    }
147

    
148
    @Test
149
    public void readPolytomousKeyDataFromNodes() {
150
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
151
        PolytomousKeyNode rootNode = pKey.getRoot();
152

    
153

    
154
        Assert.assertEquals(rootNode.getId(), 2750);
155

    
156
        Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
157
        Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
158
        Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
159
        Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
160
        Assert.assertEquals(sortIndex, new Integer(0));
161
        String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
162
        Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(english), statement);
163

    
164
        Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
165
        Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
166
        Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
167
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
168
        Assert.assertEquals(sortIndex, new Integer(1));
169
        statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
170
        Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(english), statement);
171

    
172
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
173
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
174
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
175
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
176
        Assert.assertEquals(sortIndex, new Integer(0));
177
        statement = "Ray-florets yellow";
178
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(english), statement);
179
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
180
    }
181

    
182
    @Test
183
    public void savePolytomousKeyNodeData() {
184
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
185
        //pkey = cdmEntitySession.load(pkey);
186
        PolytomousKeyNode pkeynode = pkey.getRoot();
187
        String newQuestion = "New Question";
188
        String newStatement = "New Statement";
189

    
190
        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(english).getText());
191
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
192

    
193
            node.setQuestion(null);
194
            node.setStatement(null);
195
        }
196
        //FIXME:Remoting Add tests for feature after fixing problem
197

    
198
        //Feature feature = pkeynode.getFeature();
199
        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
200
        //pkeynode.setFeature(null);
201

    
202
        Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(english).getText(),"Modifying Text 1a");
203
        String modifyingText = "Modifying Text 1a updated";
204

    
205
        //pkeynode.getChildAt(0).putModifyingText(english, modifyingText);
206

    
207
        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
208
        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
209
        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
210
        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
211
        pkeynode.getChildAt(1).setTaxon(taxon);
212

    
213
        polytomousKeyService.merge(pkey);
214

    
215
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
216
        sessionOwner = new MockSessionOwner();
217
        pkey = cdmEntitySession.load(pkey);
218

    
219
        pkeynode = pkey.getRoot();
220
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
221
            Assert.assertNull(node.getQuestion());
222
            node.setQuestion(KeyStatement.NewInstance(english,newQuestion));
223
            Assert.assertNull(node.getStatement());
224
            node.setStatement(KeyStatement.NewInstance(english,newStatement));
225
        }
226

    
227

    
228
        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
229

    
230
        polytomousKeyService.merge(pkey);
231

    
232
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
233
        sessionOwner = new MockSessionOwner();
234
        pkey = cdmEntitySession.load(pkey);
235

    
236
        pkeynode = pkey.getRoot();
237
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
238
            Assert.assertNotNull(node.getQuestion());
239
            Map<Language, LanguageString> label = node.getQuestion().getLabel();
240
            Assert.assertEquals(newQuestion, label.get(english).getText());
241
            Assert.assertNotNull(node.getStatement());
242
            Assert.assertEquals(newStatement, node.getStatement().getLabel(english).getText());
243
        }
244
        //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
245

    
246
    }
247

    
248
    @Test
249
    public void savePolytomousKeyNodeDataWithSameSubKey() {
250

    
251
        ICdmEntitySessionEnabled sessionOwner = new MockSessionOwner();
252
        cdmEntitySessionManager.bind(sessionOwner);
253
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
254
        pkey = cdmEntitySession.load(pkey);
255

    
256
        PolytomousKeyNode pkeynode = pkey.getRoot();
257

    
258
        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
259
        String subkey1title = subkey1.getTitleCache();
260
        subkey1.setTitleCache(subkey1title + "test", true);
261

    
262

    
263
        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
264
        String subkey2title = subkey2.getTitleCache();
265
        subkey2.setTitleCache(subkey2title + "test", true);
266

    
267
        Assert.assertSame(subkey1, subkey2);
268

    
269
        polytomousKeyService.merge(pkey);
270
    }
271

    
272
    @Test
273
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
274

    
275
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
276
        pkey = cdmEntitySession.load(pkey);
277

    
278
        PolytomousKeyNode pkeynode = pkey.getRoot();
279

    
280
        PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
281
        PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(children, 0);
282
        PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
283
        String subkey1title = subkey1.getTitleCache();
284
        subkey1.setTitleCache(subkey1title + "test", true);
285

    
286
        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(children, 1);
287
        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
288
        String subkey2title = subkey2.getTitleCache();
289
        subkey2.setTitleCache(subkey2title + "test", true);
290

    
291
        Assert.assertNotSame(childNode0, childNode1);
292

    
293
        Assert.assertSame(subkey1, subkey2);
294

    
295
        polytomousKeyService.merge(pkey);
296
    }
297

    
298

    
299
    @Test
300
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
301
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
302
        PolytomousKeyNode pkeynode = pkey.getRoot();
303

    
304
        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
305
        label1.size();
306

    
307

    
308
        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
309
        label2.size();
310

    
311

    
312
        polytomousKeyService.merge(pkey);
313
    }
314

    
315
    @Test
316
    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
317
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
318
        pKey = cdmEntitySession.load(pKey);
319

    
320
        PolytomousKeyNode rootNode = pKey.getRoot();
321
        PolytomousKeyNode child = rootNode.getChildAt(0);
322
        PolytomousKey subKey = child.getSubkey();
323

    
324
        polytomousKeyService.delete(subKey);
325

    
326
        // since subKey was not initialized before the delete, an attempt
327
        // to initialize it after will lead to an NPE
328
        try {
329
            PolytomousKeyNode subKeyRootNode = subKey.getRoot();
330
            Assert.fail("A NullPointerException indicating null root node should be thrown here");
331
        } catch(NullPointerException npe) {
332

    
333
        }
334
        // retrieving subkey shows its null
335
        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
336
        Assert.assertNull(subKey);
337

    
338
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
339
        pKey = cdmEntitySession.load(pKey);
340

    
341
        rootNode = pKey.getRoot();
342
        child = rootNode.getChildAt(0);
343
        subKey = child.getSubkey();
344
        // subkey will not be null, because the delete functionality of the subkey
345
        // does not currently delete it from a polytomous key node
346
        Assert.assertNotNull(subKey);
347
        // after reloading the object graph, accessing subKey should throw a null pointer exception
348
        try {
349
            PolytomousKeyNode subKeyRootNode = subKey.getRoot();
350
            Assert.fail("A NullPointerException indicating null root node should be thrown here");
351
        } catch(NullPointerException npe) {
352

    
353
        }
354

    
355
    }
356

    
357
    @Test
358
    public void deleteSubKeyInPolytomousNode() {
359
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
360
        pKey = cdmEntitySession.load(pKey);
361

    
362
        PolytomousKeyNode rootNode = pKey.getRoot();
363
        List<PolytomousKeyNode> children = rootNode.getChildren();
364
        PolytomousKeyNode child = rootNode.getChildAt(0);
365
        polytomousKeyNodeService.delete(child, true, true);
366

    
367
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
368

    
369
        rootNode = pKey.getRoot();
370
        children = rootNode.getChildren();
371
        Assert.assertFalse(children.contains(child));
372
    }
373

    
374
    @Test
375
    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
376
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
377
        pKey = cdmEntitySession.load(pKey);
378

    
379
        PolytomousKeyNode rootNode = pKey.getRoot();
380
        PolytomousKeyNode child = rootNode.getChildAt(0);
381
        PolytomousKey subKey = child.getSubkey();
382
        // this call initializes the subkey
383
        subKey.getRoot();
384

    
385
        polytomousKeyService.delete(subKey);
386

    
387
        // since subKey was initialized before the delete, it will be
388
        // accesible even after the delete. This behaviour is similar
389
        // to hibernate session delete where the deleted object is
390
        // made transient, but not deleted from the object graph
391

    
392
        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
393
        Assert.assertNotNull(subKey);
394

    
395
    }
396

    
397

    
398
}
(1-1/2)