596f015cb8becf17da4183ff1ac665cfb18a8b84
[cdmlib.git] / cdmlib-io / src / test / java / eu / etaxonomy / cdm / io / cdmLight / out / CdmLightExportTest.java
1 /**
2 * Copyright (C) 2018 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.cdmLight.out;
10
11 import java.io.BufferedReader;
12 import java.io.ByteArrayInputStream;
13 import java.io.File;
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;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.UUID;
22
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;
30
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;
58
59 /**
60 * @author k.luther
61 * @date 17.01.2018
62 *
63 */
64 public class CdmLightExportTest extends CdmTransactionalIntegrationTest{
65
66 @SuppressWarnings("unused")
67 private static final Logger logger = Logger.getLogger(CdmLightExportTest.class);
68
69 @SpringBeanByName
70 private CdmApplicationAwareDefaultExport<CdmLightExportConfigurator> defaultExport;
71
72 @SpringBeanByType
73 private IClassificationService classificationService;
74
75 @SpringBeanByType
76 private ITermService termService;
77
78 @SpringBeanByType
79 private ITaxonNodeService taxonNodeService;
80
81 @Before
82 public void setUp() {
83 // DefinedTerm ipniIdentifierTerm = DefinedTerm.NewIdentifierTypeInstance("IPNI Identifier", "IPNI Identifier", "IPNI Identifier");
84 // ipniIdentifierTerm.setUuid(DefinedTerm.uuidIpniNameIdentifier);
85 //
86 // DefinedTerm tropicosIdentifierTerm = DefinedTerm.NewIdentifierTypeInstance("Tropicos Identifier", "Tropicos Identifier", "Tropicos Identifier");
87 // tropicosIdentifierTerm.setUuid(DefinedTerm.uuidTropicosNameIdentifier);
88 //
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();
97 }
98
99
100
101
102 @Test
103 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/BlankDataSet.xml")
104 public void testSubTree(){
105
106
107 CdmLightExportConfigurator config = new CdmLightExportConfigurator(null);
108 config.setTaxonNodeFilter(TaxonNodeFilter.NewSubtreeInstance(UUID.fromString("f8c9933a-fe3a-42ce-8a92-000e27bfdfac")));
109
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();
115
116
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));
124
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));
130
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));
136
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));
142
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));
150
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));
156
157 }
158
159 @Test
160 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/BlankDataSet.xml")
161 public void testFullTreeWithUnpublished(){
162
163
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();
171
172 ByteArrayInputStream stream = new ByteArrayInputStream( data.get(CdmLightExportTable.TAXON.getTableName()));
173 BufferedReader reader = new BufferedReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
174 String line;
175 int count = 0;
176 try {
177 while ((line = reader.readLine()) != null) {
178 count ++;
179 }
180 Assert.assertTrue("There should be 5 taxa", count == 6);// 6 because of the header line
181
182 stream = new ByteArrayInputStream(data.get(CdmLightExportTable.REFERENCE.getTableName()));
183 reader = new BufferedReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
184 count = 0;
185 while ((line = reader.readLine()) != null) {
186 count ++;
187 }
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")));
191 count = 0;
192 while ((line = reader.readLine()) != null) {
193 count ++;
194 }
195 Assert.assertTrue("There should be 1 synonym", count == 2);
196 } catch (IOException e) {
197 // TODO Auto-generated catch block
198 e.printStackTrace();
199 }
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));
207
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));
213
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));
219
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));
225
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));
233
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));
239
240 }
241
242 @Test
243 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/BlankDataSet.xml")
244 public void testFullData(){
245
246 File destinationFolder = null;
247 CdmLightExportConfigurator config = new CdmLightExportConfigurator(null);
248 config.setTarget(TARGET.EXPORT_DATA);
249
250 ExportResult result = defaultExport.invoke(config);
251 ExportDataWrapper<?> exportData = result.getExportData();
252 @SuppressWarnings("unchecked")
253 Map<String, byte[]> data = (Map<String, byte[]>) exportData.getExportData();
254
255 ByteArrayInputStream stream = new ByteArrayInputStream( data.get(CdmLightExportTable.TAXON.getTableName()));
256 BufferedReader reader = new BufferedReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
257 String line;
258 int count = 0;
259 try {
260 while ((line = reader.readLine()) != null) {
261 count ++;
262 }
263 Assert.assertTrue("There should be 4 taxa", count == 5);// 5 because of the header line
264
265 stream = new ByteArrayInputStream(data.get(CdmLightExportTable.REFERENCE.getTableName()));
266 reader = new BufferedReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
267 count = 0;
268 while ((line = reader.readLine()) != null) {
269 count ++;
270 }
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")));
274 count = 0;
275 while ((line = reader.readLine()) != null) {
276 count ++;
277 }
278 Assert.assertTrue("There should be 1 synonym", count == 2);
279 } catch (IOException e) {
280 // TODO Auto-generated catch block
281 e.printStackTrace();
282 }
283
284
285 }
286
287 /**
288 * {@inheritDoc}
289 */
290 public void createFullTestDataSet() {
291 Set<TaxonNode> nodesToSave = new HashSet<>();
292
293 Reference sec1 = ReferenceFactory.newGeneric();
294 setUuid(sec1, "4b6acca1-959b-4790-b76e-e474a0882990");
295 sec1.setTitle("My sec ref");
296
297 Classification classification = Classification.NewInstance("CdmLightExportTest Classification");
298 setUuid(classification, "4096df99-7274-421e-8843-211b603d832e");
299
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);
310
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");
317
318 TaxonNode node2 = node1.addChildTaxon(genus, sec1, "33");
319 setUuid(node2, "43ca733b-fe3a-42ce-8a92-000e27badf44");
320 nodesToSave.add(node2);
321
322
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());
330
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);
339
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");
344
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);
350
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");
354
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);
361
362
363 classificationService.save(classification);
364 taxonNodeService.save(nodesToSave);
365
366 TaxonDescription description = TaxonDescription.NewInstance(species);
367
368 Distribution distribution = Distribution.NewInstance(NamedArea.AFRICA(), PresenceAbsenceTerm.PRESENT());
369 setUuid(distribution,"674e9e27-9102-4166-8626-8cb871a9a89b");
370 description.addElement(distribution);
371
372 subspecies.addDescription(description);
373 commitAndStartNewTransaction(null);
374
375
376 }
377
378 @Override
379 public void createTestDataSet() throws FileNotFoundException {
380 // try {
381 // writeDbUnitDataSetFile(new String[] {
382 // "Classification",
383 // }, "testAttachDnaSampleToDerivedUnit");
384 // } catch (FileNotFoundException e) {
385 // e.printStackTrace();
386 // }
387 }
388
389
390 private void setUuid(CdmBase cdmBase, String uuidStr) {
391 cdmBase.setUuid(UUID.fromString(uuidStr));
392 }
393
394
395
396 }