Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
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.CdmBase;
43
import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
44
import eu.etaxonomy.cdm.model.common.Language;
45
import eu.etaxonomy.cdm.model.common.LanguageString;
46
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
47
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
48
import eu.etaxonomy.cdm.model.description.Feature;
49
import eu.etaxonomy.cdm.model.description.TaxonDescription;
50
import eu.etaxonomy.cdm.model.description.TextData;
51
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
52
import eu.etaxonomy.cdm.model.name.NonViralName;
53
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
54
import eu.etaxonomy.cdm.model.reference.Reference;
55
import eu.etaxonomy.cdm.model.taxon.Classification;
56
import eu.etaxonomy.cdm.model.taxon.Synonym;
57
import eu.etaxonomy.cdm.model.taxon.Taxon;
58
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
59
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
60

    
61
/**
62
 * @author a.mueller
63
 * @created 26.08.2009
64
 * @version 1.0
65
 */
66
public class NormalExplicitImportTest extends CdmTransactionalIntegrationTest{
67
	@SuppressWarnings("unused")
68
	private static final Logger logger = Logger.getLogger(NormalExplicitImportTest.class);
69

    
70
	@SpringBeanByName
71
	CdmApplicationAwareDefaultImport defaultImport;
72

    
73
	@SpringBeanByType
74
	INameService nameService;
75
	
76
	@SpringBeanByType
77
	ITaxonService taxonService;
78
	
79
	@SpringBeanByType
80
	ITermService termService;
81
	
82
	@SpringBeanByType
83
	IClassificationService classificationService;
84

    
85
	private IImportConfigurator configurator;
86
	private IImportConfigurator uuidConfigurator;
87
	
88
	@Before
89
	public void setUp() throws URISyntaxException {
90
		String inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest-input.xls";
91
		URL url = this.getClass().getResource(inputFile);
92
	 	assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
93
		configurator = NormalExplicitImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICBN, null);
94
		assertNotNull("Configurator could not be created", configurator);
95
		
96
		inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest.testUuid-input.xls";
97
		url = this.getClass().getResource(inputFile);
98
	 	assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
99
		uuidConfigurator = NormalExplicitImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICBN, null);
100
		assertNotNull("Configurator could not be created", configurator);
101
		
102
	}
103
	
104
	@Test
105
	public void testInit() {
106
		assertNotNull("normalExplicitImport should not be null", defaultImport);
107
		assertNotNull("nameService should not be null", nameService);
108
	}
109
	
110
	@Test
111
	@DataSet
112
	@Ignore //does run standalone, but not in suite (maven)
113
	public void testDoInvoke() {
114
		//printDataSet(System.out);
115
		boolean result = defaultImport.invoke(configurator);
116
		assertTrue("Return value for import.invoke should be true", result);
117
		assertEquals("Number of TaxonNames should be 9", 9, nameService.count(null));
118
		List<Classification> treeList = classificationService.list(null, null,null,null,null);
119
		assertEquals("Number of classifications should be 1", 1, treeList.size());
120
		Classification tree = treeList.get(0);
121
		Set<TaxonNode> rootNodes = tree.getChildNodes();
122
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
123
		TaxonNode rootNode = rootNodes.iterator().next();
124
		assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
125
		TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
126
		assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
127
		TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
128
		TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
129
		TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
130
		TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
131
		assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
132
		
133
		Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
134
		TaxonNode childNode1 = it.next();
135
		TaxonNode childNode2 = it.next();
136
		
137
		TaxonNode noctuaPronubaNode;
138
		if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
139
			noctuaPronubaNode = childNode1;
140
		}else{
141
			noctuaPronubaNode = childNode2;
142
		}
143
		
144
		assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
145
		Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
146
		Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
147
		assertEquals("Number of synonyms should be 1", 1, synonyms.size());
148
		Synonym synonym = synonyms.iterator().next();
149
		assertEquals("Synonym name should be ", "Noctua atlantica", ((NonViralName)synonym.getName()).getNameCache());
150
		Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
151
		Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
152
		TaxonDescription taxonDescription = descriptions.iterator().next();
153
		Set<DescriptionElementBase> elements = taxonDescription.getElements();
154
		List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
155
		for (DescriptionElementBase element : elements){
156
			if (element.isInstanceOf(CommonTaxonName.class)){
157
				commonNames.add((CommonTaxonName)element);
158
			}
159
		}
160
		Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
161
		Set<String> commonNameStrings = new HashSet<String>();
162
		commonNameStrings.add(commonNames.get(0).getName());
163
		commonNameStrings.add(commonNames.get(1).getName());
164
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
165
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
166
	}
167
	
168
	@Test
169
	@DataSet(value="NormalExplicitImportTest.testUuid.xml")
170
	@Ignore //does run standalone, but not in suite (maven)
171
	public void testUUID() throws URISyntaxException{
172
		UUID taxonUuid = UUID.fromString("aafce7fe-0c5f-42ed-814b-4c7c2c715660");
173
		UUID synonymUuid = UUID.fromString("fc4a995b-37a9-4984-afe6-e352c6c04d92");
174
		
175
		
176
		//test data set
177
		assertEquals("Number of taxon bases should be 2", 2, taxonService.count(null));
178
		Taxon taxon = (Taxon)taxonService.find(taxonUuid);
179
		assertNotNull("Taxon with given uuid should exist", taxon);
180
		assertEquals("Taxon should have no description", 0, taxon.getDescriptions().size());
181
		Synonym synonym = (Synonym)taxonService.find(synonymUuid);
182
		assertNotNull("Synonym with given uuid should exist", synonym);
183
		assertEquals("Synonym should have 1 accepted taxon", 1, synonym.getAcceptedTaxa().size());
184
		
185
		//import
186
		boolean result = defaultImport.invoke(uuidConfigurator);
187
		//test result
188
		assertTrue("Return value for import.invoke should be true", result);
189
		assertEquals("Number of taxon names should be 2", 2, nameService.count(null));
190
		assertEquals("Number of taxa should be 2", 2, taxonService.count(null));
191
		taxon = (Taxon)taxonService.find(taxonUuid);
192
		assertEquals("Taxon should have 1 description", 1, taxon.getDescriptions().size());
193
		TaxonDescription description = taxon.getDescriptions().iterator().next();
194
		assertEquals("Number of description elements should be 2", 2, description.getElements().size());
195
		
196
		String expectedText = "Description for the first taxon";
197
		TextData textData = getTextElement(description, expectedText);
198
		assertNotNull("The element should exists", textData);
199
		Feature feature = textData.getFeature();
200
		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
201
		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
202
		Language language = textData.getMultilanguageText().keySet().iterator().next();
203
		assertEquals("Language should be German", Language.GERMAN(), language);
204
		String text = textData.getText(language);
205
		assertEquals("Unexpected description text", expectedText, text);
206
		assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
207
		DescriptionElementSource source = textData.getSources().iterator().next();
208
		Reference ref = source.getCitation();
209
		assertNotNull("Citation should not be null", ref);
210
		assertNotNull("AuthorTeam should not be null", ref.getAuthorTeam());
211
		assertEquals("Source author should be 'Meyer et. al.'", "Meyer et. al.",ref.getAuthorTeam().getTitleCache());
212
		assertEquals("Publication title should be 'My first book'", "My first book", ref.getTitle());
213
		assertEquals("Publication year should be '1987'", "1987", ref.getYear());
214
		TaxonNameBase nameUsedInSource = source.getNameUsedInSource();
215
		assertNotNull("Name used in source should not be null", nameUsedInSource);
216
		assertEquals("Name used in source title should be ", "Abies", nameUsedInSource.getTitleCache());
217
		
218
		
219
		//synonym
220
		expectedText = "A synonym description";
221
		textData = getTextElement(description, expectedText);
222
		assertNotNull("The element should exists", textData);
223
		feature = textData.getFeature();
224
		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
225
		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
226
		language = textData.getMultilanguageText().keySet().iterator().next();
227
		assertEquals("Language should be Spanish", Language.SPANISH_CATALAN(), language);
228
		text = textData.getText(language);
229
		assertEquals("Unexpected description text", expectedText, text);
230
		assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
231
		source = textData.getSources().iterator().next();
232
		ref = source.getCitation();
233
		assertNotNull("Citation should not be null", ref);
234
		assertNotNull("AuthorTeam should not be null", ref.getAuthorTeam());
235
		assertEquals("Source author should be 'Theys, A.'", "Theys, A.",ref.getAuthorTeam().getTitleCache());
236
		assertEquals("Publication title should be 'The ultimate book'", "The ultimate book", ref.getTitle());
237
		assertEquals("Publication year should be '2011'", "2011", ref.getYear());
238
		nameUsedInSource = source.getNameUsedInSource();
239
		assertNotNull("Name used in source should not be null", nameUsedInSource);
240
		assertEquals("Name used in source title should be Pinus", "Pinus", nameUsedInSource.getTitleCache());
241
		
242
	}
243

    
244
	/**
245
	 * Returns description element for record id 1
246
	 * @param description
247
	 * @return
248
	 */
249
	private TextData getTextElement(TaxonDescription description, String descriptionText) {
250
		for (DescriptionElementBase element : description.getElements()){
251
			if (element.isInstanceOf(TextData.class)){
252
				TextData textData = CdmBase.deproxy(element, TextData.class);
253
				for (LanguageString ls :textData.getMultilanguageText().values()){
254
					if (ls.getText().equals(descriptionText)){
255
						return textData;
256
					}
257
				}
258
			}
259
		}
260
		return null;
261
	}
262

    
263
}
    (1-1/1)