Project

General

Profile

Download (34.3 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.io.specimen.abcd206.in;
11

    
12
import static org.junit.Assert.assertEquals;
13
import static org.junit.Assert.assertNotNull;
14
import static org.junit.Assert.assertTrue;
15

    
16
import java.io.FileNotFoundException;
17
import java.net.URI;
18
import java.net.URISyntaxException;
19
import java.net.URL;
20
import java.text.ParseException;
21
import java.util.Collection;
22
import java.util.List;
23
import java.util.Set;
24
import java.util.UUID;
25

    
26
import org.joda.time.DateTime;
27
import org.junit.Assert;
28
import org.junit.Ignore;
29
import org.junit.Test;
30
import org.unitils.dbunit.annotation.DataSet;
31
import org.unitils.dbunit.annotation.DataSets;
32
import org.unitils.spring.annotation.SpringBeanByName;
33
import org.unitils.spring.annotation.SpringBeanByType;
34

    
35
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
36
import eu.etaxonomy.cdm.api.service.IReferenceService;
37
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
38
import eu.etaxonomy.cdm.api.service.ITaxonService;
39
import eu.etaxonomy.cdm.api.service.ITermService;
40
import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
41
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
42
import eu.etaxonomy.cdm.model.media.MediaUtils;
43
import eu.etaxonomy.cdm.model.molecular.Amplification;
44
import eu.etaxonomy.cdm.model.molecular.AmplificationResult;
45
import eu.etaxonomy.cdm.model.molecular.DnaQuality;
46
import eu.etaxonomy.cdm.model.molecular.DnaSample;
47
import eu.etaxonomy.cdm.model.molecular.Primer;
48
import eu.etaxonomy.cdm.model.molecular.Sequence;
49
import eu.etaxonomy.cdm.model.molecular.SequenceDirection;
50
import eu.etaxonomy.cdm.model.molecular.SequenceString;
51
import eu.etaxonomy.cdm.model.molecular.SingleRead;
52
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
53
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
54
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
55
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
56
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
57
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
58
import eu.etaxonomy.cdm.model.reference.Reference;
59
import eu.etaxonomy.cdm.model.taxon.Taxon;
60
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
61
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
62
import eu.etaxonomy.cdm.model.term.DefinedTerm;
63
import eu.etaxonomy.cdm.persistence.query.MatchMode;
64
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
65
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
66

    
67
/**
68
 * @author a.mueller
69
 * @since 29.01.2009
70
 */
71

    
72
public class AbcdGgbnImportTest extends CdmTransactionalIntegrationTest {
73

    
74
	@SpringBeanByName
75
	private CdmApplicationAwareDefaultImport<?> defaultImport;
76

    
77
	@SpringBeanByType
78
	private IOccurrenceService occurrenceService;
79

    
80
	@SpringBeanByType
81
	private IReferenceService referenceService;
82

    
83
	@SpringBeanByType
84
	private ITermService termService;
85

    
86
	@SpringBeanByType
87
	private ITaxonService taxonService;
88

    
89
	@SpringBeanByType
90
	private ITaxonNodeService taxonNodeService;
91

    
92
	/**
93
	 * Tests import import of two DNA unit belonging to two different taxa
94
	 * @throws ParseException
95
	 */
96
	@Test
97
	@DataSet( value="/eu/etaxonomy/cdm/database/BlankDataSet.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
98
	public void testImportTwoDnaUnitsWithTwoTaxa() throws ParseException {
99
	    String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/Campanula_2taxa.xml";
100
        URL url = this.getClass().getResource(inputFile);
101
        assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
102

    
103
        Abcd206ImportConfigurator importConfigurator = null;
104
        try {
105
            importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
106
            importConfigurator.setGetSiblings(false);
107
        } catch (URISyntaxException e) {
108
            e.printStackTrace();
109
            Assert.fail();
110
        }
111
        assertNotNull("Configurator could not be created", importConfigurator);
112

    
113
        boolean result = defaultImport.invoke(importConfigurator).isSuccess();
114
        assertTrue("Return value for import.invoke should be true", result);
115
        assertEquals("Number of derived units is incorrect", 4, occurrenceService.count(DerivedUnit.class));
116
        assertEquals("Number of dna samples is incorrect", 2, occurrenceService.count(DnaSample.class));
117
        assertEquals("Number of field units is incorrect", 2, occurrenceService.count(FieldUnit.class));
118
        /*
119
         * Default classification
120
         *  - Campanula
121
         *   - Campanula glomerata
122
         *   - Campanula bononiensis
123
         */
124
        List<TaxonNode> nodes = taxonNodeService.list(TaxonNode.class, 100, 0, null, null);
125
        // TODO: this needs to be fixed, the sortindex should never be -1
126
        for (TaxonNode node: nodes){
127
            if (node.getTaxon() != null){
128
                assertTrue("The sortindex should not be smaller than 0", node.getSortIndex() > -1 );
129
            }
130
        }
131
        assertEquals("Number of taxon nodes is incorrect", 4, taxonNodeService.count(TaxonNode.class));
132
        assertEquals("Number of taxa is incorrect", 3, taxonService.count(TaxonBase.class));
133
        assertEquals(1, taxonService.findByTitleWithRestrictions(Taxon.class, "Campanula bononiensis", MatchMode.ANYWHERE, null, null, null, null, null).getRecords().size());
134
        assertEquals(1, taxonService.findByTitleWithRestrictions(Taxon.class, "Campanula isaurica", MatchMode.ANYWHERE, null, null, null, null, null).getRecords().size());
135

    
136
        //test for sortindex=-1
137

    
138

    
139
	}
140

    
141
	/**
142
	 * Added this test because there was a unique key on the citations_id column of table
143
	 * SEQUENCE_REFERENCE which makes no sense.
144
	 */
145
    @Test
146
    @DataSet( value="/eu/etaxonomy/cdm/database/BlankDataSet.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
147
    public void testImportTwoSequencesWithSameReference() {
148
        String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/Campanula_DNA_CAM_90-91.xml";
149
        URL url = this.getClass().getResource(inputFile);
150
        assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
151

    
152
        Abcd206ImportConfigurator importConfigurator = null;
153
        try {
154
            importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
155
        } catch (URISyntaxException e) {
156
            e.printStackTrace();
157
            Assert.fail();
158
        }
159
        assertNotNull("Configurator could not be created", importConfigurator);
160

    
161
        importConfigurator.setMapUnitIdToCatalogNumber(true);
162
        boolean result = defaultImport.invoke(importConfigurator).isSuccess();
163
        assertTrue("Return value for import.invoke should be true", result);
164
    }
165

    
166
	/**
167
	 * Tests import import of 59 DNA unit
168
	 */
169
	@Test
170
	@DataSet( value="/eu/etaxonomy/cdm/database/BlankDataSet.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
171
	@Ignore
172
	public void testImport59Units() {
173
	    String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/Campanula_59taxa.xml";
174
        URL url = this.getClass().getResource(inputFile);
175
        assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
176

    
177
        Abcd206ImportConfigurator importConfigurator = null;
178
        try {
179
            importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
180
        } catch (URISyntaxException e) {
181
            e.printStackTrace();
182
            Assert.fail();
183
        }
184
        assertNotNull("Configurator could not be created", importConfigurator);
185

    
186
        importConfigurator.setIgnoreAuthorship(true);
187
        boolean result = defaultImport.invoke(importConfigurator).isSuccess();
188
    //    assertTrue("Return value for import.invoke should be true", result);
189
        assertEquals("Number of derived units is incorrect", 118, occurrenceService.count(DerivedUnit.class));
190
        assertEquals("Number of dna samples is incorrect", 59, occurrenceService.count(DnaSample.class));
191

    
192
	}
193

    
194
	/**
195
	 * Tests import import of DNA unit and all its parameters
196
	 * and sub derivatives (sequence, amplification, etc.)
197
	 * @throws ParseException
198
	 */
199
	@Test
200
    @DataSet( value="/eu/etaxonomy/cdm/database/BlankDataSet.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
201
    public void testImportGgbn() throws ParseException {
202
        String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/db6_without_association.xml";
203
        URL url = this.getClass().getResource(inputFile);
204
        assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
205

    
206
        Abcd206ImportConfigurator importConfigurator = null;
207
        try {
208
            importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
209
        } catch (URISyntaxException e) {
210
            e.printStackTrace();
211
            Assert.fail();
212
        }
213
        assertNotNull("Configurator could not be created", importConfigurator);
214

    
215
        boolean result = defaultImport.invoke(importConfigurator).isSuccess();
216
        assertTrue("Return value for import.invoke should be true", result);
217
        assertEquals("Number of derived units is incorrect", 1, occurrenceService.count(DerivedUnit.class));
218
        assertEquals("Number of dna samples is incorrect", 1, occurrenceService.count(DnaSample.class));
219
        assertEquals("Number of field units is incorrect", 1, occurrenceService.count(FieldUnit.class));
220

    
221
        //dna sample
222
        FindOccurrencesConfigurator dnaConfig = new FindOccurrencesConfigurator();
223
        dnaConfig.setSignificantIdentifier("DB 6");
224
        List<SpecimenOrObservationBase> dnaRecords = occurrenceService.findByTitle(dnaConfig).getRecords();
225
        assertEquals(1, dnaRecords.size());
226
        SpecimenOrObservationBase dnaSpecimen = dnaRecords.iterator().next();
227
        assertEquals(DnaSample.class, dnaSpecimen.getClass());
228
        DnaSample dnaSample = (DnaSample) dnaSpecimen;
229
        DerivationEvent derivedFrom = dnaSample.getDerivedFrom();
230
        assertNotNull(derivedFrom);
231
        assertEquals("Wrong derivation type!", DerivationEventType.DNA_EXTRACTION(), derivedFrom.getType());
232
        assertEquals("Wrong number of originals", 1, derivedFrom.getOriginals().size());
233
        assertTrue(derivedFrom.getOriginals().iterator().next() instanceof FieldUnit);
234
        assertEquals("DNA Bank", dnaSample.getCollection().getCode());
235
        assertEquals(SpecimenOrObservationType.DnaSample, dnaSample.getRecordBasis());
236
        //preservation/preparation
237
        assertNotNull(derivedFrom.getActor());
238
        assertEquals("Bansemer, Jana", derivedFrom.getActor().getTitleCache());
239
        assertNotNull(derivedFrom.getTimeperiod());
240
        assertEquals((Integer)2002,derivedFrom.getTimeperiod().getStartYear());
241
        assertEquals((Integer)8,derivedFrom.getTimeperiod().getStartMonth());
242
        assertEquals((Integer)13,derivedFrom.getTimeperiod().getStartDay());
243
        assertNotNull(dnaSample.getPreservation());
244
        assertEquals("DNeasy Plant Mini Spin Kit Qiagen", derivedFrom.getDescription());
245
        //sample designation
246
        Set<String> identifiers = dnaSample.getIdentifiers((DefinedTerm) termService.find(UUID.fromString("fadeba12-1be3-4bc7-9ff5-361b088d86fc")));
247
        assertNotNull(identifiers);
248
        assertEquals(1, identifiers.size());
249
        assertEquals("CAM010", identifiers.iterator().next());
250

    
251

    
252
        //dna quality
253
        DnaQuality dnaQuality = dnaSample.getDnaQuality();
254
        assertNotNull("Dna quality is null", dnaQuality!=null);
255
        assertEquals(new Double("0.77"),dnaQuality.getRatioOfAbsorbance260_230());
256
        assertEquals(new Double("1.38"),dnaQuality.getRatioOfAbsorbance260_280());
257
        assertEquals(new DateTime(2008, 4, 15, 0, 0),dnaQuality.getQualityCheckDate());
258
//        assertEquals(MeasurementUnit.NewInstance(, label, labelAbbrev)DateTime(2008, 4, 15, 0, 0),dnaQuality.getQualityCheckDate());
259

    
260
        //amplifications
261
        Set<AmplificationResult> amplificationResults = dnaSample.getAmplificationResults();
262
        assertNotNull(amplificationResults);
263
        assertEquals(1,  amplificationResults.size());
264
        AmplificationResult amplificationResult = amplificationResults.iterator().next();
265
        Amplification amplification = amplificationResult.getAmplification();
266
        assertNotNull("Amplification is null", amplification);
267
        DefinedTerm dnaMarker = amplification.getDnaMarker();
268
        assertNotNull(dnaMarker);
269
        assertEquals("ITS (ITS1, 5.8S rRNA, ITS2)", dnaMarker.getLabel());
270

    
271
        //amplification primers
272
        Primer forwardPrimer = amplification.getForwardPrimer();
273
        assertNotNull(forwardPrimer);
274
        assertEquals("PIpetB1411F", forwardPrimer.getLabel());
275
        assertEquals("5´-GCCGTMTTTATGTTAATGC-3´", forwardPrimer.getSequence().getString());
276
        assertNotNull(forwardPrimer.getPublishedIn());
277
        assertEquals("Löhne & Borsch 2005", forwardPrimer.getPublishedIn().getTitle());
278

    
279
        Primer reversePrimer = amplification.getReversePrimer();
280
        assertNotNull(reversePrimer);
281
        assertEquals("PIpetD738R", reversePrimer.getLabel());
282
        assertEquals("5´-AATTTAGCYCTTAATACAGG-3´", reversePrimer.getSequence().getString());
283

    
284
        //sequencing
285
        Set<Sequence> sequences = dnaSample.getSequences();
286
        assertNotNull(sequences);
287
        assertEquals(1, sequences.size());
288
        Sequence sequence = sequences.iterator().next();
289
        SequenceString consensusSequence = sequence.getConsensusSequence();
290
        assertNotNull(consensusSequence);
291
        assertEquals(
292
                "TTTCGGGTCC TTTATAGTGA AGATATAGCA TAGATAGTTG TAATCCATTA" +
293
        		" TGTATCATTG GGGAAGGAAG GAGAATATTT TTTTGATAGA ATACAAGTAT" +
294
        		" GGATTATTGA AACTAATACG CCATGTATTT GGATATTTCC CTTGAACTGC" +
295
        		" ATAATATTCT TTATTTTCCA TGAATAGTGT AAGGGAATTT TTCGAAGAGA" +
296
        		" AAATGGATTA TGGGAGTGTG TGACTTGAGC TATTGATTGG TCTGTGCAGA" +
297
        		" TACGGGCTTT TATCTATCTG CCACATTGTA ATTCACAAAC CAATGTGTCT" +
298
        		" TTGTTCCAAC CATCGCGTAA GCCCCATACA GAAGATAGGC TGGTTCGCTT" +
299
        		" GAAGAGAATC TTTTCTATGA TCAGATCCGA ATTATGTCGT ACATGAGCAG" +
300
        		" GCTCCGTAAG ATCTAGTTGA CTTAAGTCAA ACTTCAATAG TATAAAAATG" +
301
        		" CACTCATTTC CTCTGCATTG ACACGAGCTA TGAGACTATC GGAGTGAAAG" +
302
        		" AAAGGGTCTA AAGAAGAAGA AAGCTTGGGC TAGATTAGTA ACAAGTAAAT" +
303
        		" CCTTTGTGTG TGTGTTTGTA ATTAGTAAAT GGGCTCTCAA TATTTTGGGG" +
304
        		" CTAATTACTG ATCCTAAGGT TTGAGACGAC CCAGAAAGCA CTTGATCATA" +
305
        		" TCACGATTGA CTTTGTAAGC CTACTTGGGT ATTGAGTATT TACTTGTAAG" +
306
        		" AACCGAATTC TTTGGGGGAT AGTTGCAAAA AGAATCCAGT CAATTGTTCT" +
307
        		" TACGTAAAAC CATTCATATC TCGTATATGG ATATGTCTAG ATAGGCTATC" +
308
        		" GATTTTCGAT GGATTCGTTT GGTTCTTTTG ATTATTGCTC GAGCTGGATG" +
309
        		" ATGAAAAATT ATCATGTCCG GTTCCTTCG",consensusSequence.getString());
310
//        assertEquals((Integer)912, consensusSequence.getLength());
311
        assertNotNull(sequence.getContigFile());
312
        assertEquals(URI.create("http://ww2.biocase.org/websvn/filedetails.php?repname=campanula&path=%2FCAM385_Campa_drabifolia.pde"), MediaUtils.getFirstMediaRepresentationPart(sequence.getContigFile()).getUri());
313
        assertEquals(1, sequence.getCitations().size());
314
        Reference reference = sequence.getCitations().iterator().next();
315
        assertEquals("Gemeinholzer,B., Bachmann,K. (2005): Examining morphological "
316
                + "and molecular diagnostic character states in "
317
                + "Cichorium intybus L. (Asteraceae) and Cichorium spinosum L."
318
                + " Plant Systematics and Evolution 253 (1-3): 105-123.", reference.getTitle());
319

    
320
        //single reads
321
        Set<SingleRead> singleReads = sequence.getSingleReads();
322
        assertNotNull(singleReads);
323
        assertEquals(2, singleReads.size());
324
        for (SingleRead singleRead : singleReads) {
325
            if(singleRead.getDirection().equals(SequenceDirection.Forward)){
326
                assertNotNull(singleRead.getPherogram());
327
                assertEquals(URI.create("http://ww2.biocase.org/websvn/filedetails.php?repname=campanula&path=%2FCAM385_GM312-petD_F.ab1"), MediaUtils.getFirstMediaRepresentationPart(singleRead.getPherogram()).getUri());
328
            }
329
            else{
330
                assertNotNull(singleRead.getPherogram());
331
                assertEquals(URI.create("http://ww2.biocase.org/websvn/filedetails.php?repname=campanula&path=%2FCAM385_GM312-petD_R.ab1"), MediaUtils.getFirstMediaRepresentationPart(singleRead.getPherogram()).getUri());
332
            }
333
        }
334
        assertNotNull(amplificationResult.getSingleReads());
335
        assertEquals(amplificationResult.getSingleReads(), singleReads);
336

    
337

    
338
	}
339

    
340
	/**
341
	 * Tests import of DNA unit which is associated to a specimen being its parent derivative
342
	 * @throws ParseException
343
	 */
344
	@Test
345
	@DataSet( value="/eu/etaxonomy/cdm/database/BlankDataSet.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
346
	public void testImportAssociatedSpecimenSameIndividual() throws ParseException {
347
	    String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/db6_parent_child_association.xml";
348
	    URL url = this.getClass().getResource(inputFile);
349
	    assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
350

    
351
	    Abcd206ImportConfigurator importConfigurator = null;
352
	    try {
353
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
354
	    } catch (URISyntaxException e) {
355
	        e.printStackTrace();
356
	        Assert.fail();
357
	    }
358
	    assertNotNull("Configurator could not be created", importConfigurator);
359

    
360
	    boolean result = defaultImport.invoke(importConfigurator).isSuccess();
361
	    assertTrue("Return value for import.invoke should be true", result);
362
	    assertEquals("Number of derived units is incorrect", 2, occurrenceService.count(DerivedUnit.class));
363
	    assertEquals("Number of dna samples is incorrect", 1, occurrenceService.count(DnaSample.class));
364
	    assertEquals("Number of field units is incorrect", 1, occurrenceService.count(FieldUnit.class));
365

    
366
	    //associated specimen
367
	    FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
368
	    config.setSignificantIdentifier("B 10 0066577");
369
	    List<SpecimenOrObservationBase> records = occurrenceService.findByTitle(config).getRecords();
370
	    assertEquals(1, records.size());
371
	    SpecimenOrObservationBase derivedUnitSpecimen = records.iterator().next();
372
	    assertEquals(DerivedUnit.class, derivedUnitSpecimen.getClass());
373
	    DerivedUnit specimen = (DerivedUnit) derivedUnitSpecimen;
374
	    assertEquals("Herbarium Berolinense", specimen.getCollection().getCode());
375
	    assertTrue(SpecimenOrObservationType.DnaSample!=specimen.getRecordBasis());
376

    
377
	    //dna sample
378
	    FindOccurrencesConfigurator dnaConfig = new FindOccurrencesConfigurator();
379
	    dnaConfig.setSignificantIdentifier("DB 6");
380
	    List<SpecimenOrObservationBase> dnaRecords = occurrenceService.findByTitle(dnaConfig).getRecords();
381
	    assertEquals(1, dnaRecords.size());
382
	    SpecimenOrObservationBase dnaSpecimen = dnaRecords.iterator().next();
383
	    assertEquals(DnaSample.class, dnaSpecimen.getClass());
384
	    DnaSample dnaSample = (DnaSample) dnaSpecimen;
385
	    DerivationEvent derivedFrom = dnaSample.getDerivedFrom();
386
	    assertNotNull(derivedFrom);
387
	    assertEquals("Wrong derivation type!", DerivationEventType.DNA_EXTRACTION(), derivedFrom.getType());
388
	    assertEquals("Wrong number of originals", 1, derivedFrom.getOriginals().size());
389
	    assertTrue(derivedFrom.getOriginals().iterator().next() instanceof DerivedUnit);
390
	    assertEquals("DNA Bank", dnaSample.getCollection().getCode());
391
	    assertEquals(SpecimenOrObservationType.DnaSample, dnaSample.getRecordBasis());
392
	}
393

    
394
	/**
395
     * Tests import of DNA unit which is associated to a specimen being its sibling
396
     * by having the same field unit
397
	 * @throws ParseException
398
	 */
399
	@Test
400
	@DataSet( value="/eu/etaxonomy/cdm/database/BlankDataSet.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
401
@Ignore
402
	public void testImportAssociatedSpecimenSamePopulation() throws ParseException {
403
	    String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/db6_sibling_association.xml";
404
	    URL url = this.getClass().getResource(inputFile);
405
	    assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
406

    
407
	    Abcd206ImportConfigurator importConfigurator = null;
408
	    try {
409
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
410
	    } catch (URISyntaxException e) {
411
	        e.printStackTrace();
412
	        Assert.fail();
413
	    }
414
	    assertNotNull("Configurator could not be created", importConfigurator);
415

    
416
	    boolean result = defaultImport.invoke(importConfigurator).isSuccess();
417
	    assertTrue("Return value for import.invoke should be true", result);
418
	    assertEquals("Number of derived units is incorrect", 2, occurrenceService.count(DerivedUnit.class));
419
	    assertEquals("Number of dna samples is incorrect", 1, occurrenceService.count(DnaSample.class));
420
	    assertEquals("Number of field units is incorrect", 1, occurrenceService.count(FieldUnit.class));
421

    
422
	    //associated specimen
423
	    FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
424
	    config.setSignificantIdentifier("B 10 0066577");
425
	    List<SpecimenOrObservationBase> records = occurrenceService.findByTitle(config).getRecords();
426
	    assertEquals(1, records.size());
427
	    SpecimenOrObservationBase derivedUnitSpecimen = records.iterator().next();
428
	    assertEquals(DerivedUnit.class, derivedUnitSpecimen.getClass());
429
	    DerivedUnit specimen = (DerivedUnit) derivedUnitSpecimen;
430
	    assertEquals("Herbarium Berolinense", specimen.getCollection().getCode());
431
	    assertTrue(SpecimenOrObservationType.DnaSample!=specimen.getRecordBasis());
432

    
433
	    //dna sample
434
	    FindOccurrencesConfigurator dnaConfig = new FindOccurrencesConfigurator();
435
	    dnaConfig.setSignificantIdentifier("DB 6");
436
	    List<SpecimenOrObservationBase> dnaRecords = occurrenceService.findByTitle(dnaConfig).getRecords();
437
	    assertEquals(1, dnaRecords.size());
438
	    SpecimenOrObservationBase dnaSpecimen = dnaRecords.iterator().next();
439
	    assertEquals(DnaSample.class, dnaSpecimen.getClass());
440
	    DnaSample dnaSample = (DnaSample) dnaSpecimen;
441
	    DerivationEvent derivedFrom = dnaSample.getDerivedFrom();
442
	    assertNotNull(derivedFrom);
443
	    assertEquals("Wrong derivation type!", DerivationEventType.DNA_EXTRACTION(), derivedFrom.getType());
444
	    assertEquals("Wrong number of originals", 1, derivedFrom.getOriginals().size());
445
	    //TODO
446
//	    assertTrue(derivedFrom.getOriginals().iterator().next() instanceof FieldUnit);
447
	    assertEquals("DNA Bank", dnaSample.getCollection().getCode());
448
	    assertEquals(SpecimenOrObservationType.DnaSample, dnaSample.getRecordBasis());
449

    
450
	    //TODO field unit
451
	}
452

    
453
    /**
454
     * Tests import of DNA unit and attaching it to an existing specimen to
455
     * which it has a parent-child UnitAssociation. The derived unit should not
456
     * be imported because it already exists in the data base. The field unit
457
     * should not be overwritten by the FieldUnit of the DnaSample.
458
     */
459
	@Test
460
    @DataSets({
461
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/BlankDataSet.xml"),
462
        @DataSet( value="AbcdGgbnImportTest.testAttachDnaSampleToDerivedUnit.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
463
    })
464

    
465
	public void testAttachDnaSampleToExistingDerivedUnit_parentChild(){
466
	    UUID fieldUnit1Uuid = UUID.fromString("0f896630-48d6-4352-9c91-278be28ce19c");
467
	    UUID derivedUnit1Uuid = UUID.fromString("eb40cb0f-efb2-4985-819e-a9168f6d61fe");
468

    
469
	    String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/db6_parent_child_association.xml";
470
	    URL url = this.getClass().getResource(inputFile);
471
	    assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
472

    
473
	    Abcd206ImportConfigurator importConfigurator = null;
474
	    try {
475
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
476
	    } catch (URISyntaxException e) {
477
	        e.printStackTrace();
478
	        Assert.fail();
479
	    }
480
	    assertNotNull("Configurator could not be created", importConfigurator);
481

    
482
	    boolean result = defaultImport.invoke(importConfigurator).isSuccess();
483
	    assertTrue("Return value for import.invoke should be true", result);occurrenceService.list(DerivedUnit.class, null, null, null, null);
484
	    assertEquals("Number of derived units is incorrect", 2, occurrenceService.count(DerivedUnit.class));
485
	    List<DerivedUnit> derivedUnits = occurrenceService.list(DerivedUnit.class, null, null, null, null);
486
	    assertEquals("Number of derived units is incorrect", 2, derivedUnits.size());
487
	    assertEquals("Number of dna samples is incorrect", 1, occurrenceService.count(DnaSample.class));
488
	    assertEquals("Number of field units is incorrect", 1, occurrenceService.count(FieldUnit.class));
489

    
490

    
491
	    DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(derivedUnit1Uuid);
492
	    assertTrue(derivedUnits.contains(derivedUnit));
493

    
494
	    assertEquals("Number of dna samples is incorrect", 1, occurrenceService.count(DnaSample.class));
495
	    DnaSample dnaSample = occurrenceService.list(DnaSample.class, null, null, null, null).get(0);
496
	    assertEquals("Wrong derivation type!", DerivationEventType.DNA_EXTRACTION(), dnaSample.getDerivedFrom().getType());
497

    
498
	    assertEquals("Wrong number of originals", 1, dnaSample.getDerivedFrom().getOriginals().size());
499

    
500
	    FieldUnit specimenFieldUnit = (FieldUnit) occurrenceService.load(fieldUnit1Uuid);
501
	    Collection<FieldUnit> fieldUnits = occurrenceService.findFieldUnits(dnaSample.getUuid(), null);
502
	    assertEquals(1, fieldUnits.size());
503
	    FieldUnit dnaSampleFieldUnit = fieldUnits.iterator().next();
504
        assertEquals(specimenFieldUnit, dnaSampleFieldUnit);
505
        assertEquals("fieldUnit1", dnaSampleFieldUnit.getTitleCache());
506

    
507
	}
508

    
509
	/**
510
	 * Tests import of DNA unit and attaching it to an existing specimen to which
511
	 * it has a sibling UnitAssociation. The derived unit should not be imported because it already exists in the data base.
512
	 * The DnaSample should be attached to the existing FieldUnit of the DerivedUnit
513
	 */
514
	@Test
515
	@DataSets({
516
	    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/BlankDataSet.xml"),
517
	    @DataSet( value="AbcdGgbnImportTest.testAttachDnaSampleToDerivedUnit.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
518
	})
519

    
520
	public void testAttachDnaSampleToExistingDerivedUnit_sibling(){
521
        UUID fieldUnit1Uuid = UUID.fromString("0f896630-48d6-4352-9c91-278be28ce19c");
522
	    UUID derivedUnit1Uuid = UUID.fromString("eb40cb0f-efb2-4985-819e-a9168f6d61fe");
523

    
524
	    String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/db6_parent_child_association.xml";
525
	    URL url = this.getClass().getResource(inputFile);
526
	    assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
527

    
528
	    Abcd206ImportConfigurator importConfigurator = null;
529
	    try {
530
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
531
	    } catch (URISyntaxException e) {
532
	        e.printStackTrace();
533
	        Assert.fail();
534
	    }
535
	    assertNotNull("Configurator could not be created", importConfigurator);
536

    
537
	    boolean result = defaultImport.invoke(importConfigurator).isSuccess();
538
	    assertTrue("Return value for import.invoke should be true", result);
539
	    List<DerivedUnit> derivedUnits = occurrenceService.list(DerivedUnit.class, null, null, null, null);
540
	    for (DerivedUnit derivedUnit:derivedUnits){
541
	        System.out.println(derivedUnit.getTitleCache());
542
	    }
543
	    assertEquals("Number of derived units is incorrect", 2, occurrenceService.count(DerivedUnit.class));
544
	   // List<DerivedUnit> derivedUnits = occurrenceService.list(DerivedUnit.class, null, null, null, null);
545

    
546
	    List<FieldUnit> fieldUnitsTemp = occurrenceService.list(FieldUnit.class, null, null, null, null);
547
	    assertEquals("Number of derived units is incorrect", 2, derivedUnits.size());
548

    
549
        assertEquals("Number of field units is incorrect", 1, occurrenceService.count(FieldUnit.class));
550
	    assertEquals("Number of dna samples is incorrect", 1, occurrenceService.count(DnaSample.class));
551

    
552
	    DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(derivedUnit1Uuid);
553
	    assertTrue(derivedUnits.contains(derivedUnit));
554

    
555
	    assertEquals("Number of dna samples is incorrect", 1, occurrenceService.count(DnaSample.class));
556
	    DnaSample dnaSample = occurrenceService.list(DnaSample.class, null, null, null, null).get(0);
557
	    assertEquals("Wrong derivation type!", DerivationEventType.DNA_EXTRACTION(), dnaSample.getDerivedFrom().getType());
558

    
559
	    assertEquals("Wrong number of originals", 1, dnaSample.getDerivedFrom().getOriginals().size());
560
        FieldUnit specimenFieldUnit = (FieldUnit) occurrenceService.load(fieldUnit1Uuid);
561
        Collection<FieldUnit> fieldUnits = occurrenceService.findFieldUnits(dnaSample.getUuid(), null);
562
        assertEquals(1, fieldUnits.size());
563
        FieldUnit dnaSampleFieldUnit = fieldUnits.iterator().next();
564
        assertEquals(specimenFieldUnit, dnaSampleFieldUnit);
565
        assertEquals("fieldUnit1", dnaSampleFieldUnit.getTitleCache());
566

    
567
	}
568

    
569
	@Test
570
	public void testAvoidDuplicateMolecularData(){
571

    
572
	}
573

    
574
	/**
575
     * Tests importing of DNA unit with an ABCD with only few fields filled.
576
     * Should just check that no NPEs occur when some fields are missing.
577
     */
578
    @Test
579
    @DataSets({
580
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
581
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
582
    })
583
    public void testImportGgbnSparseData(){
584
        String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/db6_sparse_data.xml";
585
        URL url = this.getClass().getResource(inputFile);
586
	    assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
587

    
588
	    Abcd206ImportConfigurator importConfigurator = null;
589
	    try {
590
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
591
	    } catch (URISyntaxException e) {
592
	        e.printStackTrace();
593
	        Assert.fail();
594
	    }
595
	    assertNotNull("Configurator could not be created", importConfigurator);
596

    
597
	    boolean result = defaultImport.invoke(importConfigurator).isSuccess();
598
	    assertTrue("Return value for import.invoke should be true", result);
599
	}
600

    
601
	/**
602
	 * Tests importing of DNA unit without attaching it to an existing specimen.
603
	 * Creates a FieldUnit with an attached DnaSample.
604
	 */
605
	@Test
606
    @DataSets({
607
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/BlankDataSet.xml"),
608
        @DataSet( value="AbcdGgbnImportTest.testNoAttachDnaSampleToDerivedUnit.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
609
    })
610
	public void testNoAttachDnaSampleToDerivedUnit(){
611
	    UUID derivedUnit1Uuid = UUID.fromString("eb40cb0f-efb2-4985-819e-a9168f6d61fe");
612
	    UUID fieldUnit1Uuid = UUID.fromString("b5f58da5-4442-4001-9d13-33f41518b72a");
613

    
614
//        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
615
//        derivedUnit.setAccessionNumber("B 10 0066577");
616
//        derivedUnit.setTitleCache("testUnit1", true);
617
//
618
//        derivedUnit.setUuid(derivedUnit1Uuid );
619
//
620
//        occurrenceService.save(derivedUnit);
621
//
622
//        commitAndStartNewTransaction(null);
623
//
624
//        setComplete();
625
//        endTransaction();
626
//
627
//
628
//        try {
629
//            writeDbUnitDataSetFile(new String[] {
630
//                    "SpecimenOrObservationBase",
631
//            }, "testAttachDnaSampleToDerivedUnit");
632
//        } catch (FileNotFoundException e) {
633
//            e.printStackTrace();
634
//        }
635

    
636

    
637
	    String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/db6_without_association.xml";
638
	    URL url = this.getClass().getResource(inputFile);
639
	    assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
640

    
641
	    Abcd206ImportConfigurator importConfigurator = null;
642
	    try {
643
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(url.toURI(), null,false);
644
	    } catch (URISyntaxException e) {
645
	        e.printStackTrace();
646
	        Assert.fail();
647
	    }
648
	    assertNotNull("Configurator could not be created", importConfigurator);
649

    
650
	    assertEquals("Number of derived units is incorrect", 1, occurrenceService.count(DerivedUnit.class));
651
	    boolean result = defaultImport.invoke(importConfigurator).isSuccess();
652
	    assertTrue("Return value for import.invoke should be true", result);
653
	    assertEquals("Number of derived units is incorrect", 2, occurrenceService.count(DerivedUnit.class));
654
	    List<DerivedUnit> derivedUnits = occurrenceService.list(DerivedUnit.class, null, null, null, null);
655
	    assertEquals("Number of derived units is incorrect", 2, derivedUnits.size());
656
	    assertEquals("Number of field units is incorrect", 2, occurrenceService.count(FieldUnit.class));
657
	    assertEquals("Number of dna samples is incorrect", 1, occurrenceService.count(DnaSample.class));
658

    
659
	    DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(derivedUnit1Uuid);
660
	    assertTrue(derivedUnits.contains(derivedUnit));
661

    
662
	    DnaSample dnaSample = occurrenceService.list(DnaSample.class, null, null, null, null).get(0);
663
	    assertEquals("Wrong derivation type!", DerivationEventType.DNA_EXTRACTION(), dnaSample.getDerivedFrom().getType());
664

    
665
	    assertEquals("Wrong number of originals", 1, dnaSample.getDerivedFrom().getOriginals().size());
666
	    FieldUnit specimenFieldUnit = (FieldUnit) occurrenceService.load(fieldUnit1Uuid);
667
	    SpecimenOrObservationBase<?> dnaSampleFieldUnit = dnaSample.getDerivedFrom().getOriginals().iterator().next();
668
	    assertTrue(!specimenFieldUnit.equals(dnaSampleFieldUnit));
669

    
670
	}
671

    
672
    @Override
673
    public void createTestDataSet() throws FileNotFoundException {
674
        UUID derivedUnit1Uuid = UUID.fromString("eb40cb0f-efb2-4985-819e-a9168f6d61fe");
675

    
676
        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
677
        derivedUnit.setAccessionNumber("B 10 0066577");
678
        derivedUnit.setTitleCache("testUnit1", true);
679

    
680
        derivedUnit.setUuid(derivedUnit1Uuid );
681

    
682
        occurrenceService.save(derivedUnit);
683

    
684
        commitAndStartNewTransaction(null);
685

    
686
        setComplete();
687
        endTransaction();
688

    
689

    
690
        try {
691
            writeDbUnitDataSetFile(new String[] {
692
                    "SpecimenOrObservationBase",
693
            }, "testAttachDnaSampleToDerivedUnit", true);
694
        } catch (FileNotFoundException e) {
695
            e.printStackTrace();
696
        }
697
    }
698

    
699
}
(1-1/2)