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.common;
|
11
|
|
12
|
|
13
|
import static org.junit.Assert.assertEquals;
|
14
|
import static org.junit.Assert.assertFalse;
|
15
|
import static org.junit.Assert.assertNotNull;
|
16
|
import static org.junit.Assert.assertNull;
|
17
|
import static org.junit.Assert.assertTrue;
|
18
|
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.HashSet;
|
21
|
import java.util.List;
|
22
|
import java.util.Set;
|
23
|
import java.util.UUID;
|
24
|
|
25
|
import org.hibernate.Hibernate;
|
26
|
import org.junit.After;
|
27
|
import org.junit.Before;
|
28
|
import org.junit.Test;
|
29
|
import org.unitils.spring.annotation.SpringBeanByType;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
32
|
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
33
|
import eu.etaxonomy.cdm.model.common.Language;
|
34
|
import eu.etaxonomy.cdm.model.common.Representation;
|
35
|
import eu.etaxonomy.cdm.model.description.Feature;
|
36
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
37
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
38
|
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
|
39
|
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
|
40
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
41
|
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
|
42
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
43
|
|
44
|
public class DefinedTermDaoImplTest extends CdmIntegrationTest {
|
45
|
|
46
|
@SpringBeanByType
|
47
|
private IDefinedTermDao dao;
|
48
|
|
49
|
private UUID uuid;
|
50
|
private UUID armUuid;
|
51
|
private UUID northernEuropeUuid;
|
52
|
private UUID middleEuropeUuid;
|
53
|
private UUID westTropicalAfricaUuid;
|
54
|
private Set<NamedArea> namedAreas;
|
55
|
private AuditEvent auditEvent;
|
56
|
|
57
|
@Before
|
58
|
public void setUp() {
|
59
|
uuid = UUID.fromString("910307f1-dc3c-452c-a6dd-af5ac7cd365c");
|
60
|
armUuid = UUID.fromString("7a0fde13-26e9-4382-a5c9-5640fc2b3334");
|
61
|
northernEuropeUuid = UUID.fromString("22524ba2-6e57-4b71-89ab-89fc50fba6b4");
|
62
|
middleEuropeUuid = UUID.fromString("d292f237-da3d-408b-93a1-3257a8c80b97");
|
63
|
westTropicalAfricaUuid = UUID.fromString("931164ad-ec16-4133-afab-bdef25d67636");
|
64
|
namedAreas = new HashSet<NamedArea>();
|
65
|
auditEvent = new AuditEvent();
|
66
|
auditEvent.setUuid(UUID.fromString("6456c23d-6424-42dc-a240-36d34e77b249"));
|
67
|
auditEvent.setRevisionNumber(1001);
|
68
|
AuditEventContextHolder.clearContext();
|
69
|
}
|
70
|
|
71
|
@After
|
72
|
public void cleantUp() {
|
73
|
AuditEventContextHolder.clearContext();
|
74
|
}
|
75
|
@Test
|
76
|
public void findByTitle() throws Exception {
|
77
|
List<DefinedTermBase> terms = dao.findByTitle("Diagnosis");
|
78
|
assertNotNull("findByTitle should return a List", terms);
|
79
|
assertEquals("findByTitle should return one term ",terms.size(),1);
|
80
|
assertEquals("findByTitle should return Feature.DIAGNOSIS",terms.get(0),Feature.DIAGNOSIS());
|
81
|
}
|
82
|
|
83
|
@Test
|
84
|
public void getTermByUUID() {
|
85
|
DefinedTermBase<?> term = dao.findByUuid(uuid);
|
86
|
assertNotNull("findByUuid should return a term",term);
|
87
|
assertEquals("findByUuid should return Feature.UNKNOWN",Feature.UNKNOWN(),term);
|
88
|
}
|
89
|
|
90
|
|
91
|
@Test
|
92
|
public void getLanguageByIso2() {
|
93
|
Language lang = dao.getLanguageByIso("arm");
|
94
|
assertNotNull(lang);
|
95
|
assertEquals("getLanguageByIso should return the correct Language instance",lang.getUuid(), armUuid);
|
96
|
}
|
97
|
|
98
|
@Test
|
99
|
public void getLanguageByIso1() {
|
100
|
Language lang = dao.getLanguageByIso("hy");
|
101
|
assertNotNull(lang);
|
102
|
assertEquals("getLanguageByIso should return the correct Language instance",lang.getUuid(), armUuid);
|
103
|
}
|
104
|
|
105
|
@Test
|
106
|
public void getLanguageByMalformedIso1() {
|
107
|
Language lang = dao.getLanguageByIso("a");
|
108
|
assertNull("getLanguageByIso should return null for this malformed Iso \'a\'",lang);
|
109
|
}
|
110
|
|
111
|
@Test
|
112
|
public void getLanguageByMalformedIso2() {
|
113
|
Language lang = dao.getLanguageByIso("abcd");
|
114
|
assertNull("getLanguageByIso should return null for this malformed Iso \'abcd\'",lang);
|
115
|
}
|
116
|
|
117
|
@Test
|
118
|
public void getDefinedTermByIdInVocabulary(){
|
119
|
UUID tdwgVocUuid = NamedArea.uuidTdwgAreaVocabulary;
|
120
|
List<NamedArea> list = dao.getDefinedTermByIdInVocabulary("GER", tdwgVocUuid, NamedArea.class, null, null);
|
121
|
assertNotNull("Method should return a result", list);
|
122
|
assertEquals("Method should return exactly 1 area", 1, list.size());
|
123
|
NamedArea area = list.get(0);
|
124
|
assertEquals("GER", area.getIdInVocabulary());
|
125
|
assertEquals(tdwgVocUuid, area.getVocabulary().getUuid());
|
126
|
}
|
127
|
|
128
|
@Test
|
129
|
public void testGetIncludes() {
|
130
|
NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
|
131
|
assert northernEurope != null : "NamedArea must exist";
|
132
|
namedAreas.add(northernEurope);
|
133
|
|
134
|
List<String> propertyPaths = new ArrayList<String>();
|
135
|
propertyPaths.add("level");
|
136
|
|
137
|
List<NamedArea> includes = dao.getIncludes(namedAreas, null, null,propertyPaths);
|
138
|
|
139
|
assertNotNull("getIncludes should return a List",includes);
|
140
|
assertFalse("The list should not be empty",includes.isEmpty());
|
141
|
assertEquals("getIncludes should return 9 NamedArea entities",9,includes.size());
|
142
|
assertTrue("NamedArea.level should be initialized",Hibernate.isInitialized(includes.get(0).getLevel()));
|
143
|
}
|
144
|
|
145
|
@Test
|
146
|
public void countIncludes() {
|
147
|
NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
|
148
|
assert northernEurope != null : "NamedArea must exist";
|
149
|
namedAreas.add(northernEurope);
|
150
|
|
151
|
int numberOfIncludes = dao.countIncludes(namedAreas);
|
152
|
assertEquals("countIncludes should return 9",9, numberOfIncludes);
|
153
|
|
154
|
}
|
155
|
|
156
|
@Test
|
157
|
public void testGetPartOf() {
|
158
|
NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
|
159
|
NamedArea middleEurope = (NamedArea)dao.findByUuid(middleEuropeUuid);
|
160
|
NamedArea westTropicalAfrica = (NamedArea)dao.findByUuid(westTropicalAfricaUuid);
|
161
|
assert northernEurope != null : "NamedArea must exist";
|
162
|
assert middleEurope != null : "NamedArea must exist";
|
163
|
assert westTropicalAfrica != null : "NamedArea must exist";
|
164
|
namedAreas.add(northernEurope);
|
165
|
namedAreas.add(middleEurope);
|
166
|
namedAreas.add(westTropicalAfrica);
|
167
|
|
168
|
List<String> propertyPaths = new ArrayList<String>();
|
169
|
propertyPaths.add("level");
|
170
|
|
171
|
List<NamedArea> partOf = dao.getPartOf(namedAreas, null, null,propertyPaths);
|
172
|
|
173
|
assertNotNull("getPartOf should return a List",partOf);
|
174
|
assertFalse("The list should not be empty",partOf.isEmpty());
|
175
|
assertEquals("getPartOf should return 2 NamedArea entities",2,partOf.size());
|
176
|
assertTrue("NamedArea.level should be initialized",Hibernate.isInitialized(partOf.get(0).getLevel()));
|
177
|
}
|
178
|
|
179
|
@Test
|
180
|
public void countPartOf() {
|
181
|
NamedArea northernEurope = (NamedArea)dao.findByUuid(northernEuropeUuid);
|
182
|
NamedArea middleEurope = (NamedArea)dao.findByUuid(middleEuropeUuid);
|
183
|
NamedArea westTropicalAfrica = (NamedArea)dao.findByUuid(westTropicalAfricaUuid);
|
184
|
assert northernEurope != null : "NamedArea must exist";
|
185
|
assert middleEurope != null : "NamedArea must exist";
|
186
|
assert westTropicalAfrica != null : "NamedArea must exist";
|
187
|
|
188
|
namedAreas.add(northernEurope);
|
189
|
namedAreas.add(middleEurope);
|
190
|
namedAreas.add(westTropicalAfrica);
|
191
|
|
192
|
int numberOfPartOf = dao.countPartOf(namedAreas);
|
193
|
assertEquals("countPartOf should return 2",2,numberOfPartOf);
|
194
|
}
|
195
|
|
196
|
@Test
|
197
|
// NOTE: if this test is failing see
|
198
|
// http://dev.e-taxonomy.eu/trac/changeset/13291/trunk/cdmlib/cdmlib-persistence/src/test/resources/eu/etaxonomy/cdm/persistence/dao/hibernate/dataset.dtd
|
199
|
public void testListInitialization() {
|
200
|
AuditEventContextHolder.getContext().setAuditEvent(auditEvent);
|
201
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
202
|
orderHints.add(new OrderHint("titleCache",SortOrder.ASCENDING));
|
203
|
|
204
|
List<String> propertyPaths = new ArrayList<String>();
|
205
|
propertyPaths.add("representations");
|
206
|
propertyPaths.add("representations.language");
|
207
|
List<ExtensionType> extensionTypes = dao.list(ExtensionType.class,null, null, orderHints, propertyPaths);
|
208
|
|
209
|
|
210
|
assertTrue(Hibernate.isInitialized(extensionTypes.get(0).getRepresentations()));
|
211
|
Set<Representation> representations = extensionTypes.get(0).getRepresentations();
|
212
|
//TODO currently the representations list is empty, is this wanted? If not,
|
213
|
//we should first check, if the list is not empty and then iterate.
|
214
|
//Why is it empty? Does ExtensionType not have representations?
|
215
|
for(Representation representation : representations) {
|
216
|
assertTrue(Hibernate.isInitialized(representation.getLanguage()));
|
217
|
}
|
218
|
}
|
219
|
}
|