Project

General

Profile

Download (33.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.persistence.dao.hibernate.description;
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.assertTrue;
16

    
17
import java.io.FileNotFoundException;
18
import java.util.ArrayList;
19
import java.util.Collection;
20
import java.util.HashSet;
21
import java.util.Iterator;
22
import java.util.List;
23
import java.util.Map;
24
import java.util.Set;
25
import java.util.UUID;
26

    
27
import org.hibernate.Hibernate;
28
import org.junit.Assert;
29
import org.junit.Before;
30
import org.junit.Test;
31
import org.unitils.dbunit.annotation.DataSet;
32
import org.unitils.dbunit.annotation.DataSets;
33
import org.unitils.spring.annotation.SpringBeanByType;
34

    
35
import eu.etaxonomy.cdm.model.common.DefinedTerm;
36
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
37
import eu.etaxonomy.cdm.model.common.Language;
38
import eu.etaxonomy.cdm.model.common.LanguageString;
39
import eu.etaxonomy.cdm.model.common.MarkerType;
40
import eu.etaxonomy.cdm.model.common.Representation;
41
import eu.etaxonomy.cdm.model.description.DescriptionBase;
42
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
43
import eu.etaxonomy.cdm.model.description.Distribution;
44
import eu.etaxonomy.cdm.model.description.Feature;
45
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
46
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
47
import eu.etaxonomy.cdm.model.description.TaxonDescription;
48
import eu.etaxonomy.cdm.model.description.TextData;
49
import eu.etaxonomy.cdm.model.location.NamedArea;
50
import eu.etaxonomy.cdm.model.media.Media;
51
import eu.etaxonomy.cdm.model.taxon.Taxon;
52
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
53
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
54
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
55
import eu.etaxonomy.cdm.persistence.dto.TermDto;
56
import eu.etaxonomy.cdm.persistence.query.OrderHint;
57
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
58
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
59
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
60

    
61
@DataSet
62
public class DescriptionDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
63

    
64
    @SpringBeanByType
65
    IDescriptionDao descriptionDao;
66

    
67
    @SpringBeanByType
68
    IDefinedTermDao definedTermDao;
69

    
70
    @SpringBeanByType
71
    ITaxonDao taxonDao;
72

    
73
    private Set<NamedArea> namedAreas;
74
    private Set<Feature> features;
75

    
76
    private UUID northernAmericaUuid;
77
    private UUID southernAmericaUuid;
78
    private UUID antarcticaUuid;
79

    
80
    private UUID uuid;
81

    
82
    private UUID taxonSphingidaeUuid;
83

    
84
    @SuppressWarnings("unused")
85
    private static final String[] TABLE_NAMES = new String[] {"DESCRIPTIONBASE", "DESCRIPTIONELEMENTBASE", "DESCRIPTIONELEMENTBASE_LANGUAGESTRING", "HOMOTYPICALGROUP","LANGUAGESTRING"
86
            , "ORIGINALSOURCEBASE", "REFERENCE", "TAXONBASE", "TAXONNAME", "HIBERNATE_SEQUENCES" };
87

    
88
    @Before
89
    public void setUp() {
90
        uuid = UUID.fromString("5f3265ed-68ad-4ec3-826f-0d29d25986b9");
91

    
92
        namedAreas = new HashSet<NamedArea>();
93
        northernAmericaUuid = UUID.fromString("2757e726-d897-4546-93bd-7951d203bf6f");
94
        southernAmericaUuid = UUID.fromString("6310b3ba-96f4-4855-bb5b-326e7af188ea");
95
        antarcticaUuid = UUID.fromString("791b3aa0-54dd-4bed-9b68-56b4680aad0c");
96
        taxonSphingidaeUuid = UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9");
97

    
98
        features = new HashSet<Feature>();
99

    
100
//		loadDataSet(getClass().getClassLoader().getResourceAsStream("eu/etaxonomy/cdm/persistence/dao/hibernate/description/DescriptionDaoHibernateImplTest.xml"));
101
//		printDataSet(System.err, TABLE_NAMES);
102
    }
103

    
104

    
105
    @Test
106
    public void testCountByDistribution() {
107
//		printDataSet(System.err, TABLE_NAMES);
108
        NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
109
        NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
110
        NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
111

    
112
        assert northernAmerica != null : "term must exist";
113
        assert southernAmerica != null : "term must exist";
114
        assert antarctica != null : "term must exist";
115

    
116
        namedAreas.add(northernAmerica);
117
        namedAreas.add(southernAmerica);
118
        namedAreas.add(antarctica);
119

    
120
        int numberOfDescriptions = descriptionDao.countDescriptionByDistribution(namedAreas, null);
121
        assertEquals("countDescriptionsByDistribution should return 23",23,numberOfDescriptions);
122
    }
123

    
124
    @Test
125
    public void testCountByDistributionWithStatus() {
126
        NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
127
        NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
128
        NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
129

    
130
        assert northernAmerica != null : "term must exist";
131
        assert southernAmerica != null : "term must exist";
132
        assert antarctica != null : "term must exist";
133

    
134
        namedAreas.add(northernAmerica);
135
        namedAreas.add(southernAmerica);
136
        namedAreas.add(antarctica);
137

    
138
        int numberOfDescriptions = descriptionDao.countDescriptionByDistribution(namedAreas, PresenceAbsenceTerm.PRESENT());
139
        assertEquals("countDescriptionsByDistribution should return 20",20,numberOfDescriptions);
140
    }
141

    
142
    @Test
143
    public void testGetByDistribution() {
144
        NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
145
        NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
146
        NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
147

    
148
        assert northernAmerica != null : "term must exist";
149
        assert southernAmerica != null : "term must exist";
150
        assert antarctica != null : "term must exist";
151

    
152
        namedAreas.add(northernAmerica);
153
        namedAreas.add(southernAmerica);
154
        namedAreas.add(antarctica);
155

    
156
        List<String> propertyPaths = new ArrayList<String>();
157
        propertyPaths.add("taxon");
158

    
159
        List<OrderHint> orderHints = new ArrayList<OrderHint>();
160
        orderHints.add(new OrderHint("titleCache",SortOrder.ASCENDING));
161

    
162
        List<TaxonDescription> descriptions = descriptionDao.searchDescriptionByDistribution(namedAreas, null, 10,2,orderHints,propertyPaths);
163
        assertNotNull("searchDescriptionByDistribution should return a List",descriptions);
164
        assertFalse("searchDescriptionsByDistribution should not be empty",descriptions.isEmpty());
165
        assertEquals("searchDescriptionsByDistribution should return 3 elements",3,descriptions.size());
166
        assertTrue("TaxonDescription.taxon should be initialized",Hibernate.isInitialized(descriptions.get(0).getTaxon()));
167
        assertEquals("Sphingidae Linnaeus, 1758 sec. cate-sphingidae.org should come first","Sphingidae Linnaeus, 1758 sec. cate-sphingidae.org",descriptions.get(0).getTitleCache());
168
        assertEquals("Sphinx Linnaeus, 1758 sec. cate-sphingidae.org should come last","Sphinx Linnaeus, 1758 sec. cate-sphingidae.org",descriptions.get(2).getTitleCache());
169
    }
170

    
171
    @Test
172
    public void testGetByDistributionWithStatus() {
173
        NamedArea northernAmerica = (NamedArea)definedTermDao.findByUuid(northernAmericaUuid);
174
        NamedArea southernAmerica = (NamedArea)definedTermDao.findByUuid(southernAmericaUuid);
175
        NamedArea antarctica = (NamedArea)definedTermDao.findByUuid(antarcticaUuid);
176

    
177
        assert northernAmerica != null : "term must exist";
178
        assert southernAmerica != null : "term must exist";
179
        assert antarctica != null : "term must exist";
180

    
181
        namedAreas.add(northernAmerica);
182
        namedAreas.add(southernAmerica);
183
        namedAreas.add(antarctica);
184

    
185
        List<TaxonDescription> descriptions = descriptionDao.searchDescriptionByDistribution(namedAreas, PresenceAbsenceTerm.ABSENT(), 10,0,null,null);
186
        assertNotNull("searchDescriptionByDistribution should return a List",descriptions);
187
        assertFalse("searchDescriptionsByDistribution should not be empty",descriptions.isEmpty());
188
        assertEquals("searchDescriptionsByDistribution should return 3 elements",3,descriptions.size());
189
    }
190

    
191
    @Test
192
    public void testCountDescriptionsWithText() {
193
        int numberOfDescriptions = descriptionDao.countDescriptions(TaxonDescription.class, null, true, null);
194

    
195
        assertNotNull("countDescriptions should return a 2",numberOfDescriptions);
196
    }
197

    
198
    @Test
199
    public void testListDescriptionsWithText() {
200
        List<OrderHint> orderHints = new ArrayList<OrderHint>();
201
        orderHints.add(new OrderHint("titleCache",SortOrder.ASCENDING));
202
        List<String> propertyPaths = new ArrayList<String>();
203
        propertyPaths.add("taxon");
204
        List<DescriptionBase> descriptions = descriptionDao.listDescriptions(TaxonDescription.class, null, true, null,null,null,orderHints,propertyPaths);
205

    
206
        assertNotNull("listDescriptions should return a List",descriptions);
207
        assertFalse("listDescriptions should not be empty", descriptions.isEmpty());
208
        assertEquals("listDescriptions should return 2 descriptions",2,descriptions.size());
209
    }
210

    
211
    @Test
212
    public void testCountDescriptionsWithTextAndFeatures() {
213
        features.add(Feature.ECOLOGY());
214
        int numberOfDescriptions = descriptionDao.countDescriptions(TaxonDescription.class, null, true, features);
215

    
216
        assertNotNull("countDescriptions should return a 1",numberOfDescriptions);
217
    }
218

    
219
    @Test
220
    public void testListDescriptionsWithTextAndFeatures() {
221
        assert Feature.ECOLOGY() != null;
222
        features.add(Feature.ECOLOGY());
223

    
224
        List<DescriptionBase> descriptions = descriptionDao.listDescriptions(TaxonDescription.class, null, true, features, null, null, null, null);
225

    
226
        assertNotNull("listDescriptions should return a List",descriptions);
227
        assertFalse("listDescriptions should not be empty", descriptions.isEmpty());
228
        assertEquals("listDescriptions should return 1 descriptions",1,descriptions.size());
229
    }
230

    
231
    @Test
232
    public void testCountDescriptionElements() {
233

    
234
        int numberOfDescriptionElements = descriptionDao.countDescriptionElements(null, null, null, null);
235
        assertEquals("expecting 37 description elements in total", 37, numberOfDescriptionElements);
236

    
237
        numberOfDescriptionElements = descriptionDao.countDescriptionElements(null, null, null, TextData.class);
238
        assertEquals("expecting 4 description elements of type TextData", 4, numberOfDescriptionElements);
239

    
240
        numberOfDescriptionElements = descriptionDao.countDescriptionElements(null, TaxonDescription.class, null, TextData.class);
241
        assertEquals("expecting 3 description elements of type TextData", 3, numberOfDescriptionElements);
242

    
243
        DescriptionBase<?> description = descriptionDao.findByUuid(uuid);
244
        assert description != null : "description must exist";
245

    
246
        numberOfDescriptionElements = descriptionDao.countDescriptionElements(description, null, null, TextData.class);
247

    
248
        assertEquals("expecting 2 description elements of type TextData in specific description", 2, numberOfDescriptionElements);
249
    }
250

    
251
    @Test
252
    public void testGetDescriptionElements() {
253

    
254
        List<TextData> elements = descriptionDao.getDescriptionElements(null, null, null, null, null, null, null);
255
        assertEquals("expecting 37 description elements in total", 37, elements.size());
256

    
257
        elements = descriptionDao.getDescriptionElements(null, null, null, TextData.class, null, null, null);
258
        assertEquals("expecting 4 description elements of type TextData", 4, elements.size());
259

    
260
        elements = descriptionDao.getDescriptionElements(null, TaxonDescription.class, null, TextData.class, null, null, null);
261
        assertEquals("expecting 3 description elements of type TextData", 3, elements.size());
262

    
263
        elements = descriptionDao.getDescriptionElements(null, SpecimenDescription.class, null, TextData.class, null, null, null);
264
        assertEquals("expecting 1 description elements of type TextData", 1, elements.size());
265

    
266
        DescriptionBase<?> description = descriptionDao.findByUuid(uuid);
267
        assert description != null : "description must exist";
268

    
269
        List<String> propertyPaths = new ArrayList<String>();
270
        propertyPaths.add("multilanguageText");
271
        propertyPaths.add("media");
272
        propertyPaths.add("feature");
273
        propertyPaths.add("sources.citation");
274

    
275
        elements = descriptionDao.getDescriptionElements(description, null, null, TextData.class, null, null, propertyPaths);
276

    
277
        for (DescriptionElementBase descElB: elements){
278
            if (descElB instanceof TextData){
279
                Map<Language, LanguageString> multiLanguage = ((TextData)descElB).getMultilanguageText();
280
                LanguageString defaultString = multiLanguage.get(Language.DEFAULT());
281

    
282
            }
283
        }
284
        Iterator<DescriptionElementBase> elements2 = description.getElements().iterator();
285
        while(elements2.hasNext()){
286
            DescriptionElementBase element = elements2.next();
287
            if (element instanceof TextData){
288
                Map<Language, LanguageString> multiLanguage = ((TextData)element).getMultilanguageText();
289
                LanguageString defaultString = multiLanguage.get(Language.DEFAULT());
290
                defaultString.setText("blablub");
291
            }
292
        }
293
        elements = descriptionDao.getDescriptionElements(description, null, null, TextData.class, null, null, propertyPaths);
294

    
295
        DescriptionElementBase element34 = null;
296
        for (DescriptionElementBase descElB: elements){
297
            if (descElB instanceof TextData){
298
                Map<Language, LanguageString> multiLanguage = ((TextData)descElB).getMultilanguageText();
299
                LanguageString defaultString = multiLanguage.get(Language.DEFAULT());
300
                //???
301
            }
302
            if (descElB.getId() == 34){
303
                element34 = descElB;
304
            }
305
        }
306

    
307
        assertNotNull("getDescriptionElements should return a List", elements);
308
        assertEquals("getDescriptionElement should return 2 elements",2,elements.size());
309
        assertNotNull("Description Element with ID 34 should be part of the list",element34);
310
        assertTrue("DescriptionElement.sources should be initialized",Hibernate.isInitialized(element34.getSources()));
311
        assertTrue("DescriptionElement.sources should have elements",element34.getSources().size() > 0);
312
        assertTrue("ReferencedEntityBase.citation should be initialized",Hibernate.isInitialized(element34.getSources().iterator().next().getCitation()));
313
        assertTrue("DescriptionElementBase.feature should be initialized",Hibernate.isInitialized(element34.getFeature()));
314
        assertTrue("DescriptionElementBase.media should be initialized",Hibernate.isInitialized(element34.getMedia()));
315
        assertTrue("TextData.multilanguageText should be initialized",Hibernate.isInitialized(((TextData)element34).getMultilanguageText()));
316
    }
317

    
318
    @Test
319
    public void testCountDescriptionElementsFeature() {
320
        features.add(Feature.ECOLOGY());
321
        DescriptionBase<?> description = descriptionDao.findByUuid(uuid);
322
        assert description != null : "description must exist";
323

    
324
        int numberOfDescriptionElements = descriptionDao.countDescriptionElements(description, features, TextData.class);
325

    
326
        assertEquals("countDescriptionElements should return 1", 1, numberOfDescriptionElements);
327
    }
328

    
329
    @Test
330
    public void testGetDescriptionElementsByFeature() {
331

    
332
        // 2. search for one Feature: ECOLOGY
333
        features.add(Feature.ECOLOGY());
334
        DescriptionBase<?> description = descriptionDao.findByUuid(uuid);
335
        assert description != null : "description must exist";
336

    
337
        List<TextData> elements = descriptionDao.getDescriptionElements(null, null, features, TextData.class, null, null,null);
338
        assertNotNull("getDescriptionElements should return a List", elements);
339
        assertEquals("getDescriptionElement should return 2 elements", 2, elements.size());
340

    
341
        elements = descriptionDao.getDescriptionElements(description, null, features, TextData.class, null, null,null);
342
        assertEquals("getDescriptionElement should return 1 elements", 1, elements.size());
343

    
344
        elements = descriptionDao.getDescriptionElements(null, SpecimenDescription.class, features, TextData.class, null, null,null);
345
        assertEquals("getDescriptionElement should return 1 elements", 1, elements.size());
346

    
347
        // 2. search for more Features: ECOLOGY & DESCRIPTION
348
        features.add(Feature.DESCRIPTION());
349
        elements = descriptionDao.getDescriptionElements(null, null, features, TextData.class, null, null,null);
350
        assertEquals("getDescriptionElement should return 4 elements", 4, elements.size());
351

    
352
        elements = descriptionDao.getDescriptionElements(null, TaxonDescription.class, features, TextData.class, null, null,null);
353
        assertEquals("getDescriptionElement should return 3 elements", 3, elements.size());
354
    }
355

    
356
    @Test
357
    public void testGetDescriptionElementForTaxon() {
358

    
359
        Taxon taxonSphingidae = (Taxon) taxonDao.load(taxonSphingidaeUuid);
360
        assert taxonSphingidae != null : "taxon must exist";
361

    
362
        // 1.
363

    
364
        List<DescriptionElementBase> elements1 = descriptionDao.getDescriptionElementForTaxon(
365
                taxonSphingidae.getUuid() , null, null, null, 0, null);
366

    
367
        assertNotNull("getDescriptionElementForTaxon should return a List", elements1);
368
        assertEquals("getDescriptionElementForTaxon should return 1 elements",1,elements1.size());
369

    
370
        // 2.
371

    
372
        List<DescriptionElementBase> elements2 = descriptionDao.getDescriptionElementForTaxon(
373
                taxonSphingidae.getUuid() , null, DescriptionElementBase.class, null, 0, null);
374

    
375
        assertNotNull("getDescriptionElementForTaxon should return a List", elements2);
376
        assertTrue("getDescriptionElementForTaxon should be empty",elements2.isEmpty());
377

    
378
        // 3.
379

    
380
        List<Distribution> elements3 = descriptionDao.getDescriptionElementForTaxon(
381
                taxonSphingidae.getUuid() , null, Distribution.class, null, 0, null);
382

    
383
        assertNotNull("getDescriptionElementForTaxon should return a List", elements3);
384
        assertEquals("getDescriptionElementForTaxon should return 1 elements",1,elements3.size());
385
    }
386

    
387
    //see #2234
388
    @Test
389
    public void testSaveClonedDescription() {
390

    
391
//		printDataSet(System.err, new String[]{"TAXONBASE"});
392

    
393
        Taxon taxon = Taxon.NewInstance(null, null);
394
        taxon.setTitleCache("##### created in testSaveClonedDescription()", true);
395
        taxonDao.save(taxon);
396
        commitAndStartNewTransaction(null);
397

    
398
        TaxonDescription description = TaxonDescription.NewInstance(taxon);
399
        this.descriptionDao.saveOrUpdate(description);
400
//		TaxonDescription clonedDescription = (TaxonDescription)description.clone();
401
//		this.descriptionDao.saveOrUpdate(clonedDescription);
402
//		printDataSet(System.err, new String[]{"TAXONBASE"});
403

    
404
        assertTrue(true);
405

    
406
    }
407

    
408
    //see #2592
409
    @Test
410
    public void testSaveScope(){
411
        int n1 = this.descriptionDao.count();
412
        Taxon taxon = Taxon.NewInstance(null, null);
413
        TaxonDescription description = TaxonDescription.NewInstance(taxon);
414
        this.taxonDao.save(taxon);
415
        int n2 = this.descriptionDao.count();
416
        Assert.assertEquals(1, n2-n1);
417

    
418
        DefinedTerm scope = DefinedTerm.SEX_FEMALE();
419
        description.addScope(scope);
420

    
421
        this.descriptionDao.saveOrUpdate(description);
422

    
423
    }
424

    
425
    @Test
426
    public void testListTaxonDescriptionWithMarker(){
427
        Taxon taxon = (Taxon)this.taxonDao.findByUuid(UUID.fromString("b04cc9cb-2b4a-4cc4-a94a-3c93a2158b06"));
428
        Set<DefinedTerm> scopes = null;
429
        Set<NamedArea> geographicalScope = null;
430
        Integer pageSize = null;
431
        Integer pageNumber = null;
432
        List<String> propertyPaths = null;
433

    
434
        //complete
435
        MarkerType completeMarkerType = (MarkerType)this.definedTermDao.findByUuid(UUID.fromString("b4b1b2ab-89a8-4ce6-8110-d60b8b1bc433")); //Marker "complete"
436
        Assert.assertNotNull("MarkerType for 'complete' should exist", completeMarkerType);
437
        Set<MarkerType> markerTypes = new HashSet<MarkerType>();
438
        markerTypes.add(completeMarkerType);
439
        int n1 = this.descriptionDao.countTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes);
440
        Assert.assertEquals("There should be 1 description marked 'complete'", 1, n1);
441
        List<TaxonDescription> descriptions = this.descriptionDao.listTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes, pageSize, pageNumber, propertyPaths);
442
        Assert.assertEquals("There should be 1 description marked 'complete'", 1, descriptions.size());
443

    
444
        //doubtful
445
        MarkerType isDoubtfulMarkerType = (MarkerType)this.definedTermDao.findByUuid(UUID.fromString("b51325c8-05fe-421a-832b-d86fc249ef6e")); //Marker "doubtful"
446
        Assert.assertNotNull("MarkerType for 'doubtful' should exist", isDoubtfulMarkerType);
447
        markerTypes = new HashSet<MarkerType>();  //reset
448
        markerTypes.add(isDoubtfulMarkerType);
449
        int n2 = this.descriptionDao.countTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes);
450
        Assert.assertEquals("There should be no description marked 'doubtful'", 0, n2);
451
        descriptions = this.descriptionDao.listTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes, pageSize, pageNumber, propertyPaths);
452
        Assert.assertEquals("There should be 0 description marked 'doubtful'", 0, descriptions.size());
453

    
454
        //imported = false
455
        UUID uuidImported = UUID.fromString("96878790-4ceb-42a2-9738-a2242079b679");
456
        MarkerType importedMarkerType = (MarkerType)this.definedTermDao.findByUuid(uuidImported);
457
        Assert.assertNotNull("MarkerType for 'imported' should exist", completeMarkerType);
458
        markerTypes = new HashSet<MarkerType>();
459
        markerTypes.add(importedMarkerType);
460
        int n3 = this.descriptionDao.countTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes);
461
        Assert.assertEquals("There should be no description marked 'imported' as true", 0, n3);
462
        descriptions = this.descriptionDao.listTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes, pageSize, pageNumber, propertyPaths);
463
        Assert.assertEquals("There should be no description marked 'imported' as true", 0, descriptions.size());
464
        markerTypes = null;
465
        descriptions = this.descriptionDao.listTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes, pageSize, pageNumber, propertyPaths);
466
        Assert.assertEquals("There should be 1 description", 1, descriptions.size());
467
        TaxonDescription desc = descriptions.iterator().next();
468
        boolean hasMarkerImportedAsFalse = desc.hasMarker(importedMarkerType, false);
469
        Assert.assertTrue("The only description should have a negative marker on 'imported'", hasMarkerImportedAsFalse);
470

    
471
    }
472

    
473
//
474
//        //complete
475
//        MarkerType completeMarkerType = (MarkerType)this.definedTermDao.findByUuid(UUID.fromString("b4b1b2ab-89a8-4ce6-8110-d60b8b1bc433")); //Marker "complete"
476
//
477
//        Assert.assertNotNull("MarkerType for 'complete' should exist", completeMarkerType);
478
//        Set<MarkerType> markerTypes = new HashSet<MarkerType>();
479
//        markerTypes.add(completeMarkerType);
480
//        int n1 = this.descriptionDao.countTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes);
481
//        Assert.assertEquals("There should be 1 description marked 'complete'", 1, n1);
482
//        List<TaxonDescription> descriptions = this.descriptionDao.listTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes, pageSize, pageNumber, propertyPaths);
483
//        Assert.assertEquals("There should be 1 description marked 'complete'", 1, descriptions.size());
484
//
485
//        //doubtful
486
//        MarkerType isDoubtfulMarkerType = (MarkerType)this.definedTermDao.findByUuid(UUID.fromString("b51325c8-05fe-421a-832b-d86fc249ef6e")); //Marker "doubtful"
487
//        Assert.assertNotNull("MarkerType for 'doubtful' should exist", isDoubtfulMarkerType);
488
//        markerTypes = new HashSet<MarkerType>();  //reset
489
//        markerTypes.add(isDoubtfulMarkerType);
490
//        int n2 = this.descriptionDao.countTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes);
491
//        Assert.assertEquals("There should be no description marked 'doubtful'", 0, n2);
492
//        descriptions = this.descriptionDao.listTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes, pageSize, pageNumber, propertyPaths);
493
//        Assert.assertEquals("There should be 0 description marked 'doubtful'", 0, descriptions.size());
494
//
495
//        //imported = false
496
//        UUID uuidImported = UUID.fromString("96878790-4ceb-42a2-9738-a2242079b679");
497
//        MarkerType importedMarkerType = (MarkerType)this.definedTermDao.findByUuid(uuidImported);
498
//        Assert.assertNotNull("MarkerType for 'imported' should exist", completeMarkerType);
499
//        markerTypes = new HashSet<MarkerType>();
500
//        markerTypes.add(importedMarkerType);
501
//        int n3 = this.descriptionDao.countTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes);
502
//        Assert.assertEquals("There should be no description marked 'imported' as true", 0, n3);
503
//        descriptions = this.descriptionDao.listTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes, pageSize, pageNumber, propertyPaths);
504
//        Assert.assertEquals("There should be no description marked 'imported' as true", 0, descriptions.size());
505
//        markerTypes = null;
506
//        descriptions = this.descriptionDao.listTaxonDescriptions(taxon, scopes, geographicalScope, markerTypes, pageSize, pageNumber, propertyPaths);
507
//        Assert.assertEquals("There should be 1 description", 1, descriptions.size());
508
//        TaxonDescription desc = descriptions.iterator().next();
509
//        boolean hasMarkerImportedAsFalse = desc.hasMarker(importedMarkerType, false);
510
//        Assert.assertTrue("The only description should have a negative marker on 'imported'", hasMarkerImportedAsFalse);
511
//
512
//    }
513

    
514

    
515
    @Test
516
    @DataSet("DescriptionDaoHibernateImplTest.testListTaxonDescriptionMedia.xml")
517
    public void testListTaxonDescriptionMedia(){
518
        //init
519
        Taxon firstTaxon = (Taxon)this.taxonDao.findByUuid(UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2"));
520
        Taxon secondTaxon = (Taxon)this.taxonDao.findByUuid(UUID.fromString("822d98dc-9ef7-44b7-a870-94573a3bcb46"));
521
        Taxon thirdTaxon = (Taxon)this.taxonDao.findByUuid(UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9"));
522
        MarkerType markerType890 = (MarkerType)this.definedTermDao.findById(890);
523
        MarkerType markerType892 = (MarkerType)this.definedTermDao.findById(892);
524

    
525
        Assert.assertNotNull("First taxon should exist", firstTaxon);
526
        Assert.assertNotNull("Second taxon should exist", secondTaxon);
527
        Assert.assertNotNull("Third taxon should exist", thirdTaxon);
528
        Assert.assertEquals("There should be 6 descriptions in the database", 6, this.descriptionDao.count());
529

    
530

    
531
        //set parameter
532
        Set<MarkerType> markerTypes = null;
533
        Integer pageSize = null;
534
        Integer pageNumber = null;
535
        List<String> propertyPaths = null;
536

    
537
        //test
538
        //first Taxon gallery filter
539
        List<Media> mediaList = this.descriptionDao.listTaxonDescriptionMedia(firstTaxon.getUuid(), true, markerTypes, pageSize, pageNumber, propertyPaths);
540
        Assert.assertEquals("media list size for first taxon and filter on image galleries should be 1", 1, mediaList.size());
541

    
542
        //first taxon all descriptions
543
        mediaList = this.descriptionDao.listTaxonDescriptionMedia(firstTaxon.getUuid(), false, markerTypes, pageSize, pageNumber, propertyPaths);
544
        Assert.assertEquals("media list size for first taxon without filter on image galleries should be 2", 2, mediaList.size());
545

    
546
        //second taxon
547
        mediaList = this.descriptionDao.listTaxonDescriptionMedia(secondTaxon.getUuid(), true, markerTypes, pageSize, pageNumber, propertyPaths);
548
        Assert.assertEquals("media list size for second taxon and filter on image galleries should be 2", 2, mediaList.size());
549

    
550
        //all taxa
551
        mediaList = this.descriptionDao.listTaxonDescriptionMedia(null, true, markerTypes, pageSize, pageNumber, propertyPaths);
552
        Assert.assertEquals("media list size for any taxon and filter on image galleries should be 3", 3, mediaList.size());
553
        //with marker
554
        markerTypes = new HashSet<MarkerType>();
555
        mediaList = this.descriptionDao.listTaxonDescriptionMedia(null, true, markerTypes, pageSize, pageNumber, propertyPaths);
556
        Assert.assertEquals("Empty marker type set should give same result as before: 3", 3, mediaList.size());
557
        markerTypes.add(markerType890);
558
        mediaList = this.descriptionDao.listTaxonDescriptionMedia(null, true, markerTypes, pageSize, pageNumber, propertyPaths);
559
        Assert.assertEquals("markerType890 should only give 1 result", 1, mediaList.size());
560
        markerTypes.add(markerType892);
561
        mediaList = this.descriptionDao.listTaxonDescriptionMedia(null, true, markerTypes, pageSize, pageNumber, propertyPaths);
562
        Assert.assertEquals("markerType892 should not give additional result as the markers value is false", 1, mediaList.size());
563
        markerTypes = null;
564

    
565
        //check deduplication
566
        mediaList = this.descriptionDao.listTaxonDescriptionMedia(null, false, markerTypes, pageSize, pageNumber, propertyPaths);
567
        Assert.assertEquals("media list size for any taxon without filter on image galleries should be 3", 3, mediaList.size());
568

    
569
//        System.out.println(mediaList);
570
    }
571

    
572
    @Test
573
    @DataSet("DescriptionDaoHibernateImplTest.testListTaxonDescriptionMedia.xml")
574
    public void testcountTaxonDescriptionMedia(){
575
        //init
576
        Taxon firstTaxon = (Taxon)this.taxonDao.findByUuid(UUID.fromString("496b1325-be50-4b0a-9aa2-3ecd610215f2"));
577
        Taxon secondTaxon = (Taxon)this.taxonDao.findByUuid(UUID.fromString("822d98dc-9ef7-44b7-a870-94573a3bcb46"));
578
        Taxon thirdTaxon = (Taxon)this.taxonDao.findByUuid(UUID.fromString("54e767ee-894e-4540-a758-f906ecb4e2d9"));
579
        MarkerType markerType890 = (MarkerType)this.definedTermDao.findById(890);
580
        MarkerType markerType892 = (MarkerType)this.definedTermDao.findById(892);
581

    
582
        Assert.assertNotNull("First taxon should exist", firstTaxon);
583
        Assert.assertNotNull("Second taxon should exist", secondTaxon);
584
        Assert.assertNotNull("Third taxon should exist", thirdTaxon);
585
        Assert.assertEquals("There should be 6 descriptions in the database", 6, this.descriptionDao.count());
586

    
587
        //set parameter
588
        Set<MarkerType> markerTypes = null;
589

    
590
        //test
591
        //first Taxon gallery filter
592
        int mediaCount = this.descriptionDao.countTaxonDescriptionMedia(firstTaxon.getUuid(), true, markerTypes);
593
        Assert.assertEquals("media list size for first taxon and filter on image galleries should be 1", 1, mediaCount);
594
        mediaCount = this.descriptionDao.countTaxonDescriptionMedia(firstTaxon.getUuid(), false, markerTypes);
595
        Assert.assertEquals("media list size for first taxon without filter on image galleries should be 2", 2, mediaCount);
596

    
597
        //second taxon
598
        mediaCount = this.descriptionDao.countTaxonDescriptionMedia(secondTaxon.getUuid(), true, markerTypes);
599
        Assert.assertEquals("media list size for second taxon and filter on image galleries should be 2", 2, mediaCount);
600

    
601
        //all taxa
602
        mediaCount = this.descriptionDao.countTaxonDescriptionMedia(null, true, markerTypes);
603
        Assert.assertEquals("media list size for any taxon and filter on image galleries should be 3", 3, mediaCount);
604
        //with marker
605
        markerTypes = new HashSet<MarkerType>();
606
        mediaCount = this.descriptionDao.countTaxonDescriptionMedia(null, true, markerTypes);
607
        Assert.assertEquals("Empty marker type set should give same result as before: 3", 3, mediaCount);
608
        markerTypes.add(markerType890);
609
        mediaCount = this.descriptionDao.countTaxonDescriptionMedia(null, true, markerTypes);
610
        Assert.assertEquals("markerType890 should only give 1 result", 1, mediaCount);
611
        markerTypes.add(markerType892);
612
        mediaCount = this.descriptionDao.countTaxonDescriptionMedia(null, true, markerTypes);
613
        Assert.assertEquals("markerType892 should not give additional result as the markers value is false", 1, mediaCount);
614
        markerTypes = null;
615

    
616
        //check deduplication
617
        mediaCount = this.descriptionDao.countTaxonDescriptionMedia(null, false, markerTypes);
618
        Assert.assertEquals("media list size for any taxon without filter on image galleries should be 3", 3, mediaCount);
619

    
620
    }
621

    
622
    @Test
623
//    @DataSet
624
    @DataSets({
625
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
626
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
627
        @DataSet(value="DescriptionDaoHibernateImplTest.xml")
628
    })
629
    public void testListNamedAreasInUse(){
630

    
631
        Collection<TermDto> list = null;
632

    
633
        DefinedTermBase<?> antarctica = definedTermDao.load(antarcticaUuid);
634
        antarctica.getRepresentations().add(Representation.NewInstance("Antarktis", "Antarktis", "An", Language.GERMAN()));
635
        definedTermDao.saveOrUpdate(antarctica);
636
        commitAndStartNewTransaction(null);
637

    
638
        list = descriptionDao.listNamedAreasInUse(false, null, null);
639
        Assert.assertEquals(3, list.size());
640

    
641
    }
642

    
643
    @Test
644
    @DataSet
645
    // @Ignore // the first query in listNamedAreasInUse is for some reason not working with h2
646
    public void testListNamedAreasInUseWithParents(){
647

    
648
        Collection<TermDto> list = null;
649

    
650
        list = descriptionDao.listNamedAreasInUse(true, null, null);
651
        Assert.assertEquals(3, list.size());
652

    
653
    }
654

    
655

    
656
    @Override
657
    public void createTestDataSet() throws FileNotFoundException {}
658

    
659

    
660
}
(1-1/4)