2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.cdmLight
.out
;
11 import java
.io
.BufferedReader
;
12 import java
.io
.ByteArrayInputStream
;
14 import java
.io
.FileNotFoundException
;
15 import java
.io
.IOException
;
16 import java
.io
.InputStreamReader
;
17 import java
.nio
.charset
.Charset
;
18 import java
.util
.HashSet
;
21 import java
.util
.UUID
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.junit
.Assert
;
25 import org
.junit
.Before
;
26 import org
.junit
.Test
;
27 import org
.unitils
.dbunit
.annotation
.DataSet
;
28 import org
.unitils
.spring
.annotation
.SpringBeanByName
;
29 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
34 import eu
.etaxonomy
.cdm
.filter
.TaxonNodeFilter
;
35 import eu
.etaxonomy
.cdm
.io
.cdmLight
.CdmLightExportConfigurator
;
36 import eu
.etaxonomy
.cdm
.io
.cdmLight
.CdmLightExportTable
;
37 import eu
.etaxonomy
.cdm
.io
.common
.CdmApplicationAwareDefaultExport
;
38 import eu
.etaxonomy
.cdm
.io
.common
.ExportDataWrapper
;
39 import eu
.etaxonomy
.cdm
.io
.common
.ExportResult
;
40 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
.TARGET
;
41 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
42 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
43 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
44 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
45 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
46 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
47 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
48 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
49 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
50 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
53 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
54 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
55 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
56 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
57 import eu
.etaxonomy
.cdm
.test
.unitils
.CleanSweepInsertLoadStrategy
;
64 public class CdmLightExportTest
extends CdmTransactionalIntegrationTest
{
66 @SuppressWarnings("unused")
67 private static final Logger logger
= Logger
.getLogger(CdmLightExportTest
.class);
70 private CdmApplicationAwareDefaultExport
<CdmLightExportConfigurator
> defaultExport
;
73 private IClassificationService classificationService
;
76 private ITermService termService
;
79 private ITaxonNodeService taxonNodeService
;
83 // DefinedTerm ipniIdentifierTerm = DefinedTerm.NewIdentifierTypeInstance("IPNI Identifier", "IPNI Identifier", "IPNI Identifier");
84 // ipniIdentifierTerm.setUuid(DefinedTerm.uuidIpniNameIdentifier);
86 // DefinedTerm tropicosIdentifierTerm = DefinedTerm.NewIdentifierTypeInstance("Tropicos Identifier", "Tropicos Identifier", "Tropicos Identifier");
87 // tropicosIdentifierTerm.setUuid(DefinedTerm.uuidTropicosNameIdentifier);
89 // DefinedTerm wfoIdentifierTerm = DefinedTerm.NewIdentifierTypeInstance("WFO Identifier", "WFO Identifier", "WFO Identifier");
90 // wfoIdentifierTerm.setUuid(DefinedTerm.uuidWfoNameIdentifier);
91 // List<DefinedTermBase> terms = new ArrayList();
92 // terms.add(wfoIdentifierTerm);
93 // terms.add(tropicosIdentifierTerm);
94 // terms.add(ipniIdentifierTerm);
95 // termService.saveOrUpdate(terms);
96 createFullTestDataSet();
103 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class, value
="/eu/etaxonomy/cdm/database/BlankDataSet.xml")
104 public void testSubTree(){
107 CdmLightExportConfigurator config
= new CdmLightExportConfigurator(null);
108 config
.setTaxonNodeFilter(TaxonNodeFilter
.NewSubtreeInstance(UUID
.fromString("f8c9933a-fe3a-42ce-8a92-000e27bfdfac")));
110 config
.setTarget(TARGET
.EXPORT_DATA
);
111 ExportResult result
= defaultExport
.invoke(config
);
112 ExportDataWrapper
<?
> exportData
= result
.getExportData();
113 @SuppressWarnings("unchecked")
114 Map
<String
, byte[]> data
= (Map
<String
, byte[]>) exportData
.getExportData();
117 byte[] taxon
= data
.get(CdmLightExportTable
.TAXON
.getTableName());
118 Assert
.assertNotNull("Taxon table must not be null", taxon
);
119 String taxonStr
= new String(taxon
);
120 String notExpected
= "\"9182e136-f2e2-4f9a-9010-3f35908fb5e0\"";
121 Assert
.assertFalse("Result must not contain root taxon", taxonStr
.startsWith(notExpected
));
122 String expected
= "\"b2c86698-500e-4efb-b9ae-6bb6e701d4bc\",\"4096df99-7274-421e-8843-211b603d832e\",\"CdmLightExportTest Classification\",\"3483cc5e-4c77-4c80-8cb0-73d43df31ee3\",\"9182e136-f2e2-4f9a-9010-3f35908fb5e0\",\"4b6acca1-959b-4790-b76e-e474a0882990\",\"My sec ref\"";
123 Assert
.assertTrue(taxonStr
.contains(expected
));
125 byte[] reference
= data
.get(CdmLightExportTable
.REFERENCE
.getTableName());
126 String referenceString
= new String(reference
);
127 Assert
.assertNotNull("Reference table must not be null", reference
);
128 expected
="\"b8dd7f4a-0c7f-4372-bc5d-3b676363bc0f\",\"null (1804)\",\"\",\"The book of botany\",\"1804\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"3\",\"1804\",\"Mill.\"";
129 Assert
.assertTrue(referenceString
.contains(expected
));
131 byte[] geographicAreaFact
= data
.get(CdmLightExportTable
.GEOGRAPHIC_AREA_FACT
.getTableName());
132 String geographicAreaFactString
= new String(geographicAreaFact
);
133 Assert
.assertNotNull("Geographical fact table must not be null", geographicAreaFact
);
134 expected
="\"674e9e27-9102-4166-8626-8cb871a9a89b\",\"b2c86698-500e-4efb-b9ae-6bb6e701d4bc\",\"Africa\",\"present\"";
135 Assert
.assertTrue(geographicAreaFactString
.contains(expected
));
137 byte[] nomenclaturalAuthor
= data
.get(CdmLightExportTable
.NOMENCLATURAL_AUTHOR
.getTableName());
138 String nomenclaturalAuthorString
= new String(nomenclaturalAuthor
);
139 Assert
.assertNotNull("Nomenclatural Author table must not be null", nomenclaturalAuthor
);
140 expected
="\"Mill.\",\"Mill.\",\"\",\"\",\"\",\"\"";
141 Assert
.assertTrue(nomenclaturalAuthorString
.contains(expected
));
143 byte[] scientificName
= data
.get(CdmLightExportTable
.SCIENTIFIC_NAME
.getTableName());
144 String scientificNameString
= new String(scientificName
);
145 Assert
.assertNotNull("Scientific Name table must not be null", scientificName
);
146 expected
="\"3483cc5e-4c77-4c80-8cb0-73d43df31ee3\",\"\",\"Subspecies\",\"43\",\"Genus species subsp. subspec Mill.\",\"Genus species subsp. subspec\",\"Genus\",\"\",\"\",\"species\",\"subsp.\",\"subspec\",\"\",\"\",\"\",";
147 Assert
.assertTrue(scientificNameString
.contains(expected
));
148 expected
="\"Book\",\"The book of botany\",\"Mill., The book of botany 3. 1804\",\"Mill.\",\"Mill.\",\"3:22\",\"3\",\"22\",\"1804\",\"1804\",\"\",\"\",\"\",\"\"";
149 Assert
.assertTrue(scientificNameString
.contains(expected
));
151 byte[] homotypicGroup
= data
.get(CdmLightExportTable
.HOMOTYPIC_GROUP
.getTableName());
152 String homotypicGroupString
= new String(homotypicGroup
);
153 Assert
.assertNotNull("Reference table must not be null", homotypicGroup
);
154 expected
="\"Genus species subsp. subspec Mill.\",\"\"";
155 Assert
.assertTrue(homotypicGroupString
.contains(expected
));
160 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class, value
="/eu/etaxonomy/cdm/database/BlankDataSet.xml")
161 public void testFullTreeWithUnpublished(){
164 CdmLightExportConfigurator config
= new CdmLightExportConfigurator(null);
165 config
.setTarget(TARGET
.EXPORT_DATA
);
166 config
.getTaxonNodeFilter().setIncludeUnpublished(true);
167 ExportResult result
= defaultExport
.invoke(config
);
168 ExportDataWrapper
<?
> exportData
= result
.getExportData();
169 @SuppressWarnings("unchecked")
170 Map
<String
, byte[]> data
= (Map
<String
, byte[]>) exportData
.getExportData();
172 ByteArrayInputStream stream
= new ByteArrayInputStream( data
.get(CdmLightExportTable
.TAXON
.getTableName()));
173 BufferedReader reader
= new BufferedReader(new InputStreamReader(stream
, Charset
.forName("UTF-8")));
177 while ((line
= reader
.readLine()) != null) {
180 Assert
.assertTrue("There should be 5 taxa", count
== 6);// 6 because of the header line
182 stream
= new ByteArrayInputStream(data
.get(CdmLightExportTable
.REFERENCE
.getTableName()));
183 reader
= new BufferedReader(new InputStreamReader(stream
, Charset
.forName("UTF-8")));
185 while ((line
= reader
.readLine()) != null) {
188 Assert
.assertTrue("There should be 6 references", count
== 7);
189 stream
= new ByteArrayInputStream(data
.get(CdmLightExportTable
.SYNONYM
.getTableName()));
190 reader
= new BufferedReader(new InputStreamReader(stream
, Charset
.forName("UTF-8")));
192 while ((line
= reader
.readLine()) != null) {
195 Assert
.assertTrue("There should be 1 synonym", count
== 2);
196 } catch (IOException e
) {
197 // TODO Auto-generated catch block
200 byte[] taxon
= data
.get(CdmLightExportTable
.TAXON
.getTableName());
201 Assert
.assertNotNull("Taxon table must not be null", taxon
);
202 String taxonStr
= new String(taxon
);
203 String notExpected
= "\"9182e136-f2e2-4f9a-9010-3f35908fb5e0\"";
204 Assert
.assertFalse("Result must not contain root taxon", taxonStr
.startsWith(notExpected
));
205 String expected
= "\"b2c86698-500e-4efb-b9ae-6bb6e701d4bc\",\"4096df99-7274-421e-8843-211b603d832e\",\"CdmLightExportTest Classification\",\"3483cc5e-4c77-4c80-8cb0-73d43df31ee3\",\"9182e136-f2e2-4f9a-9010-3f35908fb5e0\",\"4b6acca1-959b-4790-b76e-e474a0882990\",\"My sec ref\"";
206 Assert
.assertTrue(taxonStr
.contains(expected
));
208 byte[] reference
= data
.get(CdmLightExportTable
.REFERENCE
.getTableName());
209 String referenceString
= new String(reference
);
210 Assert
.assertNotNull("Reference table must not be null", reference
);
211 expected
="\"b8dd7f4a-0c7f-4372-bc5d-3b676363bc0f\",\"null (1804)\",\"\",\"The book of botany\",\"1804\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"3\",\"1804\",\"Mill.\"";
212 Assert
.assertTrue(referenceString
.contains(expected
));
214 byte[] geographicAreaFact
= data
.get(CdmLightExportTable
.GEOGRAPHIC_AREA_FACT
.getTableName());
215 String geographicAreaFactString
= new String(geographicAreaFact
);
216 Assert
.assertNotNull("Geographical fact table must not be null", geographicAreaFact
);
217 expected
="\"674e9e27-9102-4166-8626-8cb871a9a89b\",\"b2c86698-500e-4efb-b9ae-6bb6e701d4bc\",\"Africa\",\"present\"";
218 Assert
.assertTrue(geographicAreaFactString
.contains(expected
));
220 byte[] nomenclaturalAuthor
= data
.get(CdmLightExportTable
.NOMENCLATURAL_AUTHOR
.getTableName());
221 String nomenclaturalAuthorString
= new String(nomenclaturalAuthor
);
222 Assert
.assertNotNull("Nomenclatural Author table must not be null", nomenclaturalAuthor
);
223 expected
="\"Mill.\",\"Mill.\",\"\",\"\",\"\",\"\"";
224 Assert
.assertTrue(nomenclaturalAuthorString
.contains(expected
));
226 byte[] scientificName
= data
.get(CdmLightExportTable
.SCIENTIFIC_NAME
.getTableName());
227 String scientificNameString
= new String(scientificName
);
228 Assert
.assertNotNull("Scientific Name table must not be null", scientificName
);
229 expected
="\"3483cc5e-4c77-4c80-8cb0-73d43df31ee3\",\"\",\"Subspecies\",\"43\",\"Genus species subsp. subspec Mill.\",\"Genus species subsp. subspec\",\"Genus\",\"\",\"\",\"species\",\"subsp.\",\"subspec\",\"\",\"\",\"\",";
230 Assert
.assertTrue(scientificNameString
.contains(expected
));
231 expected
="\"Book\",\"The book of botany\",\"Mill., The book of botany 3. 1804\",\"Mill.\",\"Mill.\",\"3:22\",\"3\",\"22\",\"1804\",\"1804\",\"\",\"\",\"\",\"\"";
232 Assert
.assertTrue(scientificNameString
.contains(expected
));
234 byte[] homotypicGroup
= data
.get(CdmLightExportTable
.HOMOTYPIC_GROUP
.getTableName());
235 String homotypicGroupString
= new String(homotypicGroup
);
236 Assert
.assertNotNull("Reference table must not be null", homotypicGroup
);
237 expected
="\"Genus species subsp. subspec Mill.\",\"\"";
238 Assert
.assertTrue(homotypicGroupString
.contains(expected
));
243 @DataSet(loadStrategy
=CleanSweepInsertLoadStrategy
.class, value
="/eu/etaxonomy/cdm/database/BlankDataSet.xml")
244 public void testFullData(){
246 File destinationFolder
= null;
247 CdmLightExportConfigurator config
= new CdmLightExportConfigurator(null);
248 config
.setTarget(TARGET
.EXPORT_DATA
);
250 ExportResult result
= defaultExport
.invoke(config
);
251 ExportDataWrapper
<?
> exportData
= result
.getExportData();
252 @SuppressWarnings("unchecked")
253 Map
<String
, byte[]> data
= (Map
<String
, byte[]>) exportData
.getExportData();
255 ByteArrayInputStream stream
= new ByteArrayInputStream( data
.get(CdmLightExportTable
.TAXON
.getTableName()));
256 BufferedReader reader
= new BufferedReader(new InputStreamReader(stream
, Charset
.forName("UTF-8")));
260 while ((line
= reader
.readLine()) != null) {
263 Assert
.assertTrue("There should be 4 taxa", count
== 5);// 5 because of the header line
265 stream
= new ByteArrayInputStream(data
.get(CdmLightExportTable
.REFERENCE
.getTableName()));
266 reader
= new BufferedReader(new InputStreamReader(stream
, Charset
.forName("UTF-8")));
268 while ((line
= reader
.readLine()) != null) {
271 Assert
.assertTrue("There should be 4 references", count
== 5);
272 stream
= new ByteArrayInputStream(data
.get(CdmLightExportTable
.SYNONYM
.getTableName()));
273 reader
= new BufferedReader(new InputStreamReader(stream
, Charset
.forName("UTF-8")));
275 while ((line
= reader
.readLine()) != null) {
278 Assert
.assertTrue("There should be 1 synonym", count
== 2);
279 } catch (IOException e
) {
280 // TODO Auto-generated catch block
290 public void createFullTestDataSet() {
291 Set
<TaxonNode
> nodesToSave
= new HashSet
<>();
293 Reference sec1
= ReferenceFactory
.newGeneric();
294 setUuid(sec1
, "4b6acca1-959b-4790-b76e-e474a0882990");
295 sec1
.setTitle("My sec ref");
297 Classification classification
= Classification
.NewInstance("CdmLightExportTest Classification");
298 setUuid(classification
, "4096df99-7274-421e-8843-211b603d832e");
300 NonViralNameParserImpl parser
= NonViralNameParserImpl
.NewInstance();
301 TaxonName familyName
= parser
.parseReferencedName("Family L., Sp. Pl. 3: 22. 1752",
302 NomenclaturalCode
.ICNAFP
, Rank
.FAMILY());
303 setUuid(familyName
,"e983cc5e-4c77-4c80-8cb0-73d43df31ef7");
304 setUuid((Reference
)familyName
.getNomenclaturalReference(), "b0dd7f4a-0c7f-4372-bc5d-3b676363bc63");
305 Taxon family
= Taxon
.NewInstance(familyName
, sec1
);
306 setUuid(family
,"3162e136-f2e2-4f9a-9010-3f35908fbae1");
307 TaxonNode node1
= classification
.addChildTaxon(family
, sec1
, "22");
308 setUuid(node1
, "0fae5ad5-ffa2-4100-bcd7-8aa9dda0aebc");
309 nodesToSave
.add(node1
);
311 TaxonName genusName
= parser
.parseReferencedName("Genus Humb., The book of botany 3: 22. 1804",
312 NomenclaturalCode
.ICNAFP
, Rank
.GENUS());
313 setUuid(genusName
,"5e83cc5e-4c77-4d80-8cb0-73d63df35ee3");
314 setUuid((Reference
)genusName
.getNomenclaturalReference(), "5ed27f4a-6c7f-4372-bc5d-3b67636abc52");
315 Taxon genus
= Taxon
.NewInstance(genusName
, sec1
);
316 setUuid(genus
,"3f52e136-f2e1-4f9a-9010-2f35908fbd39");
318 TaxonNode node2
= node1
.addChildTaxon(genus
, sec1
, "33");
319 setUuid(node2
, "43ca733b-fe3a-42ce-8a92-000e27badf44");
320 nodesToSave
.add(node2
);
323 TaxonName speciesName
= parser
.parseReferencedName("Genus species Mill., The book of botany 3: 22. 1804",
324 NomenclaturalCode
.ICNAFP
, Rank
.SPECIES());
325 setUuid(speciesName
,"f983cc5e-4c77-4c80-8cb0-73d43df31ee9");
326 setUuid((Reference
)speciesName
.getNomenclaturalReference(), "a0dd7f4a-0c7f-4372-bc5d-3b676363bc0e");
327 Taxon species
= Taxon
.NewInstance(speciesName
, sec1
);
328 setUuid(species
,"9182e136-f2e2-4f9a-9010-3f35908fb5e0");
329 TaxonName synonymName
= parser
.parseReferencedName("Genus synonym Mill., The book of botany 3: 22. 1804", NomenclaturalCode
.ICNAFP
, Rank
.SPECIES());
331 setUuid(synonymName
, "1584157b-5c43-4150-b271-95b2c99377b2");
332 Synonym synonymUnpublished
= Synonym
.NewInstance(synonymName
, sec1
);
333 setUuid(synonymName
, "a87c16b7-8299-4d56-a682-ce20973428ea");
334 synonymUnpublished
.setPublish(false);
335 species
.addHomotypicSynonym(synonymUnpublished
);
336 TaxonNode node3
= node2
.addChildTaxon(species
, sec1
, "33");
337 setUuid(node3
, "a0c9733a-fe3a-42ce-8a92-000e27bfdfa3");
338 nodesToSave
.add(node3
);
340 TaxonName subspeciesName
= parser
.parseReferencedName("Genus species subsp. subspec Mill., The book of botany 3: 22. 1804",
341 NomenclaturalCode
.ICNAFP
, Rank
.SUBSPECIES());
342 setUuid(subspeciesName
,"3483cc5e-4c77-4c80-8cb0-73d43df31ee3");
343 setUuid((Reference
)subspeciesName
.getNomenclaturalReference(), "b8dd7f4a-0c7f-4372-bc5d-3b676363bc0f");
345 Taxon subspecies
= Taxon
.NewInstance(subspeciesName
, sec1
);
346 setUuid(subspecies
, "b2c86698-500e-4efb-b9ae-6bb6e701d4bc");
347 TaxonNode node4
= node3
.addChildTaxon(subspecies
, sec1
, "33");
348 setUuid(node4
, "f8c9933a-fe3a-42ce-8a92-000e27bfdfac");
349 nodesToSave
.add(node4
);
351 TaxonName subspeciesNameUnpublished
= parser
.parseReferencedName("Genus species subsp. unpublished Mill., The book of botany 3: 22. 1804",
352 NomenclaturalCode
.ICNAFP
, Rank
.SUBSPECIES());
353 setUuid(subspeciesNameUnpublished
,"b6da7ab2-6c67-44b7-9719-2557542f5a23");
355 Taxon subspeciesUnpublished
= Taxon
.NewInstance(subspeciesNameUnpublished
, sec1
);
356 setUuid(subspeciesUnpublished
, "290e295a-9089-4616-a30c-15ded79e064f");
357 subspeciesUnpublished
.setPublish(false);
358 TaxonNode node5
= node3
.addChildTaxon(subspeciesUnpublished
, sec1
, "33");
359 setUuid(node5
, "81d9c9b2-c8fd-4d4f-a0b4-e7e656dcdc20");
360 nodesToSave
.add(node5
);
363 classificationService
.save(classification
);
364 taxonNodeService
.save(nodesToSave
);
366 TaxonDescription description
= TaxonDescription
.NewInstance(species
);
368 Distribution distribution
= Distribution
.NewInstance(NamedArea
.AFRICA(), PresenceAbsenceTerm
.PRESENT());
369 setUuid(distribution
,"674e9e27-9102-4166-8626-8cb871a9a89b");
370 description
.addElement(distribution
);
372 subspecies
.addDescription(description
);
373 commitAndStartNewTransaction(null);
379 public void createTestDataSet() throws FileNotFoundException
{
381 // writeDbUnitDataSetFile(new String[] {
383 // }, "testAttachDnaSampleToDerivedUnit");
384 // } catch (FileNotFoundException e) {
385 // e.printStackTrace();
390 private void setUuid(CdmBase cdmBase
, String uuidStr
) {
391 cdmBase
.setUuid(UUID
.fromString(uuidStr
));