Project

General

Profile

Download (12.4 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.io.FileNotFoundException;
18
import java.net.URISyntaxException;
19
import java.net.URL;
20
import java.util.ArrayList;
21
import java.util.HashSet;
22
import java.util.Iterator;
23
import java.util.List;
24
import java.util.Set;
25
import java.util.UUID;
26

    
27
import org.apache.log4j.Logger;
28
import org.junit.Assert;
29
import org.junit.Before;
30
import org.junit.Test;
31
import org.unitils.dbunit.annotation.DataSet;
32
import org.unitils.spring.annotation.SpringBeanByName;
33
import org.unitils.spring.annotation.SpringBeanByType;
34

    
35
import eu.etaxonomy.cdm.api.service.IClassificationService;
36
import eu.etaxonomy.cdm.api.service.INameService;
37
import eu.etaxonomy.cdm.api.service.ITaxonService;
38
import eu.etaxonomy.cdm.api.service.ITermService;
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.NonViralName;
52
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
53
import eu.etaxonomy.cdm.model.reference.Reference;
54
import eu.etaxonomy.cdm.model.taxon.Classification;
55
import eu.etaxonomy.cdm.model.taxon.Synonym;
56
import eu.etaxonomy.cdm.model.taxon.Taxon;
57
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
58
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
59

    
60
/**
61
 * @author a.mueller
62
 * @created 26.08.2009
63
 * @version 1.0
64
 */
65
public class NormalExplicitImportTest extends CdmTransactionalIntegrationTest{
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
	
87
	@Before
88
	public void setUp() throws URISyntaxException {
89
		String inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest-input.xls";
90
		URL url = this.getClass().getResource(inputFile);
91
	 	assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
92
		configurator = NormalExplicitImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
93
		assertNotNull("Configurator could not be created", configurator);
94
		
95
		inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest.testUuid-input.xls";
96
		url = this.getClass().getResource(inputFile);
97
	 	assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
98
		uuidConfigurator = NormalExplicitImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
99
		assertNotNull("Configurator could be created", configurator);
100
		
101
	}
102
	
103
	@Test
104
	public void testInit() {
105
		assertNotNull("normalExplicitImport should not be null", defaultImport);
106
		assertNotNull("nameService should not be null", nameService);
107
	}
108
	
109
	@Test
110
	@DataSet
111
	public void testDoInvoke() {
112
		//printDataSet(System.out);
113
		boolean result = defaultImport.invoke(configurator);
114
		assertTrue("Return value for import.invoke should be true", result);
115
		assertEquals("Number of TaxonNames should be 9", 9, nameService.count(null));
116
		List<Classification> treeList = classificationService.list(null, null,null,null,null);
117
		assertEquals("Number of classifications should be 1", 1, treeList.size());
118
		Classification tree = treeList.get(0);
119
		List<TaxonNode> rootNodes = tree.getChildNodes();
120
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
121
		TaxonNode rootNode = rootNodes.iterator().next();
122
		assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
123
		TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
124
		assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
125
		TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
126
		TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
127
		TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
128
		TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
129
		assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
130
		
131
		Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
132
		TaxonNode childNode1 = it.next();
133
		TaxonNode childNode2 = it.next();
134
		
135
		TaxonNode noctuaPronubaNode;
136
		if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
137
			noctuaPronubaNode = childNode1;
138
		}else{
139
			noctuaPronubaNode = childNode2;
140
		}
141
		
142
		assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
143
		Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
144
		Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
145
		assertEquals("Number of synonyms should be 1", 1, synonyms.size());
146
		Synonym synonym = synonyms.iterator().next();
147
		assertEquals("Synonym name should be ", "Noctua atlantica", ((NonViralName<?>)synonym.getName()).getNameCache());
148
		Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
149
		Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
150
		TaxonDescription taxonDescription = descriptions.iterator().next();
151
		Set<DescriptionElementBase> elements = taxonDescription.getElements();
152
		List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
153
		for (DescriptionElementBase element : elements){
154
			if (element.isInstanceOf(CommonTaxonName.class)){
155
				commonNames.add((CommonTaxonName)element);
156
			}
157
		}
158
		Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
159
		Set<String> commonNameStrings = new HashSet<String>();
160
		commonNameStrings.add(commonNames.get(0).getName());
161
		commonNameStrings.add(commonNames.get(1).getName());
162
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
163
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
164
	}
165
	
166
	@Test
167
	@DataSet(value="NormalExplicitImportTest.testUuid.xml")
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
		String expectedText = "Description for the first taxon";
194
		TextData textData = getTextElement(description, expectedText);
195
		assertNotNull("The element should exists", textData);
196
		Feature feature = textData.getFeature();
197
		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
198
		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
199
		Language language = textData.getMultilanguageText().keySet().iterator().next();
200
		assertEquals("Language should be German", Language.GERMAN(), language);
201
		String text = textData.getText(language);
202
		assertEquals("Unexpected description text", expectedText, text);
203
		assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
204
		DescriptionElementSource source = textData.getSources().iterator().next();
205
		Reference<?> ref = source.getCitation();
206
		assertNotNull("Citation should not be null", ref);
207
		assertNotNull("Authorship should not be null", ref.getAuthorship());
208
		assertEquals("Source author should be 'Meyer et. al.'", "Meyer et. al.",ref.getAuthorship().getTitleCache());
209
		assertEquals("Publication title should be 'My first book'", "My first book", ref.getTitle());
210
		assertEquals("Publication year should be '1987'", "1987", ref.getYear());
211
		TaxonNameBase<?,?> nameUsedInSource = source.getNameUsedInSource();
212
		assertNotNull("Name used in source should not be null", nameUsedInSource);
213
		assertEquals("Name used in source title should be ", "Abies", nameUsedInSource.getTitleCache());
214
		
215
		
216
		//synonym
217
		expectedText = "A synonym description";
218
		textData = getTextElement(description, expectedText);
219
		assertNotNull("The element should exists", textData);
220
		feature = textData.getFeature();
221
		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
222
		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
223
		language = textData.getMultilanguageText().keySet().iterator().next();
224
		assertEquals("Language should be Spanish", Language.SPANISH_CASTILIAN(), language);
225
		text = textData.getText(language);
226
		assertEquals("Unexpected description text", expectedText, text);
227
		assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
228
		source = textData.getSources().iterator().next();
229
		ref = source.getCitation();
230
		assertNotNull("Citation should not be null", ref);
231
		assertNotNull("Authorship should not be null", ref.getAuthorship());
232
		assertEquals("Source author should be 'Theys, A.'", "Theys, A.",ref.getAuthorship().getTitleCache());
233
		assertEquals("Publication title should be 'The ultimate book'", "The ultimate book", ref.getTitle());
234
		assertEquals("Publication year should be '2011'", "2011", ref.getYear());
235
		nameUsedInSource = source.getNameUsedInSource();
236
		assertNotNull("Name used in source should not be null", nameUsedInSource);
237
		assertEquals("Name used in source title should be Pinus", "Pinus", nameUsedInSource.getTitleCache());
238
		
239
	}
240

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

    
260
    /* (non-Javadoc)
261
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
262
     */
263
    @Override
264
    public void createTestDataSet() throws FileNotFoundException {
265
        // TODO Auto-generated method stub
266
        
267
    }
268

    
269
}
    (1-1/1)