ref #824 rename Rank.getByNameXXX to getByLatinNameXXX
[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 package eu.etaxonomy.cdm.persistence.dao.hibernate.name;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16
17 import java.io.FileNotFoundException;
18 import java.util.Arrays;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Optional;
23 import java.util.Set;
24 import java.util.UUID;
25
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.CdmBase;
32 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
33 import eu.etaxonomy.cdm.model.name.HybridRelationship;
34 import eu.etaxonomy.cdm.model.name.IBotanicalName;
35 import eu.etaxonomy.cdm.model.name.NameRelationship;
36 import eu.etaxonomy.cdm.model.name.Rank;
37 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
38 import eu.etaxonomy.cdm.model.name.TaxonName;
39 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
40 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
41 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42 import eu.etaxonomy.cdm.persistence.dao.name.IHomotypicalGroupDao;
43 import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
44 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
45 import eu.etaxonomy.cdm.persistence.dto.TaxonNameParts;
46 import eu.etaxonomy.cdm.persistence.query.OrderHint;
47 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
48 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
49 import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
50
51 @DataSet
52 public class TaxonNameDaoHibernateImplTest extends CdmIntegrationTest {
53
54 @SpringBeanByType
55 private ITaxonNameDao taxonNameDao;
56
57 @SpringBeanByType
58 private ITaxonDao taxonDao;
59
60 @SpringBeanByType
61 private IHomotypicalGroupDao homotypicalGroupDao;
62
63 private UUID cryptocoryneGriffithiiUuid;
64 private UUID acherontiaUuid;
65 private UUID acherontiaLachesisUuid;
66 private UUID atroposUuid;
67
68 @Before
69 public void setUp() {
70 cryptocoryneGriffithiiUuid = UUID.fromString("497a9955-5c5a-4f2b-b08c-2135d336d633");
71 acherontiaUuid = UUID.fromString("c2cab2ad-3e3a-47b8-8aa8-d9e1c0857647");
72 acherontiaLachesisUuid = UUID.fromString("7969821b-a2cf-4d01-95ec-6a5ed0ca3f69");
73 // Atropos Agassiz, 1846
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 @SuppressWarnings("rawtypes")
151 List<TypeDesignationBase> result1 = taxonNameDao.getTypeDesignations(acherontiaLachesis, null, null, null, null, null);
152
153 assertNotNull("getTypeDesignations should return a list",result1);
154 assertFalse("the list should not be empty", result1.isEmpty());
155 assertEquals("getTypeDesignations should return 1 TypeDesignationBase instance",1,result1.size());
156
157 List<SpecimenTypeDesignation> result2 = taxonNameDao.getTypeDesignations(acherontiaLachesis, SpecimenTypeDesignation.class, null, null, null, null);
158
159 assertNotNull("getTypeDesignations should return a list",result2);
160 assertFalse("the list should not be empty", result2.isEmpty());
161 assertEquals("getTypeDesignations should return 1 TypeDesignationBase instance",1,result2.size());
162 }
163
164 @Test
165 public void testCountTypeDesignations() {
166 TaxonName acherontiaLachesis = taxonNameDao.findByUuid(acherontiaLachesisUuid);
167 assert acherontiaLachesis != null : "name must exist";
168
169 long count = taxonNameDao.countTypeDesignations(acherontiaLachesis, null);
170
171 assertEquals("countTypeDesignations should return 1",1,count);
172 }
173
174 @Test
175 public void testSearchNames() {
176 List<TaxonName> result = taxonNameDao.searchNames("Atropos", null, null, null, Rank.GENUS(), null, null, null, null);
177
178 assertNotNull("searchNames should return a list",result);
179 assertFalse("the list should not be empty", result.isEmpty());
180 assertEquals("searchNames should return 3 TaxonName instances",3,result.size());
181 }
182
183 @Test
184 public void testFindTaxonNameParts_genusOrUninomial() {
185 Integer pageSize = 10;
186 Integer pageIndex = 0;
187
188 List<TaxonNameParts> resuls = taxonNameDao.findTaxonNameParts(
189 Optional.of("Atropos"), null, null, null,
190 Rank.GENUS(), null,
191 pageSize, pageIndex, Arrays.asList(new OrderHint("genusOrUninomial", SortOrder.ASCENDING)));
192
193 assertNotNull("searchNames should return a list",resuls);
194 assertFalse(resuls.isEmpty());
195 assertEquals(3, resuls.size());
196 }
197
198 @Test
199 public void testFindTaxonNameParts_genusOrUninomial_wildcard() {
200 Integer pageSize = 10;
201 Integer pageIndex = 0;
202
203 List<TaxonNameParts> results = taxonNameDao.findTaxonNameParts(
204 Optional.of("Atro*"), null, null, null,
205 Rank.GENUS(), null,
206 pageSize, pageIndex, Arrays.asList(new OrderHint("genusOrUninomial", SortOrder.ASCENDING)));
207
208 assertNotNull(results);
209 assertFalse(results.isEmpty());
210 assertEquals(3, results.size());
211
212 TaxonName n_atropos_agassiz = taxonNameDao.load(atroposUuid);
213 results = taxonNameDao.findTaxonNameParts(
214 Optional.of("Atro*"), null, null, null,
215 Rank.GENUS(), Arrays.asList(n_atropos_agassiz.getUuid()),
216 pageSize, pageIndex, Arrays.asList(new OrderHint("genusOrUninomial", SortOrder.ASCENDING)));
217
218 assertNotNull(results);
219 assertFalse(results.isEmpty());
220 assertEquals(2, results.size());
221
222 results = taxonNameDao.findTaxonNameParts(
223 Optional.of("Atro*"), null, null, null,
224 Rank.SPECIES(), null,
225 pageSize, pageIndex, Arrays.asList(new OrderHint("genusOrUninomial", SortOrder.ASCENDING)));
226
227 assertNotNull(results);
228 assertTrue(results.isEmpty());
229 }
230
231 @Test
232 public void testFindTaxonNameParts_rankSpecies() {
233 Integer pageSize = 10;
234 Integer pageIndex = 0;
235 // Manduca afflicta
236 // Manduca chinchilla
237 // Manduca bergarmatipes
238 List<TaxonNameParts> results = taxonNameDao.findTaxonNameParts(
239 Optional.of("Manduca"), null, Optional.of("*"), null,
240 Rank.SPECIES(), null,
241 pageSize, pageIndex, Arrays.asList(new OrderHint("specificEpithet", SortOrder.ASCENDING)));
242
243 assertEquals(3, results.size());
244 assertEquals("afflicta", results.get(0).getSpecificEpithet());
245 assertEquals("bergarmatipes", results.get(1).getSpecificEpithet());
246 assertEquals("chinchilla", results.get(2).getSpecificEpithet());
247
248 results = taxonNameDao.findTaxonNameParts(
249 Optional.of("Manduca"), null, Optional.of("chin*"), null,
250 Rank.SPECIES(), null,
251 pageSize, pageIndex, null);
252
253 assertEquals(1, results.size());
254 assertEquals("chinchilla", results.get(0).getSpecificEpithet());
255 }
256
257 @Test
258 public void testFindTaxonNameParts_rankBelowSpecies() {
259
260 Integer pageSize = 10;
261 Integer pageIndex = 0;
262 // Cryptocoryne x purpurea nothovar borneoensis
263 // Cryptocoryne cordata var. zonata
264 List<TaxonNameParts> results = taxonNameDao.findTaxonNameParts(
265 Optional.of("Cryptocoryne"), null, null, Optional.of("borneo*"),
266 Rank.VARIETY(), null,
267 pageSize, pageIndex, Arrays.asList(new OrderHint("specificEpithet", SortOrder.ASCENDING)));
268
269 assertEquals(1, results.size());
270
271 // now also with "infraGenericEpithet is null AND specificEpithet = purpurea"
272 results = taxonNameDao.findTaxonNameParts(
273 Optional.of("Cryptocoryne"), Optional.empty(), Optional.of("purpurea"), Optional.of("borneo*"),
274 Rank.VARIETY(), null,
275 pageSize, pageIndex, Arrays.asList(new OrderHint("specificEpithet", SortOrder.ASCENDING)));
276
277 assertEquals(1, results.size());
278 }
279
280
281 @Test
282 public void testCountNames() {
283 long count = taxonNameDao.countNames("Atropos", null, null, null, Rank.GENUS());
284
285 assertEquals("countNames should return 3",3,count);
286 }
287
288 @Test
289 public void testCountNamesByExample() {
290 TaxonName zoologicalName = TaxonNameFactory.NewZoologicalInstance(Rank.GENUS());
291 zoologicalName.setGenusOrUninomial("Atropos");
292 Set<String> includedProperties = new HashSet<>();
293 includedProperties.add("genusOrUninomial");
294 includedProperties.add("specificEpithet");
295 includedProperties.add("infraSpecificEpithet");
296 includedProperties.add("rank");
297 long count = taxonNameDao.count(zoologicalName, includedProperties);
298
299 assertEquals("countNames should return 3",3,count);
300 }
301
302 @Test
303 /**
304 * This test check for a specific bug (Ticket #686) where the rank of a taxon name base
305 * has no order index (=0)
306 */
307 public void testMissingRankOrderIndex() {
308 TaxonName acherontiaLachesis = taxonNameDao.findByUuid(acherontiaLachesisUuid);
309 Rank rank = null;
310 try {
311 rank = Rank.getRankByLatinName(acherontiaLachesis.getRank().getLabel());
312 } catch (UnknownCdmTypeException e) {
313 e.printStackTrace();
314 }
315 assertNotNull(rank);
316 assertFalse("Rank are equal, level must not be higher", rank.isHigher(acherontiaLachesis.getRank()));
317 assertFalse("Rank are equal, level must not be lower", rank.isLower(acherontiaLachesis.getRank()));
318 }
319
320 @Test
321 public void testDeleteTaxon(){
322 TaxonName acherontiaLachesis = CdmBase.deproxy(taxonNameDao.findByUuid(cryptocoryneGriffithiiUuid));
323 @SuppressWarnings("rawtypes")
324 Set<TaxonBase> taxonBases = acherontiaLachesis.getTaxonBases();
325 HomotypicalGroup group = acherontiaLachesis.getHomotypicalGroup();
326 UUID groupUuid = group.getUuid();
327 taxonNameDao.delete(acherontiaLachesis);
328
329 @SuppressWarnings("rawtypes")
330 Iterator<TaxonBase> taxa= taxonBases.iterator();
331 TaxonBase<?> taxon = taxa.next();
332 UUID taxonUuid = taxon.getUuid();
333
334 acherontiaLachesis = taxonNameDao.findByUuid(cryptocoryneGriffithiiUuid);
335 taxon = taxonDao.findByUuid(taxonUuid);
336 group = CdmBase.deproxy(homotypicalGroupDao.findByUuid(groupUuid));
337 assertNull("There should be no taxonName with the deleted uuid", acherontiaLachesis);
338 assertNull("There should be no taxon with the deleted uuid", taxon);
339 assertNull("There should be no homotypicalGroup with the deleted uuid", group);
340 }
341
342 /**
343 * Test if the listener on nomenclatural source also works if the name is retrieved
344 * from the database
345 */
346 @Test
347 public void testNomenclaturalSourceListener(){
348 UUID uuidAusAus = UUID.fromString("05a438d6-065f-49ef-84db-c7dc2c259975");
349 TaxonName ausAus = taxonNameDao.findByUuid(uuidAusAus);
350 //start condition
351 assertEquals("Aus aus, Sp. Pl.", ausAus.getFullTitleCache());
352
353 ausAus.getNomenclaturalSource().setCitationMicroReference("23");
354 assertEquals("Here the full cache should show the cache as stored in the database but did not",
355 "Aus aus, Sp. Pl.: 23", ausAus.getFullTitleCache());
356 }
357
358 @Override
359 public void createTestDataSet() throws FileNotFoundException {}
360 }