use long instead of int (cont.)
[cdmlib.git] / cdmlib-persistence / src / test / java / eu / etaxonomy / cdm / persistence / dao / hibernate / name / TaxonNameDaoHibernateImplTest.java
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.persistence.dao.hibernate.name;
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.assertNull;
16 import static org.junit.Assert.assertTrue;
17
18 import java.io.FileNotFoundException;
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Optional;
24 import java.util.Set;
25 import java.util.UUID;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.unitils.dbunit.annotation.DataSet;
30 import org.unitils.spring.annotation.SpringBeanByType;
31
32 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
33 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
34 import eu.etaxonomy.cdm.model.name.HybridRelationship;
35 import eu.etaxonomy.cdm.model.name.IBotanicalName;
36 import eu.etaxonomy.cdm.model.name.NameRelationship;
37 import eu.etaxonomy.cdm.model.name.Rank;
38 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
39 import eu.etaxonomy.cdm.model.name.TaxonName;
40 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
41 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
42 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43 import eu.etaxonomy.cdm.persistence.dao.name.IHomotypicalGroupDao;
44 import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
45 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
46 import eu.etaxonomy.cdm.persistence.dto.TaxonNameParts;
47 import eu.etaxonomy.cdm.persistence.query.OrderHint;
48 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
49 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
50 import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
51
52 @DataSet
53 public class TaxonNameDaoHibernateImplTest extends CdmIntegrationTest {
54
55 @SpringBeanByType
56 ITaxonNameDao taxonNameDao;
57
58 @SpringBeanByType
59 ITaxonDao taxonDao;
60
61 @SpringBeanByType
62 IHomotypicalGroupDao homotypicalGroupDao;
63
64 private UUID cryptocoryneGriffithiiUuid;
65 private UUID acherontiaUuid;
66 private UUID acherontiaLachesisUuid;
67 private UUID atroposUuid;
68
69 @Before
70 public void setUp() {
71 cryptocoryneGriffithiiUuid = UUID.fromString("497a9955-5c5a-4f2b-b08c-2135d336d633");
72 acherontiaUuid = UUID.fromString("c2cab2ad-3e3a-47b8-8aa8-d9e1c0857647");
73 acherontiaLachesisUuid = UUID.fromString("7969821b-a2cf-4d01-95ec-6a5ed0ca3f69");
74 atroposUuid = UUID.fromString("27004fcc-14d4-47d4-a3e1-75750fdb5b79");
75 }
76
77 @Test
78 public void testGetHybridRelationships() {
79 IBotanicalName cryptocoryneGriffithii = taxonNameDao.findByUuid(cryptocoryneGriffithiiUuid);
80 assert cryptocoryneGriffithii!= null : "name must exist";
81
82 List<HybridRelationship> result = taxonNameDao.getHybridNames(cryptocoryneGriffithii, null, null, null,null,null);
83
84 assertNotNull("getHybridNames should return a list",result);
85 assertFalse("the list should not be empty", result.isEmpty());
86 assertEquals("getHybridNames should return 1 HybridRelationship instance",1,result.size());
87 }
88
89 @Test
90 public void testCountHybridRelationships() {
91 IBotanicalName cryptocoryneGriffithii = taxonNameDao.findByUuid(cryptocoryneGriffithiiUuid);
92 assert cryptocoryneGriffithii != null : "name must exist";
93
94 int count = taxonNameDao.countHybridNames(cryptocoryneGriffithii, null);
95
96 assertEquals("countHybridNames should return 1",1,count);
97 }
98
99 @Test
100 public void testGetNameRelationships() {
101 TaxonName acherontia = taxonNameDao.findByUuid(acherontiaUuid);
102 assert acherontia != null : "name must exist";
103
104 List<NameRelationship> result = taxonNameDao.getNameRelationships(acherontia, NameRelationship.Direction.relatedFrom, null, null,null,null, null);
105
106 assertNotNull("getRelatedNames should return a list",result);
107 assertFalse("the list should not be empty", result.isEmpty());
108 assertEquals("getRelatedNames should return 1 NameRelationship instance",1,result.size());
109
110 // testing inverted direction
111 TaxonName atropos = taxonNameDao.findByUuid(atroposUuid);
112 assert atropos != null : "name must exist";
113
114 result = taxonNameDao.getNameRelationships(atropos, NameRelationship.Direction.relatedTo, null, null,null,null, null);
115
116 assertNotNull("getRelatedNames should return a list",result);
117 assertFalse("the list should not be empty", result.isEmpty());
118 assertEquals("getRelatedNames should return 2 NameRelationship instance",2,result.size());
119
120 result = taxonNameDao.getNameRelationships(null, null, null, null,null,null, null);
121
122 assertNotNull("getRelatedNames should return a list",result);
123 assertFalse("the list should not be empty", result.isEmpty());
124 assertEquals("getRelatedNames should return all 2 NameRelationship instance",2,result.size());
125 }
126
127 @Test
128 public void testCountNameRelationships() {
129 TaxonName acherontia = taxonNameDao.findByUuid(acherontiaUuid);
130 assert acherontia != null : "name must exist";
131
132 int count = taxonNameDao.countNameRelationships(acherontia, NameRelationship.Direction.relatedFrom, null);
133
134 assertEquals("countRelatedNames should return 1",1,count);
135
136 // testing inverted direction
137 TaxonName atropos = taxonNameDao.findByUuid(atroposUuid);
138 assert atropos != null : "name must exist";
139
140 count = taxonNameDao.countNameRelationships(atropos, NameRelationship.Direction.relatedTo, null);
141
142 assertEquals("countRelatedNames should return 2",2,count);
143 }
144
145 @Test
146 public void testGetTypeDesignations() {
147 TaxonName acherontiaLachesis = taxonNameDao.findByUuid(acherontiaLachesisUuid);
148 assert acherontiaLachesis != null : "name must exist";
149
150 List<TypeDesignationBase> result1 = taxonNameDao.getTypeDesignations(acherontiaLachesis, null, null, null, null, null);
151
152 assertNotNull("getTypeDesignations should return a list",result1);
153 assertFalse("the list should not be empty", result1.isEmpty());
154 assertEquals("getTypeDesignations should return 1 TypeDesignationBase instance",1,result1.size());
155
156 List<SpecimenTypeDesignation> result2 = taxonNameDao.getTypeDesignations(acherontiaLachesis, SpecimenTypeDesignation.class, null, null, null, null);
157
158 assertNotNull("getTypeDesignations should return a list",result2);
159 assertFalse("the list should not be empty", result2.isEmpty());
160 assertEquals("getTypeDesignations should return 1 TypeDesignationBase instance",1,result2.size());
161 }
162
163 @Test
164 public void testCountTypeDesignations() {
165 TaxonName acherontiaLachesis = taxonNameDao.findByUuid(acherontiaLachesisUuid);
166 assert acherontiaLachesis != null : "name must exist";
167
168 int count = taxonNameDao.countTypeDesignations(acherontiaLachesis, null);
169
170 assertEquals("countTypeDesignations should return 1",1,count);
171 }
172
173 @Test
174 public void testSearchNames() {
175 List<TaxonName> result = taxonNameDao.searchNames("Atropos", null, null, null, Rank.GENUS(), null, null, null, null);
176
177 assertNotNull("searchNames should return a list",result);
178 assertFalse("the list should not be empty", result.isEmpty());
179 assertEquals("searchNames should return 3 TaxonName instances",3,result.size());
180 }
181
182 @Test
183 public void testFindTaxonNameParts_genusOrUninomial() {
184 Integer pageSize = 10;
185 Integer pageIndex = 0;
186
187 List<TaxonNameParts> resuls = taxonNameDao.findTaxonNameParts(
188 Optional.of("Atropos"), null, null, null,
189 Rank.GENUS(),
190 pageSize, pageIndex, Arrays.asList(new OrderHint("genusOrUninomial", SortOrder.ASCENDING)));
191
192 assertNotNull("searchNames should return a list",resuls);
193 assertFalse(resuls.isEmpty());
194 assertEquals(3, resuls.size());
195 }
196
197 @Test
198 public void testFindTaxonNameParts_genusOrUninomial_wildcard() {
199 Integer pageSize = 10;
200 Integer pageIndex = 0;
201
202 List<TaxonNameParts> results = taxonNameDao.findTaxonNameParts(
203 Optional.of("Atro*"), null, null, null,
204 Rank.GENUS(),
205 pageSize, pageIndex, Arrays.asList(new OrderHint("genusOrUninomial", SortOrder.ASCENDING)));
206
207 assertNotNull(results);
208 assertFalse(results.isEmpty());
209 assertEquals(3, results.size());
210
211 results = taxonNameDao.findTaxonNameParts(
212 Optional.of("Atro*"), null, null, null,
213 Rank.SPECIES(),
214 pageSize, pageIndex, Arrays.asList(new OrderHint("genusOrUninomial", SortOrder.ASCENDING)));
215
216 assertNotNull(results);
217 assertTrue(results.isEmpty());
218 }
219
220 @Test
221 public void testFindTaxonNameParts_rankSpecies() {
222 Integer pageSize = 10;
223 Integer pageIndex = 0;
224 // Manduca afflicta
225 // Manduca chinchilla
226 // Manduca bergarmatipes
227 List<TaxonNameParts> results = taxonNameDao.findTaxonNameParts(
228 Optional.of("Manduca"), null, Optional.of("*"), null,
229 Rank.SPECIES(),
230 pageSize, pageIndex, Arrays.asList(new OrderHint("specificEpithet", SortOrder.ASCENDING)));
231
232 assertEquals(3, results.size());
233 assertEquals("afflicta", results.get(0).getSpecificEpithet());
234 assertEquals("bergarmatipes", results.get(1).getSpecificEpithet());
235 assertEquals("chinchilla", results.get(2).getSpecificEpithet());
236
237 results = taxonNameDao.findTaxonNameParts(
238 Optional.of("Manduca"), null, Optional.of("chin*"), null,
239 Rank.SPECIES(),
240 pageSize, pageIndex, null);
241
242 assertEquals(1, results.size());
243 assertEquals("chinchilla", results.get(0).getSpecificEpithet());
244 }
245
246 @Test
247 public void testFindTaxonNameParts_rankBelowSpecies() {
248
249 Integer pageSize = 10;
250 Integer pageIndex = 0;
251 // Cryptocoryne x purpurea nothovar borneoensis
252 // Cryptocoryne cordata var. zonata
253 List<TaxonNameParts> results = taxonNameDao.findTaxonNameParts(
254 Optional.of("Cryptocoryne"), null, null, Optional.of("borneo*"),
255 Rank.VARIETY(),
256 pageSize, pageIndex, Arrays.asList(new OrderHint("specificEpithet", SortOrder.ASCENDING)));
257
258 assertEquals(1, results.size());
259
260 // now also with "infraGenericEpithet is null AND specificEpithet = purpurea"
261 results = taxonNameDao.findTaxonNameParts(
262 Optional.of("Cryptocoryne"), Optional.empty(), Optional.of("purpurea"), Optional.of("borneo*"),
263 Rank.VARIETY(),
264 pageSize, pageIndex, Arrays.asList(new OrderHint("specificEpithet", SortOrder.ASCENDING)));
265
266 assertEquals(1, results.size());
267 }
268
269
270 @Test
271 public void testCountNames() {
272 long count = taxonNameDao.countNames("Atropos", null, null, null, Rank.GENUS());
273
274 assertEquals("countNames should return 3",3,count);
275 }
276
277 @Test
278 public void testCountNamesByExample() {
279 TaxonName zoologicalName = TaxonNameFactory.NewZoologicalInstance(Rank.GENUS());
280 zoologicalName.setGenusOrUninomial("Atropos");
281 Set<String> includedProperties = new HashSet<String>();
282 includedProperties.add("genusOrUninomial");
283 includedProperties.add("specificEpithet");
284 includedProperties.add("infraSpecificEpithet");
285 includedProperties.add("rank");
286 long count = taxonNameDao.count(zoologicalName, includedProperties);
287
288 assertEquals("countNames should return 3",3,count);
289 }
290
291 @Test
292 /**
293 * This test check for a specific bug (Ticket #686) where the rank of a taxon name base
294 * has no order index (=0)
295 */
296 public void testMissingRankOrderIndex() {
297 TaxonName acherontiaLachesis = taxonNameDao.findByUuid(acherontiaLachesisUuid);
298 Rank rank = null;
299 try {
300 rank = Rank.getRankByName(acherontiaLachesis.getRank().getLabel());
301 } catch (UnknownCdmTypeException e) {
302 e.printStackTrace();
303 }
304 assertNotNull(rank);
305 assertFalse("Rank are equal, level must not be higher", rank.isHigher(acherontiaLachesis.getRank()));
306 assertFalse("Rank are equal, level must not be lower", rank.isLower(acherontiaLachesis.getRank()));
307 }
308
309 @Test
310 public void testDeleteTaxon(){
311 TaxonName acherontiaLachesis = taxonNameDao.findByUuid(UUID.fromString("497a9955-5c5a-4f2b-b08c-2135d336d633"));
312 HibernateProxyHelper.deproxy(acherontiaLachesis, TaxonName.class);
313 Set<TaxonBase> taxonBases = acherontiaLachesis.getTaxonBases();
314 HomotypicalGroup group = acherontiaLachesis.getHomotypicalGroup();
315 UUID groupUuid = group.getUuid();
316 taxonNameDao.delete(acherontiaLachesis);
317
318 Iterator<TaxonBase> taxa= taxonBases.iterator();
319 TaxonBase taxon = taxa.next();
320 UUID taxonUuid = taxon.getUuid();
321
322 //int numbOfTaxa = taxonDao.count(TaxonBase.class);
323 List<TaxonBase> taxaList = taxonDao.list(100, 0);
324
325 acherontiaLachesis = taxonNameDao.findByUuid(UUID.fromString("497a9955-5c5a-4f2b-b08c-2135d336d633"));
326 taxon = taxonDao.findByUuid(taxonUuid);
327 group = homotypicalGroupDao.findByUuid(groupUuid);
328 group = HibernateProxyHelper.deproxy(group, HomotypicalGroup.class);
329 assertNull("There should be no taxonName with the deleted uuid", acherontiaLachesis);
330 assertNull("There should be no taxon with the deleted uuid", taxon);
331 assertNull("There should be no homotypicalGroup with the deleted uuid", group);
332
333 }
334
335 /* (non-Javadoc)
336 * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
337 */
338 @Override
339 public void createTestDataSet() throws FileNotFoundException {
340 // TODO Auto-generated method stub
341
342 }
343 }