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.Feature;
|
37
|
import eu.etaxonomy.cdm.model.description.PresenceTerm;
|
38
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
39
|
import eu.etaxonomy.cdm.model.description.TextData;
|
40
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
41
|
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
|
42
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
|
43
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
44
|
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
|
45
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
46
|
|
47
|
@DataSet
|
48
|
public class DescriptionDaoHibernateImplTest extends CdmIntegrationTest {
|
49
|
|
50
|
@SpringBeanByType
|
51
|
IDescriptionDao descriptionDao;
|
52
|
|
53
|
@SpringBeanByType
|
54
|
IDefinedTermDao definedTermDao;
|
55
|
|
56
|
private Set<NamedArea> namedAreas;
|
57
|
private Set<Feature> features;
|
58
|
|
59
|
private UUID northernAmericaUuid;
|
60
|
private UUID southernAmericaUuid;
|
61
|
private UUID antarcticaUuid;
|
62
|
|
63
|
private UUID uuid;
|
64
|
|
65
|
@Before
|
66
|
public void setUp() {
|
67
|
uuid = UUID.fromString("5f3265ed-68ad-4ec3-826f-0d29d25986b9");
|
68
|
|
69
|
namedAreas = new HashSet<NamedArea>();
|
70
|
northernAmericaUuid = UUID.fromString("2757e726-d897-4546-93bd-7951d203bf6f");
|
71
|
southernAmericaUuid = UUID.fromString("6310b3ba-96f4-4855-bb5b-326e7af188ea");
|
72
|
antarcticaUuid = UUID.fromString("791b3aa0-54dd-4bed-9b68-56b4680aad0c");
|
73
|
|
74
|
features = new HashSet<Feature>();
|
75
|
}
|
76
|
|
77
|
|
78
|
@Test
|
79
|
public void testCountByDistribution() {
|
80
|
NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
|
81
|
NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
|
82
|
NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
|
83
|
|
84
|
assert northernAmerica != null : "term must exist";
|
85
|
assert southernAmerica != null : "term must exist";
|
86
|
assert antarctica != null : "term must exist";
|
87
|
|
88
|
namedAreas.add(northernAmerica);
|
89
|
namedAreas.add(southernAmerica);
|
90
|
namedAreas.add(antarctica);
|
91
|
|
92
|
int numberOfDescriptions = descriptionDao.countDescriptionByDistribution(namedAreas, null);
|
93
|
assertEquals("countDescriptionsByDistribution should return 23",23,numberOfDescriptions);
|
94
|
}
|
95
|
|
96
|
@Test
|
97
|
public void testCountByDistributionWithStatus() {
|
98
|
NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
|
99
|
NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
|
100
|
NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
|
101
|
|
102
|
assert northernAmerica != null : "term must exist";
|
103
|
assert southernAmerica != null : "term must exist";
|
104
|
assert antarctica != null : "term must exist";
|
105
|
|
106
|
namedAreas.add(northernAmerica);
|
107
|
namedAreas.add(southernAmerica);
|
108
|
namedAreas.add(antarctica);
|
109
|
|
110
|
int numberOfDescriptions = descriptionDao.countDescriptionByDistribution(namedAreas, PresenceTerm.PRESENT());
|
111
|
assertEquals("countDescriptionsByDistribution should return 20",20,numberOfDescriptions);
|
112
|
}
|
113
|
|
114
|
@Test
|
115
|
public void testGetByDistribution() {
|
116
|
NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
|
117
|
NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
|
118
|
NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
|
119
|
|
120
|
assert northernAmerica != null : "term must exist";
|
121
|
assert southernAmerica != null : "term must exist";
|
122
|
assert antarctica != null : "term must exist";
|
123
|
|
124
|
namedAreas.add(northernAmerica);
|
125
|
namedAreas.add(southernAmerica);
|
126
|
namedAreas.add(antarctica);
|
127
|
|
128
|
List<String> propertyPaths = new ArrayList<String>();
|
129
|
propertyPaths.add("taxon");
|
130
|
|
131
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
132
|
orderHints.add(new OrderHint("titleCache",SortOrder.ASCENDING));
|
133
|
|
134
|
List<TaxonDescription> descriptions = descriptionDao.searchDescriptionByDistribution(namedAreas, null, 10,2,orderHints,propertyPaths);
|
135
|
assertNotNull("searchDescriptionByDistribution should return a List",descriptions);
|
136
|
assertFalse("searchDescriptionsByDistribution should not be empty",descriptions.isEmpty());
|
137
|
assertEquals("searchDescriptionsByDistribution should return 3 elements",3,descriptions.size());
|
138
|
assertTrue("TaxonDescription.taxon should be initialized",Hibernate.isInitialized(descriptions.get(0).getTaxon()));
|
139
|
assertEquals("Sphingidae Linnaeus, 1758 sec. cate-sphingidae.org should come first","Sphingidae Linnaeus, 1758 sec. cate-sphingidae.org",descriptions.get(0).getTitleCache());
|
140
|
assertEquals("Sphinx Linnaeus, 1758 sec. cate-sphingidae.org should come last","Sphinx Linnaeus, 1758 sec. cate-sphingidae.org",descriptions.get(2).getTitleCache());
|
141
|
}
|
142
|
|
143
|
@Test
|
144
|
public void testGetByDistributionWithStatus() {
|
145
|
NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
|
146
|
NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
|
147
|
NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
|
148
|
|
149
|
assert northernAmerica != null : "term must exist";
|
150
|
assert southernAmerica != null : "term must exist";
|
151
|
assert antarctica != null : "term must exist";
|
152
|
|
153
|
namedAreas.add(northernAmerica);
|
154
|
namedAreas.add(southernAmerica);
|
155
|
namedAreas.add(antarctica);
|
156
|
|
157
|
List<TaxonDescription> descriptions = descriptionDao.searchDescriptionByDistribution(namedAreas, AbsenceTerm.ABSENT(), 10,0,null,null);
|
158
|
assertNotNull("searchDescriptionByDistribution should return a List",descriptions);
|
159
|
assertFalse("searchDescriptionsByDistribution should not be empty",descriptions.isEmpty());
|
160
|
assertEquals("searchDescriptionsByDistribution should return 3 elements",3,descriptions.size());
|
161
|
}
|
162
|
|
163
|
@Test
|
164
|
public void testCountDescriptionsWithText() {
|
165
|
int numberOfDescriptions = descriptionDao.countDescriptions(TaxonDescription.class, null, true, null);
|
166
|
|
167
|
assertNotNull("countDescriptions should return a 2",numberOfDescriptions);
|
168
|
}
|
169
|
|
170
|
@Test
|
171
|
public void testListDescriptionsWithText() {
|
172
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
173
|
orderHints.add(new OrderHint("titleCache",SortOrder.ASCENDING));
|
174
|
List<String> propertyPaths = new ArrayList<String>();
|
175
|
propertyPaths.add("taxon");
|
176
|
List<DescriptionBase> descriptions = descriptionDao.listDescriptions(TaxonDescription.class, null, true, null,null,null,orderHints,propertyPaths);
|
177
|
|
178
|
assertNotNull("listDescriptions should return a List",descriptions);
|
179
|
assertFalse("listDescriptions should not be empty", descriptions.isEmpty());
|
180
|
assertEquals("listDescriptions should return 2 descriptions",2,descriptions.size());
|
181
|
}
|
182
|
|
183
|
@Test
|
184
|
public void testCountDescriptionsWithTextAndFeatures() {
|
185
|
features.add(Feature.ECOLOGY());
|
186
|
int numberOfDescriptions = descriptionDao.countDescriptions(TaxonDescription.class, null, true, features);
|
187
|
|
188
|
assertNotNull("countDescriptions should return a 1",numberOfDescriptions);
|
189
|
}
|
190
|
|
191
|
@Test
|
192
|
public void testListDescriptionsWithTextAndFeatures() {
|
193
|
assert Feature.ECOLOGY() != null;
|
194
|
features.add(Feature.ECOLOGY());
|
195
|
List<DescriptionBase> descriptions = descriptionDao.listDescriptions(TaxonDescription.class, null, true, features,null,null,null,null);
|
196
|
|
197
|
assertNotNull("listDescriptions should return a List",descriptions);
|
198
|
assertFalse("listDescriptions should not be empty", descriptions.isEmpty());
|
199
|
assertEquals("listDescriptions should return 1 descriptions",1,descriptions.size());
|
200
|
}
|
201
|
|
202
|
@Test
|
203
|
public void testCountDescriptionElements() {
|
204
|
DescriptionBase description = descriptionDao.findByUuid(uuid);
|
205
|
assert description != null : "description must exist";
|
206
|
|
207
|
int numberOfDescriptionElements = descriptionDao.countDescriptionElements(description, null, TextData.class);
|
208
|
|
209
|
assertEquals("countDescriptionElements should return 2",2,numberOfDescriptionElements);
|
210
|
}
|
211
|
|
212
|
@Test
|
213
|
public void testGetDescriptionElements() {
|
214
|
DescriptionBase description = descriptionDao.findByUuid(uuid);
|
215
|
assert description != null : "description must exist";
|
216
|
|
217
|
List<String> propertyPaths = new ArrayList<String>();
|
218
|
propertyPaths.add("multilanguageText");
|
219
|
propertyPaths.add("media");
|
220
|
propertyPaths.add("citation");
|
221
|
propertyPaths.add("feature");
|
222
|
|
223
|
List<DescriptionElementBase> elements = descriptionDao.getDescriptionElements(description, null, TextData.class, null, null,propertyPaths);
|
224
|
//System.err.println(elements.size());
|
225
|
for (DescriptionElementBase descElB: elements){
|
226
|
if (descElB instanceof TextData){
|
227
|
Map<Language, LanguageString> multiLanguage = ((TextData)descElB).getMultilanguageText();
|
228
|
LanguageString defaultString = multiLanguage.get(Language.DEFAULT());
|
229
|
//System.err.println(defaultString);
|
230
|
}
|
231
|
}
|
232
|
Iterator<DescriptionElementBase> elements2 = description.getElements().iterator();
|
233
|
while(elements2.hasNext()){
|
234
|
DescriptionElementBase element = elements2.next();
|
235
|
if (element instanceof TextData){
|
236
|
Map<Language, LanguageString> multiLanguage = ((TextData)element).getMultilanguageText();
|
237
|
LanguageString defaultString = multiLanguage.get(Language.DEFAULT());
|
238
|
defaultString.setText("blablub");
|
239
|
}
|
240
|
}
|
241
|
elements = descriptionDao.getDescriptionElements(description, null, TextData.class, null, null,propertyPaths);
|
242
|
//System.err.println(elements.size());
|
243
|
for (DescriptionElementBase descElB: elements){
|
244
|
if (descElB instanceof TextData){
|
245
|
Map<Language, LanguageString> multiLanguage = ((TextData)descElB).getMultilanguageText();
|
246
|
LanguageString defaultString = multiLanguage.get(Language.DEFAULT());
|
247
|
//System.err.println(defaultString);
|
248
|
}
|
249
|
}
|
250
|
|
251
|
assertNotNull("getDescriptionElements should return a List");
|
252
|
assertFalse("getDescriptionElements should not be empty",elements.isEmpty());
|
253
|
assertEquals("getDescriptionElement should return 2 elements",2,elements.size());
|
254
|
assertTrue("ReferencedEntityBase.citation should be initialized",Hibernate.isInitialized(elements.get(0).getCitation()));
|
255
|
assertTrue("DescriptionElementBase.feature should be initialized",Hibernate.isInitialized(elements.get(0).getFeature()));
|
256
|
assertTrue("DescriptionElementBase.media should be initialized",Hibernate.isInitialized(elements.get(0).getMedia()));
|
257
|
assertTrue("TextData.multilanguageText should be initialized",Hibernate.isInitialized(((TextData)elements.get(0)).getMultilanguageText()));
|
258
|
}
|
259
|
|
260
|
@Test
|
261
|
public void testCountDescriptionElementsFeature() {
|
262
|
features.add(Feature.ECOLOGY());
|
263
|
DescriptionBase description = descriptionDao.findByUuid(uuid);
|
264
|
assert description != null : "description must exist";
|
265
|
|
266
|
int numberOfDescriptionElements = descriptionDao.countDescriptionElements(description, features, TextData.class);
|
267
|
|
268
|
assertEquals("countDescriptionElements should return 1",1,numberOfDescriptionElements);
|
269
|
}
|
270
|
|
271
|
@Test
|
272
|
public void testGetDescriptionElementsByFeature() {
|
273
|
features.add(Feature.ECOLOGY());
|
274
|
DescriptionBase description = descriptionDao.findByUuid(uuid);
|
275
|
assert description != null : "description must exist";
|
276
|
|
277
|
List<DescriptionElementBase> elements = descriptionDao.getDescriptionElements(description, features, TextData.class, null, null,null);
|
278
|
|
279
|
assertNotNull("getDescriptionElements should return a List");
|
280
|
assertFalse("getDescriptionElements should not be empty",elements.isEmpty());
|
281
|
assertEquals("getDescriptionElement should return 1 elements",1,elements.size());
|
282
|
}
|
283
|
|
284
|
}
|