1
|
/**
|
2
|
* Copyright (C) 2007 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.persistence.dao.hibernate.description;
|
11
|
|
12
|
import static org.junit.Assert.assertEquals;
|
13
|
import static org.junit.Assert.assertFalse;
|
14
|
import static org.junit.Assert.assertNotNull;
|
15
|
import static org.junit.Assert.assertTrue;
|
16
|
|
17
|
import java.util.ArrayList;
|
18
|
import java.util.HashSet;
|
19
|
import java.util.Iterator;
|
20
|
import java.util.List;
|
21
|
import java.util.Map;
|
22
|
import java.util.Set;
|
23
|
import java.util.UUID;
|
24
|
|
25
|
import org.hibernate.Hibernate;
|
26
|
import org.junit.Before;
|
27
|
import org.junit.Test;
|
28
|
import org.unitils.dbunit.annotation.DataSet;
|
29
|
import org.unitils.spring.annotation.SpringBeanByType;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.common.Language;
|
32
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
33
|
import eu.etaxonomy.cdm.model.description.AbsenceTerm;
|
34
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
35
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
36
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
37
|
import eu.etaxonomy.cdm.model.description.Feature;
|
38
|
import eu.etaxonomy.cdm.model.description.PresenceTerm;
|
39
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
40
|
import eu.etaxonomy.cdm.model.description.TextData;
|
41
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
42
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
43
|
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
|
44
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
|
45
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
46
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
47
|
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
|
48
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
49
|
|
50
|
@DataSet
|
51
|
public class DescriptionDaoHibernateImplTest extends CdmIntegrationTest {
|
52
|
|
53
|
@SpringBeanByType
|
54
|
IDescriptionDao descriptionDao;
|
55
|
|
56
|
@SpringBeanByType
|
57
|
IDefinedTermDao definedTermDao;
|
58
|
|
59
|
@SpringBeanByType
|
60
|
ITaxonDao taxonDao;
|
61
|
|
62
|
private Set<NamedArea> namedAreas;
|
63
|
private Set<Feature> features;
|
64
|
|
65
|
private UUID northernAmericaUuid;
|
66
|
private UUID southernAmericaUuid;
|
67
|
private UUID antarcticaUuid;
|
68
|
|
69
|
private UUID uuid;
|
70
|
|
71
|
private UUID taxonSphingidaeUuid;
|
72
|
|
73
|
@Before
|
74
|
public void setUp() {
|
75
|
uuid = UUID.fromString("5f3265ed-68ad-4ec3-826f-0d29d25986b9");
|
76
|
|
77
|
namedAreas = new HashSet<NamedArea>();
|
78
|
northernAmericaUuid = UUID.fromString("2757e726-d897-4546-93bd-7951d203bf6f");
|
79
|
southernAmericaUuid = UUID.fromString("6310b3ba-96f4-4855-bb5b-326e7af188ea");
|
80
|
antarcticaUuid = UUID.fromString("791b3aa0-54dd-4bed-9b68-56b4680aad0c");
|
81
|
taxonSphingidaeUuid = UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
|
82
|
|
83
|
features = new HashSet<Feature>();
|
84
|
}
|
85
|
|
86
|
|
87
|
@Test
|
88
|
public void testCountByDistribution() {
|
89
|
NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
|
90
|
NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
|
91
|
NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
|
92
|
|
93
|
assert northernAmerica != null : "term must exist";
|
94
|
assert southernAmerica != null : "term must exist";
|
95
|
assert antarctica != null : "term must exist";
|
96
|
|
97
|
namedAreas.add(northernAmerica);
|
98
|
namedAreas.add(southernAmerica);
|
99
|
namedAreas.add(antarctica);
|
100
|
|
101
|
int numberOfDescriptions = descriptionDao.countDescriptionByDistribution(namedAreas, null);
|
102
|
assertEquals("countDescriptionsByDistribution should return 23",23,numberOfDescriptions);
|
103
|
}
|
104
|
|
105
|
@Test
|
106
|
public void testCountByDistributionWithStatus() {
|
107
|
NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
|
108
|
NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
|
109
|
NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
|
110
|
|
111
|
assert northernAmerica != null : "term must exist";
|
112
|
assert southernAmerica != null : "term must exist";
|
113
|
assert antarctica != null : "term must exist";
|
114
|
|
115
|
namedAreas.add(northernAmerica);
|
116
|
namedAreas.add(southernAmerica);
|
117
|
namedAreas.add(antarctica);
|
118
|
|
119
|
int numberOfDescriptions = descriptionDao.countDescriptionByDistribution(namedAreas, PresenceTerm.PRESENT());
|
120
|
assertEquals("countDescriptionsByDistribution should return 20",20,numberOfDescriptions);
|
121
|
}
|
122
|
|
123
|
@Test
|
124
|
public void testGetByDistribution() {
|
125
|
NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
|
126
|
NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
|
127
|
NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
|
128
|
|
129
|
assert northernAmerica != null : "term must exist";
|
130
|
assert southernAmerica != null : "term must exist";
|
131
|
assert antarctica != null : "term must exist";
|
132
|
|
133
|
namedAreas.add(northernAmerica);
|
134
|
namedAreas.add(southernAmerica);
|
135
|
namedAreas.add(antarctica);
|
136
|
|
137
|
List<String> propertyPaths = new ArrayList<String>();
|
138
|
propertyPaths.add("taxon");
|
139
|
|
140
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
141
|
orderHints.add(new OrderHint("titleCache",SortOrder.ASCENDING));
|
142
|
|
143
|
List<TaxonDescription> descriptions = descriptionDao.searchDescriptionByDistribution(namedAreas, null, 10,2,orderHints,propertyPaths);
|
144
|
assertNotNull("searchDescriptionByDistribution should return a List",descriptions);
|
145
|
assertFalse("searchDescriptionsByDistribution should not be empty",descriptions.isEmpty());
|
146
|
assertEquals("searchDescriptionsByDistribution should return 3 elements",3,descriptions.size());
|
147
|
assertTrue("TaxonDescription.taxon should be initialized",Hibernate.isInitialized(descriptions.get(0).getTaxon()));
|
148
|
assertEquals("Sphingidae Linnaeus, 1758 sec. cate-sphingidae.org should come first","Sphingidae Linnaeus, 1758 sec. cate-sphingidae.org",descriptions.get(0).getTitleCache());
|
149
|
assertEquals("Sphinx Linnaeus, 1758 sec. cate-sphingidae.org should come last","Sphinx Linnaeus, 1758 sec. cate-sphingidae.org",descriptions.get(2).getTitleCache());
|
150
|
}
|
151
|
|
152
|
@Test
|
153
|
public void testGetByDistributionWithStatus() {
|
154
|
NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
|
155
|
NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
|
156
|
NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
|
157
|
|
158
|
assert northernAmerica != null : "term must exist";
|
159
|
assert southernAmerica != null : "term must exist";
|
160
|
assert antarctica != null : "term must exist";
|
161
|
|
162
|
namedAreas.add(northernAmerica);
|
163
|
namedAreas.add(southernAmerica);
|
164
|
namedAreas.add(antarctica);
|
165
|
|
166
|
List<TaxonDescription> descriptions = descriptionDao.searchDescriptionByDistribution(namedAreas, AbsenceTerm.ABSENT(), 10,0,null,null);
|
167
|
assertNotNull("searchDescriptionByDistribution should return a List",descriptions);
|
168
|
assertFalse("searchDescriptionsByDistribution should not be empty",descriptions.isEmpty());
|
169
|
assertEquals("searchDescriptionsByDistribution should return 3 elements",3,descriptions.size());
|
170
|
}
|
171
|
|
172
|
@Test
|
173
|
public void testCountDescriptionsWithText() {
|
174
|
int numberOfDescriptions = descriptionDao.countDescriptions(TaxonDescription.class, null, true, null);
|
175
|
|
176
|
assertNotNull("countDescriptions should return a 2",numberOfDescriptions);
|
177
|
}
|
178
|
|
179
|
@Test
|
180
|
public void testListDescriptionsWithText() {
|
181
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
182
|
orderHints.add(new OrderHint("titleCache",SortOrder.ASCENDING));
|
183
|
List<String> propertyPaths = new ArrayList<String>();
|
184
|
propertyPaths.add("taxon");
|
185
|
List<DescriptionBase> descriptions = descriptionDao.listDescriptions(TaxonDescription.class, null, true, null,null,null,orderHints,propertyPaths);
|
186
|
|
187
|
assertNotNull("listDescriptions should return a List",descriptions);
|
188
|
assertFalse("listDescriptions should not be empty", descriptions.isEmpty());
|
189
|
assertEquals("listDescriptions should return 2 descriptions",2,descriptions.size());
|
190
|
}
|
191
|
|
192
|
@Test
|
193
|
public void testCountDescriptionsWithTextAndFeatures() {
|
194
|
features.add(Feature.ECOLOGY());
|
195
|
int numberOfDescriptions = descriptionDao.countDescriptions(TaxonDescription.class, null, true, features);
|
196
|
|
197
|
assertNotNull("countDescriptions should return a 1",numberOfDescriptions);
|
198
|
}
|
199
|
|
200
|
@Test
|
201
|
public void testListDescriptionsWithTextAndFeatures() {
|
202
|
assert Feature.ECOLOGY() != null;
|
203
|
features.add(Feature.ECOLOGY());
|
204
|
List<DescriptionBase> descriptions = descriptionDao.listDescriptions(TaxonDescription.class, null, true, features,null,null,null,null);
|
205
|
|
206
|
assertNotNull("listDescriptions should return a List",descriptions);
|
207
|
assertFalse("listDescriptions should not be empty", descriptions.isEmpty());
|
208
|
assertEquals("listDescriptions should return 1 descriptions",1,descriptions.size());
|
209
|
}
|
210
|
|
211
|
@Test
|
212
|
public void testCountDescriptionElements() {
|
213
|
DescriptionBase description = descriptionDao.findByUuid(uuid);
|
214
|
assert description != null : "description must exist";
|
215
|
|
216
|
int numberOfDescriptionElements = descriptionDao.countDescriptionElements(description, null, TextData.class);
|
217
|
|
218
|
assertEquals("countDescriptionElements should return 2",2,numberOfDescriptionElements);
|
219
|
}
|
220
|
|
221
|
@Test
|
222
|
public void testGetDescriptionElements() {
|
223
|
DescriptionBase description = descriptionDao.findByUuid(uuid);
|
224
|
assert description != null : "description must exist";
|
225
|
|
226
|
List<String> propertyPaths = new ArrayList<String>();
|
227
|
propertyPaths.add("multilanguageText");
|
228
|
propertyPaths.add("media");
|
229
|
propertyPaths.add("sources.citation");
|
230
|
propertyPaths.add("feature");
|
231
|
|
232
|
List<DescriptionElementBase> elements = descriptionDao.getDescriptionElements(description, null, TextData.class, null, null,propertyPaths);
|
233
|
|
234
|
for (DescriptionElementBase descElB: elements){
|
235
|
if (descElB instanceof TextData){
|
236
|
Map<Language, LanguageString> multiLanguage = ((TextData)descElB).getMultilanguageText();
|
237
|
LanguageString defaultString = multiLanguage.get(Language.DEFAULT());
|
238
|
|
239
|
}
|
240
|
}
|
241
|
Iterator<DescriptionElementBase> elements2 = description.getElements().iterator();
|
242
|
while(elements2.hasNext()){
|
243
|
DescriptionElementBase element = elements2.next();
|
244
|
if (element instanceof TextData){
|
245
|
Map<Language, LanguageString> multiLanguage = ((TextData)element).getMultilanguageText();
|
246
|
LanguageString defaultString = multiLanguage.get(Language.DEFAULT());
|
247
|
defaultString.setText("blablub");
|
248
|
}
|
249
|
}
|
250
|
elements = descriptionDao.getDescriptionElements(description, null, TextData.class, null, null,propertyPaths);
|
251
|
|
252
|
DescriptionElementBase element34 = null;
|
253
|
for (DescriptionElementBase descElB: elements){
|
254
|
if (descElB instanceof TextData){
|
255
|
Map<Language, LanguageString> multiLanguage = ((TextData)descElB).getMultilanguageText();
|
256
|
LanguageString defaultString = multiLanguage.get(Language.DEFAULT());
|
257
|
//???
|
258
|
}
|
259
|
if (descElB.getId() == 34){
|
260
|
element34 = descElB;
|
261
|
}
|
262
|
}
|
263
|
|
264
|
assertNotNull("getDescriptionElements should return a List", elements);
|
265
|
assertFalse("getDescriptionElements should not be empty",elements.isEmpty());
|
266
|
assertEquals("getDescriptionElement should return 2 elements",2,elements.size());
|
267
|
assertNotNull("Description Element with ID 34 should be part of the list",element34);
|
268
|
assertTrue("ReferencedEntityBase.citation should be initialized",Hibernate.isInitialized(element34.getSources().iterator().next().getCitation()));
|
269
|
assertTrue("DescriptionElementBase.feature should be initialized",Hibernate.isInitialized(element34.getFeature()));
|
270
|
assertTrue("DescriptionElementBase.media should be initialized",Hibernate.isInitialized(element34.getMedia()));
|
271
|
assertTrue("TextData.multilanguageText should be initialized",Hibernate.isInitialized(((TextData)element34).getMultilanguageText()));
|
272
|
}
|
273
|
|
274
|
@Test
|
275
|
public void testCountDescriptionElementsFeature() {
|
276
|
features.add(Feature.ECOLOGY());
|
277
|
DescriptionBase description = descriptionDao.findByUuid(uuid);
|
278
|
assert description != null : "description must exist";
|
279
|
|
280
|
int numberOfDescriptionElements = descriptionDao.countDescriptionElements(description, features, TextData.class);
|
281
|
|
282
|
assertEquals("countDescriptionElements should return 1",1,numberOfDescriptionElements);
|
283
|
}
|
284
|
|
285
|
@Test
|
286
|
public void testGetDescriptionElementsByFeature() {
|
287
|
features.add(Feature.ECOLOGY());
|
288
|
DescriptionBase description = descriptionDao.findByUuid(uuid);
|
289
|
assert description != null : "description must exist";
|
290
|
|
291
|
List<DescriptionElementBase> elements = descriptionDao.getDescriptionElements(description, features, TextData.class, null, null,null);
|
292
|
|
293
|
assertNotNull("getDescriptionElements should return a List");
|
294
|
assertFalse("getDescriptionElements should not be empty",elements.isEmpty());
|
295
|
assertEquals("getDescriptionElement should return 1 elements",1,elements.size());
|
296
|
}
|
297
|
|
298
|
@Test
|
299
|
public void testGetDescriptionElementForTaxon() {
|
300
|
|
301
|
Taxon taxonSphingidae = (Taxon) taxonDao.load(taxonSphingidaeUuid);
|
302
|
assert taxonSphingidae != null : "taxon must exist";
|
303
|
|
304
|
// 1.
|
305
|
|
306
|
List<DescriptionElementBase> elements1 = descriptionDao.getDescriptionElementForTaxon(
|
307
|
taxonSphingidae , null, null, null, 0, null);
|
308
|
|
309
|
assertNotNull("getDescriptionElementForTaxon should return a List", elements1);
|
310
|
assertFalse("getDescriptionElementForTaxon should not be empty",elements1.isEmpty());
|
311
|
assertEquals("getDescriptionElementForTaxon should return 1 elements",1,elements1.size());
|
312
|
|
313
|
// 2.
|
314
|
|
315
|
List<DescriptionElementBase> elements2 = descriptionDao.getDescriptionElementForTaxon(
|
316
|
taxonSphingidae , null, DescriptionElementBase.class, null, 0, null);
|
317
|
|
318
|
assertNotNull("getDescriptionElementForTaxon should return a List", elements2);
|
319
|
assertTrue("getDescriptionElementForTaxon should be empty",elements2.isEmpty());
|
320
|
|
321
|
// 3.
|
322
|
|
323
|
List<Distribution> elements3 = descriptionDao.getDescriptionElementForTaxon(
|
324
|
taxonSphingidae , null, Distribution.class, null, 0, null);
|
325
|
|
326
|
assertNotNull("getDescriptionElementForTaxon should return a List", elements3);
|
327
|
assertFalse("getDescriptionElementForTaxon should not be empty",elements3.isEmpty());
|
328
|
assertEquals("getDescriptionElementForTaxon should return 1 elements",1,elements3.size());
|
329
|
}
|
330
|
|
331
|
//see #2234
|
332
|
@Test
|
333
|
public void testSaveClonedDescription() {
|
334
|
Taxon taxon = Taxon.NewInstance(null, null);
|
335
|
TaxonDescription description = TaxonDescription.NewInstance(taxon);
|
336
|
this.descriptionDao.save(description);
|
337
|
TaxonDescription clonedDescription = (TaxonDescription)description.clone();
|
338
|
this.descriptionDao.save(clonedDescription);
|
339
|
taxonDao.save(taxon);
|
340
|
|
341
|
}
|
342
|
|
343
|
}
|