Project

General

Profile

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

    
12
import static org.junit.Assert.assertEquals;
13
import static org.junit.Assert.assertFalse;
14
import static org.junit.Assert.assertNotEquals;
15
import static org.junit.Assert.assertNotNull;
16
import static org.junit.Assert.assertNull;
17
import static org.junit.Assert.assertTrue;
18

    
19
import java.io.FileNotFoundException;
20
import java.util.List;
21
import java.util.Set;
22
import java.util.UUID;
23

    
24
import org.apache.log4j.Logger;
25
import org.junit.Test;
26
import org.unitils.dbunit.annotation.DataSet;
27
import org.unitils.spring.annotation.SpringBeanByType;
28

    
29
import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
30
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
31
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
32
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
33
import eu.etaxonomy.cdm.model.agent.Institution;
34
import eu.etaxonomy.cdm.model.agent.Person;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.common.DefinedTerm;
37
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
38
import eu.etaxonomy.cdm.model.common.Language;
39
import eu.etaxonomy.cdm.model.common.TimePeriod;
40
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
41
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
42
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
43
import eu.etaxonomy.cdm.model.description.TaxonDescription;
44
import eu.etaxonomy.cdm.model.location.Country;
45
import eu.etaxonomy.cdm.model.location.NamedArea;
46
import eu.etaxonomy.cdm.model.location.Point;
47
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
48
import eu.etaxonomy.cdm.model.molecular.DnaSample;
49
import eu.etaxonomy.cdm.model.molecular.Sequence;
50
import eu.etaxonomy.cdm.model.name.BotanicalName;
51
import eu.etaxonomy.cdm.model.name.Rank;
52
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
53
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
54
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
55
import eu.etaxonomy.cdm.model.occurrence.Collection;
56
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
57
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
58
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
59
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
60
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
61
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
62
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
63
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
64
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
65
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
66
import eu.etaxonomy.cdm.model.reference.Reference;
67
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
68
import eu.etaxonomy.cdm.model.taxon.Taxon;
69
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
70
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
71

    
72
/**
73
 * @author pplitzner
74
 * @date 31.03.2014
75
 *
76
 */
77
public class OccurrenceServiceTest extends CdmTransactionalIntegrationTest {
78

    
79
    @SuppressWarnings("unused")
80
    private static final Logger logger = Logger.getLogger(OccurrenceServiceTest.class);
81

    
82
    @SpringBeanByType
83
    private IOccurrenceService occurrenceService;
84

    
85
    @SpringBeanByType
86
    private ISequenceService sequenceService;
87

    
88
    @SpringBeanByType
89
    private ITaxonService taxonService;
90

    
91
    @SpringBeanByType
92
    private INameService nameService;
93

    
94
    @SpringBeanByType
95
    private IDescriptionService descriptionService;
96

    
97
    @Test
98
    public void testGetNonCascadedAssociatedElements(){
99
        //Collection
100
        Collection collection = Collection.NewInstance();
101
        Collection subCollection = Collection.NewInstance();
102
        subCollection.setSuperCollection(collection);
103

    
104
        Institution institution = Institution.NewInstance();
105
        institution.addType(DefinedTerm.NewInstitutionTypeInstance("Research and teaching", "botanical garden", "BGBM"));
106
        collection.setInstitute(institution);
107

    
108
        //Source
109
        Reference<?> article = ReferenceFactory.newArticle(getReference(), Person.NewInstance(), "title", "pages", "series", "volume", TimePeriod.NewInstance(2014));
110
        IdentifiableSource source = IdentifiableSource.NewPrimarySourceInstance(article, "microCitation");
111

    
112
        //FieldUnit
113
        FieldUnit fieldUnit = FieldUnit.NewInstance();
114
        Person primaryCollector = Person.NewInstance();
115
        primaryCollector.setLifespan(TimePeriod.NewInstance(2014));
116
        fieldUnit.setPrimaryCollector(primaryCollector);
117
        fieldUnit.addSource(source);
118

    
119
        //GatheringEvent
120
        GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
121
        fieldUnit.setGatheringEvent(gatheringEvent);
122
        gatheringEvent.putLocality(Language.ENGLISH(), "locality");
123
        gatheringEvent.setExactLocation(Point.NewInstance(22.4, -34.2,
124
                ReferenceSystem.NewInstance("MyReferenceSystem", "label", "labelAbbrev"), 33));
125
        gatheringEvent.setCountry(Country.GERMANY());
126
        gatheringEvent.addCollectingArea(NamedArea.EUROPE());
127

    
128
        //Derived Unit
129
        MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(SpecimenOrObservationType.StillImage);
130
        mediaSpecimen.setCollection(collection);
131
        BotanicalName storedUnder = BotanicalName.NewInstance(Rank.SPECIES());
132
        mediaSpecimen.setStoredUnder(storedUnder);
133
        PreservationMethod preservation = PreservationMethod.NewInstance(null, "My preservation");
134
        preservation.setMedium(DefinedTerm.NewDnaMarkerInstance("medium", "medium", "medium"));//dummy defined term
135
        mediaSpecimen.setPreservation(preservation);
136

    
137
        //DerivationEvent
138
        DerivationEvent event = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
139
        event.addOriginal(fieldUnit);
140
        event.addDerivative(mediaSpecimen);
141

    
142
        //SpecOrObservationBase
143
        fieldUnit.setSex(DefinedTerm.SEX_FEMALE());
144
        fieldUnit.setLifeStage(DefinedTerm.NewStageInstance("Live stage", "stage", null));
145
        fieldUnit.setKindOfUnit(DefinedTerm.NewKindOfUnitInstance("Kind of unit", "Kind of unit", null));
146
        fieldUnit.putDefinition(Language.ENGLISH(), "definition");
147

    
148
        //Determination
149
        DeterminationEvent determinationEvent = DeterminationEvent.NewInstance(getTaxon(), mediaSpecimen);
150
        determinationEvent.setModifier(DefinedTerm.NewModifierInstance("modifierDescription", "modifierLabel", "mofifierLabelAbbrev"));
151
        determinationEvent.setPreferredFlag(true);
152
        Reference<?> reference = getReference();
153
        determinationEvent.addReference(reference);
154

    
155
        /*NonCascaded
156
         * SOOB
157
         *  - sex (FEMALE)
158
         *  - stage (Live stage)
159
         *  - kindOfUnit (Kind of unit)
160
         * GatheringEvent
161
         *  - country (GERMANY)
162
         *  - collectingArea (EUROPE)
163
         *  DerivedUnit
164
         *  - storedUnder (botanical name)
165
         *  DerivedUnit-> Collection -> institiute
166
         *  - type (botanical garden)
167
         *
168
         * */
169

    
170
        assertEquals("Incorrect number of non cascaded CDM entities", 9, occurrenceService.getNonCascadedAssociatedElements(fieldUnit).size());
171
        assertEquals("Incorrect number of non cascaded CDM entities", 9, occurrenceService.getNonCascadedAssociatedElements(mediaSpecimen).size());
172

    
173
    }
174
    private Reference<?> getReference() {
175
        Reference<?> result = ReferenceFactory.newGeneric();
176
        result.setTitle("some generic reference");
177
        return result;
178
   }
179
   private Taxon getTaxon() {
180
       Reference<?> sec = getReference();
181
       TaxonNameBase<?,?> name = BotanicalName.NewInstance(Rank.GENUS());
182
       Taxon taxon = Taxon.NewInstance(name, sec);
183
       return taxon;
184

    
185
   }
186

    
187
    @Test
188
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurenceServiceTest.move.xml")
189
    public void testMoveDerivate(){
190
        DerivedUnit specimenA = (DerivedUnit) occurrenceService.load(UUID.fromString("35cfb0b3-588d-4eee-9db6-ac9caa44e39a"));
191
        DerivedUnit specimenB = (DerivedUnit) occurrenceService.load(UUID.fromString("09496534-efd0-44c8-b1ce-01a34a8a0229"));
192
        DerivedUnit dnaSample = (DnaSample) occurrenceService.load(UUID.fromString("5995f852-0e78-405c-b849-d923bd6781d9"));
193

    
194

    
195
        occurrenceService.saveOrUpdate(specimenA);
196
        occurrenceService.saveOrUpdate(specimenB);
197
        occurrenceService.saveOrUpdate(dnaSample);
198

    
199
        Person derivationActor = Person.NewTitledInstance("Derivation Actor");
200
        String derivationDescription = "Derivation Description";
201
        Institution derivationInstitution = Institution.NewInstance();
202
        TimePeriod derivationTimePeriod = TimePeriod.NewInstance(2015);
203

    
204
        DerivationEvent originalDerivedFromEvent = DerivationEvent.NewSimpleInstance(specimenA, dnaSample, DerivationEventType.DNA_EXTRACTION());
205

    
206
        originalDerivedFromEvent.setActor(derivationActor);
207
        originalDerivedFromEvent.setDescription(derivationDescription);
208
        originalDerivedFromEvent.setInstitution(derivationInstitution);
209
        originalDerivedFromEvent.setTimeperiod(derivationTimePeriod);
210

    
211
        occurrenceService.moveDerivate(specimenA, specimenB, dnaSample);
212
        assertTrue("DerivationEvent not removed from source!", specimenA.getDerivationEvents().isEmpty());
213
        assertEquals("DerivationEvent not moved to source!", 1, specimenB.getDerivationEvents().size());
214

    
215
        DerivationEvent derivationEvent = specimenB.getDerivationEvents().iterator().next();
216
        assertEquals("Moved DerivationEvent not of same type!", DerivationEventType.DNA_EXTRACTION(), derivationEvent.getType());
217
        assertEquals(derivationActor, derivationEvent.getActor());
218
        assertEquals(derivationDescription, derivationEvent.getDescription());
219
        assertEquals(derivationInstitution, derivationEvent.getInstitution());
220
        assertEquals(derivationTimePeriod, derivationEvent.getTimeperiod());
221
        assertEquals(DerivationEventType.DNA_EXTRACTION(), derivationEvent.getType());
222

    
223
        assertEquals("Wrong number of derivation originals!", 1, derivationEvent.getOriginals().size());
224
        SpecimenOrObservationBase<?> newOriginal = derivationEvent.getOriginals().iterator().next();
225
        assertEquals("Origin of moved object not correct", specimenB, newOriginal);
226
        assertEquals("Wrong number of derivatives!", 1, derivationEvent.getDerivatives().size());
227

    
228
        DerivedUnit movedDerivate = derivationEvent.getDerivatives().iterator().next();
229
        assertEquals("Moved derivate has wrong type", SpecimenOrObservationType.DnaSample, movedDerivate.getRecordBasis());
230
        assertNotEquals("DerivationEvent 'derivedFrom' has not been changed after moving", originalDerivedFromEvent, movedDerivate.getDerivedFrom());
231

    
232
    }
233

    
234
    @Test
235
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurenceServiceTest.move.xml")
236
    public void testMoveSequence(){
237
        DnaSample dnaSampleA = (DnaSample) occurrenceService.load(UUID.fromString("5995f852-0e78-405c-b849-d923bd6781d9"));
238
        DnaSample dnaSampleB = (DnaSample) occurrenceService.load(UUID.fromString("85fccc2f-c796-46b3-b2fc-6c9a4d68cfda"));
239
        String consensusSequence = "ATTCG";
240
        Sequence sequence = sequenceService.load(UUID.fromString("6da4f378-9861-4338-861b-7b8073763e7a"));
241

    
242
        occurrenceService.saveOrUpdate(dnaSampleA);
243
        occurrenceService.saveOrUpdate(dnaSampleB);
244
        sequenceService.saveOrUpdate(sequence);
245

    
246
        dnaSampleA.addSequence(sequence);
247

    
248
        occurrenceService.moveSequence(dnaSampleA, dnaSampleB, sequence);
249
        assertEquals("Number of sequences is wrong", 0, dnaSampleA.getSequences().size());
250
        assertEquals("Number of sequences is wrong", 1, dnaSampleB.getSequences().size());
251
        Sequence next = dnaSampleB.getSequences().iterator().next();
252
        assertEquals("Sequences are not equals", sequence, next);
253
        assertEquals("Sequences are not equals", consensusSequence, next.getSequenceString());
254
    }
255

    
256
    @Test
257
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurrenceServiceTest.testDeleteIndividualAssociatedAndTypeSpecimen.xml")
258
    public void testDeleteIndividualAssociatedAndTypeSpecimen(){
259
        final UUID taxonDEscriptionUuid = UUID.fromString("a87a893e-2ea8-427d-a26b-dbd2515d6b8a");
260
        final UUID botanicalNameUuid = UUID.fromString("a604774e-d66a-4d47-b9d1-d0e38a8c787a");
261
        final UUID fieldUnitUuid = UUID.fromString("67e81ca8-ff91-4df6-bf48-e4600c7f15a2");
262
        final UUID derivedUnitUuid = UUID.fromString("d229713b-0123-4f15-bffc-76ae45c37564");
263

    
264
        //        //how the XML was generated
265
//        FieldUnit fieldUnit = FieldUnit.NewInstance();
266
//        fieldUnit.setUuid(fieldUnitUuid);
267
//        //sub derivates (DerivedUnit, DnaSample)
268
//        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
269
//        derivedUnit.setUuid(derivedUnitUuid);
270
//
271
//        //derivation events
272
//        DerivationEvent.NewSimpleInstance(fieldUnit, derivedUnit, DerivationEventType.ACCESSIONING());
273
//
274
//        occurrenceService.save(fieldUnit);
275
//        occurrenceService.save(derivedUnit);
276
//
277
//        //create name with type specimen
278
//        BotanicalName name = BotanicalName.PARSED_NAME("Campanula patual sec L.");
279
//        name.setUuid(BOTANICAL_NAME_UUID);
280
//        SpecimenTypeDesignation typeDesignation = SpecimenTypeDesignation.NewInstance();
281
//        typeDesignation.setTypeSpecimen(derivedUnit);
282
//        //add type designation to name
283
//        name.addTypeDesignation(typeDesignation, false);
284
//
285
//        // create taxon with name and two taxon descriptions (one with
286
//        // IndividualsAssociations and a "described" voucher specimen, and an
287
//        // empty one)
288
//        Taxon taxon = Taxon.NewInstance(name, null);
289
//        taxon.setUuid(taxonUuid);
290
//        TaxonDescription taxonDescription = TaxonDescription.NewInstance();
291
//        taxonDescription.setUuid(TAXON_DESCRIPTION_UUID);
292
//        //add voucher
293
//        taxonDescription.addElement(IndividualsAssociation.NewInstance(fieldUnit));
294
//        taxon.addDescription(taxonDescription);
295
//        //add another taxon description to taxon which is not associated with a specimen thus should not be taken into account
296
//        taxon.addDescription(TaxonDescription.NewInstance());
297
//        taxonService.saveOrUpdate(taxon);
298
//
299
//
300
//        commitAndStartNewTransaction(null);
301
//
302
//        setComplete();
303
//        endTransaction();
304
//
305
//
306
//        try {
307
//            writeDbUnitDataSetFile(new String[] {
308
//                    "SpecimenOrObservationBase",
309
//                    "SpecimenOrObservationBase_DerivationEvent",
310
//                    "DerivationEvent",
311
//                    "Sequence",
312
//                    "Sequence_SingleRead",
313
//                    "SingleRead",
314
//                    "AmplificationResult",
315
//                    "DescriptionElementBase",
316
//                    "DescriptionBase",
317
//                    "TaxonBase",
318
//                    "TypeDesignationBase",
319
//                    "TaxonNameBase",
320
//                    "TaxonNameBase_TypeDesignationBase",
321
//                    "HomotypicalGroup"
322
//            }, "testDeleteIndividualAssociatedAndTypeSpecimen");
323
//        } catch (FileNotFoundException e) {
324
//            e.printStackTrace();
325
//        }
326

    
327
        FieldUnit associatedFieldUnit = (FieldUnit) occurrenceService.load(fieldUnitUuid);
328
        DerivedUnit typeSpecimen = (DerivedUnit) occurrenceService.load(derivedUnitUuid);
329
        BotanicalName name = (BotanicalName) nameService.load(botanicalNameUuid);
330
        TaxonDescription taxonDescription = (TaxonDescription) descriptionService.load(taxonDEscriptionUuid);
331
        //check initial state (IndividualsAssociation)
332
        DescriptionElementBase descriptionElement = taxonDescription.getElements().iterator().next();
333
        assertTrue("wrong type of description element", descriptionElement.isInstanceOf(IndividualsAssociation.class));
334
        assertEquals("associated specimen is incorrect", associatedFieldUnit, ((IndividualsAssociation)descriptionElement).getAssociatedSpecimenOrObservation());
335
        //check initial state (Type Designation)
336
        Set<TypeDesignationBase> typeDesignations = name.getTypeDesignations();
337
        TypeDesignationBase<?> typeDesignation = typeDesignations.iterator().next();
338
        assertTrue("wrong type of type designation", typeDesignation.isInstanceOf(SpecimenTypeDesignation.class));
339
        assertEquals("type specimen is incorrect", typeSpecimen, ((SpecimenTypeDesignation)typeDesignation).getTypeSpecimen());
340

    
341
        SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
342
        //delete type specimen from type designation
343
        config.setDeleteFromTypeDesignation(true);
344
        occurrenceService.delete(typeSpecimen, config);
345
        assertTrue(((SpecimenTypeDesignation)typeDesignation).getTypeSpecimen()==null);
346

    
347
        //delete associated field unit from IndividualsAssociation
348
        config.setDeleteFromIndividualsAssociation(true);
349
        occurrenceService.delete(associatedFieldUnit, config);
350
        assertTrue(((IndividualsAssociation)descriptionElement).getAssociatedSpecimenOrObservation()==null);
351

    
352
    }
353

    
354
    @Test
355
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurrenceService.loadData.xml")
356
    public void testLoadData(){
357
        String fieldUnitUuid = "5a31df5a-2e4d-40b1-8d4e-5754736ae7ef";
358
        String derivedUnitUuid = "18f70977-5d9c-400a-96c4-0cb7a2cd287e";
359

    
360
        FieldUnit fieldUnit = (FieldUnit) occurrenceService.load(UUID.fromString(fieldUnitUuid));
361
        DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(UUID.fromString(derivedUnitUuid));
362

    
363
        assertFalse(fieldUnit.getDerivationEvents().iterator().next().getDerivatives().isEmpty());
364
        assertTrue(derivedUnit.getDerivedFrom()!=null);
365
    }
366

    
367
    @Test
368
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurrenceServiceTest.testListAssociatedAndTypedTaxa.xml")
369
    public void testListAssociatedAndTypedTaxa(){
370
        UUID fieldUnitUuid = UUID.fromString("b359ff20-98de-46bf-aa43-3e10bb072cd4");
371
        UUID typeSpecimenUuid = UUID.fromString("0f8608c7-ffe7-40e6-828c-cb3382580878");
372
        UUID taxonDescriptionUuid = UUID.fromString("d77db2d4-45a1-4aa1-ab34-f33395f54965");
373
        UUID taxonUuid = UUID.fromString("b0de794c-8cb7-4369-8f83-870ca37abbe0");
374
//        //how the XML was generated
375
//        FieldUnit associatedFieldUnit = FieldUnit.NewInstance();
376
//        associatedFieldUnit.setUuid(fieldUnitUuid);
377
//        //sub derivates (DerivedUnit, DnaSample)
378
//        DerivedUnit typeSpecimen = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
379
//        typeSpecimen.setUuid(typeSpecimenUuid);
380
//
381
//        //derivation events
382
//        DerivationEvent.NewSimpleInstance(associatedFieldUnit, typeSpecimen, DerivationEventType.ACCESSIONING());
383
//
384
//        occurrenceService.save(associatedFieldUnit);
385
//        occurrenceService.save(typeSpecimen);
386
//
387
//        //create name with type specimen
388
//        BotanicalName name = BotanicalName.PARSED_NAME("Campanula patual sec L.");
389
//        SpecimenTypeDesignation typeDesignation = SpecimenTypeDesignation.NewInstance();
390
//        typeDesignation.setTypeSpecimen(typeSpecimen);
391
//
392
//        // create taxon with name and two taxon descriptions (one with
393
//        // IndividualsAssociations and a "described" voucher specimen, and an
394
//        // empty one)
395
//        Taxon taxon = Taxon.NewInstance(name, null);
396
//        taxon.setUuid(taxonUuid);
397
//        TaxonDescription taxonDescription = TaxonDescription.NewInstance();
398
//        taxonDescription.setUuid(taxonDescriptionUuid);
399
//        //add voucher
400
//        taxonDescription.addElement(IndividualsAssociation.NewInstance(associatedFieldUnit));
401
//        taxon.addDescription(taxonDescription);
402
//        //add type designation to name
403
//        name.addTypeDesignation(typeDesignation, false);
404
//        //add another taxon description to taxon which is not associated with a specimen thus should not be taken into account
405
//        taxon.addDescription(TaxonDescription.NewInstance());
406
//        taxonService.saveOrUpdate(taxon);
407
//
408
//        commitAndStartNewTransaction(null);
409
//
410
//        setComplete();
411
//        endTransaction();
412
//
413
//        try {
414
//            writeDbUnitDataSetFile(new String[]{
415
//                                   "SpecimenOrObservationBase",
416
//                    "SpecimenOrObservationBase_DerivationEvent",
417
//                    "DerivationEvent",
418
//                    "Sequence",
419
//                    "Sequence_SingleRead",
420
//                    "SingleRead",
421
//                    "AmplificationResult",
422
//                    "DescriptionElementBase",
423
//                    "DescriptionBase",
424
//                    "TaxonBase",
425
//                    "TypeDesignationBase",
426
//                    "TaxonNameBase",
427
//                    "TaxonNameBase_TypeDesignationBase",
428
//                    "HomotypicalGroup"}, "testListAssociatedAndTypedTaxa");
429
//        } catch (FileNotFoundException e) {
430
//            // TODO Auto-generated catch block
431
//            e.printStackTrace();
432
//        }
433
//
434
//        System.out.println("associatedFieldUnit.getUuid() " + associatedFieldUnit.getUuid());
435
//        System.out.println("typeSpecimen.getUuid() "+typeSpecimen.getUuid());
436
//        System.out.println("taxonDescription.getUuid() "+taxonDescription.getUuid());
437
//        System.out.println("taxon.getUuid() "+taxon.getUuid());
438

    
439

    
440
        FieldUnit associatedFieldUnit = (FieldUnit) occurrenceService.load(fieldUnitUuid);
441
        DerivedUnit typeSpecimen = (DerivedUnit) occurrenceService.load(typeSpecimenUuid);
442
        Taxon taxon = (Taxon) taxonService.load(taxonUuid);
443
        TaxonDescription taxonDescription = (TaxonDescription) descriptionService.load(taxonDescriptionUuid);
444
        //check for FieldUnit (IndividualsAssociation)
445
        java.util.Collection<IndividualsAssociation> individualsAssociations = occurrenceService.listIndividualsAssociations(associatedFieldUnit, null, null, null,null);
446
        assertEquals("Number of individuals associations is incorrect", 1, individualsAssociations.size());
447
        IndividualsAssociation individualsAssociation = individualsAssociations.iterator().next();
448
        assertTrue("association has wrong type", individualsAssociation.getInDescription().isInstanceOf(TaxonDescription.class));
449
        TaxonDescription retrievedTaxonDescription = HibernateProxyHelper.deproxy(individualsAssociation.getInDescription(), TaxonDescription.class);
450
        assertEquals(taxonDescription, retrievedTaxonDescription);
451
        assertEquals("Associated taxon is incorrect", taxon, retrievedTaxonDescription.getTaxon());
452

    
453

    
454
        //check for DerivedUnit (Type Designation should exist)
455
        java.util.Collection<SpecimenTypeDesignation> typeDesignations = occurrenceService.listTypeDesignations(typeSpecimen, null, null, null,null);
456
        assertEquals("Number of type designations is incorrect", 1, typeDesignations.size());
457
        SpecimenTypeDesignation specimenTypeDesignation = typeDesignations.iterator().next();
458
        Set<TaxonNameBase> typifiedNames = specimenTypeDesignation.getTypifiedNames();
459
        assertEquals("number of typified names is incorrect", 1, typifiedNames.size());
460
        Set<?> taxonBases = typifiedNames.iterator().next().getTaxonBases();
461
        assertEquals("number of taxa incorrect", 1, taxonBases.size());
462
        Object next = taxonBases.iterator().next();
463
        assertTrue(next instanceof CdmBase && ((CdmBase)next).isInstanceOf(Taxon.class));
464
        assertEquals("Typed taxon is incorrect", taxon, next);
465
    }
466

    
467
    @Test
468
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurrenceServiceTest.testIsDeletableWithSpecimenDescription.xml")
469
    public void testIsDeletableWithSpecimenDescription(){
470
        UUID derivedUnitUuid = UUID.fromString("68095c8e-025d-49f0-8bb2-ed36378b75c3");
471
        UUID specimenDescriptionUuid = UUID.fromString("4094f947-ce84-47b1-bad5-e57e33239d3c");
472
//        //how the XML was generated
473
//        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
474
//        derivedUnit.setUuid(derivedUnitUuid);
475
//        SpecimenDescription specimenDescription = SpecimenDescription.NewInstance();
476
//        specimenDescription.setUuid(specimenDescriptionUuid);
477
//        derivedUnit.addDescription(specimenDescription);
478
//        occurrenceService.save(derivedUnit);
479
//
480
//        commitAndStartNewTransaction(null);
481
//
482
//        setComplete();
483
//        endTransaction();
484
//
485
//        try {
486
//            writeDbUnitDataSetFile(new String[] {
487
//                    "SpecimenOrObservationBase",
488
//                    "SpecimenOrObservationBase_DerivationEvent",
489
//                    "DerivationEvent",
490
//                    "Sequence",
491
//                    "Sequence_SingleRead",
492
//                    "SingleRead",
493
//                    "AmplificationResult",
494
//                    "DescriptionElementBase",
495
//                    "DescriptionBase",
496
//                    "TaxonBase",
497
//                    "TypeDesignationBase",
498
//                    "TaxonNameBase",
499
//                    "TaxonNameBase_TypeDesignationBase",
500
//                    "HomotypicalGroup"
501
//            }, "testIsDeletableWithSpecimenDescription");
502
//        } catch (FileNotFoundException e) {
503
//            e.printStackTrace();
504
//        }
505

    
506

    
507
        DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(derivedUnitUuid);
508
        SpecimenDescription specimenDescription = (SpecimenDescription) descriptionService.load(specimenDescriptionUuid);
509

    
510
        SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
511
        DeleteResult deleteResult = null;
512
        //delete derivedUnit1
513
        deleteResult = occurrenceService.isDeletable(derivedUnit, config);
514
        assertFalse(deleteResult.toString(), deleteResult.isOk());
515

    
516
        //allow deletion from Descriptions
517
        config.setDeleteFromDescription(true);
518
        deleteResult = occurrenceService.isDeletable(derivedUnit, config);
519
        assertTrue(deleteResult.toString(), deleteResult.isOk());
520
        occurrenceService.delete(derivedUnit, config);
521
        specimenDescription =  (SpecimenDescription) descriptionService.find(specimenDescriptionUuid);
522

    
523
        assertNull(specimenDescription);
524
    }
525

    
526
    @Test
527
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurrenceServiceTest.testIsDeletableWithDescribedSpecimenInTaxonDescription.xml")
528
    public void testIsDeletableWithDescribedSpecimenInTaxonDescription(){
529
        UUID fieldUnitUuid = UUID.fromString("d656a004-38ee-404c-810a-87ffb0ab16c2");
530
//        //how the XML was generated
531
//        FieldUnit fieldUnit = FieldUnit.NewInstance();
532
//        fieldUnit.setUuid(fieldUnitUuid);
533
//        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
534
//
535
//        //derivation events
536
//        DerivationEvent.NewSimpleInstance(fieldUnit, derivedUnit, DerivationEventType.ACCESSIONING());
537
//
538
//        occurrenceService.save(fieldUnit);
539
//        occurrenceService.save(derivedUnit);
540
//
541
//        // create taxon with name and two taxon descriptions
542
//        //(one with a "described" voucher specimen, and an empty one)
543
//        Taxon taxon = Taxon.NewInstance(BotanicalName.PARSED_NAME("Campanula patual sec L."), null);
544
//        taxonDescription.setUuid(taxonDescriptionUuid);
545
//        //add voucher
546
//        taxonDescription.setDescribedSpecimenOrObservation(derivedUnit);
547
//        taxon.addDescription(taxonDescription);
548
//        //add another taxon description to taxon which is not associated with a specimen thus should not be taken into account
549
//        taxon.addDescription(TaxonDescription.NewInstance());
550
//        taxonService.saveOrUpdate(taxon);
551
//
552
//
553
//        commitAndStartNewTransaction(null);
554
//
555
//        setComplete();
556
//        endTransaction();
557
//
558
//
559
//        try {
560
//            writeDbUnitDataSetFile(new String[] {
561
//                    "SpecimenOrObservationBase",
562
//                    "SpecimenOrObservationBase_DerivationEvent",
563
//                    "DerivationEvent",
564
//                    "Sequence",
565
//                    "Sequence_SingleRead",
566
//                    "SingleRead",
567
//                    "AmplificationResult",
568
//                    "DescriptionElementBase",
569
//                    "DescriptionBase",
570
//                    "TaxonBase",
571
//                    "TypeDesignationBase",
572
//                    "TaxonNameBase",
573
//                    "TaxonNameBase_TypeDesignationBase",
574
//                    "HomotypicalGroup"
575
//            }, "testIsDeletableWithDescribedSpecimenInTaxonDescription");
576
//        } catch (FileNotFoundException e) {
577
//            e.printStackTrace();
578
//        }
579

    
580
        FieldUnit associatedFieldUnit = (FieldUnit) occurrenceService.load(fieldUnitUuid);
581

    
582
        SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
583
        DeleteResult deleteResult = null;
584
        //check deletion of field unit -> should fail because of voucher specimen (describedSpecimen) in TaxonDescription
585
        deleteResult = occurrenceService.isDeletable(associatedFieldUnit, config);
586
        assertFalse(deleteResult.toString(), deleteResult.isOk());
587

    
588
        //allow deletion from TaxonDescription and deletion of child derivates
589
        config.setDeleteFromDescription(true);
590
        config.setDeleteChildren(true);
591
        deleteResult = occurrenceService.isDeletable(associatedFieldUnit, config);
592
        assertTrue(deleteResult.toString(), deleteResult.isOk());
593
    }
594

    
595
    @Test
596
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurrenceServiceTest.testIsDeletableWithIndividualsAssociationTaxonDescription.xml")
597
    public void testIsDeletableWithIndividualsAssociationTaxonDescription(){
598
        UUID fieldUnitUuid = UUID.fromString("7978b978-5100-4c7a-82ef-3a23e0f3c723");
599
        UUID taxonDescriptionUuid = UUID.fromString("d4b0d561-6e7e-4fd8-bf3c-925530f949eb");
600
//        //how the XML was generated
601
//        FieldUnit fieldUnit = FieldUnit.NewInstance();
602
//        fieldUnit.setUuid(fieldUnitUuid);
603
//
604
//        occurrenceService.save(fieldUnit);
605
//
606
//        // create taxon with name and two taxon descriptions (one with
607
//        // IndividualsAssociations and a "described" voucher specimen, and an
608
//        // empty one)
609
//        Taxon taxon = Taxon.NewInstance(BotanicalName.PARSED_NAME("Campanula patual sec L."), null);
610
//        TaxonDescription taxonDescription = TaxonDescription.NewInstance();
611
//        taxonDescription.setUuid(taxonDescriptionUuid);
612
//        //add voucher
613
//        taxonDescription.addElement(IndividualsAssociation.NewInstance(fieldUnit));
614
//        taxon.addDescription(taxonDescription);
615
//        //add another taxon description to taxon which is not associated with a specimen thus should not be taken into account
616
//        taxon.addDescription(TaxonDescription.NewInstance());
617
//        taxonService.saveOrUpdate(taxon);
618
//
619
//
620
//        commitAndStartNewTransaction(null);
621
//
622
//        setComplete();
623
//        endTransaction();
624
//
625
//
626
//        try {
627
//            writeDbUnitDataSetFile(new String[] {
628
//                    "SpecimenOrObservationBase",
629
//                    "SpecimenOrObservationBase_DerivationEvent",
630
//                    "DerivationEvent",
631
//                    "Sequence",
632
//                    "Sequence_SingleRead",
633
//                    "SingleRead",
634
//                    "AmplificationResult",
635
//                    "DescriptionElementBase",
636
//                    "DescriptionBase",
637
//                    "TaxonBase",
638
//                    "TypeDesignationBase",
639
//                    "TaxonNameBase",
640
//                    "TaxonNameBase_TypeDesignationBase",
641
//                    "HomotypicalGroup"
642
//            }, "testIsDeletableWithIndividualsAssociationTaxonDescription");
643
//        } catch (FileNotFoundException e) {
644
//            e.printStackTrace();
645
//        }
646

    
647
        FieldUnit associatedFieldUnit = (FieldUnit) occurrenceService.load(fieldUnitUuid);
648
        TaxonDescription taxonDescription = (TaxonDescription) descriptionService.load(taxonDescriptionUuid);
649
        IndividualsAssociation individualsAssociation = (IndividualsAssociation) taxonDescription.getElements().iterator().next();
650

    
651
        SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
652
        DeleteResult deleteResult = null;
653
        //check deletion of field unit -> should fail because of IndividualAssociation
654
        deleteResult = occurrenceService.isDeletable(associatedFieldUnit, config);
655
        assertFalse(deleteResult.toString(), deleteResult.isOk());
656
        assertTrue(deleteResult.toString(), deleteResult.getRelatedObjects().contains(individualsAssociation));
657

    
658
        //allow deletion of individuals association
659
        config.setDeleteFromIndividualsAssociation(true);
660
        deleteResult = occurrenceService.isDeletable(associatedFieldUnit, config);
661
        assertTrue(deleteResult.toString(), deleteResult.isOk());
662

    
663
    }
664

    
665
    @Test
666
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurrenceServiceTest.testIsDeletableWithTypeDesignation.xml")
667
    public void testIsDeletableWithTypeDesignation(){
668
        UUID derivedUnitUuid = UUID.fromString("f7fd1dc1-3c93-42a7-8279-cde5bfe37ea0");
669
        UUID botanicalNameUuid = UUID.fromString("7396430c-c932-4dd3-a45a-40c2808b132e");
670
//        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
671
//        derivedUnit.setUuid(derivedUnitUuid);
672
//
673
//        occurrenceService.save(derivedUnit);
674
//
675
//        //create name with type specimen
676
//        BotanicalName name = BotanicalName.PARSED_NAME("Campanula patual sec L.");
677
//        name.setUuid(botanicalNameUuid);
678
//        SpecimenTypeDesignation typeDesignation = SpecimenTypeDesignation.NewInstance();
679
//        typeDesignation.setTypeSpecimen(derivedUnit);
680
//        //add type designation to name
681
//        name.addTypeDesignation(typeDesignation, false);
682
//
683
//        nameService.saveOrUpdate(name);
684
//
685
//        commitAndStartNewTransaction(null);
686
//
687
//        setComplete();
688
//        endTransaction();
689
//
690
//
691
//        try {
692
//            writeDbUnitDataSetFile(new String[] {
693
//                    "SpecimenOrObservationBase",
694
//                    "SpecimenOrObservationBase_DerivationEvent",
695
//                    "DerivationEvent",
696
//                    "Sequence",
697
//                    "Sequence_SingleRead",
698
//                    "SingleRead",
699
//                    "AmplificationResult",
700
//                    "DescriptionElementBase",
701
//                    "DescriptionBase",
702
//                    "TaxonBase",
703
//                    "TypeDesignationBase",
704
//                    "TaxonNameBase",
705
//                    "TaxonNameBase_TypeDesignationBase",
706
//                    "HomotypicalGroup"
707
//            }, "testIsDeletableWithTypeDesignation");
708
//        } catch (FileNotFoundException e) {
709
//            e.printStackTrace();
710
//        }
711

    
712

    
713

    
714
        DerivedUnit typeSpecimen = (DerivedUnit) occurrenceService.load(derivedUnitUuid);
715

    
716
        //create name with type specimen
717
        BotanicalName name = (BotanicalName) nameService.load(botanicalNameUuid);
718
        SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation) name.getTypeDesignations().iterator().next();
719

    
720
        //add type designation to name
721
        name.addTypeDesignation(typeDesignation, false);
722

    
723
        SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
724
        DeleteResult deleteResult = null;
725

    
726
        //check deletion of specimen
727
        deleteResult = occurrenceService.isDeletable(typeSpecimen, config);
728
        assertFalse(deleteResult.toString(), deleteResult.isOk());
729
        assertTrue(deleteResult.toString(), deleteResult.getRelatedObjects().contains(typeDesignation));
730

    
731
        //allow deletion of type designation
732
        config.setDeleteFromTypeDesignation(true);
733

    
734
        deleteResult = occurrenceService.isDeletable(typeSpecimen, config);
735
        assertTrue(deleteResult.toString(), deleteResult.isOk());
736
    }
737

    
738
    @Test
739
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurrenceServiceTest.testIsDeletableWithChildren.xml")
740
    public void testIsDeletableWithChildren(){
741
        UUID fieldUnitUuid = UUID.fromString("92ada058-4c14-4131-8ecd-b82dc1dd2882");
742
        UUID derivedUnitUuid = UUID.fromString("896dffdc-6809-4914-8950-5501fee1c0fd");
743
        UUID dnaSampleUuid = UUID.fromString("7efd1d66-ac7f-4202-acdf-a72cbb9c3a21");
744
        //if this test fails be sure to check if there are left-over elements in the DB
745
        //e.g. clear by adding "<AMPLIFICATIONRESULT/>" to the data set XML
746

    
747
//        //how the XML was generated
748
//        FieldUnit fieldUnit = FieldUnit.NewInstance();
749
//        fieldUnit.setUuid(fieldUnitUuid);
750
//        //sub derivates (DerivedUnit, DnaSample)
751
//        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
752
//        derivedUnit.setUuid(derivedUnitUuid);
753
//        DnaSample dnaSample = DnaSample.NewInstance();
754
//        dnaSample.setUuid(dnaSampleUuid);
755
//
756
//        //derivation events
757
//        DerivationEvent.NewSimpleInstance(fieldUnit, derivedUnit, DerivationEventType.ACCESSIONING());
758
//        DerivationEvent.NewSimpleInstance(derivedUnit, dnaSample, DerivationEventType.DNA_EXTRACTION());
759
//
760
//        occurrenceService.save(fieldUnit);
761
//        occurrenceService.save(derivedUnit);
762
//        occurrenceService.save(dnaSample);
763
//
764
//        commitAndStartNewTransaction(null);
765
//
766
//        setComplete();
767
//        endTransaction();
768
//
769
//        try {
770
//            writeDbUnitDataSetFile(new String[] {
771
//                    "SpecimenOrObservationBase",
772
//                    "SpecimenOrObservationBase_DerivationEvent",
773
//                    "DerivationEvent",
774
//                    "Sequence",
775
//                    "SingleRead",
776
//                    "SingleReadAlignment",
777
//                    "Amplification",
778
//                    "AmplificationResult",
779
//                    "DescriptionElementBase",
780
//                    "DescriptionBase",
781
//                    "TaxonBase",
782
//                    "TypeDesignationBase",
783
//                    "TaxonNameBase",
784
//                    "TaxonNameBase_TypeDesignationBase",
785
//                    "HomotypicalGroup"
786
//            }, "testIsDeletableWithChildren");
787
//        } catch (FileNotFoundException e) {
788
//            e.printStackTrace();
789
//        }
790
        FieldUnit fieldUnit = (FieldUnit) occurrenceService.load(fieldUnitUuid);
791
        //sub derivates (DerivedUnit, DnaSample)
792
        DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(derivedUnitUuid);
793
        DnaSample dnaSample = (DnaSample) occurrenceService.load(dnaSampleUuid);
794

    
795
        //derivation events
796
        DerivationEvent fieldUnitToDerivedUnitEvent = fieldUnit.getDerivationEvents().iterator().next();
797
        DerivationEvent derivedUnitToDnaSampleEvent = derivedUnit.getDerivationEvents().iterator().next();
798

    
799
        SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
800

    
801
        DeleteResult deleteResult = null;
802
        //check deletion of DnaSample
803
        deleteResult = occurrenceService.isDeletable(dnaSample, config);
804
        assertTrue(deleteResult.toString(), deleteResult.isOk());
805

    
806
        //check deletion of Specimen
807
        deleteResult = occurrenceService.isDeletable(derivedUnit, config);
808
        assertFalse(deleteResult.toString(), deleteResult.isOk());
809
        assertTrue(deleteResult.toString(), deleteResult.getRelatedObjects().contains(derivedUnitToDnaSampleEvent));
810

    
811
        //check deletion of fieldUnit
812
        deleteResult = occurrenceService.isDeletable(fieldUnit, config);
813
        assertFalse(deleteResult.toString(), deleteResult.isOk());
814
        assertTrue(deleteResult.toString(), deleteResult.getRelatedObjects().contains(fieldUnitToDerivedUnitEvent));
815
    }
816

    
817
    @Test
818
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="OccurrenceServiceTest.testFindOcurrences.xml")
819
    public void testFindOccurrences(){
820
        UUID derivedUnit1Uuid = UUID.fromString("843bc8c9-c0fe-4735-bf40-82f1996dcefb");
821
        UUID derivedUnit2Uuid = UUID.fromString("40cd9cb1-7c74-4e7d-a1f8-8a1e0314e940");
822
        UUID dnaSampleUuid = UUID.fromString("364969a6-2457-4e2e-ae1e-29a6fcaa741a");
823
        UUID tissueUuid = UUID.fromString("b608613c-1b5a-4882-8b14-d643b6fc5998");
824

    
825
        UUID taxonUuid = UUID.fromString("dfca7629-8a60-4d51-998d-371897f725e9");
826

    
827
//        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
828
//        derivedUnit.setTitleCache("testUnit1");
829
//        derivedUnit.setAccessionNumber("ACC1");
830
//        DerivedUnit derivedUnit2 = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
831
//        derivedUnit2.setTitleCache("testUnit2");
832
//        derivedUnit2.setBarcode("ACC2");
833
//        DerivedUnit dnaSample = DerivedUnit.NewInstance(SpecimenOrObservationType.DnaSample);
834
//        dnaSample.setTitleCache("dna");
835
//        dnaSample.setCatalogNumber("ACC1");
836
//        DerivedUnit tissue = DerivedUnit.NewInstance(SpecimenOrObservationType.TissueSample);
837
//        tissue.setTitleCache("tissue");
838
//
839
//        DerivationEvent.NewSimpleInstance(derivedUnit, dnaSample, DerivationEventType.DNA_EXTRACTION());
840
//
841
//        derivedUnit.setUuid(derivedUnit1Uuid);
842
//        derivedUnit2.setUuid(derivedUnit2Uuid);
843
//        dnaSample.setUuid(dnaSampleUuid);
844
//        tissue.setUuid(tissueUuid);
845
//
846
//        occurrenceService.save(derivedUnit);
847
//        occurrenceService.save(derivedUnit2);
848
//        occurrenceService.save(dnaSample);
849
//        occurrenceService.save(tissue);
850
//
851
//        Taxon taxon = Taxon.NewInstance(BotanicalName.PARSED_NAME("Campanula patual"), null);
852
//        taxon.setUuid(taxonUuid);
853
//        TaxonDescription taxonDescription = TaxonDescription.NewInstance();
854
//        taxonDescription.setUuid(UUID.fromString("272d4d28-662c-468e-94d8-16993fab91ba"));
855
//        //add voucher
856
//        taxonDescription.addElement(IndividualsAssociation.NewInstance(derivedUnit));
857
//        taxonDescription.addElement(IndividualsAssociation.NewInstance(tissue));
858
//        taxon.addDescription(taxonDescription);
859
//        taxonService.saveOrUpdate(taxon);
860
//
861
//        commitAndStartNewTransaction(null);
862
//
863
//        setComplete();
864
//        endTransaction();
865
//
866
//
867
//        try {
868
//            writeDbUnitDataSetFile(new String[] {
869
//                    "SpecimenOrObservationBase",
870
//                    "SpecimenOrObservationBase_DerivationEvent",
871
//                    "DerivationEvent",
872
//                    "DescriptionElementBase",
873
//                    "DescriptionBase",
874
//                    "TaxonBase",
875
//                    "TypeDesignationBase",
876
//                    "TaxonNameBase",
877
//                    "TaxonNameBase_TypeDesignationBase",
878
//                    "HomotypicalGroup",
879
//                    "TeamOrPersonBase"
880
//            }, "testFindOcurrences");
881
//        } catch (FileNotFoundException e) {
882
//            e.printStackTrace();
883
//        }
884

    
885
        SpecimenOrObservationBase derivedUnit1 = occurrenceService.load(derivedUnit1Uuid);
886
        SpecimenOrObservationBase derivedUnit2 = occurrenceService.load(derivedUnit2Uuid);
887
        SpecimenOrObservationBase tissue = occurrenceService.load(tissueUuid);
888
        SpecimenOrObservationBase dnaSample = occurrenceService.load(dnaSampleUuid);
889
        Taxon taxon = (Taxon) taxonService.load(taxonUuid);
890

    
891
        assertNotNull(derivedUnit1);
892
        assertNotNull(derivedUnit2);
893
        assertNotNull(tissue);
894
        assertNotNull(dnaSample);
895
        assertNotNull(taxon);
896

    
897
        //wildcard search => all derivates
898
        FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
899
        config.setTitleSearchString("*");
900
        assertEquals(4, occurrenceService.countOccurrences(config));
901
        List<SpecimenOrObservationBase> allDerivates = occurrenceService.findByTitle(config).getRecords();
902
        assertEquals(4, allDerivates.size());
903
        assertTrue(allDerivates.contains(derivedUnit1));
904
        assertTrue(allDerivates.contains(derivedUnit2));
905
        assertTrue(allDerivates.contains(tissue));
906
        assertTrue(allDerivates.contains(dnaSample));
907

    
908
        //queryString search => 2 derivates
909
        config = new FindOccurrencesConfigurator();
910
        config.setTitleSearchString("test*");
911
//        config.setClazz(SpecimenOrObservationBase.class);
912
        assertEquals(2, occurrenceService.countOccurrences(config));
913
        List<SpecimenOrObservationBase> queryStringDerivates = occurrenceService.findByTitle(config).getRecords();
914
        assertEquals(2, queryStringDerivates.size());
915
        assertTrue(queryStringDerivates.contains(derivedUnit1));
916
        assertTrue(queryStringDerivates.contains(derivedUnit2));
917

    
918
        //class search => 0 results
919
        config = new FindOccurrencesConfigurator();
920
        config.setClazz(FieldUnit.class);
921
        assertEquals(0, occurrenceService.countOccurrences(config));
922
        List<SpecimenOrObservationBase> fieldUnits = occurrenceService.findByTitle(config).getRecords();
923
        assertEquals(0, fieldUnits.size());
924

    
925
        //class search => 4 results
926
        config = new FindOccurrencesConfigurator();
927
        config.setClazz(DerivedUnit.class);
928
        assertEquals(4, occurrenceService.countOccurrences(config));
929
        List<SpecimenOrObservationBase> derivedUnits = occurrenceService.findByTitle(config).getRecords();
930
        assertEquals(4, derivedUnits.size());
931
        assertTrue(derivedUnits.contains(derivedUnit1));
932
        assertTrue(derivedUnits.contains(derivedUnit2));
933
        assertTrue(derivedUnits.contains(tissue));
934
        assertTrue(derivedUnits.contains(dnaSample));
935

    
936
        //significant identifier search
937
        config = new FindOccurrencesConfigurator();
938
        config.setClazz(DerivedUnit.class);
939
        config.setSignificantIdentifier("ACC1");
940
        assertEquals(2, occurrenceService.countOccurrences(config));
941
        List<SpecimenOrObservationBase> accessionedUnits = occurrenceService.findByTitle(config).getRecords();
942
        assertEquals(2, accessionedUnits.size());
943
        assertTrue(accessionedUnits.contains(derivedUnit1));
944
        assertFalse(accessionedUnits.contains(derivedUnit2));
945
        assertFalse(accessionedUnits.contains(tissue));
946
        assertTrue(accessionedUnits.contains(dnaSample));
947

    
948
        config = new FindOccurrencesConfigurator();
949
        config.setClazz(DerivedUnit.class);
950
        config.setSignificantIdentifier("ACC2");
951
        assertEquals(1, occurrenceService.countOccurrences(config));
952
        List<SpecimenOrObservationBase> barcodedUnits = occurrenceService.findByTitle(config).getRecords();
953
        assertEquals(1, barcodedUnits.size());
954
        assertFalse(barcodedUnits.contains(derivedUnit1));
955
        assertTrue(barcodedUnits.contains(derivedUnit2));
956
        assertFalse(barcodedUnits.contains(tissue));
957
        assertFalse(barcodedUnits.contains(dnaSample));
958

    
959

    
960
        //recordBasis search => 1 Fossil
961
        config = new FindOccurrencesConfigurator();
962
        config.setSpecimenType(SpecimenOrObservationType.Fossil);
963
        assertEquals(1, occurrenceService.countOccurrences(config));
964
        List<SpecimenOrObservationBase> fossils = occurrenceService.findByTitle(config).getRecords();
965
        assertEquals(1, fossils.size());
966
        assertTrue(fossils.contains(derivedUnit1));
967

    
968
        //taxon determination search => 2 associated specimens
969
        config = new FindOccurrencesConfigurator();
970
        config.setClazz(DerivedUnit.class);
971
        config.setAssociatedTaxonUuid(taxon.getUuid());
972
        assertEquals(2, occurrenceService.countOccurrences(config));
973
        List<SpecimenOrObservationBase> associatedSpecimens = occurrenceService.findByTitle(config).getRecords();
974
        assertEquals(2, associatedSpecimens.size());
975
        assertTrue(associatedSpecimens.contains(derivedUnit1));
976
        assertTrue(associatedSpecimens.contains(tissue));
977

    
978
        //taxon determination search (indirectly associated) => 3 associated specimens
979
        config = new FindOccurrencesConfigurator();
980
        config.setClazz(DerivedUnit.class);
981
        config.setAssociatedTaxonUuid(taxon.getUuid());
982
        config.setRetrieveIndirectlyAssociatedSpecimens(true);
983
        assertEquals(3, occurrenceService.countOccurrences(config));
984
        List<SpecimenOrObservationBase> indirectlyAssociatedSpecimens = occurrenceService.findByTitle(config).getRecords();
985
        assertEquals(3, indirectlyAssociatedSpecimens.size());
986
        assertTrue(indirectlyAssociatedSpecimens.contains(derivedUnit1));
987
        assertTrue(indirectlyAssociatedSpecimens.contains(dnaSample));
988
        assertTrue(indirectlyAssociatedSpecimens.contains(tissue));
989

    
990
        //using the super class will lead to 0 results because listByAssociatedTaxon does type matching which obviously does not understand inheritance
991
        config = new FindOccurrencesConfigurator();
992
        config.setClazz(SpecimenOrObservationBase.class);
993
        config.setAssociatedTaxonUuid(taxon.getUuid());
994
        assertEquals(0, occurrenceService.countOccurrences(config));
995
        List<SpecimenOrObservationBase> specimens = occurrenceService.findByTitle(config).getRecords();
996
        assertEquals(0, specimens.size());
997

    
998
    }
999

    
1000
    /* (non-Javadoc)
1001
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
1002
     */
1003
    @Override
1004
//    @Test
1005
    public void createTestDataSet() throws FileNotFoundException {
1006
        UUID derivedUnit1Uuid = UUID.fromString("843bc8c9-c0fe-4735-bf40-82f1996dcefb");
1007
        UUID derivedUnit2Uuid = UUID.fromString("40cd9cb1-7c74-4e7d-a1f8-8a1e0314e940");
1008
        UUID dnaSampleUuid = UUID.fromString("364969a6-2457-4e2e-ae1e-29a6fcaa741a");
1009
        UUID tissueUuid = UUID.fromString("b608613c-1b5a-4882-8b14-d643b6fc5998");
1010

    
1011
        UUID taxonUuid = UUID.fromString("dfca7629-8a60-4d51-998d-371897f725e9");
1012

    
1013
        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
1014
        derivedUnit.setAccessionNumber("ACC1");
1015
        derivedUnit.setTitleCache("testUnit1", true);
1016
        DerivedUnit derivedUnit2 = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
1017
        derivedUnit2.setBarcode("ACC2");
1018
        derivedUnit2.setTitleCache("testUnit2", true);
1019
        DerivedUnit dnaSample = DerivedUnit.NewInstance(SpecimenOrObservationType.DnaSample);
1020
        dnaSample.setTitleCache("dna", true);
1021
        dnaSample.setCatalogNumber("ACC1");
1022
        DerivedUnit tissue = DerivedUnit.NewInstance(SpecimenOrObservationType.TissueSample);
1023
        tissue.setTitleCache("tissue", true);
1024

    
1025
        DerivationEvent.NewSimpleInstance(derivedUnit, dnaSample, DerivationEventType.DNA_EXTRACTION());
1026

    
1027
        derivedUnit.setUuid(derivedUnit1Uuid);
1028
        derivedUnit2.setUuid(derivedUnit2Uuid);
1029
        dnaSample.setUuid(dnaSampleUuid);
1030
        tissue.setUuid(tissueUuid);
1031

    
1032
        occurrenceService.save(derivedUnit);
1033
        occurrenceService.save(derivedUnit2);
1034
        occurrenceService.save(dnaSample);
1035
        occurrenceService.save(tissue);
1036

    
1037
        Taxon taxon = Taxon.NewInstance(BotanicalName.PARSED_NAME("Campanula patual"), null);
1038
        taxon.setUuid(taxonUuid);
1039
        TaxonDescription taxonDescription = TaxonDescription.NewInstance();
1040
        taxonDescription.setUuid(UUID.fromString("272d4d28-662c-468e-94d8-16993fab91ba"));
1041
        //add voucher
1042
        taxonDescription.addElement(IndividualsAssociation.NewInstance(derivedUnit));
1043
        taxonDescription.addElement(IndividualsAssociation.NewInstance(tissue));
1044
        taxon.addDescription(taxonDescription);
1045
        taxonService.saveOrUpdate(taxon);
1046

    
1047
        commitAndStartNewTransaction(null);
1048

    
1049
        setComplete();
1050
        endTransaction();
1051

    
1052

    
1053
        try {
1054
            writeDbUnitDataSetFile(new String[] {
1055
                    "SpecimenOrObservationBase",
1056
                    "SpecimenOrObservationBase_DerivationEvent",
1057
                    "DerivationEvent",
1058
                    "DescriptionElementBase",
1059
                    "DescriptionBase",
1060
                    "TaxonBase",
1061
                    "TypeDesignationBase",
1062
                    "TaxonNameBase",
1063
                    "TaxonNameBase_TypeDesignationBase",
1064
                    "HomotypicalGroup",
1065
                    "TeamOrPersonBase"
1066
            }, "testFindOcurrences");
1067
        } catch (FileNotFoundException e) {
1068
            e.printStackTrace();
1069
        }
1070
    }
1071

    
1072
}
(15-15/31)