Project

General

Profile

Download (66.3 KB) Statistics
| Branch: | Tag: | Revision:
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.taxon;
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
import static org.junit.Assert.fail;
18

    
19
import java.io.FileNotFoundException;
20
import java.util.ArrayList;
21
import java.util.HashSet;
22
import java.util.List;
23
import java.util.Set;
24
import java.util.UUID;
25

    
26
import org.hibernate.Hibernate;
27
import org.hibernate.envers.query.AuditEntity;
28
import org.hibernate.envers.query.criteria.AuditCriterion;
29
import org.hibernate.proxy.HibernateProxy;
30
import org.junit.After;
31
import org.junit.Assert;
32
import org.junit.Before;
33
import org.junit.Test;
34
import org.unitils.dbunit.annotation.DataSet;
35
import org.unitils.dbunit.annotation.ExpectedDataSet;
36
import org.unitils.spring.annotation.SpringBeanByType;
37

    
38
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
39
import eu.etaxonomy.cdm.model.common.Marker;
40
import eu.etaxonomy.cdm.model.common.MarkerType;
41
import eu.etaxonomy.cdm.model.location.NamedArea;
42
import eu.etaxonomy.cdm.model.name.TaxonName;
43
import eu.etaxonomy.cdm.model.reference.Reference;
44
import eu.etaxonomy.cdm.model.taxon.Classification;
45
import eu.etaxonomy.cdm.model.taxon.Synonym;
46
import eu.etaxonomy.cdm.model.taxon.SynonymType;
47
import eu.etaxonomy.cdm.model.taxon.Taxon;
48
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
49
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
50
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
51
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
52
import eu.etaxonomy.cdm.model.view.AuditEvent;
53
import eu.etaxonomy.cdm.model.view.AuditEventRecord;
54
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
55
import eu.etaxonomy.cdm.persistence.dao.common.AuditEventSort;
56
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
57
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
58
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
59
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
60
import eu.etaxonomy.cdm.persistence.dao.term.IDefinedTermDao;
61
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
62
import eu.etaxonomy.cdm.persistence.query.GroupByCount;
63
import eu.etaxonomy.cdm.persistence.query.GroupByDate;
64
import eu.etaxonomy.cdm.persistence.query.Grouping;
65
import eu.etaxonomy.cdm.persistence.query.MatchMode;
66
import eu.etaxonomy.cdm.persistence.query.NativeSqlOrderHint;
67
import eu.etaxonomy.cdm.persistence.query.OrderHint;
68
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
69
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
70
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
71

    
72
/**
73
 * @author a.mueller
74
 * @author ben.clark
75
 */
76
public class TaxonDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
77

    
78
    @SpringBeanByType
79
    private ITaxonDao taxonDao;
80

    
81
    @SpringBeanByType
82
    private ITaxonNodeDao taxonNodeDao;
83

    
84
    @SpringBeanByType
85
    private IClassificationDao classificationDao;
86

    
87
    @SpringBeanByType
88
    private IReferenceDao referenceDao;
89

    
90
    @SpringBeanByType
91
    private IDefinedTermDao definedTermDao;
92

    
93
    private UUID uuid = UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
94
    private UUID sphingidae = UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
95
    private UUID acherontia = UUID.fromString("c5cc8674-4242-49a4-aada-72d63194f5fa");
96
    private UUID rethera = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
97
    private UUID retheraSecCdmtest = UUID.fromString("a9f42927-e507-4fda-9629-62073a908aae");
98
    private UUID atroposAgassiz = UUID.fromString("d75b2e3d-7394-4ada-b6a5-93175b8751c1"); // a Synonym
99
    private UUID atroposOken = UUID.fromString("6bfedf25-6dbc-4d5c-9d56-84f9052f3b2a");  // a Synonym
100
    private UUID atroposLeach = UUID.fromString("3da4ab34-6c50-4586-801e-732615899b07"); // a Synonym
101
    private UUID acherontiaLachesis = UUID.fromString("b04cc9cb-2b4a-4cc4-a94a-3c93a2158b06");
102
    private UUID aus = UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2");
103

    
104
    private UUID UUID_ACHERONTIA_NODE = UUID.fromString("56b10cf0-9522-407e-9f90-0c2dba263c94");
105
    private UUID UUID_CLASSIFICATION2 = UUID.fromString("a71467a6-74dc-4148-9530-484628a5ab0e");
106

    
107
    private AuditEvent previousAuditEvent;
108
    private AuditEvent mostRecentAuditEvent;
109

    
110
    private UUID northernAmericaUuid;
111
    private UUID southernAmericaUuid;
112

    
113
    private UUID classificationUuid;
114

    
115
    private boolean includeUnpublished = true;
116
    private boolean NO_UNPUBLISHED = false;
117

    
118
    private static final boolean doTaxa = true;
119
    private static final boolean noTaxa = false;
120
    private static final boolean doSynonyms = true;
121
    private static final boolean noSynonyms = false;
122
    private static final boolean doMisapplied = true;
123
    private static final boolean noMisapplied = false;
124
    private static final boolean doCommonNames = true;
125
    private static final boolean noCommonNames = false;
126

    
127

    
128

    
129
    @SuppressWarnings("unused")
130
    private static final String[] TABLE_NAMES = new String[] {
131
        "HOMOTYPICALGROUP", "HOMOTYPICALGROUP_AUD", "REFERENCE", "REFERENCE_AUD", "TAXONBASE", "TAXONBASE_AUD"
132
        , "TAXONNAME", "TAXONNAME_AUD", "TAXONRELATIONSHIP", "TAXONRELATIONSHIP_AUD" };
133

    
134

    
135
    @Before
136
    public void setUp() {
137

    
138

    
139
        previousAuditEvent = new AuditEvent();
140
        previousAuditEvent.setRevisionNumber(1025);
141
        previousAuditEvent.setUuid(UUID.fromString("a680fab4-365e-4765-b49e-768f2ee30cda"));
142
        mostRecentAuditEvent = new AuditEvent();
143
        mostRecentAuditEvent.setRevisionNumber(1026);
144
        mostRecentAuditEvent.setUuid(UUID.fromString("afe8e761-8545-497b-9134-6a6791fc0b0d"));
145
        AuditEventContextHolder.clearContext(); // By default we're in the current view (i.e. view == null)
146

    
147
        northernAmericaUuid = UUID.fromString("2757e726-d897-4546-93bd-7951d203bf6f");
148
        southernAmericaUuid = UUID.fromString("6310b3ba-96f4-4855-bb5b-326e7af188ea");
149

    
150
        classificationUuid = UUID.fromString("aeee7448-5298-4991-b724-8d5b75a0a7a9");
151
        includeUnpublished = true;
152
    }
153

    
154
    @After
155
    public void tearDown() {
156
        AuditEventContextHolder.clearContext();
157
    }
158

    
159
    /**
160
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#TaxonDaoHibernateImpl()}.
161
     */
162
    @Test
163
    @DataSet
164
    public void testInit() {
165
        logger.warn("testInit()");
166
        assertNotNull("Instance of ITaxonDao expected",taxonDao);
167
        assertNotNull("Instance of IReferenceDao expected",referenceDao);
168
    }
169

    
170
    @Test
171
    @DataSet
172
    public void testGetTaxaByName() {
173

    
174
        includeUnpublished= true;
175
        boolean accepted = true;
176

    
177
        Reference sec = referenceDao.findById(1);
178
        assert sec != null : "sec must exist";
179

    
180
        @SuppressWarnings("rawtypes")
181
        List<TaxonBase> results = taxonDao.getTaxaByName("Aus", includeUnpublished, sec);
182
        assertNotNull("getTaxaByName should return a List", results);
183
        //assertFalse("The list should not be empty", results.isEmpty());
184
        assertEquals(1, results.size());
185

    
186
        results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING, accepted, includeUnpublished, null, null);
187
        assertNotNull("getTaxaByName should return a List", results);
188

    
189
        int numberOfTaxaByName_A = 5;
190

    
191
        //logger.setLevel(Level.DEBUG); //FIXME #######################
192
        if (logger.isDebugEnabled()) {
193
            for (int i = 0; i < results.size(); i++) {
194
                String nameCache = "";
195
                TaxonName taxonName= results.get(i).getName();
196
                nameCache = taxonName.getNameCache();
197
                logger.debug(results.get(i).getClass() + "(" + i +")" +
198
                        ": Name Cache = " + nameCache + ", Title Cache = " + results.get(i).getTitleCache());
199
            }
200
        }
201

    
202
        assertEquals(numberOfTaxaByName_A, results.size());
203

    
204
        includeUnpublished = false;
205
        results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING, accepted, includeUnpublished, null, null);
206
        assertEquals(numberOfTaxaByName_A, results.size());  // no unpublished yet
207

    
208

    
209
        //System.err.println("Species group: " + Rank.SPECIESGROUP().getId() + "Species: " + Rank.SPECIES().getId() + "Section Botany: "+ Rank.SECTION_BOTANY());
210

    
211
//		assertEquals(results.get(0).getTitleCache(), "Abies sec. ???");
212
//		assertEquals(results.get(1).getTitleCache(), "Abies Mill.");
213
//		assertEquals(results.get(2).getTitleCache(), "Abies mill. sec. ???");
214
//		assertEquals(results.get(3).getTitleCache(), "Abies alba sec. ???");
215
//		assertEquals(results.get(4).getTitleCache(), "Abies alba Michx. sec. ???");
216
//		assertEquals(results.get(5).getTitleCache(), "Abies alba Mill. sec. ???");
217

    
218
        includeUnpublished = true;
219
        results = taxonDao.getTaxaByName("A*", MatchMode.BEGINNING, accepted, includeUnpublished, null, null);
220
        assertNotNull("getTaxaByName should return a List", results);
221
        assertEquals(numberOfTaxaByName_A, results.size());
222

    
223
        results = taxonDao.getTaxaByName("Aus", MatchMode.EXACT, accepted, includeUnpublished, null, null);
224
        assertNotNull("getTaxaByName should return a List", results);
225
        assertEquals("Results list should contain one entity",1,results.size());
226
    }
227

    
228

    
229
    @Test
230
    @DataSet (loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
231
    public void testGetTaxaByNameWithMisappliedNames(){
232

    
233
        Classification classification = classificationDao.load(classificationUuid);
234
        TaxonNode subtree = null;
235
        /* NOTE:
236
         * The testdata contains 3 misapplied names (1. nameCache = Aus, 2. nameCache = Rethera, 3. nameCache = Daphnis),
237
         * two contained in the classification used in this test,
238
         * the other one is not contained in any classification. This later case is the more general situation.
239
         * Misapplied names should be found regardless of whether they are contained in a classification or not.
240
         */
241
        //two accepted taxa starting with R in classification "TestBaum"
242
        @SuppressWarnings("rawtypes")
243
        List<TaxonBase> results = taxonDao.getTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "R*", classification, subtree, MatchMode.BEGINNING,
244
                null, includeUnpublished, null, null, null, null);
245
        Assert.assertEquals("There should be 2 Taxa", 2, results.size());
246

    
247
        //three taxa, 2 accepted and 1 misapplied name starting with R
248
        results = taxonDao.getTaxaByName(doTaxa, noSynonyms, doMisapplied, noCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
249
                null, includeUnpublished, null, null, null, null);
250
        Assert.assertEquals("There should be 3 Taxa", 3, results.size());
251

    
252
        //one synonym has no accepted taxon
253
        results = taxonDao.getTaxaByName(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, "A*", null, subtree, MatchMode.BEGINNING,
254
                null, includeUnpublished, null, null, null, null);
255
        Assert.assertEquals("There should be 11 Taxa",11, results.size());
256

    
257
        //two accepted taxa in classification and 1 misapplied name with accepted name in classification
258
        results = taxonDao.getTaxaByName(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, "R*", classification, subtree, MatchMode.BEGINNING,
259
                null, includeUnpublished, null, null, null, null);
260
        Assert.assertEquals("There should be 3 Taxa", 3, results.size());
261
        //same with unpublished
262
        includeUnpublished = false;
263
        results = taxonDao.getTaxaByName(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, "R*", classification, subtree, MatchMode.BEGINNING,
264
                null, includeUnpublished, null, null, null, null);
265
        Assert.assertEquals("There should be 3 Taxa", 3, results.size());
266
        includeUnpublished = true;
267

    
268

    
269
        //same as above because all taxa, synonyms and misapplied names starting with R are in the classification
270
        results = taxonDao.getTaxaByName(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
271
                null, includeUnpublished, null, null, null, null);
272
        Assert.assertEquals("There should be 3 Taxa", 3, results.size());
273

    
274
        //find misapplied names with accepted taxon in the classification, the accepted taxa of two misapplied names are in the classification
275
        results = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, noCommonNames, false, "*", classification, subtree, MatchMode.BEGINNING,
276
                null, includeUnpublished, null, null, null, null);
277
        Assert.assertEquals("There should be 2 Taxa", 2, results.size());
278

    
279
        //find misapplied names beginning with R
280
        results = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, noCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
281
                null, includeUnpublished, null, null, null, null);
282
        Assert.assertEquals("There should be 1 Taxa", 1, results.size());
283

    
284
        //find all three misapplied names
285
        results = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, noCommonNames, false, "*", null, subtree, MatchMode.BEGINNING,
286
                null, includeUnpublished, null, null, null, null);
287
        Assert.assertEquals("There should be 3 Taxa", 3, results.size());
288

    
289
    }
290
    /**
291
     * Test for all not covered possibilities of searches
292
     */
293
    @Test
294
    @DataSet (loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
295
    public void testGetTaxaByNameVariants(){
296
        TaxonNode subtree = null;
297
        @SuppressWarnings("rawtypes")
298
        List<TaxonBase> results = taxonDao.getTaxaByName(noTaxa, noSynonyms, noMisapplied, doCommonNames, false, "c*", null, subtree, MatchMode.BEGINNING,
299
                null, includeUnpublished, null, null, null, null);
300
        Assert.assertEquals("There should be 2 Taxa",2, results.size());
301

    
302
        results = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, doCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
303
                null, includeUnpublished, null, null, null, null);
304
        Assert.assertEquals("There should be 1 Taxa", 1, results.size());
305

    
306
        results = taxonDao.getTaxaByName(noTaxa, doSynonyms, doMisapplied, doCommonNames, false, "R*", null, subtree, MatchMode.BEGINNING,
307
                null, includeUnpublished, null, null, null, null);
308
        Assert.assertEquals("There should be 1 Taxa", 1, results.size());
309

    
310
        results = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, doCommonNames, false, "c*", null, subtree, MatchMode.BEGINNING,
311
                null, includeUnpublished, null, null, null, null);
312
        Assert.assertEquals("There should be 2 Taxa", 2, results.size());
313

    
314
        results = taxonDao.getTaxaByName(doTaxa, noSynonyms, noMisapplied, doCommonNames, false, "c*", null, subtree, MatchMode.BEGINNING,
315
                null, includeUnpublished, null, null, null, null);
316
        Assert.assertEquals("There should be 2 Taxa", 2, results.size());
317

    
318
        Classification classification = classificationDao.load(classificationUuid);
319
        results = taxonDao.getTaxaByName(noTaxa, noSynonyms, noMisapplied, doCommonNames, false, "c*", classification, subtree, MatchMode.BEGINNING,
320
                null, includeUnpublished, null, null, null, null);
321
        Assert.assertEquals("There should be 1 Taxa", 1, results.size());
322

    
323
        Set<NamedArea> namedAreas = new HashSet<>();
324
        namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
325
        results = taxonDao.getTaxaByName(noTaxa, noSynonyms, noMisapplied, doCommonNames, false, "c*", null, subtree, MatchMode.BEGINNING,
326
                namedAreas, includeUnpublished, null, null, null, null);
327
        Assert.assertEquals("There should be 1 Taxa", 1, results.size());
328
    }
329

    
330
    /**
331
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}.
332
     */
333
    @Test
334
    @DataSet
335
    public void testGetTaxaByNameForEditor() {
336
        TaxonNode subtree = null;
337
        Reference sec = referenceDao.findById(1);
338
        assert sec != null : "sec must exist";
339

    
340
        @SuppressWarnings("rawtypes")
341
        List<UuidAndTitleCache<? extends IdentifiableEntity>> results = taxonDao.getTaxaByNameForEditor(
342
                doTaxa, doSynonyms, noMisapplied, noCommonNames, false, includeUnpublished, "Acher", null, subtree, MatchMode.BEGINNING, null, null);
343
        assertNotNull("getTaxaByName should return a List", results);
344
        assertFalse("The list should not be empty", results.isEmpty());
345
        assertEquals(4, results.size());
346

    
347
        results = taxonDao.getTaxaByNameForEditor(doTaxa, doSynonyms, noMisapplied, noCommonNames, false,includeUnpublished, "A", null, subtree, MatchMode.BEGINNING, null, null);
348
        assertNotNull("getTaxaByName should return a List", results);
349
        assertEquals(7, results.size());
350

    
351
        results = taxonDao.getTaxaByNameForEditor(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, includeUnpublished, "A", null, subtree, MatchMode.BEGINNING, null, null);
352
        assertNotNull("getTaxaByName should return a List", results);
353
        assertEquals(5, results.size());
354
        assertEquals(results.get(0).getType(), Taxon.class);
355

    
356
        results = taxonDao.getTaxaByNameForEditor(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, includeUnpublished, "A", null, subtree, MatchMode.BEGINNING, null, null);
357
        assertNotNull("getTaxaByName should return a List", results);
358
        assertEquals(2, results.size());
359
        assertEquals(results.get(0).getType(), Synonym.class);
360

    
361
        results = taxonDao.getTaxaByNameForEditor(doTaxa, doSynonyms, noMisapplied, noCommonNames, false,includeUnpublished, "Aus", null, subtree, MatchMode.EXACT, null, null);
362
        assertNotNull("getTaxaByName should return a List", results);
363
        assertEquals("Results list should contain one entity",1,results.size());
364

    
365
        results = taxonDao.getTaxaByNameForEditor(doTaxa, doSynonyms, doMisapplied, noCommonNames, false, includeUnpublished, "A", null, subtree, MatchMode.BEGINNING, null, null);
366
        assertNotNull("getTaxaByName should return a List", results);
367
        assertEquals("Results list should contain one entity", 8, results.size());
368

    
369
        //TODO: test the search for misapplied names
370

    
371
    }
372

    
373

    
374
    /**
375
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#getTaxaByName(java.lang.String, eu.etaxonomy.cdm.model.reference.Reference)}
376
     * restricting the search by a set of Areas.
377
     */
378
    @Test
379
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
380
    public void testGetTaxaByNameAndArea() {
381
        TaxonNode subtree = null;
382
        Set<NamedArea> namedAreas = new HashSet<>();
383
        namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
384
        //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
385
        //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
386

    
387
        Classification classification = classificationDao.findByUuid(classificationUuid);
388

    
389
        // prepare some synonym relation ships for some tests
390
        Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
391
        Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
392
        taxonRethera.addSynonym(synAtroposAgassiz, SynonymType.SYNONYM_OF());
393
//        logger.warn("addSynonym(..)");
394

    
395
        Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
396
        Taxon taxonRetheraSecCdmTest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
397
        taxonRetheraSecCdmTest.addSynonym(synAtroposLeach, SynonymType.SYNONYM_OF());
398
        this.taxonDao.save(taxonRetheraSecCdmTest);
399

    
400
        Taxon test = (Taxon)this.taxonDao.findByUuid(retheraSecCdmtest);
401
//        Set<Synonym> synonyms3 = test.getSynonyms();
402
        // 1. searching for a taxon (Rethera)
403
        //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
404

    
405
        @SuppressWarnings("rawtypes")
406
        List<TaxonBase> results = taxonDao.getTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "Rethera", null, subtree, MatchMode.BEGINNING, namedAreas,
407
                includeUnpublished, null, null, null, null);
408
        assertNotNull("getTaxaByName should return a List", results);
409
        assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
410

    
411
        results = taxonDao.getTaxaByName(noTaxa, noSynonyms, noMisapplied, doCommonNames, false, "com*", null, subtree, MatchMode.BEGINNING, namedAreas,
412
                includeUnpublished, null, null, null, null);
413
            assertNotNull("getTaxaByName should return a List", results);
414
            assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
415

    
416
        // 2. searching for a taxon (Rethera) contained in a specific classification
417
        results = taxonDao.getTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "Rethera", classification, subtree, MatchMode.BEGINNING, namedAreas,
418
                includeUnpublished, null, null, null, null);
419
        assertNotNull("getTaxaByName should return a List", results);
420
        assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
421

    
422

    
423
        // 3. searching for Synonyms
424
        results = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, "Atropo", null, subtree, MatchMode.ANYWHERE, null,
425
                includeUnpublished, null, null, null, null);
426
        assertNotNull("getTaxaByName should return a List", results);
427
        /*System.err.println(results.get(0).getTitleCache() + " - " +results.get(1).getTitleCache() + " - " +results.get(2).getTitleCache() );
428

    
429

    
430
        System.err.println(((Synonym)results.get(0)).getAcceptedTaxa().contains(taxonRethera)+ " - " +((Synonym)results.get(1)).getAcceptedTaxa().contains(taxonRethera)+ " - "  +((Synonym)results.get(2)).getAcceptedTaxa().contains(taxonRethera)+ " - "  );
431
        */
432
        assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
433

    
434
        // 4. searching for Synonyms
435
        results = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false,"Atropo", null, subtree, MatchMode.BEGINNING, null,
436
                includeUnpublished, null, null, null, null);
437
        assertNotNull("getTaxaByName should return a List", results);
438
        assertTrue("expected to find three taxa but found "+results.size(), results.size() == 3);
439

    
440

    
441
        // 5. searching for a Synonyms and Taxa
442
        results = taxonDao.getTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false,"A", null, subtree, MatchMode.BEGINNING, namedAreas,
443
                includeUnpublished, null, null, null, null);
444
        //only five taxa have a distribution
445
        assertNotNull("getTaxaByName should return a List", results);
446
        assertTrue("expected to find 8 taxa but found "+results.size(), results.size() == 8);
447
    }
448

    
449

    
450
    /**
451
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.taxon.TaxonDaoHibernateImpl#findByNameTitleCache(Class<? extends TaxonBase>clazz, String queryString, Classification classification, TaxonNode subtree, MatchMode matchMode, Set<NamedArea> namedAreas, Integer pageNumber, Integer pageSize, List<String> propertyPaths)}
452
     * restricting the search by a set of Areas.
453
     */
454
    @Test
455
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
456
    public void testFindByNameTitleCache() {
457
        TaxonNode subtree = null;
458

    
459
        Set<NamedArea> namedAreas = new HashSet<>();
460
        namedAreas.add((NamedArea)definedTermDao.load(northernAmericaUuid));
461
        //namedAreas.add((NamedArea)definedTermDao.load(southernAmericaUuid));
462
        //namedAreas.add((NamedArea)definedTermDao.load(antarcticaUuid));
463

    
464
        Classification classification = classificationDao.findByUuid(classificationUuid);
465

    
466
        // prepare some synonym relation ships for some tests
467
        Synonym synAtroposAgassiz = (Synonym)taxonDao.findByUuid(atroposAgassiz);
468
        Taxon taxonRethera = (Taxon)taxonDao.findByUuid(rethera);
469
        taxonRethera.addSynonym(synAtroposAgassiz, SynonymType.SYNONYM_OF());
470
        //logger.warn("addSynonym(..)");
471
        this.taxonDao.clear();
472
        Synonym synAtroposLeach = (Synonym)taxonDao.findByUuid(atroposLeach);
473
        Taxon taxonRetheraSecCdmtest = (Taxon)taxonDao.findByUuid(retheraSecCdmtest);
474
        taxonRetheraSecCdmtest.addSynonym(synAtroposLeach, SynonymType.SYNONYM_OF());
475
        this.taxonDao.clear();
476

    
477
        // 1. searching for a taxon (Rethera)
478
        //long numberOfTaxa = taxonDao.countTaxaByName(Taxon.class, "Rethera", null, MatchMode.BEGINNING, namedAreas);
479

    
480
        @SuppressWarnings("rawtypes")
481
        List<TaxonBase> results = taxonDao.findByNameTitleCache(doTaxa, noSynonyms, includeUnpublished, "Rethera Rothschild & Jordan, 1903", null, subtree, MatchMode.EXACT, namedAreas,
482
                null, null, null, null);
483
        assertNotNull("getTaxaByName should return a List", results);
484
        assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
485

    
486
        // 2. searching for a taxon (Rethera) contained in a specific classification
487
        results = taxonDao.findByNameTitleCache(doTaxa, noSynonyms, includeUnpublished, "Rethera Rothschild & Jordan, 1903", classification, subtree, MatchMode.EXACT, namedAreas,
488
                null, null, null, null);
489
        assertNotNull("getTaxaByName should return a List", results);
490
        assertTrue("expected to find one taxon but found "+results.size(), results.size() == 1);
491

    
492
        // 3. searching for Synonyms
493
        results = taxonDao.findByNameTitleCache(noTaxa, doSynonyms, includeUnpublished, "*Atropo", null, subtree, MatchMode.ANYWHERE, null,
494
                null, null, null, null);
495
        assertNotNull("getTaxaByName should return a List", results);
496
        assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
497

    
498
        // 4. searching for Synonyms
499
        results = taxonDao.findByNameTitleCache(noTaxa, doSynonyms, includeUnpublished, "Atropo", null, subtree, MatchMode.BEGINNING, null,
500
                null, null, null, null);
501
        assertNotNull("getTaxaByName should return a List", results);
502
        assertTrue("expected to find two taxa but found "+results.size(), results.size() == 2);
503

    
504
        // 5. searching for a Synonyms and Taxa
505
        //   attache a synonym first
506
        Synonym syn = (Synonym)taxonDao.findByUuid(this.atroposLeach);
507
        Taxon tax = (Taxon) taxonDao.findByUuid(rethera);
508
        tax.addSynonym(syn, SynonymType.HETEROTYPIC_SYNONYM_OF());
509

    
510
        taxonDao.save(tax);
511
        results = taxonDao.findByNameTitleCache(doTaxa, doSynonyms, includeUnpublished, "A", null, subtree, MatchMode.BEGINNING, namedAreas,
512
                null, null, null, null);
513
        assertNotNull("getTaxaByName should return a List", results);
514
        assertTrue("expected to find 8 taxa but found "+results.size(), results.size() == 8);
515
    }
516

    
517
    @Test
518
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
519
    public void testTaxonNameInTwoClassifications(){
520
        TaxonNode subtree = null;
521
        List<String> propertyPaths = new ArrayList<>();
522
        propertyPaths.add("taxonNodes");
523
        @SuppressWarnings("rawtypes")
524
        List<TaxonBase> taxa = taxonDao.getTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false,"P", null, subtree, MatchMode.BEGINNING,
525
                null, includeUnpublished, null, null, null, null);
526
        Taxon taxon = (Taxon)taxa.get(0);
527
        Set<TaxonNode> nodes = taxon.getTaxonNodes();
528
        assertTrue(nodes.size() == 1);
529
        //assertNotNull(taxa);
530
        //assertTrue(taxa.size() > 0);
531
    }
532

    
533
    @Test
534
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
535
    public void testGetTaxaByNameProParteSynonym(){
536
        TaxonNode subtree = null;
537
        @SuppressWarnings("rawtypes")
538
        List<TaxonBase> taxa = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", null,subtree,
539
                MatchMode.BEGINNING, null, includeUnpublished, null, null, null, null);
540
        Assert.assertEquals("2 synonyms and 1 pro parte synonym should be returned.", 3, taxa.size());
541
        assertTrue("Pro parte should exist", existsInCollection(taxa, acherontiaLachesis));
542
        assertTrue("Normal synonym should exist", existsInCollection(taxa, atroposAgassiz));
543
        assertTrue("2. normal synonym should exist", existsInCollection(taxa, atroposOken));
544
        //TODO shouldn't we also find orphaned synonyms (without accepted taxon) like Atropos Leach?
545

    
546
        taxa = taxonDao.getTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", null,subtree,
547
                MatchMode.BEGINNING, null, NO_UNPUBLISHED, null, null, null, null);
548
        Assert.assertEquals("2 synonyms and no pro parte synonym should be returned.", 2, taxa.size());
549
        assertTrue("Normal synonym should exist", existsInCollection(taxa, atroposAgassiz));
550
        assertTrue("2. normal synonym should exist", existsInCollection(taxa, atroposOken));
551

    
552
        taxa = taxonDao.getTaxaByName(noTaxa, noSynonyms, doMisapplied, noCommonNames, false, "A", null,subtree,
553
                MatchMode.BEGINNING, null, includeUnpublished, null, null, null, null);
554
        Assert.assertEquals("1 misapplied name, no pro parte synonym should be returned.", 1, taxa.size());
555
        assertTrue("Pro parte should exist", existsInCollection(taxa, aus));
556

    
557
    }
558

    
559
    @Test
560
    @DataSet
561
    public void testFindByUuid() {
562
        Taxon taxon = (Taxon)taxonDao.findByUuid(uuid);
563
        assertNotNull("findByUuid should return a taxon", taxon);
564
        assertFalse("findByUuid should not return a taxon with it's name initialized",Hibernate.isInitialized(taxon.getName()));
565
    }
566

    
567
    @Test
568
    @DataSet
569
    public void testLoad() {
570
        List<String> propertyPaths = new ArrayList<>();
571
        propertyPaths.add("name");
572
        propertyPaths.add("sec");
573
        Taxon taxon = (Taxon)taxonDao.load(uuid, propertyPaths);
574
        assertNotNull("findByUuid should return a taxon",taxon);
575
        assertTrue("load should return a taxon with it's name initialized, given that the property was specified in the method",Hibernate.isInitialized(taxon.getName()));
576
        assertTrue("load should return a taxon with it's secundum reference initialized, given that the property was specified in the method",Hibernate.isInitialized(taxon.getSec()));
577
    }
578

    
579
    @Test
580
    @DataSet
581
    public void testCountTaxonRelationshipsByTaxon()	{
582
        Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
583
        assert taxon != null : "taxon must exist";
584

    
585
        long numberOfRelatedTaxa = taxonDao.countTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
586
                includeUnpublished, TaxonRelationship.Direction.relatedTo);
587
        assertEquals("countTaxonRelationships should return 8", 8, numberOfRelatedTaxa);
588
    }
589

    
590
    @Test
591
    @DataSet
592
    public void testCountTaxaByName() {
593
        TaxonNode subtree = null;
594
        Classification classification= null;
595
        long numberOfTaxa = taxonDao.countTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames,false, "A", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
596
        assertEquals(5, numberOfTaxa);
597
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "S", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
598
        assertEquals("Sphingidae, Smerinthus, Smerinthus kindermannii and Sphingonaepiopsis expected", 4, numberOfTaxa);
599
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "Smerinthus kindermannii", classification, subtree, MatchMode.EXACT, null, includeUnpublished);
600
        assertEquals(1, numberOfTaxa);
601
        numberOfTaxa = taxonDao.countTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
602
        assertEquals(2, numberOfTaxa);
603
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
604
        assertEquals(7, numberOfTaxa);
605
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false, "Aasfwerfwf fffe", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
606
        assertEquals(0, numberOfTaxa);
607

    
608
        subtree = taxonNodeDao.findByUuid(UUID_ACHERONTIA_NODE);
609
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "A", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
610
        assertEquals("Acherontia and 2 A. species expected", 3, numberOfTaxa);
611
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "S", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
612
        assertEquals("", 0, numberOfTaxa);
613
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "Smerinthus kindermannii", classification, subtree, MatchMode.EXACT, null, includeUnpublished);
614
        assertEquals("Smerinthus is not in subtree", 0, numberOfTaxa);
615
        numberOfTaxa = taxonDao.countTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
616
        assertEquals("Atropos Agassiz and Atropos Oken expected as Synonyms", 2, numberOfTaxa);
617
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
618
        assertEquals("The above accepted and synonyms expected", 5, numberOfTaxa);
619
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false, "Aasfwerfwf fffe", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
620
        assertEquals(0, numberOfTaxa);
621

    
622
        classification = classificationDao.findByUuid(UUID_CLASSIFICATION2);
623
        subtree = null;
624
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "A", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
625
        assertEquals("Acherontia and 2 A. species expected", 3, numberOfTaxa);
626
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "S", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
627
        assertEquals("Sphingidae expected", 1, numberOfTaxa);
628
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, noSynonyms, noMisapplied, noCommonNames, false, "Smerinthus kindermannii", classification, subtree, MatchMode.EXACT, null, includeUnpublished);
629
        assertEquals("Smerinthus is not in subtree", 0, numberOfTaxa);
630
        numberOfTaxa = taxonDao.countTaxaByName(noTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
631
        assertEquals("Atropos Agassiz and Atropos Oken expected as Synonyms", 2, numberOfTaxa);
632
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false, "A", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
633
        assertEquals("The above accepted and synonyms expected", 5, numberOfTaxa);
634
        numberOfTaxa = taxonDao.countTaxaByName(doTaxa, doSynonyms, noMisapplied, noCommonNames, false, "Aasfwerfwf fffe", classification, subtree, MatchMode.BEGINNING, null, includeUnpublished);
635
        assertEquals(0, numberOfTaxa);
636
    }
637

    
638
    @Test
639
    @DataSet
640
    public void testRelatedTaxa() {
641
        Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
642
        assert taxon != null : "taxon must exist";
643

    
644
        List<String> propertyPaths = new ArrayList<>();
645
        propertyPaths.add("fromTaxon");
646
        propertyPaths.add("fromTaxon.name");
647
        List<OrderHint> orderHints = new ArrayList<>();
648
        orderHints.add(new OrderHint("relatedFrom.name.genusOrUninomial", SortOrder.ASCENDING));
649
        orderHints.add(new OrderHint("relatedFrom.name.specificEpithet", SortOrder.ASCENDING));
650
        orderHints.add(new OrderHint("relatedFrom.name.infraSpecificEpithet", SortOrder.ASCENDING));
651

    
652
        long count = taxonDao.countTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
653
                includeUnpublished, TaxonRelationship.Direction.relatedTo);
654
        assertEquals("Count should return 8 (related taxa)", 8, count);
655

    
656
        List<TaxonRelationship> relatedTaxa = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
657
                includeUnpublished, null, null, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
658
        assertNotNull("getRelatedTaxa should return a List",relatedTaxa);
659
        assertEquals("getRelatedTaxa should return all 8 related taxa", 8, relatedTaxa.size());
660
        assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon()));
661
        assertTrue("getRelatedTaxa should return TaxonRelationship objects with the relatedFrom taxon initialized",Hibernate.isInitialized(relatedTaxa.get(0).getFromTaxon().getName()));
662

    
663
        assertEquals("Acherontia should appear first in the list of related taxa", relatedTaxa.get(0).getFromTaxon().getTitleCache(), "Acherontia Laspeyres, 1809 sec. cate-sphingidae.org");
664
        assertEquals("Sphingonaepiopsis should appear last in the list of related taxa", "Sphingonaepiopsis Wallengren, 1858 sec. cate-sphingidae.org", relatedTaxa.get(relatedTaxa.size()-1).getFromTaxon().getTitleCache());
665
    }
666

    
667
    @Test
668
    @DataSet
669
    public void testGetRelatedTaxaPaged()	{
670
        Taxon taxon = (Taxon)taxonDao.findByUuid(sphingidae);
671
        assert taxon != null : "taxon must exist";
672

    
673
        List<String> propertyPaths = new ArrayList<>();
674
        propertyPaths.add("fromTaxon");
675
        propertyPaths.add("fromTaxon.name");
676

    
677
        List<OrderHint> orderHints = new ArrayList<>();
678
        orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
679

    
680
        int pageSize = 3;
681
        List<TaxonRelationship> firstPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
682
                includeUnpublished, pageSize, 0, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
683
        List<TaxonRelationship> secondPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
684
                includeUnpublished, pageSize, 1, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
685
        List<TaxonRelationship> thirdPage = taxonDao.getTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
686
                includeUnpublished, pageSize, 2, orderHints,propertyPaths, TaxonRelationship.Direction.relatedTo);
687

    
688
        assertNotNull("getRelatedTaxa: 3, 0 should return a List",firstPage);
689
        assertEquals("getRelatedTaxa: 3, 0 should return a List with 3 elements", pageSize,firstPage.size());
690
        assertNotNull("getRelatedTaxa: 3, 1 should return a List",secondPage);
691
        assertEquals("getRelatedTaxa: 3, 1 should return a List with 3 elements", pageSize, secondPage.size());
692
        assertNotNull("getRelatedTaxa: 3, 2 should return a List",thirdPage);
693
        assertEquals("getRelatedTaxa: 3, 2 should return a List with 2 elements", 2, thirdPage.size());
694
    }
695

    
696
    @Test
697
    @DataSet
698
    public void testCountSynonyms() {
699
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
700
        assert taxon != null : "taxon must exist";
701

    
702
        long numberOfRelatedSynonym = taxonDao.countSynonyms(taxon,null);
703
        assertEquals("countSynonyms should return 3", 3, numberOfRelatedSynonym);
704
    }
705

    
706
    @Test
707
    @DataSet
708
    public void testGetSynonyms()	{
709
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
710
        assert taxon != null : "taxon must exist";
711
        List<String> propertyPaths = new ArrayList<String>();
712
        propertyPaths.add("synonym");
713
        propertyPaths.add("synonym.name");
714

    
715
        List<OrderHint> orderHints = new ArrayList<>();
716
        orderHints.add(new OrderHint("titleCache", SortOrder.ASCENDING));
717

    
718
        List<Synonym> synonyms = taxonDao.getSynonyms(taxon, null, null, null,orderHints,propertyPaths);
719

    
720
        assertNotNull("getSynonyms should return a List", synonyms);
721
        assertEquals("getSynonyms should return 3 synonyms", 3, synonyms.size());
722
        assertTrue("getSynonyms should return synonym objects with the synonym initialized", Hibernate.isInitialized(synonyms.get(0)));
723
    }
724

    
725
    @Test
726
    @DataSet
727
    public void testCountSynonymsByType()	{
728
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
729
        assert taxon != null : "taxon must exist";
730

    
731
        long numberOfTaxonomicSynonyms = taxonDao.countSynonyms(taxon, SynonymType.HETEROTYPIC_SYNONYM_OF());
732
        assertEquals("countSynonyms should return 4", 3, numberOfTaxonomicSynonyms);
733
    }
734

    
735
    @Test
736
    @DataSet
737
    public void testSynonymsByType() {
738
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
739
        assert taxon != null : "taxon must exist";
740

    
741
        List<Synonym> synonyms = taxonDao.getSynonyms(taxon, SynonymType.HETEROTYPIC_SYNONYM_OF(), null, null,null,null);
742

    
743
        assertNotNull("getSynonyms should return a List", synonyms);
744
        assertEquals("getSynonyms should return 4 Synonyms", 3, synonyms.size());
745
    }
746

    
747
    @Test
748
    @DataSet
749
    public void testPageSynonyms(){
750
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
751
        assert taxon != null : "taxon must exist";
752

    
753
        int pageSize = 2;
754
        List<Synonym> firstPage = taxonDao.getSynonyms(taxon, null, pageSize, 0,null,null);
755
        List<Synonym> secondPage = taxonDao.getSynonyms(taxon, null, pageSize, 1,null,null);
756

    
757
        assertNotNull("getSynonyms: 2, 0 should return a List",firstPage);
758
        assertEquals("getSynonyms: 2, 0 should return 2 synonyms", pageSize,firstPage.size());
759
        assertNotNull("getSynonyms: 2, 1 should return a List",secondPage);
760
        assertEquals("getSynonyms: 2, 1 should return 1 synonym", 1, secondPage.size());
761
    }
762

    
763
    @Test
764
    @DataSet
765
    public void testCountTaxonRelationships() {
766
        long count = taxonDao.countTaxonRelationships(null);
767
        assertEquals("There should be 11 relationships", 11, count);
768

    
769
        Set<TaxonRelationshipType> types = new HashSet<>();
770
        count = taxonDao.countTaxonRelationships(types);
771
        assertEquals("Empty filter should return empty result", 0, count);
772

    
773
        types.add(TaxonRelationshipType.CONGRUENT_TO());
774
        count = taxonDao.countTaxonRelationships(types);
775
        assertEquals("There should be no congruent relationship", 0, count);
776

    
777
        types.add(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN());
778
        count = taxonDao.countTaxonRelationships(types);
779
        assertEquals("There should be 11 tax included relationships", 11, count);
780
    }
781

    
782
    @Test
783
    @DataSet
784
    public void testlistTaxonRelationships() {
785
        List<TaxonRelationship> rels = taxonDao.getTaxonRelationships(null, null, null, null, null);
786
        assertEquals("There should be 11 relationships", 11, rels.size());
787

    
788
        rels = taxonDao.getTaxonRelationships(null, 2, 3, null, null);
789
        assertEquals("There should be 11 relationships", 2, rels.size());
790

    
791
        Set<TaxonRelationshipType> types = new HashSet<>();
792
        rels = taxonDao.getTaxonRelationships(types, null, null, null, null);
793
        assertEquals("Empty filter should return empty result", 0, rels.size());
794

    
795
        types.add(TaxonRelationshipType.CONGRUENT_TO());
796
        rels = taxonDao.getTaxonRelationships(types, null, null, null, null);
797
        assertEquals("There should be no congruent relationship", 0, rels.size());
798

    
799
        types.add(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN());
800
        rels = taxonDao.getTaxonRelationships(types, null, null, null, null);
801
        assertEquals("There should be 11 tax included relationships", 11, rels.size());
802
    }
803

    
804
    @Test
805
    @DataSet("TaxonNodeDaoHibernateImplTest.xml")
806
    public void testListAcceptedTaxaFor()  {
807
        UUID acheontitia_ciprosus = UUID.fromString("3ef145f7-bd92-4a64-8afd-2b8203e00e02");
808

    
809
        Synonym synonym = (Synonym)taxonDao.findByUuid(acheontitia_ciprosus);
810
        assertNotNull("synonym must exist", synonym);
811

    
812
        Taxon taxon = taxonDao.acceptedTaxonFor(synonym, null, null);
813
        assertNotNull("listAcceptedTaxaFor should return a taxon", taxon);
814

    
815
        Classification classification = classificationDao.load(classificationUuid);
816
        assertNotNull("classification must exist", classification);
817

    
818
        taxon = taxonDao.acceptedTaxonFor(synonym, classification, null);
819
        assertNull("listAcceptedTaxaFor should return not taxon due to classification filter", taxon);
820
    }
821

    
822

    
823
    @Test
824
    @DataSet
825
    public void testGetTaxonMatchingUninomial() {
826
        @SuppressWarnings("rawtypes")
827
        List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", "*", "*", "*","*",null,null,null);
828

    
829
        assertNotNull("findTaxaByName should return a List", result);
830
        assertEquals("findTaxaByName should return two Taxa",2,result.size());
831
        assertEquals("findTaxaByName should return a Taxon with id 5",5,result.get(0).getId());
832
    }
833

    
834
    @Test
835
    @DataSet
836
    public void testGetTaxonMatchingSpeciesBinomial() {
837
        @SuppressWarnings("rawtypes")
838
        List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class, "Smerinthus", null, "kindermannii", null,"*",null,null,null);
839

    
840
        assertNotNull("findTaxaByName should return a List", result);
841
        assertEquals("findTaxaByName should return one Taxon",1,result.size());
842
        assertEquals("findTaxaByName should return a Taxon with id 8",8,result.get(0).getId());
843
    }
844

    
845
    @Test
846
    @DataSet
847
    public void testGetTaxonMatchingTrinomial() {
848
        @SuppressWarnings("rawtypes")
849
        List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Cryptocoryne", null,"purpurea","borneoensis","*",null,null,null);
850

    
851
        assertNotNull("findTaxaByName should return a List", result);
852
        assertEquals("findTaxaByName should return one Taxon",1,result.size());
853
        assertEquals("findTaxaByName should return a Taxon with id 38",38,result.get(0).getId());
854
    }
855

    
856
    @Test
857
    @DataSet
858
    public void testNegativeMatch() {
859
        @SuppressWarnings("rawtypes")
860
        List<TaxonBase> result = taxonDao.findTaxaByName(Taxon.class,"Acherontia", null,"atropos","dehli",null,null,null,null);
861

    
862
        assertNotNull("findTaxaByName should return a List", result);
863
        assertTrue("findTaxaByName should return an empty List",result.isEmpty());
864
    }
865

    
866
    @Test
867
    @DataSet
868
    public void testCountAllTaxa() {
869
        long numberOfTaxa = taxonDao.count(Taxon.class);
870
        assertEquals("count should return 14 taxa", 14, numberOfTaxa);
871
    }
872

    
873
    @Test
874
    @DataSet
875
    public void testListAllTaxa() {
876
        List<Taxon> taxa = taxonDao.list(Taxon.class,100, 0);
877
        assertNotNull("list should return a List", taxa);
878
        assertEquals("list should return 14 taxa", 14, taxa.size());
879
    }
880

    
881
    @Test
882
    @DataSet
883
   // @ExpectedDataSet
884
    public void testDelete() {
885
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
886
        assert taxon != null : "taxon must exist";
887
        taxonDao.delete(taxon);
888
        taxon = (Taxon)taxonDao.findByUuid(acherontia);
889
        assert taxon == null : "taxon must not exist";
890
        setComplete();
891
        endTransaction();
892
//        try {
893
//            printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
894
//        } catch (FileNotFoundException e) {
895
//            e.printStackTrace();
896
//        }
897
    }
898

    
899
    @Test
900
    @DataSet
901
    @ExpectedDataSet("TaxonDaoHibernateImplTest.testDelete-result.xml")
902
    public void testDeleteWithMarker() {
903
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
904
        taxon.addMarker(Marker.NewInstance(MarkerType.IS_DOUBTFUL(), true));
905
        taxonDao.save(taxon);
906
        assert taxon != null : "taxon must exist";
907

    
908
        taxonDao.delete(taxon);
909
        commitAndStartNewTransaction(null);
910
        taxon = (Taxon)taxonDao.findByUuid(acherontia);
911
        assert taxon == null : "taxon must not exist";
912
        setComplete();
913
        endTransaction();
914
//        try {
915
//            printDataSet(new FileOutputStream("test.xml"), TABLE_NAMES);
916
//        } catch (FileNotFoundException e) {
917
//            e.printStackTrace();
918
//        }
919
    }
920

    
921
    @Test
922
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
923
    public void testFindDeleted() {
924
        TaxonBase<?> taxon = taxonDao.findByUuid(acherontia);
925
        assertNull("findByUuid should return null in this view", taxon);
926
        assertFalse("exist should return false in this view",taxonDao.exists(acherontia));
927
    }
928

    
929
    @Test
930
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
931
    public void testFindDeletedInPreviousView() {
932
        AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
933
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontia);
934
        assertNotNull("findByUuid should return a taxon in this view",taxon);
935
        assertTrue("exists should return true in this view", taxonDao.exists(acherontia));
936

    
937
        try{
938
            assertEquals("There should be 3 relations to this taxon in this view",3,taxon.getRelationsToThisTaxon().size());
939
        } catch(Exception e) {
940
            fail("We should not experience any problems initializing proxies with envers");
941
        }
942
    }
943

    
944
    @Test
945
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
946
    public void testGetAuditEvents() {
947
        TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
948
        assert taxon != null : "taxon cannot be null";
949

    
950
        List<String> propertyPaths = new ArrayList<>();
951
        propertyPaths.add("name");
952
        propertyPaths.add("createdBy");
953
        propertyPaths.add("updatedBy");
954

    
955
        @SuppressWarnings("rawtypes")
956
        List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,null,propertyPaths);
957
        assertNotNull("getAuditEvents should return a list",auditEvents);
958
        assertFalse("the list should not be empty",auditEvents.isEmpty());
959
        assertEquals("There should be two AuditEventRecords in the list",2, auditEvents.size());
960
    }
961

    
962
    @Test
963
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
964
    public void testGetAuditEventsFromNow() {
965
        AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
966
        TaxonBase<?> taxon =  taxonDao.findByUuid(sphingidae);
967
        assert taxon != null : "taxon cannot be null";
968

    
969
        @SuppressWarnings("rawtypes")
970
        List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(taxon, null,null,AuditEventSort.FORWARDS,null);
971
        assertNotNull("getAuditEvents should return a list",auditEvents);
972
        assertFalse("the list should not be empty",auditEvents.isEmpty());
973
        assertEquals("There should be one audit event in the list",1,auditEvents.size());
974
    }
975

    
976
    @Test
977
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
978
    public void testCountAuditEvents() {
979
        TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
980
        assert taxon != null : "taxon cannot be null";
981

    
982
        long numberOfAuditEvents = taxonDao.countAuditEvents(taxon, null);
983
        assertEquals("countAuditEvents should return 2",numberOfAuditEvents,2);
984
    }
985

    
986
    @Test
987
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
988
    public void getPreviousAuditEvent() {
989
        TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
990
        assert taxon != null : "taxon cannot be null";
991

    
992
        AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
993
        assertNotNull("getPreviousAuditEvent should not return null as there is at least one audit event prior to the current one",auditEvent);
994
    }
995

    
996
    @Test
997
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
998
    public void getPreviousAuditEventAtBeginning() {
999
        AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
1000
        TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
1001
        assert taxon != null : "taxon cannot be null";
1002

    
1003
        @SuppressWarnings("rawtypes")
1004
        AuditEventRecord<TaxonBase> auditEvent = taxonDao.getPreviousAuditEvent(taxon);
1005
        assertNull("getPreviousAuditEvent should return null if we're at the first audit event anyway",auditEvent);
1006
    }
1007

    
1008
    @Test
1009
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1010
    public void getNextAuditEvent() {
1011
        AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
1012
        TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
1013
        assert taxon != null : "taxon cannot be null";
1014

    
1015
        @SuppressWarnings("rawtypes")
1016
        AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
1017
        assertNotNull("getNextAuditEvent should not return null as there is at least one audit event after the current one",auditEvent);
1018
    }
1019

    
1020
    @Test
1021
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1022
    public void getNextAuditEventAtEnd() {
1023
        AuditEventContextHolder.getContext().setAuditEvent(mostRecentAuditEvent);
1024
        TaxonBase<?> taxon = taxonDao.findByUuid(sphingidae);
1025
        assert taxon != null : "taxon cannot be null";
1026

    
1027
        @SuppressWarnings("rawtypes")
1028
        AuditEventRecord<TaxonBase> auditEvent = taxonDao.getNextAuditEvent(taxon);
1029
        assertNull("getNextAuditEvent should return null as there no more audit events after the current one",auditEvent);
1030
    }
1031

    
1032
    @Test
1033
    @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1034
    public void testFind() {
1035
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1036
        assert taxon != null : "taxon cannot be null";
1037

    
1038
       assertEquals("getRelationsToThisTaxon should contain 1 TaxonRelationship in this view",1,taxon.getRelationsToThisTaxon().size());
1039
    }
1040

    
1041
    @Test
1042
    @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1043
    public void testFindInPreviousView() {
1044
        AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
1045
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1046
        assert taxon != null : "taxon cannot be null";
1047

    
1048
        assertTrue("getRelationsToThisTaxon should contain 0 TaxonRelationship in this view",taxon.getRelationsToThisTaxon().isEmpty());
1049
    }
1050

    
1051
    @Test
1052
    @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1053
    public void testGetRelations() {
1054
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1055
        assert taxon != null : "taxon cannot be null";
1056

    
1057
        List<String> propertyPaths = new ArrayList<>();
1058
         propertyPaths.add("fromTaxon");
1059
         propertyPaths.add("fromTaxon.name");
1060

    
1061
         List<OrderHint> orderHints = new ArrayList<>();
1062
         orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
1063

    
1064
        List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
1065
                includeUnpublished, null, null,orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
1066
        assertNotNull("getRelatedTaxa should return a list", taxonRelations);
1067
        assertEquals("there should be one TaxonRelationship in the list in the current view",1,taxonRelations.size());
1068
        assertTrue("TaxonRelationship.relatedFrom should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon()));
1069
        assertTrue("TaxonRelationship.relatedFrom.name should be initialized",Hibernate.isInitialized(taxonRelations.get(0).getFromTaxon().getName()));
1070
    }
1071

    
1072
    @Test
1073
    @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1074
    public void testCountRelations() {
1075
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1076
        assert taxon != null : "taxon cannot be null";
1077
        assertEquals("countRelatedTaxa should return 1 in the current view",1, taxonDao.countTaxonRelationships(
1078
                taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
1079
                includeUnpublished, TaxonRelationship.Direction.relatedTo));
1080
    }
1081

    
1082
    @Test
1083
    @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1084
    public void testGetRelationsInPreviousView() {
1085
       AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
1086
       Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1087
       assert taxon != null : "taxon cannot be null";
1088

    
1089
       List<String> propertyPaths = new ArrayList<String>();
1090
       propertyPaths.add("relatedFrom");
1091
       propertyPaths.add("relatedFrom.name");
1092

    
1093
       List<OrderHint> orderHints = new ArrayList<>();
1094
       orderHints.add(new OrderHint("relatedFrom.titleCache", SortOrder.ASCENDING));
1095

    
1096
       List<TaxonRelationship> taxonRelations = taxonDao.getTaxonRelationships(taxon, TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
1097
               includeUnpublished, null, null, orderHints,propertyPaths, TaxonRelationship.Direction.relatedFrom);
1098
       assertNotNull("getRelatedTaxa should return a list",taxonRelations);
1099
       assertTrue("there should be no TaxonRelationships in the list in the prior view", taxonRelations.isEmpty());
1100
    }
1101

    
1102
    @Test
1103
    @DataSet("TaxonDaoHibernateImplTest.testFind.xml")
1104
    public void testCountRelationsInPreviousView() {
1105
        AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
1106
        Taxon taxon = (Taxon)taxonDao.findByUuid(acherontiaLachesis);
1107
        assert taxon != null : "taxon cannot be null";
1108
        assertEquals("countRelatedTaxa should return 0 in the current view",0,
1109
                taxonDao.countTaxonRelationships(taxon,TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
1110
                        includeUnpublished, TaxonRelationship.Direction.relatedTo));
1111
    }
1112

    
1113
    @Test
1114
    @DataSet
1115
    public void testGroupTaxa() {
1116
        List<Grouping> groups = new ArrayList<>();
1117
        groups.add(new GroupByCount("count",SortOrder.DESCENDING));
1118
        groups.add(new Grouping("name.genusOrUninomial", "genus", "n", SortOrder.ASCENDING));
1119
        List<Object[]> results = taxonDao.group(null, null, null, groups,null);
1120
        if(logger.isTraceEnabled()){
1121
            System.out.println("count\tname.genuOrUninomial");
1122
            for(Object[] result : results) {
1123
                System.out.println(result[0] + "\t" + result[1]);
1124
            }
1125
        }
1126
    }
1127

    
1128
    @Test
1129
    @DataSet
1130
    public void testGroupTaxaByClass() {
1131
        List<Grouping> groups = new ArrayList<>();
1132
        groups.add(new GroupByCount("count",SortOrder.DESCENDING));
1133
        groups.add(new Grouping("class", "class",null, SortOrder.ASCENDING));
1134
        List<Object[]> results = taxonDao.group(null, null, null, groups,null);
1135
        if(logger.isTraceEnabled()){
1136
            System.out.println("count\tclass");
1137
            for(Object[] result : results) {
1138
                System.out.println(result[0] + "\t" + result[1]);
1139
            }
1140
        }
1141
    }
1142

    
1143
    @Test
1144
    @DataSet
1145
    public void testNativeSQLOrder() {
1146
        List<OrderHint> orderHints = new ArrayList<>();
1147
        orderHints.add(new NativeSqlOrderHint("case when {alias}.titleCache like 'C%' then 0 else 1 end",SortOrder.ASCENDING));
1148

    
1149
        @SuppressWarnings("rawtypes")
1150
        List<TaxonBase> results = taxonDao.list(null, null, orderHints);
1151
        if(logger.isTraceEnabled()){
1152
            System.out.println("native SQL order");
1153
            for(TaxonBase<?> result : results) {
1154
                System.out.println(result.getTitleCache());
1155
            }
1156
        }
1157
    }
1158

    
1159
    @Test
1160
    @DataSet
1161
    public void testGroupByDateTaxa() {
1162
        List<Grouping> groups = new ArrayList<>();
1163
        groups.add(new GroupByCount("count",null));
1164
        groups.add(new GroupByDate("created", "dateGroup", SortOrder.ASCENDING, GroupByDate.Resolution.MONTH));
1165
        List<Object[]> results = taxonDao.group(null, null, null, groups,null);
1166
        if(logger.isTraceEnabled()){
1167
            System.out.println("count\tyear\tmonth");
1168
            for(Object[] result : results) {
1169
                System.out.println(result[0] + "\t" + result[1] + "\t" + result[2]);
1170
            }
1171
        }
1172
    }
1173

    
1174

    
1175

    
1176

    
1177
    @Test
1178
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1179
    //NOTE: There is a problem with loading AuditEvents if this test runs
1180
    //stand alone or as first (one of the first) in the test suite. For some reason
1181
    //the AuditEvent records from the @DataSet are not inserted into the database then,
1182
    //while those inserted by the terms dataset are inserted as well as a completely new one.
1183
    //This problem for some reason does not happen if not running at first place
1184
    public void testGetAuditEventsByTypeWithRestrictions() {
1185
        commitAndStartNewTransaction(new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1186

    
1187
        List<String> propertyPaths = new ArrayList<>();
1188
        propertyPaths.add("name");
1189
        propertyPaths.add("createdBy");
1190
        propertyPaths.add("updatedBy");
1191

    
1192
        List<AuditCriterion> criteria = new ArrayList<>();
1193
        criteria.add(AuditEntity.property("lsid_lsid").isNotNull());
1194

    
1195
        long count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1196

    
1197
        @SuppressWarnings("rawtypes")
1198
        List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, criteria,null, null, AuditEventSort.FORWARDS, propertyPaths);
1199
        assertNotNull("getAuditEvents should return a list",auditEvents);
1200
        assertFalse("the list should not be empty",auditEvents.isEmpty());
1201
        assertEquals("There should be one AuditEventRecord in the list",1, auditEvents.size());
1202
    }
1203

    
1204

    
1205
    @Test
1206
    @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml")
1207
//    @DataSets({  //for testing only
1208
//        @DataSet("TaxonDaoHibernateImplTest.testFindDeleted.xml"),
1209
//        @DataSet("TaxonDaoHibernateImplTest.testFindDeletedAuditEvents.xml")
1210
//    })
1211
    //NOTE: There is a problem with loading AuditEvents if this test runs
1212
    //stand alone or as first (one of the first) in the test suite. For some reason
1213
    //the AuditEvent records from the @DataSet are not inserted into the database then,
1214
    //while those inserted by the terms dataset are inserted as well as a completely new one.
1215
    //This problem for some reason does not happen if not running at first place
1216
    public void testGetAuditEventsByTypeWithNoRestrictions() {
1217
//        printDataSet(System.out, new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1218
        commitAndStartNewTransaction(new String[]{"AUDITEVENT", "TAXONBASE_AUD"});
1219

    
1220
        List<String> propertyPaths = new ArrayList<>();
1221
        propertyPaths.add("name");
1222
        propertyPaths.add("createdBy");
1223
        propertyPaths.add("updatedBy");
1224
        long count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1225
        @SuppressWarnings("rawtypes")
1226
        List<AuditEventRecord<TaxonBase>> auditEvents = taxonDao.getAuditEvents(TaxonBase.class, previousAuditEvent, mostRecentAuditEvent, null,null, null, AuditEventSort.FORWARDS, propertyPaths);
1227
        assertNotNull("getAuditEvents should return a list", auditEvents);
1228
        assertFalse("the list should not be empty", auditEvents.isEmpty());
1229
        assertEquals("There should be thirty eight AuditEventRecords in the list", 2, auditEvents.size());
1230
    }
1231

    
1232

    
1233
    @Test
1234
    @DataSet("TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
1235
    public void testGetCommonName(){
1236
       List<Taxon> commonNameResults = taxonDao.getTaxaByCommonName("common%", null,
1237
                MatchMode.BEGINNING, null, null, null, null);
1238

    
1239
        assertNotNull("getTaxaByCommonName should return a list", commonNameResults);
1240
        assertFalse("the list should not be empty", commonNameResults.isEmpty());
1241
        assertEquals("There should be two taxa with common name", 2,commonNameResults.size());
1242

    
1243
        List<UuidAndTitleCache<Taxon>> list = taxonDao.getTaxaByCommonNameForEditor(
1244
                "common%", null, MatchMode.BEGINNING, null);
1245

    
1246
        assertNotNull("getTaxaByCommonName should return a list", list);
1247
        assertFalse("the list should not be empty", list.isEmpty());
1248
        assertEquals("There should be two Taxon with common name", 2,list.size());
1249

    
1250
    }
1251

    
1252
    @Test
1253
    @DataSet
1254
    public void testGetTitleCache(){
1255
        UUID uuid = UUID.fromString("7b8b5cb3-37ba-4dba-91ac-4c6ffd6ac331");
1256
        String titleCache = taxonDao.getTitleCache(uuid, false);
1257
        Assert.assertEquals("Acherontia styx Westwood, 1847 sec. cate-sphingidae.org", titleCache);
1258
        titleCache = taxonDao.getTitleCache(uuid, true);
1259
        Assert.assertEquals("Acherontia styxx Westwood, 1847 sec. cate-sphingidae.org", titleCache);
1260
   }
1261

    
1262

    
1263
    @Test
1264
    @DataSet("TaxonDaoHibernateImplTest.testPropertyPath.xml")
1265
    public void testPropertyPath(){
1266
        //Test that BeanInitializer also works on HiberanteProxys
1267
        Classification c = classificationDao.load(UUID.fromString("4bceea53-893f-4685-8c63-6dcec6e85ab1"));
1268
        TaxonNode singleNode = c.getRootNode().getChildNodes().iterator().next();
1269
        Taxon taxonProxy = singleNode.getTaxon();
1270
        Assert.assertTrue("Object to test should be a proxy ", taxonProxy instanceof HibernateProxy);
1271

    
1272
        List<String> propertyPaths = new ArrayList<>();
1273
        propertyPaths.add("taxonNodes");
1274
        Taxon taxon = (Taxon)this.taxonDao.load(
1275
                UUID.fromString("4a5bc930-844f-45ec-aea8-dd155e1ab25f"),
1276
                propertyPaths);
1277
        Assert.assertSame("Returned object should be the same proxy to assure that we ran initialization on this proxy", taxonProxy, taxon);
1278
    }
1279

    
1280

    
1281
    /**
1282
     * {@inheritDoc}
1283
     */
1284
    @Override
1285
//    @Test
1286
    public void createTestDataSet() throws FileNotFoundException {
1287
//        Classification classification  = Classification.NewInstance("Test");
1288
//        BotanicalName TaxonName = null;
1289
//        Reference sec = null;
1290
//        Taxon taxon = Taxon.NewInstance(taxonName, sec);
1291
//        classification.addChildTaxon(taxon, sec, null);
1292
//
1293
//        classificationDao.save(classification);
1294
//        this.commitAndStartNewTransaction(null);
1295
//
1296
//        writeDbUnitDataSetFile(new String[] {
1297
//                "CLASSIFICATION", "TAXONNAME",
1298
//                "REFERENCE","TAXONNODE",
1299
//                "TAXONBASE","LANGUAGESTRING",
1300
//                "HIBERNATE_SEQUENCES" // IMPORTANT!!!
1301
//                },
1302
//                "testPropertyPath" );
1303
    }
1304

    
1305
}
(3-3/5)