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
package eu.etaxonomy.cdm.io.specimen.abcd206.in;
10

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

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

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

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

    
66
/**
67
 * @author a.mueller
68
 * @since 29.01.2009
69
 */
70
public class AbcdGgbnImportTest extends CdmTransactionalIntegrationTest {
71

    
72
	@SpringBeanByName
73
	private CdmApplicationAwareDefaultImport<?> defaultImport;
74

    
75
	@SpringBeanByType
76
	private IOccurrenceService occurrenceService;
77

    
78
	@SpringBeanByType
79
	private IReferenceService referenceService;
80

    
81
	@SpringBeanByType
82
	private ITermService termService;
83

    
84
	@SpringBeanByType
85
	private ITaxonService taxonService;
86

    
87
	@SpringBeanByType
88
	private ITaxonNodeService taxonNodeService;
89

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

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

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

    
134
        //test for sortindex=-1
135

    
136

    
137
	}
138

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

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

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

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

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

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

    
190
	}
191

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

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

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

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

    
249

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

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

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

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

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

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

    
335

    
336
	}
337

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

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

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

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

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

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

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

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

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

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

    
448
	    //TODO field unit
449
	}
450

    
451
    /**
452
     * Tests import of DNA unit and attaching it to an existing specimen to
453
     * which it has a parent-child UnitAssociation. The derived unit should not
454
     * be imported because it already exists in the data base. The field unit
455
     * should not be overwritten by the FieldUnit of the DnaSample.
456
     */
457
	@Test
458
    @DataSets({
459
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDBDataSet.xml"),
460
        @DataSet( value="AbcdGgbnImportTest.testAttachDnaSampleToDerivedUnit.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
461
    })
462
	public void testAttachDnaSampleToExistingDerivedUnit_parentChild(){
463
	    UUID fieldUnit1Uuid = UUID.fromString("0f896630-48d6-4352-9c91-278be28ce19c");
464
	    UUID derivedUnit1Uuid = UUID.fromString("eb40cb0f-efb2-4985-819e-a9168f6d61fe");
465

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

    
470
	    Abcd206ImportConfigurator importConfigurator = null;
471
	    try {
472
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(new URI(url), null,false);
473
	    } catch (URISyntaxException e) {
474
	        e.printStackTrace();
475
	        Assert.fail();
476
	    }
477
	    assertNotNull("Configurator could not be created", importConfigurator);
478

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

    
487

    
488
	    DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(derivedUnit1Uuid);
489
	    assertTrue(derivedUnits.contains(derivedUnit));
490

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

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

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

    
504
	}
505

    
506
	/**
507
	 * Tests import of DNA unit and attaching it to an existing specimen to which
508
	 * it has a sibling UnitAssociation. The derived unit should not be imported because it already exists in the data base.
509
	 * The DnaSample should be attached to the existing FieldUnit of the DerivedUnit
510
	 */
511
	@Test
512
	@DataSets({
513
	    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDBDataSet.xml"),
514
	    @DataSet( value="AbcdGgbnImportTest.testAttachDnaSampleToDerivedUnit.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
515
	})
516
	public void testAttachDnaSampleToExistingDerivedUnit_sibling(){
517
        UUID fieldUnit1Uuid = UUID.fromString("0f896630-48d6-4352-9c91-278be28ce19c");
518
	    UUID derivedUnit1Uuid = UUID.fromString("eb40cb0f-efb2-4985-819e-a9168f6d61fe");
519

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

    
524
	    Abcd206ImportConfigurator importConfigurator = null;
525
	    try {
526
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(new URI(url), null,false);
527
	    } catch (URISyntaxException e) {
528
	        e.printStackTrace();
529
	        Assert.fail();
530
	    }
531
	    assertNotNull("Configurator could not be created", importConfigurator);
532

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

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

    
545
        assertEquals("Number of field units is incorrect", 1, occurrenceService.count(FieldUnit.class));
546
	    assertEquals("Number of dna samples is incorrect", 1, occurrenceService.count(DnaSample.class));
547

    
548
	    DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(derivedUnit1Uuid);
549
	    assertTrue(derivedUnits.contains(derivedUnit));
550

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

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

    
563
	}
564

    
565
	@Test
566
	public void testAvoidDuplicateMolecularData(){
567

    
568
	}
569

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

    
584
	    Abcd206ImportConfigurator importConfigurator = null;
585
	    try {
586
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(new URI(url), null,false);
587
	    } catch (URISyntaxException e) {
588
	        e.printStackTrace();
589
	        Assert.fail();
590
	    }
591
	    assertNotNull("Configurator could not be created", importConfigurator);
592

    
593
	    boolean result = defaultImport.invoke(importConfigurator).isSuccess();
594
	    assertTrue("Return value for import.invoke should be true", result);
595
	}
596

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

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

    
632

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

    
637
	    Abcd206ImportConfigurator importConfigurator = null;
638
	    try {
639
	        importConfigurator = Abcd206ImportConfigurator.NewInstance(new URI(url), null,false);
640
	    } catch (URISyntaxException e) {
641
	        e.printStackTrace();
642
	        Assert.fail();
643
	    }
644
	    assertNotNull("Configurator could not be created", importConfigurator);
645

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

    
655
	    DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(derivedUnit1Uuid);
656
	    assertTrue(derivedUnits.contains(derivedUnit));
657

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

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

    
666
	}
667

    
668
    @Override
669
    public void createTestDataSet() throws FileNotFoundException {
670
        UUID derivedUnit1Uuid = UUID.fromString("eb40cb0f-efb2-4985-819e-a9168f6d61fe");
671

    
672
        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
673
        derivedUnit.setAccessionNumber("B 10 0066577");
674
        derivedUnit.setTitleCache("testUnit1", true);
675

    
676
        derivedUnit.setUuid(derivedUnit1Uuid );
677

    
678
        occurrenceService.save(derivedUnit);
679

    
680
        commitAndStartNewTransaction(null);
681

    
682
        setComplete();
683
        endTransaction();
684

    
685
        try {
686
            writeDbUnitDataSetFile(new String[] {
687
                    "SpecimenOrObservationBase",
688
            }, "testAttachDnaSampleToDerivedUnit", true);
689
        } catch (FileNotFoundException e) {
690
            e.printStackTrace();
691
        }
692
    }
693
}
(1-1/2)