Project

General

Profile

Download (12.2 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.polytomouskey;
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.BeforeClass;
23
import org.junit.Test;
24
import org.unitils.dbunit.annotation.DataSet;
25

    
26
import eu.etaxonomy.cdm.api.cache.CachedCommonServiceImpl;
27
import eu.etaxonomy.cdm.api.service.ICommonService;
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.name.NomenclaturalCode;
42
import eu.etaxonomy.cdm.model.taxon.Taxon;
43
import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
44
import eu.etaxonomy.taxeditor.httpinvoker.CDMServer;
45

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

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

    
56
    UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
57
    UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
58

    
59

    
60
    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
61
    ICommonService commonService = getRemoteApplicationController().getCommonService();
62
    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
63

    
64
    Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
65

    
66
    @BeforeClass
67
    public static void initializePolytomousKeyTest() {
68
        logger.setLevel(Level.INFO);
69
        CDMServer.getInstance().setKeepServerRunning(true);
70

    
71
        initializeController("default", "127.0.0.1", 8080, "", NomenclaturalCode.ICNAFP);
72
    }
73

    
74
    @Test
75
    public void readAllPolytomousKeys() {
76
        List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
77
        for(PolytomousKey pKey : pKeys) {
78
            logger.info("Polytomous Key : " + pKey.getTitleCache());
79
        }
80
    }
81

    
82

    
83
    @Test
84
    //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
85
    public void readPolytmousKeyData() {
86
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
87
        Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
88
        Iterator<Taxon> tsItr = taxonomicScope.iterator();
89
        Taxon taxon = tsItr.next();
90
        logger.info("taxonomic scope : " + taxon.getTitleCache());
91
        Assert.assertEquals(taxon.getTitleCache(), "Achillea cretica L. sec. Cyprus");
92
        taxon = tsItr.next();
93
        logger.info("taxonomic scope : " + taxon.getTitleCache());
94
        Assert.assertEquals(taxon.getTitleCache(), "Achillea sec. Cyprus");
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
        PolytomousKeyNode pkeynode = pkey.getRoot();
161
        String newQuestion = "New Question";
162
        String newStatement = "New Statement";
163

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

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

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

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

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

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

    
187
        polytomousKeyService.merge(pkey);
188

    
189
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
190
        pkeynode = pkey.getRoot();
191
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
192
            Assert.assertNull(node.getQuestion());
193
            node.setQuestion(KeyStatement.NewInstance(english,newQuestion));
194
            Assert.assertNull(node.getStatement());
195
            node.setStatement(KeyStatement.NewInstance(english,newStatement));
196
        }
197
        //Assert.assertNull(pkeynode.getFeature());
198
        //pkeynode.setFeature(feature);
199

    
200
        //Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(english).getText(),modifyingText);
201
        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
202

    
203
        polytomousKeyService.merge(pkey);
204

    
205
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
206
        pkeynode = pkey.getRoot();
207
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
208
            Assert.assertNotNull(node.getQuestion());
209
            Map<Language, LanguageString> label = node.getQuestion().getLabel();
210
            Assert.assertEquals(newQuestion, label.get(english).getText());
211
            Assert.assertNotNull(node.getStatement());
212
            Assert.assertEquals(newStatement, node.getStatement().getLabel(english).getText());
213
        }
214
        //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
215

    
216
    }
217

    
218
    @Test
219
    public void savePolytomousKeyNodeDataWithSameSubKey() {
220
        //CachedCommonServiceImpl.setCacheEnabled(false);
221
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
222
        PolytomousKeyNode pkeynode = pkey.getRoot();
223

    
224
        PolytomousKey subkey1 = pkeynode.getChildAt(0).getSubkey();
225
        String subkey1title = subkey1.getTitleCache();
226
        subkey1.setTitleCache(subkey1title + "test", true);
227

    
228

    
229
        PolytomousKey subkey2 = pkeynode.getChildAt(1).getChildAt(0).getSubkey();
230
        String subkey2title = subkey2.getTitleCache();
231
        subkey2.setTitleCache(subkey2title + "test", true);
232

    
233
        Assert.assertNotSame(subkey1, subkey2);
234

    
235

    
236
        polytomousKeyService.merge(pkey);
237
    }
238

    
239
    @Test
240
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
241
        CachedCommonServiceImpl.setCacheEnabled(false);
242
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
243
        PolytomousKeyNode pkeynode = pkey.getRoot();
244

    
245
        PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
246
        PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(children, 0);
247
        PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
248
        String subkey1title = subkey1.getTitleCache();
249
        subkey1.setTitleCache(subkey1title + "test", true);
250

    
251
        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(children, 1);
252
        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
253
        String subkey2title = subkey2.getTitleCache();
254
        subkey2.setTitleCache(subkey2title + "test", true);
255

    
256
        Assert.assertNotSame(subkey1, subkey2);
257

    
258
        polytomousKeyService.merge(pkey);
259
    }
260

    
261

    
262
    @Test
263
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
264
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
265
        PolytomousKeyNode pkeynode = pkey.getRoot();
266

    
267
        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
268
        label1.size();
269
        //Language lang1 = label1.get(english).getLanguage();
270
        //String title1 = lang1.getTitleCache();
271

    
272
        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
273
        label2.size();
274
        //Language lang2 = label2.get(english).getLanguage();
275
        //String title2 = lang2.getTitleCache();
276

    
277
        polytomousKeyService.merge(pkey);
278
    }
279

    
280

    
281
}
    (1-1/1)