1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.api.service;
|
11
|
|
12
|
import static org.junit.Assert.*;
|
13
|
import static org.junit.Assert.assertNotNull;
|
14
|
import static org.junit.Assert.assertTrue;
|
15
|
|
16
|
import java.io.FileNotFoundException;
|
17
|
import java.io.FileOutputStream;
|
18
|
import java.io.IOException;
|
19
|
import java.util.Arrays;
|
20
|
import java.util.List;
|
21
|
import java.util.Set;
|
22
|
import java.util.UUID;
|
23
|
|
24
|
import org.apache.commons.lang.RandomStringUtils;
|
25
|
import org.apache.log4j.Logger;
|
26
|
import org.apache.lucene.document.Document;
|
27
|
import org.apache.lucene.index.CorruptIndexException;
|
28
|
import org.apache.lucene.queryParser.ParseException;
|
29
|
import org.junit.Assert;
|
30
|
import org.junit.Ignore;
|
31
|
import org.junit.Test;
|
32
|
import org.unitils.dbunit.annotation.DataSet;
|
33
|
import org.unitils.spring.annotation.SpringBeanByType;
|
34
|
|
35
|
import com.mchange.util.AssertException;
|
36
|
|
37
|
import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
|
38
|
import eu.etaxonomy.cdm.api.service.config.TaxonServiceConfiguratorImpl;
|
39
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
40
|
import eu.etaxonomy.cdm.api.service.search.SearchResult;
|
41
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
42
|
import eu.etaxonomy.cdm.model.common.Language;
|
43
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
44
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
45
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
46
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
47
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
48
|
import eu.etaxonomy.cdm.model.description.Feature;
|
49
|
import eu.etaxonomy.cdm.model.description.Modifier;
|
50
|
import eu.etaxonomy.cdm.model.description.State;
|
51
|
import eu.etaxonomy.cdm.model.description.StateData;
|
52
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
53
|
import eu.etaxonomy.cdm.model.description.TextData;
|
54
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
55
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
56
|
import eu.etaxonomy.cdm.model.name.Rank;
|
57
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
58
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
59
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
60
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
61
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
62
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
63
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
64
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
65
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
66
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
67
|
import eu.etaxonomy.cdm.search.ICdmMassIndexer;
|
68
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
69
|
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
|
70
|
|
71
|
|
72
|
/**
|
73
|
* @author a.babadshanjan, a.kohlbecker
|
74
|
* @created 04.02.2009
|
75
|
* @version 1.0
|
76
|
*/
|
77
|
|
78
|
public class TaxonServiceSearchTest extends CdmTransactionalIntegrationTest {
|
79
|
|
80
|
private static final String ABIES_BALSAMEA_UUID = "f65d47bd-4f49-4ab1-bc4a-bc4551eaa1a8";
|
81
|
|
82
|
private static final String CLASSIFICATION_UUID = "2a5ceebb-4830-4524-b330-78461bf8cb6b";
|
83
|
|
84
|
private static final String CLASSIFICATION_ALT_UUID = "d7c741e3-ae9e-4a7d-a566-9e3a7a0b51ce";
|
85
|
|
86
|
private static final String D_ABIES_ALBA_UUID = "900108d8-e6ce-495e-b32e-7aad3099135e";
|
87
|
|
88
|
private static final int NUM_OF_NEW_RADOM_ENTITIES = 1000;
|
89
|
|
90
|
private static Logger logger = Logger.getLogger(TaxonServiceSearchTest.class);
|
91
|
|
92
|
@SpringBeanByType
|
93
|
private ITaxonService taxonService;
|
94
|
@SpringBeanByType
|
95
|
private ITermService termService;
|
96
|
@SpringBeanByType
|
97
|
private IClassificationService classificationService;
|
98
|
@SpringBeanByType
|
99
|
private IReferenceService referenceService;
|
100
|
@SpringBeanByType
|
101
|
private IDescriptionService descriptionService;
|
102
|
@SpringBeanByType
|
103
|
private INameService nameService;
|
104
|
@SpringBeanByType
|
105
|
private ICdmMassIndexer indexer;
|
106
|
|
107
|
private static final int BENCHMARK_ROUNDS = 300;
|
108
|
|
109
|
@Test
|
110
|
public void testDbUnitUsageTest() throws Exception {
|
111
|
assertNotNull("taxonService should exist", taxonService);
|
112
|
assertNotNull("nameService should exist", nameService);
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Test method for
|
117
|
* {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#findTaxaAndNames(eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator)}
|
118
|
* .
|
119
|
*/
|
120
|
@Test
|
121
|
@DataSet
|
122
|
public final void testFindTaxaAndNames() {
|
123
|
|
124
|
// pass 1
|
125
|
ITaxonServiceConfigurator configurator = new TaxonServiceConfiguratorImpl();
|
126
|
configurator.setTitleSearchString("Abies*");
|
127
|
configurator.setMatchMode(MatchMode.BEGINNING);
|
128
|
configurator.setDoTaxa(true);
|
129
|
configurator.setDoSynonyms(true);
|
130
|
configurator.setDoNamesWithoutTaxa(true);
|
131
|
configurator.setDoTaxaByCommonNames(true);
|
132
|
|
133
|
Pager<IdentifiableEntity> pager = taxonService.findTaxaAndNames(configurator);
|
134
|
List<IdentifiableEntity> list = pager.getRecords();
|
135
|
|
136
|
if (logger.isDebugEnabled()) {
|
137
|
for (int i = 0; i < list.size(); i++) {
|
138
|
String nameCache = "";
|
139
|
if (list.get(i) instanceof NonViralName) {
|
140
|
nameCache = ((NonViralName<?>) list.get(i)).getNameCache();
|
141
|
} else if (list.get(i) instanceof TaxonBase) {
|
142
|
TaxonNameBase taxonNameBase = ((TaxonBase) list.get(i)).getName();
|
143
|
nameCache = ((NonViralName) taxonNameBase).getNameCache();
|
144
|
} else {
|
145
|
}
|
146
|
logger.debug(list.get(i).getClass() + "(" + i + ")" + ": Name Cache = " + nameCache + ", Title Cache = "
|
147
|
+ list.get(i).getTitleCache());
|
148
|
}
|
149
|
}
|
150
|
|
151
|
logger.debug("number of taxa: " + list.size());
|
152
|
assertEquals(7, list.size());
|
153
|
|
154
|
// pass 2
|
155
|
// configurator.setDoTaxaByCommonNames(false);
|
156
|
// configurator.setDoMisappliedNames(true);
|
157
|
// configurator.setClassification(classificationService.load(UUID.fromString(CLASSIFICATION_UUID)));
|
158
|
// pager = taxonService.findTaxaAndNames(configurator);
|
159
|
// list = pager.getRecords();
|
160
|
// assertEquals(0, list.size());
|
161
|
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Test method for
|
166
|
* {@link eu.etaxonomy.cdm.api.service.TaxonServiceImpl#searchTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}
|
167
|
* .
|
168
|
*/
|
169
|
@Test
|
170
|
@DataSet
|
171
|
public final void testSearchTaxaByName() {
|
172
|
logger.warn("testSearchTaxaByName not yet implemented"); // TODO
|
173
|
}
|
174
|
|
175
|
@SuppressWarnings("rawtypes")
|
176
|
@Test
|
177
|
@DataSet
|
178
|
public final void testFindByDescriptionElementFullText_CommonName() throws CorruptIndexException, IOException, ParseException {
|
179
|
|
180
|
refreshLuceneIndex();
|
181
|
|
182
|
Pager<SearchResult<TaxonBase>> pager;
|
183
|
|
184
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Weißtanne", null, null, null, null, null, null);
|
185
|
Assert.assertEquals("Expecting one entity when searching for CommonTaxonName", Integer.valueOf(1), pager.getCount());
|
186
|
|
187
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Weißtanne", null, Arrays.asList(new Language[]{Language.GERMAN()}), null, null, null, null);
|
188
|
Assert.assertEquals("Expecting one entity when searching in German", Integer.valueOf(1), pager.getCount());
|
189
|
|
190
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Weißtanne", null, Arrays.asList(new Language[]{Language.RUSSIAN()}), null, null, null, null);
|
191
|
Assert.assertEquals("Expecting no entity when searching in Russian", Integer.valueOf(0), pager.getCount());
|
192
|
}
|
193
|
|
194
|
@SuppressWarnings("rawtypes")
|
195
|
@Test
|
196
|
@DataSet
|
197
|
public final void testFindByDescriptionElementFullText_wildcard() throws CorruptIndexException, IOException, ParseException {
|
198
|
|
199
|
refreshLuceneIndex();
|
200
|
|
201
|
Pager<SearchResult<TaxonBase>> pager;
|
202
|
|
203
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Weiß*", null, null, null, null, null, null);
|
204
|
Assert.assertEquals("Expecting one entity when searching for CommonTaxonName", Integer.valueOf(1), pager.getCount());
|
205
|
}
|
206
|
|
207
|
@SuppressWarnings("rawtypes")
|
208
|
@Test
|
209
|
@DataSet
|
210
|
public final void testFindByDescriptionElementFullText_TextData() throws CorruptIndexException, IOException, ParseException {
|
211
|
|
212
|
refreshLuceneIndex();
|
213
|
|
214
|
Pager<SearchResult<TaxonBase>> pager;
|
215
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Abies", null, null, null, null, null, null);
|
216
|
Assert.assertEquals("Expecting one entity when searching for any TextData", Integer.valueOf(1), pager.getCount());
|
217
|
Assert.assertEquals("Abies balsamea sec. ", pager.getRecords().get(0).getEntity().getTitleCache());
|
218
|
Assert.assertEquals("Abies balsamea sec. ", pager.getRecords().get(0).getDoc().get("inDescription.taxon.titleCache"));
|
219
|
|
220
|
pager = taxonService.findByDescriptionElementFullText(null, "Abies", null, null, null, null, null, null);
|
221
|
Assert.assertEquals("Expecting one entity when searching for any type", Integer.valueOf(1), pager.getCount());
|
222
|
|
223
|
pager = taxonService.findByDescriptionElementFullText(Distribution.class, "Abies", null, null, null, null, null, null);
|
224
|
Assert.assertEquals("Expecting no entity when searching for Distribution", Integer.valueOf(0), pager.getCount());
|
225
|
|
226
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Бальзам", null, Arrays.asList(new Language[]{}), null, null, null, null);
|
227
|
Assert.assertEquals("Expecting one entity", Integer.valueOf(1), pager.getCount());
|
228
|
Assert.assertEquals("Abies balsamea sec. ", pager.getRecords().get(0).getEntity().getTitleCache());
|
229
|
|
230
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Бальзам", null, Arrays.asList(new Language[]{Language.RUSSIAN()}), null, null, null, null);
|
231
|
Assert.assertEquals("Expecting one entity", Integer.valueOf(1), pager.getCount());
|
232
|
Assert.assertEquals("Abies balsamea sec. ", pager.getRecords().get(0).getEntity().getTitleCache());
|
233
|
|
234
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Бальзам", null, Arrays.asList(new Language[]{Language.GERMAN()}), null, null, null, null);
|
235
|
Assert.assertEquals("Expecting no entity", Integer.valueOf(0), pager.getCount());
|
236
|
|
237
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, Arrays.asList(new Language[]{Language.GERMAN(), Language.RUSSIAN()}), null, null, null, null);
|
238
|
Assert.assertEquals("Expecting one entity", Integer.valueOf(1), pager.getCount());
|
239
|
Assert.assertEquals("Abies balsamea sec. ", pager.getRecords().get(0).getEntity().getTitleCache());
|
240
|
}
|
241
|
|
242
|
@SuppressWarnings("rawtypes")
|
243
|
@Test
|
244
|
@DataSet
|
245
|
public final void testFindByDescriptionElementFullText_modify_DescriptionElement() throws CorruptIndexException, IOException, ParseException {
|
246
|
|
247
|
refreshLuceneIndex();
|
248
|
|
249
|
Pager<SearchResult<TaxonBase>> pager;
|
250
|
//
|
251
|
// modify the DescriptionElement
|
252
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, Arrays.asList(new Language[]{Language.GERMAN(), Language.RUSSIAN()}), null, null, null, null);
|
253
|
Document indexDocument = pager.getRecords().get(0).getDoc();
|
254
|
String[] descriptionElementUuidStr = indexDocument.getValues("uuid");
|
255
|
String[] inDescriptionUuidStr = indexDocument.getValues("inDescription.uuid");
|
256
|
// is only one uuid!
|
257
|
DescriptionElementBase textData = descriptionService.getDescriptionElementByUuid(UUID.fromString(descriptionElementUuidStr[0]));
|
258
|
|
259
|
((TextData)textData).removeText(Language.GERMAN());
|
260
|
((TextData)textData).putText(Language.SPANISH_CASTILIAN(), "abeto balsámico");
|
261
|
|
262
|
descriptionService.saveDescriptionElement(textData);
|
263
|
commitAndStartNewTransaction(null);
|
264
|
// printDataSet(System.out, new String[] {
|
265
|
// "DESCRIPTIONELEMENTBASE", "LANGUAGESTRING", "DESCRIPTIONELEMENTBASE_LANGUAGESTRING" }
|
266
|
// );
|
267
|
|
268
|
//
|
269
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, Arrays.asList(new Language[]{Language.GERMAN(), Language.RUSSIAN()}), null, null, null, null);
|
270
|
Assert.assertEquals("The german 'Balsam-Tanne' TextData should no longer be indexed", Integer.valueOf(0), pager.getCount());
|
271
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "abeto", null, Arrays.asList(new Language[]{Language.SPANISH_CASTILIAN()}), null, null, null, null);
|
272
|
Assert.assertEquals("expecting to find the SPANISH_CASTILIAN 'abeto balsámico'", Integer.valueOf(1), pager.getCount());
|
273
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "balsámico", null, null, null, null, null, null);
|
274
|
Assert.assertEquals("expecting to find the SPANISH_CASTILIAN 'abeto balsámico'", Integer.valueOf(1), pager.getCount());
|
275
|
|
276
|
//
|
277
|
// modify the DescriptionElement via the Description object
|
278
|
DescriptionBase description = descriptionService.find(UUID.fromString(inDescriptionUuidStr[0]));
|
279
|
Set<DescriptionElementBase> elements = description.getElements();
|
280
|
for( DescriptionElementBase elm : elements){
|
281
|
if(elm.getUuid().toString().equals(descriptionElementUuidStr[0])){
|
282
|
((TextData)elm).removeText(Language.SPANISH_CASTILIAN());
|
283
|
((TextData)elm).putText(Language.POLISH(), "Jodła balsamiczna");
|
284
|
}
|
285
|
}
|
286
|
descriptionService.saveOrUpdate(description);
|
287
|
commitAndStartNewTransaction(null);
|
288
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "abeto", null, Arrays.asList(new Language[]{Language.SPANISH_CASTILIAN()}), null, null, null, null);
|
289
|
Assert.assertEquals("The spanish 'abeto balsámico' TextData should no longer be indexed", Integer.valueOf(0), pager.getCount());
|
290
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "balsamiczna", null, Arrays.asList(new Language[]{Language.POLISH()}), null, null, null, null);
|
291
|
Assert.assertEquals("expecting to find the POLISH 'Jodła balsamiczna'", Integer.valueOf(1), pager.getCount());
|
292
|
}
|
293
|
|
294
|
@SuppressWarnings("rawtypes")
|
295
|
@Test
|
296
|
@DataSet
|
297
|
public final void testFindByDescriptionElementFullText_modify_Taxon() throws CorruptIndexException, IOException, ParseException {
|
298
|
|
299
|
refreshLuceneIndex();
|
300
|
|
301
|
Pager<SearchResult<TaxonBase>> pager;
|
302
|
Taxon t_abies_balsamea = (Taxon)taxonService.find(UUID.fromString(ABIES_BALSAMEA_UUID));
|
303
|
TaxonDescription d_abies_balsamea = (TaxonDescription)descriptionService.find(UUID.fromString(D_ABIES_ALBA_UUID));
|
304
|
|
305
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, Arrays.asList(new Language[]{Language.GERMAN()}), null, null, null, null);
|
306
|
Assert.assertEquals("expecting to find the GERMAN 'Balsam-Tanne'", Integer.valueOf(1), pager.getCount());
|
307
|
|
308
|
// exchange the Taxon with another one via the Taxon object
|
309
|
// 1.) remove existing description:
|
310
|
t_abies_balsamea.removeDescription(d_abies_balsamea);
|
311
|
|
312
|
taxonService.saveOrUpdate(t_abies_balsamea);
|
313
|
commitAndStartNewTransaction(null);
|
314
|
|
315
|
|
316
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, Arrays.asList(new Language[]{Language.GERMAN()}), null, null, null, null);
|
317
|
Assert.assertEquals("'Balsam-Tanne' should no longer be found", Integer.valueOf(0), pager.getCount());
|
318
|
|
319
|
// 2.) create new description and add to taxon:
|
320
|
TaxonDescription d_abies_balsamea_new = TaxonDescription.NewInstance();
|
321
|
d_abies_balsamea_new
|
322
|
.addElement(TextData
|
323
|
.NewInstance(
|
324
|
"Die Balsamtanne ist mit bis zu 30 m Höhe ein mittelgroßer Baum und kann bis zu 200 Jahre alt werden",
|
325
|
Language.GERMAN(), null));
|
326
|
t_abies_balsamea.addDescription(d_abies_balsamea_new);
|
327
|
taxonService.saveOrUpdate(t_abies_balsamea);
|
328
|
commitAndStartNewTransaction(null);
|
329
|
|
330
|
printDataSet(System.out, new String[] {
|
331
|
"DESCRIPTIONBASE"
|
332
|
});
|
333
|
|
334
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "mittelgroßer Baum", null, Arrays.asList(new Language[]{Language.GERMAN()}), null, null, null, null);
|
335
|
Assert.assertEquals("the taxon should be found via the new Description", Integer.valueOf(1), pager.getCount());
|
336
|
}
|
337
|
|
338
|
@SuppressWarnings("rawtypes")
|
339
|
@Test
|
340
|
@DataSet
|
341
|
public final void testFindByDescriptionElementFullText_modify_Classification() throws CorruptIndexException, IOException, ParseException {
|
342
|
|
343
|
refreshLuceneIndex();
|
344
|
|
345
|
Pager<SearchResult<TaxonBase>> pager;
|
346
|
|
347
|
// put taxon into other classification, new taxon node
|
348
|
Classification classification = classificationService.find(UUID.fromString(CLASSIFICATION_UUID));
|
349
|
Classification alternateClassification = classificationService.find(UUID.fromString(CLASSIFICATION_ALT_UUID));
|
350
|
|
351
|
// TODO: why is the test failing when the childNode is already retrieved here, and not after the following four lines?
|
352
|
//TaxonNode childNode = classification.getChildNodes().iterator().next();
|
353
|
|
354
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", null, Arrays.asList(new Language[]{Language.GERMAN()}), null, null, null, null);
|
355
|
Assert.assertEquals("expecting to find the GERMAN 'Balsam-Tanne' even if filtering by classification", Integer.valueOf(1), pager.getCount());
|
356
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", alternateClassification, Arrays.asList(new Language[]{Language.GERMAN()}), null, null, null, null);
|
357
|
Assert.assertEquals("GERMAN 'Balsam-Tanne' should NOT be found in other classification", Integer.valueOf(0), pager.getCount());
|
358
|
|
359
|
// check for the right taxon node
|
360
|
TaxonNode childNode = classification.getChildNodes().iterator().next();
|
361
|
Assert.assertEquals("expecting Abies balsamea sec.", childNode.getTaxon().getUuid().toString(), ABIES_BALSAMEA_UUID);
|
362
|
Assert.assertEquals("expecting default classification", childNode.getClassification().getUuid().toString(), CLASSIFICATION_UUID);
|
363
|
|
364
|
// moving the taxon around
|
365
|
alternateClassification.addChildNode(childNode, null, null, null);
|
366
|
classificationService.saveOrUpdate(alternateClassification);
|
367
|
commitAndStartNewTransaction(null);
|
368
|
|
369
|
// printDataSet(System.out, new String[] {
|
370
|
// "TAXONBASE", "TAXONNODE", "CLASSIFICATION"
|
371
|
// });
|
372
|
|
373
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", alternateClassification, Arrays.asList(new Language[]{Language.GERMAN()}), null, null, null, null);
|
374
|
Assert.assertEquals("GERMAN 'Balsam-Tanne' should now be found in other classification", Integer.valueOf(1), pager.getCount());
|
375
|
|
376
|
classification.getChildNodes().clear();
|
377
|
classificationService.saveOrUpdate(classification);
|
378
|
commitAndStartNewTransaction(null);
|
379
|
|
380
|
pager = taxonService.findByDescriptionElementFullText(TextData.class, "Balsam-Tanne", classification, Arrays.asList(new Language[]{Language.GERMAN()}), null, null, null, null);
|
381
|
Assert.assertEquals("Now the GERMAN 'Balsam-Tanne' should NOT be found in original classification", Integer.valueOf(0), pager.getCount());
|
382
|
|
383
|
}
|
384
|
|
385
|
@SuppressWarnings("rawtypes")
|
386
|
@Test
|
387
|
@DataSet
|
388
|
public final void testFindByDescriptionElementFullText_CategoricalData() throws CorruptIndexException, IOException, ParseException {
|
389
|
|
390
|
// add CategoricalData
|
391
|
DescriptionBase d_abies_balsamea = descriptionService.find(UUID.fromString(D_ABIES_ALBA_UUID));
|
392
|
// Categorical data
|
393
|
CategoricalData cdata = CategoricalData.NewInstance();
|
394
|
cdata.setFeature(Feature.DESCRIPTION());
|
395
|
State state = State.NewInstance("green", "green", "gn");
|
396
|
|
397
|
StateData statedata = StateData.NewInstance(state);
|
398
|
statedata.putModifyingText(Language.ENGLISH(), "always, even during winter");
|
399
|
cdata.addState(statedata);
|
400
|
d_abies_balsamea.addElement(cdata);
|
401
|
|
402
|
termService.save(state);
|
403
|
descriptionService.save(d_abies_balsamea);
|
404
|
|
405
|
commitAndStartNewTransaction(null);
|
406
|
|
407
|
// printDataSet(System.out, new String[] {
|
408
|
// "STATEDATA", "STATEDATA_DEFINEDTERMBASE", "STATEDATA_LANGUAGESTRING", "LANGUAGESTRING"});
|
409
|
|
410
|
refreshLuceneIndex();
|
411
|
|
412
|
Pager<SearchResult<TaxonBase>> pager;
|
413
|
pager = taxonService.findByDescriptionElementFullText(CategoricalData.class, "green", null, null, null, null, null, null);
|
414
|
Assert.assertEquals("Expecting one entity", Integer.valueOf(1), pager.getCount());
|
415
|
Assert.assertEquals("Abies balsamea sec. ", pager.getRecords().get(0).getEntity().getTitleCache());
|
416
|
Assert.assertEquals("Abies balsamea sec. ", pager.getRecords().get(0).getDoc().get("inDescription.taxon.titleCache"));
|
417
|
|
418
|
|
419
|
//TODO modify the StateData
|
420
|
TaxonBase taxon = pager.getRecords().get(0).getEntity();
|
421
|
|
422
|
String newName = "Quercus robur";
|
423
|
taxon.setTitleCache(newName + " sec. ", true);
|
424
|
|
425
|
taxonService.saveOrUpdate(taxon);
|
426
|
commitAndStartNewTransaction(null);
|
427
|
|
428
|
taxon = taxonService.load(taxon.getUuid());
|
429
|
Assert.assertEquals(newName + " sec. ", taxon.getTitleCache());
|
430
|
}
|
431
|
|
432
|
/**
|
433
|
*
|
434
|
*/
|
435
|
private void refreshLuceneIndex() {
|
436
|
|
437
|
commitAndStartNewTransaction(null);
|
438
|
indexer.purge();
|
439
|
indexer.reindex();
|
440
|
commitAndStartNewTransaction(null);
|
441
|
}
|
442
|
|
443
|
@SuppressWarnings("rawtypes")
|
444
|
@Test
|
445
|
@DataSet
|
446
|
public final void testFindByCommonNameHqlBenchmark() throws CorruptIndexException, IOException, ParseException {
|
447
|
|
448
|
// printDataSet(System.err, new String[] { "TaxonBase" });
|
449
|
|
450
|
createRandomTaxonWithCommonName(NUM_OF_NEW_RADOM_ENTITIES);
|
451
|
|
452
|
ITaxonServiceConfigurator configurator = new TaxonServiceConfiguratorImpl();
|
453
|
configurator.setTitleSearchString("Weiß%");
|
454
|
configurator.setMatchMode(MatchMode.BEGINNING);
|
455
|
configurator.setDoTaxa(false);
|
456
|
configurator.setDoSynonyms(false);
|
457
|
configurator.setDoNamesWithoutTaxa(false);
|
458
|
configurator.setDoTaxaByCommonNames(true);
|
459
|
|
460
|
Pager<IdentifiableEntity> pager;
|
461
|
|
462
|
long startMillis = System.currentTimeMillis();
|
463
|
for (int indx = 0; indx < BENCHMARK_ROUNDS; indx++) {
|
464
|
pager = taxonService.findTaxaAndNames(configurator);
|
465
|
if (logger.isDebugEnabled()) {
|
466
|
logger.debug("[" + indx + "]" + pager.getRecords().get(0).getTitleCache());
|
467
|
}
|
468
|
}
|
469
|
double duration = ((double) (System.currentTimeMillis() - startMillis)) / BENCHMARK_ROUNDS;
|
470
|
logger.info("Benchmark result - [find taxon by CommonName via HQL] : " + duration + "ms (" + BENCHMARK_ROUNDS + " benchmark rounds )");
|
471
|
}
|
472
|
|
473
|
@SuppressWarnings("rawtypes")
|
474
|
@Test
|
475
|
@DataSet
|
476
|
public final void testFindByCommonNameLuceneBenchmark() throws CorruptIndexException, IOException, ParseException {
|
477
|
|
478
|
// printDataSet(System.err, new String[] { "TaxonBase" });
|
479
|
|
480
|
createRandomTaxonWithCommonName(NUM_OF_NEW_RADOM_ENTITIES);
|
481
|
|
482
|
refreshLuceneIndex();
|
483
|
|
484
|
Pager<SearchResult<TaxonBase>> pager;
|
485
|
|
486
|
long startMillis = System.currentTimeMillis();
|
487
|
for (int indx = 0; indx < BENCHMARK_ROUNDS; indx++) {
|
488
|
pager = taxonService.findByDescriptionElementFullText(CommonTaxonName.class, "Weiß*", null, null, null, null, null, null);
|
489
|
if (logger.isDebugEnabled()) {
|
490
|
logger.debug("[" + indx + "]" + pager.getRecords().get(0).getEntity().getTitleCache());
|
491
|
}
|
492
|
}
|
493
|
double duration = ((double) (System.currentTimeMillis() - startMillis)) / BENCHMARK_ROUNDS;
|
494
|
logger.info("Benchmark result - [find taxon by CommonName via lucene] : " + duration + "ms (" + BENCHMARK_ROUNDS + " benchmark rounds )");
|
495
|
}
|
496
|
|
497
|
// @Test
|
498
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="BlankDataSet.xml")
|
499
|
public final void createDataSet() throws FileNotFoundException {
|
500
|
|
501
|
Classification classification = Classification.NewInstance("European Abies");
|
502
|
classification.setUuid(UUID.fromString(CLASSIFICATION_UUID));
|
503
|
Classification alternativeClassification = Classification.NewInstance("Abies alternative");
|
504
|
classification.setUuid(UUID.fromString(CLASSIFICATION_ALT_UUID));
|
505
|
classificationService.save(classification);
|
506
|
classificationService.save(alternativeClassification);
|
507
|
|
508
|
Reference sec = ReferenceFactory.newBook();
|
509
|
referenceService.save(sec);
|
510
|
|
511
|
BotanicalName n_abies = BotanicalName.NewInstance(Rank.GENUS());
|
512
|
n_abies.setNameCache("Abies", true);
|
513
|
Taxon t_abies = Taxon.NewInstance(n_abies, sec);
|
514
|
taxonService.save(t_abies);
|
515
|
|
516
|
BotanicalName n_abies_alba = BotanicalName.NewInstance(Rank.SPECIES());
|
517
|
n_abies_alba.setNameCache("Abies alba", true);
|
518
|
Taxon t_abies_alba = Taxon.NewInstance(n_abies_alba, sec);
|
519
|
taxonService.save(t_abies_alba);
|
520
|
|
521
|
BotanicalName n_abies_balsamea = BotanicalName.NewInstance(Rank.SPECIES());
|
522
|
n_abies_balsamea.setNameCache("Abies balsamea", true);
|
523
|
Taxon t_abies_balsamea = Taxon.NewInstance(n_abies_balsamea, sec);
|
524
|
t_abies_balsamea.setUuid(UUID.fromString(ABIES_BALSAMEA_UUID));
|
525
|
taxonService.save(t_abies_balsamea);
|
526
|
|
527
|
BotanicalName n_abies_grandis = BotanicalName.NewInstance(Rank.SPECIES());
|
528
|
n_abies_grandis.setNameCache("Abies grandis", true);
|
529
|
Taxon t_abies_grandis = Taxon.NewInstance(n_abies_grandis, sec);
|
530
|
taxonService.save(t_abies_grandis);
|
531
|
|
532
|
BotanicalName n_abies_kawakamii = BotanicalName.NewInstance(Rank.SPECIES());
|
533
|
n_abies_kawakamii.setNameCache("Abies kawakamii", true);
|
534
|
Taxon t_abies_kawakamii = Taxon.NewInstance(n_abies_kawakamii, sec);
|
535
|
taxonService.save(t_abies_kawakamii);
|
536
|
|
537
|
BotanicalName n_abies_subalpina = BotanicalName.NewInstance(Rank.SPECIES());
|
538
|
n_abies_subalpina.setNameCache("Abies subalpina", true);
|
539
|
Synonym s_abies_subalpina = Synonym.NewInstance(n_abies_subalpina, sec);
|
540
|
taxonService.save(s_abies_subalpina);
|
541
|
|
542
|
BotanicalName n_abies_lasiocarpa = BotanicalName.NewInstance(Rank.SPECIES());
|
543
|
n_abies_lasiocarpa.setNameCache("Abies lasiocarpa", true);
|
544
|
Taxon t_abies_lasiocarpa = Taxon.NewInstance(n_abies_lasiocarpa, sec);
|
545
|
t_abies_lasiocarpa.addSynonym(s_abies_subalpina, SynonymRelationshipType.SYNONYM_OF());
|
546
|
taxonService.save(t_abies_lasiocarpa);
|
547
|
|
548
|
// add taxa to classifications
|
549
|
classification.addChildTaxon(t_abies_balsamea, null, null, null);
|
550
|
alternativeClassification.addChildTaxon(t_abies_lasiocarpa, null, null, null);
|
551
|
classificationService.saveOrUpdate(classification);
|
552
|
classificationService.saveOrUpdate(alternativeClassification);
|
553
|
|
554
|
// t_abies_balsamea.se
|
555
|
|
556
|
//
|
557
|
// Description
|
558
|
//
|
559
|
TaxonDescription d_abies_alba = TaxonDescription.NewInstance(t_abies_alba);
|
560
|
|
561
|
d_abies_alba.setUuid(UUID.fromString(D_ABIES_ALBA_UUID));
|
562
|
// CommonTaxonName
|
563
|
d_abies_alba.addElement(CommonTaxonName.NewInstance("Weißtanne", Language.GERMAN()));
|
564
|
d_abies_alba.addElement(CommonTaxonName.NewInstance("silver fir", Language.ENGLISH()));
|
565
|
// TextData
|
566
|
TaxonDescription d_abies_balsamea = TaxonDescription.NewInstance(t_abies_balsamea);
|
567
|
d_abies_balsamea
|
568
|
.addElement(TextData
|
569
|
.NewInstance(
|
570
|
"Die Balsam-Tanne (Abies balsamea) ist eine Pflanzenart aus der Gattung der Tannen (Abies). Sie wächst im nordöstlichen Nordamerika, wo sie sowohl Tief- als auch Bergland besiedelt. Sie gilt als relativ anspruchslos gegenüber dem Standort und ist frosthart. In vielen Teilen des natürlichen Verbreitungsgebietes stellt sie die Klimaxbaumart dar.",
|
571
|
Language.GERMAN(), null));
|
572
|
d_abies_balsamea
|
573
|
.addElement(TextData
|
574
|
.NewInstance(
|
575
|
"Бальзам ньыв (лат. Abies balsamea) – быдмассэзлӧн пожум котырись ньыв увтырын торья вид. Ньывпуыс быдмӧ 14–20 метра вылына да овлӧ 10–60 см кыза диаметрын. Ньывпу пантасьӧ Ойвыв Америкаын.",
|
576
|
Language.RUSSIAN(), null));
|
577
|
setComplete();
|
578
|
endTransaction();
|
579
|
|
580
|
printDataSet(new FileOutputStream("TaxonServiceSearchTest.xml"), new String[] {
|
581
|
"TAXONBASE", "TAXONNAMEBASE", "SYNONYMRELATIONSHIP",
|
582
|
"REFERENCE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONBASE",
|
583
|
"AGENTBASE", "HOMOTYPICALGROUP", "CLASSIFICATION", "CLASSIFICATION_TAXONNODE", "TAXONNODE",
|
584
|
"LANGUAGESTRING", "DESCRIPTIONELEMENTBASE_LANGUAGESTRING" });
|
585
|
|
586
|
}
|
587
|
|
588
|
/**
|
589
|
* @param numberOfNew
|
590
|
*
|
591
|
*/
|
592
|
private void createRandomTaxonWithCommonName(int numberOfNew) {
|
593
|
|
594
|
logger.debug(String.format("creating %1$s random taxan with CommonName", numberOfNew));
|
595
|
|
596
|
Reference sec = ReferenceFactory.newBook();
|
597
|
referenceService.save(sec);
|
598
|
|
599
|
for (int i = numberOfNew; i < numberOfNew; i++) {
|
600
|
RandomStringUtils.randomAlphabetic(10);
|
601
|
String radomName = RandomStringUtils.randomAlphabetic(5) + " " + RandomStringUtils.randomAlphabetic(10);
|
602
|
String radomCommonName = RandomStringUtils.randomAlphabetic(10);
|
603
|
|
604
|
BotanicalName name = BotanicalName.NewInstance(Rank.SPECIES());
|
605
|
name.setNameCache(radomName, true);
|
606
|
Taxon taxon = Taxon.NewInstance(name, sec);
|
607
|
taxonService.save(taxon);
|
608
|
|
609
|
TaxonDescription description = TaxonDescription.NewInstance(taxon);
|
610
|
description.addElement(CommonTaxonName.NewInstance(radomCommonName, Language.GERMAN()));
|
611
|
descriptionService.save(description);
|
612
|
}
|
613
|
|
614
|
commitAndStartNewTransaction(null);
|
615
|
|
616
|
}
|
617
|
|
618
|
}
|