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.remoting.cache.EntityCacherDebugResult;
|
48
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
49
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
50
|
import eu.etaxonomy.taxeditor.session.MockSessionOwner;
|
51
|
|
52
|
/**
|
53
|
* @author cmathew
|
54
|
* @date 7 Oct 2014
|
55
|
*
|
56
|
*/
|
57
|
@DataSet
|
58
|
public class PolytomousKeyTest extends BaseRemotingTest {
|
59
|
|
60
|
private static final Logger logger = Logger.getLogger(PolytomousKeyTest.class);
|
61
|
|
62
|
UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
|
63
|
UUID subKeyUuid = UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01");
|
64
|
//UUID polytomousKeyUuid = UUID.fromString("bab66772-2c83-428a-bb6d-655d12ac6097");
|
65
|
UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
|
66
|
|
67
|
|
68
|
IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
|
69
|
IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
|
70
|
ICommonService commonService = getRemoteApplicationController().getCommonService();
|
71
|
ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
|
72
|
|
73
|
|
74
|
private CdmTransientEntityCacher cacher;
|
75
|
private ICdmEntitySessionEnabled sessionOwner;
|
76
|
|
77
|
private ICdmEntitySession cdmEntitySession;
|
78
|
|
79
|
Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
|
80
|
|
81
|
|
82
|
|
83
|
@BeforeClass
|
84
|
public static void initializePolytomousKeyTest() {
|
85
|
logger.setLevel(Level.INFO);
|
86
|
CDMServer.getInstance().setKeepServerRunning(true);
|
87
|
initializeController("default",
|
88
|
"localhost",
|
89
|
8080,
|
90
|
"",
|
91
|
NomenclaturalCode.ICNAFP,
|
92
|
"admin",
|
93
|
"00000");
|
94
|
}
|
95
|
|
96
|
@Before
|
97
|
public void initializeSession() {
|
98
|
sessionOwner = new MockSessionOwner();
|
99
|
cdmEntitySession = cdmEntitySessionManager.newSession(sessionOwner, true);
|
100
|
cacher = getCacher(sessionOwner);
|
101
|
}
|
102
|
|
103
|
@Test
|
104
|
public void readAllPolytomousKeys() {
|
105
|
List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
|
106
|
Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
|
107
|
Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
|
108
|
Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
|
109
|
}
|
110
|
|
111
|
|
112
|
@Test
|
113
|
//@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
|
114
|
public void readPolytmousKeyData() {
|
115
|
PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
116
|
Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
|
117
|
Iterator<Taxon> tsItr = taxonomicScope.iterator();
|
118
|
Taxon taxon = tsItr.next();
|
119
|
|
120
|
Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
|
121
|
Assert.assertEquals(tsItr.hasNext(), false);
|
122
|
|
123
|
List<Credit> credits = pKey.getCredits();
|
124
|
AgentBase agent = credits.get(0).getAgent();
|
125
|
Assert.assertEquals(agent.getId(),4809);
|
126
|
Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
|
127
|
Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
|
128
|
|
129
|
Set<Extension> exts = pKey.getExtensions();
|
130
|
Iterator<Extension> extItr = exts.iterator();
|
131
|
Extension ext = extItr.next();
|
132
|
Assert.assertEquals(ext.getValue(), "http://test.com");
|
133
|
|
134
|
Set<Rights> rights = pKey.getRights();
|
135
|
Iterator<Rights> rightsItr = rights.iterator();
|
136
|
Rights right = rightsItr.next();
|
137
|
Assert.assertEquals(right.getText(),"Rights Text Test");
|
138
|
|
139
|
Set<IdentifiableSource> sources = pKey.getSources();
|
140
|
Iterator<IdentifiableSource> sourcesItr = sources.iterator();
|
141
|
IdentifiableSource source = sourcesItr.next();
|
142
|
Assert.assertEquals(source.getId(), 23710);
|
143
|
source = sourcesItr.next();
|
144
|
Assert.assertEquals(source.getId(), 23711);
|
145
|
|
146
|
// TO DO : Added tests for Annotations , Markers
|
147
|
}
|
148
|
|
149
|
@Test
|
150
|
public void readPolytomousKeyDataFromNodes() {
|
151
|
PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
152
|
PolytomousKeyNode rootNode = pKey.getRoot();
|
153
|
|
154
|
|
155
|
Assert.assertEquals(rootNode.getId(), 2750);
|
156
|
|
157
|
Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
|
158
|
Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
|
159
|
Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
|
160
|
Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
|
161
|
Assert.assertEquals(sortIndex, new Integer(0));
|
162
|
String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
|
163
|
Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(english), statement);
|
164
|
|
165
|
Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
|
166
|
Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
|
167
|
Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
|
168
|
sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
|
169
|
Assert.assertEquals(sortIndex, new Integer(1));
|
170
|
statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
|
171
|
Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(english), statement);
|
172
|
|
173
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
|
174
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
|
175
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
|
176
|
sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
|
177
|
Assert.assertEquals(sortIndex, new Integer(0));
|
178
|
statement = "Ray-florets yellow";
|
179
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(english), statement);
|
180
|
Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
|
181
|
}
|
182
|
|
183
|
@Test
|
184
|
public void savePolytomousKeyNodeData() {
|
185
|
PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
186
|
|
187
|
PolytomousKeyNode pkeynode = pkey.getRoot();
|
188
|
String newQuestion = "New Question";
|
189
|
String newStatement = "New Statement";
|
190
|
|
191
|
Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(english).getText());
|
192
|
for(PolytomousKeyNode node : pkeynode.getChildren()) {
|
193
|
|
194
|
node.setQuestion(null);
|
195
|
node.setStatement(null);
|
196
|
}
|
197
|
//FIXME:Remoting Add tests for feature after fixing problem
|
198
|
|
199
|
//Feature feature = pkeynode.getFeature();
|
200
|
//Assert.assertEquals(feature.getTitleCache(),"Systematics");
|
201
|
//pkeynode.setFeature(null);
|
202
|
|
203
|
Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(english).getText(),"Modifying Text 1a");
|
204
|
String modifyingText = "Modifying Text 1a updated";
|
205
|
|
206
|
//pkeynode.getChildAt(0).putModifyingText(english, modifyingText);
|
207
|
|
208
|
Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
|
209
|
Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
|
210
|
Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
|
211
|
Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
|
212
|
pkeynode.getChildAt(1).setTaxon(taxon);
|
213
|
|
214
|
EntityCacherDebugResult debugResult = cacher.debug(pkey, true);
|
215
|
debugResult.print();
|
216
|
polytomousKeyService.merge(pkey);
|
217
|
|
218
|
pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
219
|
sessionOwner = new MockSessionOwner();
|
220
|
|
221
|
|
222
|
pkeynode = pkey.getRoot();
|
223
|
for(PolytomousKeyNode node : pkeynode.getChildren()) {
|
224
|
Assert.assertNull(node.getQuestion());
|
225
|
node.setQuestion(KeyStatement.NewInstance(english,newQuestion));
|
226
|
Assert.assertNull(node.getStatement());
|
227
|
node.setStatement(KeyStatement.NewInstance(english,newStatement));
|
228
|
}
|
229
|
|
230
|
|
231
|
Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
|
232
|
|
233
|
polytomousKeyService.merge(pkey);
|
234
|
|
235
|
pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
236
|
sessionOwner = new MockSessionOwner();
|
237
|
|
238
|
|
239
|
pkeynode = pkey.getRoot();
|
240
|
for(PolytomousKeyNode node : pkeynode.getChildren()) {
|
241
|
Assert.assertNotNull(node.getQuestion());
|
242
|
Map<Language, LanguageString> label = node.getQuestion().getLabel();
|
243
|
Assert.assertEquals(newQuestion, label.get(english).getText());
|
244
|
Assert.assertNotNull(node.getStatement());
|
245
|
Assert.assertEquals(newStatement, node.getStatement().getLabel(english).getText());
|
246
|
}
|
247
|
//Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
|
248
|
|
249
|
}
|
250
|
|
251
|
@Test
|
252
|
public void savePolytomousKeyNodeDataWithSameSubKey() {
|
253
|
|
254
|
ICdmEntitySessionEnabled sessionOwner = new MockSessionOwner();
|
255
|
cdmEntitySessionManager.bind(sessionOwner);
|
256
|
PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
257
|
|
258
|
|
259
|
PolytomousKeyNode pkeynode = pkey.getRoot();
|
260
|
|
261
|
PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
|
262
|
String subkey1title = subkey1.getTitleCache();
|
263
|
subkey1.setTitleCache(subkey1title + "test", true);
|
264
|
|
265
|
|
266
|
PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
|
267
|
String subkey2title = subkey2.getTitleCache();
|
268
|
subkey2.setTitleCache(subkey2title + "test", true);
|
269
|
|
270
|
Assert.assertSame(subkey1, subkey2);
|
271
|
|
272
|
polytomousKeyService.merge(pkey);
|
273
|
}
|
274
|
|
275
|
@Test
|
276
|
public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
|
277
|
|
278
|
PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
279
|
|
280
|
|
281
|
PolytomousKeyNode pkeynode = pkey.getRoot();
|
282
|
|
283
|
PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
|
284
|
PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(children, 0);
|
285
|
PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
|
286
|
String subkey1title = subkey1.getTitleCache();
|
287
|
subkey1.setTitleCache(subkey1title + "test", true);
|
288
|
|
289
|
PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(children, 1);
|
290
|
PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
|
291
|
String subkey2title = subkey2.getTitleCache();
|
292
|
subkey2.setTitleCache(subkey2title + "test", true);
|
293
|
|
294
|
Assert.assertNotSame(childNode0, childNode1);
|
295
|
|
296
|
Assert.assertSame(subkey1, subkey2);
|
297
|
|
298
|
polytomousKeyService.merge(pkey);
|
299
|
}
|
300
|
|
301
|
|
302
|
@Test
|
303
|
public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
|
304
|
PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
305
|
PolytomousKeyNode pkeynode = pkey.getRoot();
|
306
|
|
307
|
Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
|
308
|
label1.size();
|
309
|
|
310
|
|
311
|
Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
|
312
|
label2.size();
|
313
|
|
314
|
|
315
|
polytomousKeyService.merge(pkey);
|
316
|
}
|
317
|
|
318
|
@Test
|
319
|
public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
|
320
|
PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
321
|
|
322
|
|
323
|
PolytomousKeyNode rootNode = pKey.getRoot();
|
324
|
PolytomousKeyNode child = rootNode.getChildAt(0);
|
325
|
PolytomousKey subKey = child.getSubkey();
|
326
|
|
327
|
polytomousKeyService.delete(subKey);
|
328
|
|
329
|
// since subKey was not initialized before the delete, an attempt
|
330
|
// to initialize it after will lead to an NPE
|
331
|
try {
|
332
|
PolytomousKeyNode subKeyRootNode = subKey.getRoot();
|
333
|
Assert.fail("A NullPointerException indicating null root node should be thrown here");
|
334
|
} catch(NullPointerException npe) {
|
335
|
|
336
|
}
|
337
|
// retrieving subkey shows its null
|
338
|
subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
|
339
|
Assert.assertNull(subKey);
|
340
|
|
341
|
pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
342
|
|
343
|
|
344
|
rootNode = pKey.getRoot();
|
345
|
child = rootNode.getChildAt(0);
|
346
|
subKey = child.getSubkey();
|
347
|
// subkey will not be null, because the delete functionality of the subkey
|
348
|
// does not currently delete it from a polytomous key node
|
349
|
Assert.assertNotNull(subKey);
|
350
|
// after reloading the object graph, accessing subKey should throw a null pointer exception
|
351
|
try {
|
352
|
PolytomousKeyNode subKeyRootNode = subKey.getRoot();
|
353
|
Assert.fail("A NullPointerException indicating null root node should be thrown here");
|
354
|
} catch(NullPointerException npe) {
|
355
|
|
356
|
}
|
357
|
|
358
|
}
|
359
|
|
360
|
@Test
|
361
|
public void deleteSubKeyInPolytomousNode() {
|
362
|
PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
363
|
|
364
|
|
365
|
PolytomousKeyNode rootNode = pKey.getRoot();
|
366
|
List<PolytomousKeyNode> children = rootNode.getChildren();
|
367
|
PolytomousKeyNode child = rootNode.getChildAt(0);
|
368
|
polytomousKeyNodeService.delete(child.getUuid(), true);
|
369
|
|
370
|
pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
371
|
|
372
|
rootNode = pKey.getRoot();
|
373
|
children = rootNode.getChildren();
|
374
|
Assert.assertFalse(children.contains(child));
|
375
|
}
|
376
|
|
377
|
@Test
|
378
|
public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
|
379
|
PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
|
380
|
|
381
|
|
382
|
PolytomousKeyNode rootNode = pKey.getRoot();
|
383
|
PolytomousKeyNode child = rootNode.getChildAt(0);
|
384
|
PolytomousKey subKey = child.getSubkey();
|
385
|
// this call initializes the subkey
|
386
|
subKey.getRoot();
|
387
|
|
388
|
polytomousKeyService.delete(subKey);
|
389
|
|
390
|
// since subKey was initialized before the delete, it will be
|
391
|
// accesible even after the delete. This behaviour is similar
|
392
|
// to hibernate session delete where the deleted object is
|
393
|
// made transient, but not deleted from the object graph
|
394
|
|
395
|
PolytomousKeyNode subKeyRootNode = subKey.getRoot();
|
396
|
Assert.assertNotNull(subKey);
|
397
|
|
398
|
}
|
399
|
|
400
|
|
401
|
}
|