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.common.IDefinedTermDao;
57
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
58
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
59
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
60
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
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

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

    
352

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

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

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

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

    
371
        //TODO: test the search for misapplied names
372

    
373
    }
374

    
375

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

    
389
        Classification classification = classificationDao.findByUuid(classificationUuid);
390

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

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

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

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

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

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

    
424

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

    
431

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

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

    
442

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

    
451

    
452
    /**
453
     * 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)}
454
     * restricting the search by a set of Areas.
455
     */
456
    @Test
457
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="TaxonDaoHibernateImplTest.testGetTaxaByNameAndArea.xml")
458
    public void testFindByNameTitleCache() {
459
        TaxonNode subtree = null;
460

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

    
466
        Classification classification = classificationDao.findByUuid(classificationUuid);
467

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

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

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

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

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

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

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

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

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

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

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

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

    
559
    }
560

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

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

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

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

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

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

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

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

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

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

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

    
665
        assertEquals("Acherontia should appear first in the list of related taxa", relatedTaxa.get(0).getFromTaxon().getTitleCache(), "Acherontia Laspeyres, 1809 sec. cate-sphingidae.org");
666
        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());
667
    }
668

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
824

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1176

    
1177

    
1178

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

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

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

    
1197
        long count = taxonDao.countAuditEvents(TaxonBase.class, null, null, null);
1198

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

    
1206

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

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

    
1234

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

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

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

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

    
1252
    }
1253

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

    
1264

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

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

    
1282

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

    
1307
}
(3-3/5)