3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.io
.excel
.taxa
;
13 import static org
.junit
.Assert
.assertEquals
;
14 import static org
.junit
.Assert
.assertNotNull
;
15 import static org
.junit
.Assert
.assertTrue
;
17 import java
.net
.URISyntaxException
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashSet
;
21 import java
.util
.Iterator
;
22 import java
.util
.List
;
24 import java
.util
.UUID
;
26 import org
.apache
.log4j
.Logger
;
27 import org
.junit
.Assert
;
28 import org
.junit
.Before
;
29 import org
.junit
.Test
;
30 import org
.unitils
.dbunit
.annotation
.DataSet
;
31 import org
.unitils
.spring
.annotation
.SpringBeanByName
;
32 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
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
.io
.common
.CdmApplicationAwareDefaultImport
;
39 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
40 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
41 import eu
.etaxonomy
.cdm
.model
.common
.DescriptionElementSource
;
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
.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
.name
.TaxonNameBase
;
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
;
64 public class NormalExplicitImportTest
extends CdmTransactionalIntegrationTest
{
65 @SuppressWarnings("unused")
66 private static final Logger logger
= Logger
.getLogger(NormalExplicitImportTest
.class);
69 CdmApplicationAwareDefaultImport
<?
> defaultImport
;
72 INameService nameService
;
75 ITaxonService taxonService
;
78 ITermService termService
;
81 IClassificationService classificationService
;
83 private IImportConfigurator configurator
;
84 private IImportConfigurator uuidConfigurator
;
87 public void setUp() throws URISyntaxException
{
88 String inputFile
= "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest-input.xls";
89 URL url
= this.getClass().getResource(inputFile
);
90 assertNotNull("URL for the test file '" + inputFile
+ "' does not exist", url
);
91 configurator
= NormalExplicitImportConfigurator
.NewInstance(url
.toURI(), null, NomenclaturalCode
.ICBN
, null);
92 assertNotNull("Configurator could not be created", configurator
);
94 inputFile
= "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest.testUuid-input.xls";
95 url
= this.getClass().getResource(inputFile
);
96 assertNotNull("URL for the test file '" + inputFile
+ "' does not exist", url
);
97 uuidConfigurator
= NormalExplicitImportConfigurator
.NewInstance(url
.toURI(), null, NomenclaturalCode
.ICBN
, null);
98 assertNotNull("Configurator could not be created", configurator
);
103 public void testInit() {
104 assertNotNull("normalExplicitImport should not be null", defaultImport
);
105 assertNotNull("nameService should not be null", nameService
);
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());
130 Iterator
<TaxonNode
> it
= noctuaNode
.getChildNodes().iterator();
131 TaxonNode childNode1
= it
.next();
132 TaxonNode childNode2
= it
.next();
134 TaxonNode noctuaPronubaNode
;
135 if (childNode1
.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
136 noctuaPronubaNode
= childNode1
;
138 noctuaPronubaNode
= childNode2
;
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
);
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"));
166 @DataSet(value
="NormalExplicitImportTest.testUuid.xml")
167 public void testUUID() throws URISyntaxException
{
168 UUID taxonUuid
= UUID
.fromString("aafce7fe-0c5f-42ed-814b-4c7c2c715660");
169 UUID synonymUuid
= UUID
.fromString("fc4a995b-37a9-4984-afe6-e352c6c04d92");
173 assertEquals("Number of taxon bases should be 2", 2, taxonService
.count(null));
174 Taxon taxon
= (Taxon
)taxonService
.find(taxonUuid
);
175 assertNotNull("Taxon with given uuid should exist", taxon
);
176 assertEquals("Taxon should have no description", 0, taxon
.getDescriptions().size());
177 Synonym synonym
= (Synonym
)taxonService
.find(synonymUuid
);
178 assertNotNull("Synonym with given uuid should exist", synonym
);
179 assertEquals("Synonym should have 1 accepted taxon", 1, synonym
.getAcceptedTaxa().size());
182 boolean result
= defaultImport
.invoke(uuidConfigurator
);
184 assertTrue("Return value for import.invoke should be true", result
);
185 assertEquals("Number of taxon names should be 2", 2, nameService
.count(null));
186 assertEquals("Number of taxa should be 2", 2, taxonService
.count(null));
187 taxon
= (Taxon
)taxonService
.find(taxonUuid
);
188 assertEquals("Taxon should have 1 description", 1, taxon
.getDescriptions().size());
189 TaxonDescription description
= taxon
.getDescriptions().iterator().next();
190 assertEquals("Number of description elements should be 2", 2, description
.getElements().size());
192 String expectedText
= "Description for the first taxon";
193 TextData textData
= getTextElement(description
, expectedText
);
194 assertNotNull("The element should exists", textData
);
195 Feature feature
= textData
.getFeature();
196 assertEquals("Unexpected feature", Feature
.DESCRIPTION(), feature
);
197 assertEquals("There should be exactly 1 language", 1,textData
.getMultilanguageText().size());
198 Language language
= textData
.getMultilanguageText().keySet().iterator().next();
199 assertEquals("Language should be German", Language
.GERMAN(), language
);
200 String text
= textData
.getText(language
);
201 assertEquals("Unexpected description text", expectedText
, 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 title should be 'My first book'", "My first book", ref
.getTitle());
209 assertEquals("Publication year should be '1987'", "1987", ref
.getYear());
210 TaxonNameBase
<?
,?
> nameUsedInSource
= source
.getNameUsedInSource();
211 assertNotNull("Name used in source should not be null", nameUsedInSource
);
212 assertEquals("Name used in source title should be ", "Abies", nameUsedInSource
.getTitleCache());
216 expectedText
= "A synonym description";
217 textData
= getTextElement(description
, expectedText
);
218 assertNotNull("The element should exists", textData
);
219 feature
= textData
.getFeature();
220 assertEquals("Unexpected feature", Feature
.DESCRIPTION(), feature
);
221 assertEquals("There should be exactly 1 language", 1,textData
.getMultilanguageText().size());
222 language
= textData
.getMultilanguageText().keySet().iterator().next();
223 assertEquals("Language should be Spanish", Language
.SPANISH_CASTILIAN(), language
);
224 text
= textData
.getText(language
);
225 assertEquals("Unexpected description text", expectedText
, text
);
226 assertEquals("Number of source elements should be 1", 1, textData
.getSources().size());
227 source
= textData
.getSources().iterator().next();
228 ref
= source
.getCitation();
229 assertNotNull("Citation should not be null", ref
);
230 assertNotNull("AuthorTeam should not be null", ref
.getAuthorTeam());
231 assertEquals("Source author should be 'Theys, A.'", "Theys, A.",ref
.getAuthorTeam().getTitleCache());
232 assertEquals("Publication title should be 'The ultimate book'", "The ultimate book", ref
.getTitle());
233 assertEquals("Publication year should be '2011'", "2011", ref
.getYear());
234 nameUsedInSource
= source
.getNameUsedInSource();
235 assertNotNull("Name used in source should not be null", nameUsedInSource
);
236 assertEquals("Name used in source title should be Pinus", "Pinus", nameUsedInSource
.getTitleCache());
241 * Returns description element for record id 1
245 private TextData
getTextElement(TaxonDescription description
, String descriptionText
) {
246 for (DescriptionElementBase element
: description
.getElements()){
247 if (element
.isInstanceOf(TextData
.class)){
248 TextData textData
= CdmBase
.deproxy(element
, TextData
.class);
249 for (LanguageString ls
:textData
.getMultilanguageText().values()){
250 if (ls
.getText().equals(descriptionText
)){