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.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.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.BaseRemotingTest;
|
43
|
import eu.etaxonomy.taxeditor.httpinvoker.CDMServer;
|
44
|
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
|
45
|
import eu.etaxonomy.taxeditor.session.ISessionEventListener;
|
46
|
import eu.etaxonomy.taxeditor.session.MockSessionOwner;
|
47
|
|
48
|
/**
|
49
|
* @author cmathew
|
50
|
* @date 7 Oct 2014
|
51
|
*
|
52
|
*/
|
53
|
@DataSet
|
54
|
public class PolytomousKeyTest extends BaseRemotingTest {
|
55
|
|
56
|
private static final Logger logger = Logger.getLogger(PolytomousKeyTest.class);
|
57
|
|
58
|
UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
|
59
|
UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
|
60
|
|
61
|
|
62
|
IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
|
63
|
ICommonService commonService = getRemoteApplicationController().getCommonService();
|
64
|
ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
|
65
|
|
66
|
|
67
|
private CdmTransientEntityCacher cacher;
|
68
|
private ISessionEventListener sessionOwner;
|
69
|
|
70
|
Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
|
71
|
|
72
|
|
73
|
|
74
|
@BeforeClass
|
75
|
public static void initializePolytomousKeyTest() {
|
76
|
logger.setLevel(Level.INFO);
|
77
|
CDMServer.getInstance().setKeepServerRunning(true);
|
78
|
}
|
79
|
|
80
|
@Before
|
81
|
public void initializeSession() {
|
82
|
sessionOwner = new MockSessionOwner();
|
83
|
cdmEntitySessionManager.bind(sessionOwner);
|
84
|
cacher = getCacher(sessionOwner);
|
85
|
}
|
86
|
|
87
|
@Test
|
88
|
public void readAllPolytomousKeys() {
|
89
|
List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
|
90
|
Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
|
91
|
Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
|
92
|
Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
|
93
|
}
|
94
|
|
95
|
|
96
|
@Test
|
97
|
//@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
|
98
|
public void readPolytmousKeyData() {
|
99
|
PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
100
|
Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
|
101
|
Iterator<Taxon> tsItr = taxonomicScope.iterator();
|
102
|
Taxon taxon = tsItr.next();
|
103
|
|
104
|
Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
|
105
|
Assert.assertEquals(tsItr.hasNext(), false);
|
106
|
|
107
|
List<Credit> credits = pKey.getCredits();
|
108
|
AgentBase agent = credits.get(0).getAgent();
|
109
|
Assert.assertEquals(agent.getId(),4809);
|
110
|
Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
|
111
|
Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
|
112
|
|
113
|
Set<Extension> exts = pKey.getExtensions();
|
114
|
Iterator<Extension> extItr = exts.iterator();
|
115
|
Extension ext = extItr.next();
|
116
|
Assert.assertEquals(ext.getValue(), "http://test.com");
|
117
|
|
118
|
Set<Rights> rights = pKey.getRights();
|
119
|
Iterator<Rights> rightsItr = rights.iterator();
|
120
|
Rights right = rightsItr.next();
|
121
|
Assert.assertEquals(right.getText(),"Rights Text Test");
|
122
|
|
123
|
Set<IdentifiableSource> sources = pKey.getSources();
|
124
|
Iterator<IdentifiableSource> sourcesItr = sources.iterator();
|
125
|
IdentifiableSource source = sourcesItr.next();
|
126
|
Assert.assertEquals(source.getId(), 23710);
|
127
|
source = sourcesItr.next();
|
128
|
Assert.assertEquals(source.getId(), 23711);
|
129
|
|
130
|
// TO DO : Added tests for Annotations , Markers
|
131
|
}
|
132
|
|
133
|
@Test
|
134
|
public void readPolytomousKeyDataFromNodes() {
|
135
|
PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
136
|
PolytomousKeyNode rootNode = pKey.getRoot();
|
137
|
|
138
|
|
139
|
Assert.assertEquals(rootNode.getId(), 2750);
|
140
|
|
141
|
Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
|
142
|
Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
|
143
|
Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
|
144
|
Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
|
145
|
Assert.assertEquals(sortIndex, new Integer(0));
|
146
|
String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
|
147
|
Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(english), statement);
|
148
|
|
149
|
Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
|
150
|
Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
|
151
|
Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
|
152
|
sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
|
153
|
Assert.assertEquals(sortIndex, new Integer(1));
|
154
|
statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
|
155
|
Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(english), statement);
|
156
|
|
157
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
|
158
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
|
159
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
|
160
|
sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
|
161
|
Assert.assertEquals(sortIndex, new Integer(0));
|
162
|
statement = "Ray-florets yellow";
|
163
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(english), statement);
|
164
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
|
165
|
}
|
166
|
|
167
|
@Test
|
168
|
public void savePolytomousKeyNodeData() {
|
169
|
PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
170
|
pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
|
171
|
PolytomousKeyNode pkeynode = pkey.getRoot();
|
172
|
String newQuestion = "New Question";
|
173
|
String newStatement = "New Statement";
|
174
|
|
175
|
Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(english).getText());
|
176
|
for(PolytomousKeyNode node : pkeynode.getChildren()) {
|
177
|
|
178
|
node.setQuestion(null);
|
179
|
node.setStatement(null);
|
180
|
}
|
181
|
//FIXME:Remoting Add tests for feature after fixing problem
|
182
|
|
183
|
//Feature feature = pkeynode.getFeature();
|
184
|
//Assert.assertEquals(feature.getTitleCache(),"Systematics");
|
185
|
//pkeynode.setFeature(null);
|
186
|
|
187
|
Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(english).getText(),"Modifying Text 1a");
|
188
|
String modifyingText = "Modifying Text 1a updated";
|
189
|
|
190
|
//pkeynode.getChildAt(0).putModifyingText(english, modifyingText);
|
191
|
|
192
|
Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),1550);
|
193
|
Assert.assertEquals("Achillea", pkeynode.getChildAt(0).getSubkey().getTitleCache());
|
194
|
Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
|
195
|
Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
|
196
|
pkeynode.getChildAt(1).setTaxon(taxon);
|
197
|
|
198
|
polytomousKeyService.merge(pkey);
|
199
|
|
200
|
pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
201
|
sessionOwner = new MockSessionOwner();
|
202
|
pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
|
203
|
|
204
|
pkeynode = pkey.getRoot();
|
205
|
for(PolytomousKeyNode node : pkeynode.getChildren()) {
|
206
|
Assert.assertNull(node.getQuestion());
|
207
|
node.setQuestion(KeyStatement.NewInstance(english,newQuestion));
|
208
|
Assert.assertNull(node.getStatement());
|
209
|
node.setStatement(KeyStatement.NewInstance(english,newStatement));
|
210
|
}
|
211
|
|
212
|
|
213
|
Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
|
214
|
|
215
|
polytomousKeyService.merge(pkey);
|
216
|
|
217
|
pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
218
|
sessionOwner = new MockSessionOwner();
|
219
|
pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
|
220
|
|
221
|
pkeynode = pkey.getRoot();
|
222
|
for(PolytomousKeyNode node : pkeynode.getChildren()) {
|
223
|
Assert.assertNotNull(node.getQuestion());
|
224
|
Map<Language, LanguageString> label = node.getQuestion().getLabel();
|
225
|
Assert.assertEquals(newQuestion, label.get(english).getText());
|
226
|
Assert.assertNotNull(node.getStatement());
|
227
|
Assert.assertEquals(newStatement, node.getStatement().getLabel(english).getText());
|
228
|
}
|
229
|
//Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
|
230
|
|
231
|
}
|
232
|
|
233
|
@Test
|
234
|
public void savePolytomousKeyNodeDataWithSameSubKey() {
|
235
|
|
236
|
ISessionEventListener sessionOwner = new MockSessionOwner();
|
237
|
cdmEntitySessionManager.bind(sessionOwner);
|
238
|
PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
239
|
pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
|
240
|
|
241
|
PolytomousKeyNode pkeynode = pkey.getRoot();
|
242
|
|
243
|
PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
|
244
|
String subkey1title = subkey1.getTitleCache();
|
245
|
subkey1.setTitleCache(subkey1title + "test", true);
|
246
|
|
247
|
|
248
|
PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
|
249
|
String subkey2title = subkey2.getTitleCache();
|
250
|
subkey2.setTitleCache(subkey2title + "test", true);
|
251
|
|
252
|
Assert.assertSame(subkey1, subkey2);
|
253
|
|
254
|
polytomousKeyService.merge(pkey);
|
255
|
}
|
256
|
|
257
|
@Test
|
258
|
public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
|
259
|
|
260
|
PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
261
|
pkey = cdmEntitySessionManager.load(sessionOwner, pkey);
|
262
|
|
263
|
PolytomousKeyNode pkeynode = pkey.getRoot();
|
264
|
|
265
|
PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
|
266
|
PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(children, 0);
|
267
|
PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
|
268
|
String subkey1title = subkey1.getTitleCache();
|
269
|
subkey1.setTitleCache(subkey1title + "test", true);
|
270
|
|
271
|
PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(children, 1);
|
272
|
PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
|
273
|
String subkey2title = subkey2.getTitleCache();
|
274
|
subkey2.setTitleCache(subkey2title + "test", true);
|
275
|
|
276
|
Assert.assertNotSame(childNode0, childNode1);
|
277
|
|
278
|
Assert.assertSame(subkey1, subkey2);
|
279
|
|
280
|
polytomousKeyService.merge(pkey);
|
281
|
}
|
282
|
|
283
|
|
284
|
@Test
|
285
|
public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
|
286
|
PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
287
|
PolytomousKeyNode pkeynode = pkey.getRoot();
|
288
|
|
289
|
Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
|
290
|
label1.size();
|
291
|
|
292
|
|
293
|
Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
|
294
|
label2.size();
|
295
|
|
296
|
|
297
|
polytomousKeyService.merge(pkey);
|
298
|
}
|
299
|
|
300
|
|
301
|
}
|