Project

General

Profile

Download (34.2 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.time.ZoneId;
22
import java.time.ZonedDateTime;
23
import java.util.Collection;
24
import java.util.List;
25
import java.util.Set;
26
import java.util.UUID;
27

    
28
import org.junit.Assert;
29
import org.junit.Ignore;
30
import org.junit.Test;
31
import org.unitils.dbunit.annotation.DataSet;
32
import org.unitils.dbunit.annotation.DataSets;
33
import org.unitils.spring.annotation.SpringBeanByName;
34
import org.unitils.spring.annotation.SpringBeanByType;
35

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

    
68
/**
69
 * @author a.mueller
70
 * @created 29.01.2009
71
 */
72

    
73
public class AbcdGgbnImportTest extends CdmTransactionalIntegrationTest {
74

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

    
78
	@SpringBeanByType
79
	private IOccurrenceService occurrenceService;
80

    
81
	@SpringBeanByType
82
	private IReferenceService referenceService;
83

    
84
	@SpringBeanByType
85
	private ITermService termService;
86

    
87
	@SpringBeanByType
88
	private ITaxonService taxonService;
89

    
90
	@SpringBeanByType
91
	private ITaxonNodeService taxonNodeService;
92

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

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

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

    
137
        //test for sortindex=-1
138

    
139

    
140
	}
141

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

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

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

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

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

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

    
193
	}
194

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

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

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

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

    
252

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

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

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

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

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

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

    
338

    
339
	}
340

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

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

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

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

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

    
395
	/**
396
     * Tests import of DNA unit which is associated to a specimen being its sibling
397
     * by having the same field unit
398
	 * @throws ParseException
399
	 */
400
	@Test
401
	@DataSet( value="/eu/etaxonomy/cdm/database/BlankDataSet.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
402

    
403
	public void testImportAssociatedSpecimenSamePopulation() throws ParseException {
404
	    String inputFile = "/eu/etaxonomy/cdm/io/specimen/abcd206/in/db6_sibling_association.xml";
405
	    URL url = this.getClass().getResource(inputFile);
406
	    assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
407

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

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

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

    
434
	    //dna sample
435
	    FindOccurrencesConfigurator dnaConfig = new FindOccurrencesConfigurator();
436
	    dnaConfig.setSignificantIdentifier("DB 6");
437
	    List<SpecimenOrObservationBase> dnaRecords = occurrenceService.findByTitle(dnaConfig).getRecords();
438
	    assertEquals(1, dnaRecords.size());
439
	    SpecimenOrObservationBase dnaSpecimen = dnaRecords.iterator().next();
440
	    assertEquals(DnaSample.class, dnaSpecimen.getClass());
441
	    DnaSample dnaSample = (DnaSample) dnaSpecimen;
442
	    DerivationEvent derivedFrom = dnaSample.getDerivedFrom();
443
	    assertNotNull(derivedFrom);
444
	    assertEquals("Wrong derivation type!", DerivationEventType.DNA_EXTRACTION(), derivedFrom.getType());
445
	    assertEquals("Wrong number of originals", 1, derivedFrom.getOriginals().size());
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.getFieldUnits(dnaSample.getUuid());
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.getFieldUnits(dnaSample.getUuid());
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");
694
        } catch (FileNotFoundException e) {
695
            e.printStackTrace();
696
        }
697
    }
698

    
699
}
(1-1/2)