1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 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
|
|
11
|
package eu.etaxonomy.cdm.io.excel.taxa;
|
12
|
|
13
|
import static org.junit.Assert.assertEquals;
|
14
|
import static org.junit.Assert.assertNotNull;
|
15
|
import static org.junit.Assert.assertTrue;
|
16
|
|
17
|
import java.net.URISyntaxException;
|
18
|
import java.net.URL;
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.HashSet;
|
21
|
import java.util.Iterator;
|
22
|
import java.util.List;
|
23
|
import java.util.Set;
|
24
|
import java.util.UUID;
|
25
|
|
26
|
import junit.framework.Assert;
|
27
|
|
28
|
import org.apache.log4j.Logger;
|
29
|
import org.junit.Before;
|
30
|
import org.junit.Ignore;
|
31
|
import org.junit.Test;
|
32
|
import org.unitils.dbunit.annotation.DataSet;
|
33
|
import org.unitils.spring.annotation.SpringBeanByName;
|
34
|
import org.unitils.spring.annotation.SpringBeanByType;
|
35
|
|
36
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
37
|
import eu.etaxonomy.cdm.api.service.INameService;
|
38
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
39
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
40
|
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
|
41
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
42
|
import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
|
43
|
import eu.etaxonomy.cdm.model.common.Language;
|
44
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
45
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
46
|
import eu.etaxonomy.cdm.model.description.Feature;
|
47
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
48
|
import eu.etaxonomy.cdm.model.description.TextData;
|
49
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
50
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
51
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
52
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
53
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
54
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
55
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
56
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
57
|
|
58
|
/**
|
59
|
* @author a.mueller
|
60
|
* @created 26.08.2009
|
61
|
* @version 1.0
|
62
|
*/
|
63
|
public class NormalExplicitImportTest extends CdmTransactionalIntegrationTest{
|
64
|
@SuppressWarnings("unused")
|
65
|
private static final Logger logger = Logger.getLogger(NormalExplicitImportTest.class);
|
66
|
|
67
|
@SpringBeanByName
|
68
|
CdmApplicationAwareDefaultImport defaultImport;
|
69
|
|
70
|
@SpringBeanByType
|
71
|
INameService nameService;
|
72
|
|
73
|
@SpringBeanByType
|
74
|
ITaxonService taxonService;
|
75
|
|
76
|
@SpringBeanByType
|
77
|
ITermService termService;
|
78
|
|
79
|
@SpringBeanByType
|
80
|
IClassificationService classificationService;
|
81
|
|
82
|
private IImportConfigurator configurator;
|
83
|
private IImportConfigurator uuidConfigurator;
|
84
|
|
85
|
@Before
|
86
|
public void setUp() throws URISyntaxException {
|
87
|
String inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest-input.xls";
|
88
|
URL url = this.getClass().getResource(inputFile);
|
89
|
assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
|
90
|
configurator = NormalExplicitImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICBN, null);
|
91
|
assertNotNull("Configurator could not be created", configurator);
|
92
|
|
93
|
inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest.testUuid-input.xls";
|
94
|
url = this.getClass().getResource(inputFile);
|
95
|
assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
|
96
|
uuidConfigurator = NormalExplicitImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICBN, null);
|
97
|
assertNotNull("Configurator could not be created", configurator);
|
98
|
|
99
|
}
|
100
|
|
101
|
@Test
|
102
|
public void testInit() {
|
103
|
assertNotNull("normalExplicitImport should not be null", defaultImport);
|
104
|
assertNotNull("nameService should not be null", nameService);
|
105
|
}
|
106
|
|
107
|
@Test
|
108
|
@DataSet
|
109
|
// @Ignore //does run standalone, but not in suite (maven)
|
110
|
public void testDoInvoke() {
|
111
|
//printDataSet(System.out);
|
112
|
boolean result = defaultImport.invoke(configurator);
|
113
|
assertTrue("Return value for import.invoke should be true", result);
|
114
|
assertEquals("Number of TaxonNames should be 9", 9, nameService.count(null));
|
115
|
List<Classification> treeList = classificationService.list(null, null,null,null,null);
|
116
|
assertEquals("Number of classifications should be 1", 1, treeList.size());
|
117
|
Classification tree = treeList.get(0);
|
118
|
Set<TaxonNode> rootNodes = tree.getChildNodes();
|
119
|
assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
|
120
|
TaxonNode rootNode = rootNodes.iterator().next();
|
121
|
assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
|
122
|
TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
|
123
|
assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
|
124
|
TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
|
125
|
TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
|
126
|
TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
|
127
|
TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
|
128
|
assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
|
129
|
|
130
|
Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
|
131
|
TaxonNode childNode1 = it.next();
|
132
|
TaxonNode childNode2 = it.next();
|
133
|
|
134
|
TaxonNode noctuaPronubaNode;
|
135
|
if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
|
136
|
noctuaPronubaNode = childNode1;
|
137
|
}else{
|
138
|
noctuaPronubaNode = childNode2;
|
139
|
}
|
140
|
|
141
|
assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
|
142
|
Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
|
143
|
Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
|
144
|
assertEquals("Number of synonyms should be 1", 1, synonyms.size());
|
145
|
Synonym synonym = synonyms.iterator().next();
|
146
|
assertEquals("Synonym name should be ", "Noctua atlantica", ((NonViralName)synonym.getName()).getNameCache());
|
147
|
Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
|
148
|
Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
|
149
|
TaxonDescription taxonDescription = descriptions.iterator().next();
|
150
|
Set<DescriptionElementBase> elements = taxonDescription.getElements();
|
151
|
List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
|
152
|
for (DescriptionElementBase element : elements){
|
153
|
if (element.isInstanceOf(CommonTaxonName.class)){
|
154
|
commonNames.add((CommonTaxonName)element);
|
155
|
}
|
156
|
}
|
157
|
Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
|
158
|
Set<String> commonNameStrings = new HashSet<String>();
|
159
|
commonNameStrings.add(commonNames.get(0).getName());
|
160
|
commonNameStrings.add(commonNames.get(1).getName());
|
161
|
Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
|
162
|
Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
|
163
|
}
|
164
|
|
165
|
@Test
|
166
|
@DataSet(value="NormalExplicitImportTest.testUuid.xml")
|
167
|
// @Ignore //does run standalone, but not in suite (maven)
|
168
|
public void testUUID() throws URISyntaxException{
|
169
|
UUID taxonUuid = UUID.fromString("aafce7fe-0c5f-42ed-814b-4c7c2c715660");
|
170
|
UUID synonymUuid = UUID.fromString("fc4a995b-37a9-4984-afe6-e352c6c04d92");
|
171
|
|
172
|
|
173
|
//test data set
|
174
|
assertEquals("Number of taxon bases should be 2", 2, taxonService.count(null));
|
175
|
Taxon taxon = (Taxon)taxonService.find(taxonUuid);
|
176
|
assertNotNull("Taxon with given uuid should exist", taxon);
|
177
|
assertEquals("Taxon should have no description", 0, taxon.getDescriptions().size());
|
178
|
Synonym synonym = (Synonym)taxonService.find(synonymUuid);
|
179
|
assertNotNull("Synonym with given uuid should exist", synonym);
|
180
|
assertEquals("Synonym should have 1 accepted taxon", 1, synonym.getAcceptedTaxa().size());
|
181
|
|
182
|
//import
|
183
|
boolean result = defaultImport.invoke(uuidConfigurator);
|
184
|
//test result
|
185
|
assertTrue("Return value for import.invoke should be true", result);
|
186
|
assertEquals("Number of taxon names should be 2", 2, nameService.count(null));
|
187
|
assertEquals("Number of taxa should be 2", 2, taxonService.count(null));
|
188
|
taxon = (Taxon)taxonService.find(taxonUuid);
|
189
|
assertEquals("Taxon should have 1 description", 1, taxon.getDescriptions().size());
|
190
|
TaxonDescription description = taxon.getDescriptions().iterator().next();
|
191
|
assertEquals("Number of description elements should be 2", 2, description.getElements().size());
|
192
|
|
193
|
DescriptionElementBase element = getFirstElement(description);
|
194
|
// DescriptionElementBase element = description.getElements().iterator().next();
|
195
|
assertEquals("Element should be of class TextData", TextData.class, element.getClass());
|
196
|
TextData textData = (TextData)element;
|
197
|
Feature feature = textData.getFeature();
|
198
|
assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
|
199
|
String text = textData.getText(Language.DEFAULT());
|
200
|
String expected = "Description for the first taxon";
|
201
|
assertEquals("Unexpected description text", expected, text);
|
202
|
assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
|
203
|
DescriptionElementSource source = textData.getSources().iterator().next();
|
204
|
Reference ref = source.getCitation();
|
205
|
assertNotNull("Citation should not be null", ref);
|
206
|
assertNotNull("AuthorTeam should not be null", ref.getAuthorTeam());
|
207
|
assertEquals("Source author should be 'Meyer et. al.'", "Meyer et. al.",ref.getAuthorTeam().getTitleCache());
|
208
|
assertEquals("Publication year should be '1987'", "1987", ref.getYear());
|
209
|
//synonym
|
210
|
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* Returns description element for record id 1
|
215
|
* @param description
|
216
|
* @return
|
217
|
*/
|
218
|
private DescriptionElementBase getFirstElement(TaxonDescription description) {
|
219
|
// TODO Auto-generated method stub
|
220
|
return null;
|
221
|
}
|
222
|
|
223
|
}
|