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