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.excel.taxa;
|
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.util.ArrayList;
|
19
|
import java.util.HashSet;
|
20
|
import java.util.Iterator;
|
21
|
import java.util.List;
|
22
|
import java.util.Set;
|
23
|
import java.util.UUID;
|
24
|
|
25
|
import org.apache.log4j.Logger;
|
26
|
import org.junit.Assert;
|
27
|
import org.junit.Before;
|
28
|
import org.junit.Test;
|
29
|
import org.unitils.dbunit.annotation.DataSet;
|
30
|
import org.unitils.dbunit.annotation.DataSets;
|
31
|
import org.unitils.spring.annotation.SpringBeanByName;
|
32
|
import org.unitils.spring.annotation.SpringBeanByType;
|
33
|
|
34
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
35
|
import eu.etaxonomy.cdm.api.service.INameService;
|
36
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
37
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
38
|
import eu.etaxonomy.cdm.common.URI;
|
39
|
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
|
40
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
41
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
42
|
import eu.etaxonomy.cdm.model.common.Language;
|
43
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
44
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
45
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
46
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
47
|
import eu.etaxonomy.cdm.model.description.Feature;
|
48
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
49
|
import eu.etaxonomy.cdm.model.description.TextData;
|
50
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
51
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
52
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
53
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
54
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
55
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
56
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
57
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
58
|
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
|
59
|
|
60
|
/**
|
61
|
* @author a.mueller
|
62
|
* @since 26.08.2009
|
63
|
*/
|
64
|
public class NormalExplicitImportTest extends CdmTransactionalIntegrationTest{
|
65
|
|
66
|
@SuppressWarnings("unused")
|
67
|
private static final Logger logger = Logger.getLogger(NormalExplicitImportTest.class);
|
68
|
|
69
|
@SpringBeanByName
|
70
|
CdmApplicationAwareDefaultImport<?> defaultImport;
|
71
|
|
72
|
@SpringBeanByType
|
73
|
INameService nameService;
|
74
|
|
75
|
@SpringBeanByType
|
76
|
ITaxonService taxonService;
|
77
|
|
78
|
@SpringBeanByType
|
79
|
ITermService termService;
|
80
|
|
81
|
@SpringBeanByType
|
82
|
IClassificationService classificationService;
|
83
|
|
84
|
private IImportConfigurator configurator;
|
85
|
private IImportConfigurator uuidConfigurator;
|
86
|
private IImportConfigurator configuratorXslx;
|
87
|
private IImportConfigurator configuratorTropicos;
|
88
|
|
89
|
@Before
|
90
|
public void setUp() throws URISyntaxException {
|
91
|
String inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest-input.xls";
|
92
|
URL url = this.getClass().getResource(inputFile);
|
93
|
assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
|
94
|
configurator = NormalExplicitImportConfigurator.NewInstance(URI.fromUrl(url), null, NomenclaturalCode.ICNAFP, null);
|
95
|
assertNotNull("Configurator could not be created", configurator);
|
96
|
|
97
|
inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest.testUuid-input.xls";
|
98
|
url = this.getClass().getResource(inputFile);
|
99
|
assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
|
100
|
uuidConfigurator = NormalExplicitImportConfigurator.NewInstance(URI.fromUrl(url), null, NomenclaturalCode.ICNAFP, null);
|
101
|
assertNotNull("Configurator could be created", configurator);
|
102
|
|
103
|
String inputFileXslx = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest-input.xlsx";
|
104
|
url = this.getClass().getResource(inputFileXslx);
|
105
|
assertNotNull("URL for the test file '" + inputFileXslx + "' does not exist", url);
|
106
|
configuratorXslx = NormalExplicitImportConfigurator.NewInstance(URI.fromUrl(url), null, NomenclaturalCode.ICNAFP, null);
|
107
|
assertNotNull("Configurator could not be created", configuratorXslx);
|
108
|
|
109
|
String inputFileTropicos = "/eu/etaxonomy/cdm/io/excel/taxa/ExcelTropicosImportExampleTest-input.xlsx";
|
110
|
url = this.getClass().getResource(inputFileTropicos);
|
111
|
assertNotNull("URL for the test file '" + inputFileTropicos + "' does not exist", url);
|
112
|
configuratorTropicos = NormalExplicitImportConfigurator.NewInstance(URI.fromUrl(url), null, NomenclaturalCode.ICNAFP, null);
|
113
|
assertNotNull("Configurator could not be created", configuratorTropicos);
|
114
|
}
|
115
|
|
116
|
@Test
|
117
|
public void testInit() {
|
118
|
assertNotNull("normalExplicitImport should not be null", defaultImport);
|
119
|
assertNotNull("nameService should not be null", nameService);
|
120
|
}
|
121
|
|
122
|
@Test
|
123
|
@DataSets({
|
124
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
|
125
|
@DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
|
126
|
@DataSet(value="NormalExplicitImportTest.xml")
|
127
|
})
|
128
|
public void testDoInvoke() {
|
129
|
//printDataSet(System.out);
|
130
|
boolean result = defaultImport.invoke(configurator).isSuccess();
|
131
|
assertTrue("Return value for import.invoke should be true", result);
|
132
|
assertEquals("Number of TaxonNames should be 9", 9, nameService.count(null));
|
133
|
List<Classification> treeList = classificationService.list(null, null,null,null,null);
|
134
|
assertEquals("Number of classifications should be 1", 1, treeList.size());
|
135
|
Classification tree = treeList.get(0);
|
136
|
List<TaxonNode> rootNodes = tree.getChildNodes();
|
137
|
assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
|
138
|
TaxonNode rootNode = rootNodes.iterator().next();
|
139
|
assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
|
140
|
TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
|
141
|
assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
|
142
|
TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
|
143
|
TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
|
144
|
TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
|
145
|
TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
|
146
|
assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
|
147
|
|
148
|
Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
|
149
|
TaxonNode childNode1 = it.next();
|
150
|
TaxonNode childNode2 = it.next();
|
151
|
|
152
|
TaxonNode noctuaPronubaNode;
|
153
|
if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
|
154
|
noctuaPronubaNode = childNode1;
|
155
|
}else{
|
156
|
noctuaPronubaNode = childNode2;
|
157
|
}
|
158
|
|
159
|
assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
|
160
|
Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
|
161
|
Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
|
162
|
assertEquals("Number of synonyms should be 1", 1, synonyms.size());
|
163
|
Synonym synonym = synonyms.iterator().next();
|
164
|
assertEquals("Synonym name should be ", "Noctua atlantica", synonym.getName().getNameCache());
|
165
|
Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
|
166
|
Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
|
167
|
TaxonDescription taxonDescription = descriptions.iterator().next();
|
168
|
Set<DescriptionElementBase> elements = taxonDescription.getElements();
|
169
|
List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
|
170
|
for (DescriptionElementBase element : elements){
|
171
|
if (element.isInstanceOf(CommonTaxonName.class)){
|
172
|
commonNames.add((CommonTaxonName)element);
|
173
|
}
|
174
|
}
|
175
|
Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
|
176
|
Set<String> commonNameStrings = new HashSet<>();
|
177
|
commonNameStrings.add(commonNames.get(0).getName());
|
178
|
commonNameStrings.add(commonNames.get(1).getName());
|
179
|
Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
|
180
|
Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
|
181
|
}
|
182
|
|
183
|
@Test
|
184
|
@DataSets({
|
185
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
|
186
|
@DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
|
187
|
@DataSet(value="NormalExplicitImportTest.xml")
|
188
|
})
|
189
|
public void testDoInvokeXslx() {
|
190
|
//printDataSet(System.out);
|
191
|
boolean result = defaultImport.invoke(configuratorXslx).isSuccess();
|
192
|
assertTrue("Return value for import.invoke should be true", result);
|
193
|
assertEquals("Number of TaxonNames should be 9", 9, nameService.count(null));
|
194
|
List<Classification> treeList = classificationService.list(null, null,null,null,null);
|
195
|
assertEquals("Number of classifications should be 1", 1, treeList.size());
|
196
|
Classification tree = treeList.get(0);
|
197
|
List<TaxonNode> rootNodes = tree.getChildNodes();
|
198
|
assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
|
199
|
TaxonNode rootNode = rootNodes.iterator().next();
|
200
|
assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
|
201
|
TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
|
202
|
assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
|
203
|
TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
|
204
|
TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
|
205
|
TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
|
206
|
TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
|
207
|
assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
|
208
|
|
209
|
Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
|
210
|
TaxonNode childNode1 = it.next();
|
211
|
TaxonNode childNode2 = it.next();
|
212
|
|
213
|
TaxonNode noctuaPronubaNode;
|
214
|
if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
|
215
|
noctuaPronubaNode = childNode1;
|
216
|
}else{
|
217
|
noctuaPronubaNode = childNode2;
|
218
|
}
|
219
|
|
220
|
assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
|
221
|
Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
|
222
|
Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
|
223
|
assertEquals("Number of synonyms should be 1", 1, synonyms.size());
|
224
|
Synonym synonym = synonyms.iterator().next();
|
225
|
assertEquals("Synonym name should be ", "Noctua atlantica", synonym.getName().getNameCache());
|
226
|
Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
|
227
|
Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
|
228
|
TaxonDescription taxonDescription = descriptions.iterator().next();
|
229
|
Set<DescriptionElementBase> elements = taxonDescription.getElements();
|
230
|
List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
|
231
|
for (DescriptionElementBase element : elements){
|
232
|
if (element.isInstanceOf(CommonTaxonName.class)){
|
233
|
commonNames.add((CommonTaxonName)element);
|
234
|
}
|
235
|
}
|
236
|
Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
|
237
|
Set<String> commonNameStrings = new HashSet<>();
|
238
|
commonNameStrings.add(commonNames.get(0).getName());
|
239
|
commonNameStrings.add(commonNames.get(1).getName());
|
240
|
Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
|
241
|
Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
|
242
|
}
|
243
|
|
244
|
@Test
|
245
|
// @DataSet(value="NormalExplicitImportTest.testUuid.xml")
|
246
|
@DataSets({
|
247
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
|
248
|
@DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
|
249
|
@DataSet(value="NormalExplicitImportTest.testUuid.xml"),
|
250
|
})
|
251
|
public void testUUID(){
|
252
|
UUID taxonUuid = UUID.fromString("aafce7fe-0c5f-42ed-814b-4c7c2c715660");
|
253
|
UUID synonymUuid = UUID.fromString("fc4a995b-37a9-4984-afe6-e352c6c04d92");
|
254
|
|
255
|
|
256
|
//test data set
|
257
|
assertEquals("Number of taxon bases should be 2", 2, taxonService.count(null));
|
258
|
Taxon taxon = (Taxon)taxonService.find(taxonUuid);
|
259
|
assertNotNull("Taxon with given uuid should exist", taxon);
|
260
|
assertEquals("Taxon should have no description", 0, taxon.getDescriptions().size());
|
261
|
Synonym synonym = (Synonym)taxonService.find(synonymUuid);
|
262
|
assertNotNull("Synonym with given uuid should exist", synonym);
|
263
|
assertNotNull("Synonym should have 1 accepted taxon", synonym.getAcceptedTaxon());
|
264
|
|
265
|
//import
|
266
|
boolean result = defaultImport.invoke(uuidConfigurator).isSuccess();
|
267
|
//test result
|
268
|
assertTrue("Return value for import.invoke should be true", result);
|
269
|
assertEquals("Number of taxon names should be 2", 2, nameService.count(null));
|
270
|
assertEquals("Number of taxa should be 2", 2, taxonService.count(null));
|
271
|
taxon = (Taxon)taxonService.find(taxonUuid);
|
272
|
assertEquals("Taxon should have 1 description", 1, taxon.getDescriptions().size());
|
273
|
TaxonDescription description = taxon.getDescriptions().iterator().next();
|
274
|
assertEquals("Number of description elements should be 2", 2, description.getElements().size());
|
275
|
|
276
|
String expectedText = "Description for the first taxon";
|
277
|
TextData textData = getTextElement(description, expectedText);
|
278
|
assertNotNull("The element should exists", textData);
|
279
|
Feature feature = textData.getFeature();
|
280
|
assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
|
281
|
assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
|
282
|
Language language = textData.getMultilanguageText().keySet().iterator().next();
|
283
|
assertEquals("Language should be German", Language.GERMAN(), language);
|
284
|
String text = textData.getText(language);
|
285
|
assertEquals("Unexpected description text", expectedText, text);
|
286
|
assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
|
287
|
DescriptionElementSource source = textData.getSources().iterator().next();
|
288
|
Reference ref = source.getCitation();
|
289
|
assertNotNull("Citation should not be null", ref);
|
290
|
assertNotNull("Authorship should not be null", ref.getAuthorship());
|
291
|
assertEquals("Source author should be 'Meyer et. al.'", "Meyer et. al.",ref.getAuthorship().getTitleCache());
|
292
|
assertEquals("Publication title should be 'My first book'", "My first book", ref.getTitle());
|
293
|
assertEquals("Publication year should be '1987'", "1987", ref.getYear());
|
294
|
TaxonName nameUsedInSource = source.getNameUsedInSource();
|
295
|
assertNotNull("Name used in source should not be null", nameUsedInSource);
|
296
|
assertEquals("Name used in source title should be ", "Abies", nameUsedInSource.getTitleCache());
|
297
|
|
298
|
|
299
|
//synonym
|
300
|
expectedText = "A synonym description";
|
301
|
textData = getTextElement(description, expectedText);
|
302
|
assertNotNull("The element should exists", textData);
|
303
|
feature = textData.getFeature();
|
304
|
assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
|
305
|
assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
|
306
|
language = textData.getMultilanguageText().keySet().iterator().next();
|
307
|
assertEquals("Language should be Spanish", Language.SPANISH_CASTILIAN(), language);
|
308
|
text = textData.getText(language);
|
309
|
assertEquals("Unexpected description text", expectedText, text);
|
310
|
assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
|
311
|
source = textData.getSources().iterator().next();
|
312
|
ref = source.getCitation();
|
313
|
assertNotNull("Citation should not be null", ref);
|
314
|
assertNotNull("Authorship should not be null", ref.getAuthorship());
|
315
|
assertEquals("Source author should be 'Theys, A.'", "Theys, A.",ref.getAuthorship().getTitleCache());
|
316
|
assertEquals("Publication title should be 'The ultimate book'", "The ultimate book", ref.getTitle());
|
317
|
assertEquals("Publication year should be '2011'", "2011", ref.getYear());
|
318
|
nameUsedInSource = source.getNameUsedInSource();
|
319
|
assertNotNull("Name used in source should not be null", nameUsedInSource);
|
320
|
assertEquals("Name used in source title should be Pinus", "Pinus", nameUsedInSource.getTitleCache());
|
321
|
|
322
|
}
|
323
|
|
324
|
/**
|
325
|
* Returns description element for record id 1
|
326
|
* @param description
|
327
|
* @return
|
328
|
*/
|
329
|
private TextData getTextElement(TaxonDescription description, String descriptionText) {
|
330
|
for (DescriptionElementBase element : description.getElements()){
|
331
|
if (element.isInstanceOf(TextData.class)){
|
332
|
TextData textData = CdmBase.deproxy(element, TextData.class);
|
333
|
for (LanguageString ls :textData.getMultilanguageText().values()){
|
334
|
if (ls.getText().equals(descriptionText)){
|
335
|
return textData;
|
336
|
}
|
337
|
}
|
338
|
}
|
339
|
}
|
340
|
return null;
|
341
|
}
|
342
|
|
343
|
@Override
|
344
|
public void createTestDataSet() throws FileNotFoundException {}
|
345
|
|
346
|
@Test
|
347
|
@DataSets({
|
348
|
@DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
|
349
|
@DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
|
350
|
@DataSet(value="NormalExplicitImportTest.xml")
|
351
|
})
|
352
|
public void testDoInvokeTropicos() {
|
353
|
//printDataSet(System.out);
|
354
|
boolean result = defaultImport.invoke(configuratorTropicos).isSuccess();
|
355
|
assertTrue("Return value for import.invoke should be true", result);
|
356
|
int count = nameService.count(null);
|
357
|
|
358
|
assertEquals("Number of TaxonNames should be 19", 19, count);
|
359
|
int countTaxa = taxonService.count(Taxon.class);
|
360
|
assertEquals("Number of Taxa should be 18", 18, countTaxa);
|
361
|
}
|
362
|
|
363
|
}
|