merge hibernate4 migration branch into trunk
[cdmlib.git] / cdmlib-io / src / test / java / eu / etaxonomy / cdm / io / excel / taxa / NormalExplicitImportTest.java
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 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;
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.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;
58
59 /**
60 * @author a.mueller
61 * @created 26.08.2009
62 * @version 1.0
63 */
64 public class NormalExplicitImportTest extends CdmTransactionalIntegrationTest{
65 @SuppressWarnings("unused")
66 private static final Logger logger = Logger.getLogger(NormalExplicitImportTest.class);
67
68 @SpringBeanByName
69 CdmApplicationAwareDefaultImport<?> defaultImport;
70
71 @SpringBeanByType
72 INameService nameService;
73
74 @SpringBeanByType
75 ITaxonService taxonService;
76
77 @SpringBeanByType
78 ITermService termService;
79
80 @SpringBeanByType
81 IClassificationService classificationService;
82
83 private IImportConfigurator configurator;
84 private IImportConfigurator uuidConfigurator;
85
86 @Before
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);
93
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);
99
100 }
101
102 @Test
103 public void testInit() {
104 assertNotNull("normalExplicitImport should not be null", defaultImport);
105 assertNotNull("nameService should not be null", nameService);
106 }
107
108 @Test
109 @DataSet
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 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");
170
171
172 //test data set
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());
180
181 //import
182 boolean result = defaultImport.invoke(uuidConfigurator);
183 //test result
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());
191
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());
213
214
215 //synonym
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());
237
238 }
239
240 /**
241 * Returns description element for record id 1
242 * @param description
243 * @return
244 */
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)){
251 return textData;
252 }
253 }
254 }
255 }
256 return null;
257 }
258
259 }