1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.taxeditor.service;
|
11
|
|
12
|
import java.io.FileNotFoundException;
|
13
|
import java.io.FileOutputStream;
|
14
|
import java.io.IOException;
|
15
|
import java.nio.file.Files;
|
16
|
import java.nio.file.Path;
|
17
|
import java.nio.file.Paths;
|
18
|
import java.util.List;
|
19
|
|
20
|
import org.junit.Assert;
|
21
|
import org.junit.Ignore;
|
22
|
import org.junit.Test;
|
23
|
import org.unitils.dbunit.annotation.DataSet;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
26
|
import eu.etaxonomy.cdm.api.service.INameService;
|
27
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
28
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
29
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
|
30
|
import eu.etaxonomy.cdm.io.common.ImportResult;
|
31
|
import eu.etaxonomy.cdm.io.excel.taxa.NormalExplicitImportConfigurator;
|
32
|
import eu.etaxonomy.cdm.io.sdd.in.SDDImportConfigurator;
|
33
|
import eu.etaxonomy.cdm.io.service.IIOService;
|
34
|
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
|
35
|
import eu.etaxonomy.cdm.io.specimen.excel.in.SpecimenCdmExcelImportConfigurator;
|
36
|
import eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportConfigurator;
|
37
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
38
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
39
|
import eu.etaxonomy.taxeditor.httpinvoker.BaseRemotingTest;
|
40
|
|
41
|
/**
|
42
|
*
|
43
|
* Note : One issue with this test class is that when the tests are run a second time
|
44
|
* against the same server there are errors due to the fact that not all the imported data is deleted.
|
45
|
* This needs to be fixed
|
46
|
* @author cmathew
|
47
|
* @date 5 Aug 2015
|
48
|
*
|
49
|
*/
|
50
|
@Ignore // seems to cause problems with other tests due to data not deleted
|
51
|
@DataSet
|
52
|
public class IOServiceImportTest extends BaseRemotingTest {
|
53
|
|
54
|
private final IIOService ioService = CdmApplicationState.getIOService();
|
55
|
private final INameService nameService = CdmApplicationState.getCurrentAppConfig().getNameService();
|
56
|
private final IOccurrenceService occurrenceService = CdmApplicationState.getCurrentAppConfig().getOccurrenceService();
|
57
|
|
58
|
@Test
|
59
|
public void importNormalExplicit() throws IOException {
|
60
|
String importFilePath = "src/test/resources/imports/NormalExplicitImport.xls";
|
61
|
NormalExplicitImportConfigurator config =
|
62
|
NormalExplicitImportConfigurator.NewInstance(null, null, null, null);
|
63
|
config.setNomenclaturalCode(NomenclaturalCode.ICNB);
|
64
|
config.setDbSchemaValidation(DbSchemaValidation.CREATE);
|
65
|
ImportResult result = ioService.importData(config, fileToByteArray(importFilePath), SOURCE_TYPE.URI);
|
66
|
Assert.assertTrue(result.isSuccess());
|
67
|
List<TaxonNameBase> list = nameService.list(TaxonNameBase.class, null, null, null, null);
|
68
|
Assert.assertEquals(9,list.size());
|
69
|
}
|
70
|
|
71
|
|
72
|
@Test
|
73
|
public void importAbcd() throws IOException {
|
74
|
String importFilePath = "src/test/resources/imports/ABCDImport.xml";
|
75
|
Abcd206ImportConfigurator config =
|
76
|
Abcd206ImportConfigurator.NewInstance(null, null);
|
77
|
config.setNomenclaturalCode(NomenclaturalCode.ICNB);
|
78
|
config.setDbSchemaValidation(DbSchemaValidation.CREATE);
|
79
|
ImportResult result = ioService.importData(config, fileToByteArray(importFilePath), SOURCE_TYPE.INPUTSTREAM);
|
80
|
Assert.assertTrue(result.isSuccess());
|
81
|
Assert.assertTrue(!result.getReports().isEmpty());
|
82
|
saveToFile("target/importAbcdReport.txt", result.getReports().get(0));
|
83
|
List<TaxonNameBase> list = nameService.list(TaxonNameBase.class, null, null, null, null);
|
84
|
Assert.assertEquals(2,list.size());
|
85
|
}
|
86
|
|
87
|
@Test
|
88
|
public void importSDD() throws IOException {
|
89
|
String importFilePath = "src/test/resources/imports/SDD.xml";
|
90
|
SDDImportConfigurator config =
|
91
|
SDDImportConfigurator.NewInstance(null, null);
|
92
|
config.setNomenclaturalCode(NomenclaturalCode.ICNB);
|
93
|
config.setDbSchemaValidation(DbSchemaValidation.CREATE);
|
94
|
ImportResult result = ioService.importData(config, fileToByteArray(importFilePath), SOURCE_TYPE.URI);
|
95
|
Assert.assertTrue(result.isSuccess());
|
96
|
List<TaxonNameBase> list = nameService.list(TaxonNameBase.class, null, null, null, null);
|
97
|
Assert.assertEquals(9,list.size());
|
98
|
}
|
99
|
|
100
|
@Test
|
101
|
public void importSpecimenExcel() throws IOException {
|
102
|
String importFilePath = "src/test/resources/imports/SpecimenExcelImport.xls";
|
103
|
SpecimenCdmExcelImportConfigurator config = SpecimenCdmExcelImportConfigurator.NewInstance(null, null);
|
104
|
config.setNomenclaturalCode(NomenclaturalCode.ICNB);
|
105
|
config.setDbSchemaValidation(DbSchemaValidation.CREATE);
|
106
|
ImportResult result = ioService.importData(config, fileToByteArray(importFilePath), SOURCE_TYPE.URI);
|
107
|
Assert.assertTrue(result.isSuccess());
|
108
|
// List<SpecimenOrObservationBase> occurrences = occurrenceService.list(SpecimenOrObservationBase.class, null, null, null, null);
|
109
|
// Assert.assertEquals(3,occurrences.size());
|
110
|
}
|
111
|
|
112
|
@Test
|
113
|
public void importTCS() throws IOException {
|
114
|
String importFilePath = "src/test/resources/imports/TcsImport.xml";
|
115
|
TcsXmlImportConfigurator config = TcsXmlImportConfigurator.NewInstance(null, null);
|
116
|
config.setNomenclaturalCode(NomenclaturalCode.ICNB);
|
117
|
config.setDbSchemaValidation(DbSchemaValidation.CREATE);
|
118
|
ImportResult result = ioService.importData(config, fileToByteArray(importFilePath), SOURCE_TYPE.URI);
|
119
|
Assert.assertTrue(result.isSuccess());
|
120
|
List<TaxonNameBase> list = nameService.list(TaxonNameBase.class, null, null, null, null);
|
121
|
Assert.assertEquals(16,list.size());
|
122
|
}
|
123
|
|
124
|
private byte[] fileToByteArray(String filePath) throws IOException {
|
125
|
Path path = Paths.get(filePath);
|
126
|
return Files.readAllBytes(path);
|
127
|
}
|
128
|
|
129
|
private static void saveToFile(String filePath, byte[] data) {
|
130
|
FileOutputStream stream = null;
|
131
|
try {
|
132
|
stream = new FileOutputStream(filePath);
|
133
|
stream.write(data);
|
134
|
} catch (FileNotFoundException e) {
|
135
|
e.printStackTrace();
|
136
|
} catch (IOException e) {
|
137
|
e.printStackTrace();
|
138
|
} finally {
|
139
|
try {
|
140
|
if(stream != null) {
|
141
|
stream.close();
|
142
|
}
|
143
|
} catch (IOException e) {
|
144
|
e.printStackTrace();
|
145
|
}
|
146
|
}
|
147
|
}
|
148
|
|
149
|
}
|