Project

General

Profile

Download (13.8 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.stream;
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.List;
20
import java.util.Set;
21

    
22
import org.apache.log4j.Logger;
23
import org.junit.Before;
24
import org.junit.Test;
25
import org.unitils.dbunit.annotation.DataSet;
26
import org.unitils.spring.annotation.SpringBeanByName;
27
import org.unitils.spring.annotation.SpringBeanByType;
28

    
29
import eu.etaxonomy.cdm.api.service.IClassificationService;
30
import eu.etaxonomy.cdm.api.service.IDescriptionService;
31
import eu.etaxonomy.cdm.api.service.INameService;
32
import eu.etaxonomy.cdm.api.service.ITaxonService;
33
import eu.etaxonomy.cdm.api.service.ITermService;
34
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
35
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
36
import eu.etaxonomy.cdm.io.common.events.LoggingIoObserver;
37
import eu.etaxonomy.cdm.model.common.Annotation;
38
import eu.etaxonomy.cdm.model.common.CdmBase;
39
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
40
import eu.etaxonomy.cdm.model.common.LanguageString;
41
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
42
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
43
import eu.etaxonomy.cdm.model.description.Distribution;
44
import eu.etaxonomy.cdm.model.description.TaxonDescription;
45
import eu.etaxonomy.cdm.model.description.TextData;
46
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
47
import eu.etaxonomy.cdm.model.taxon.Classification;
48
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
49
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
50

    
51
/**
52
 * @author a.mueller
53
 * @created 24.05.2013
54
 */
55
public class ExcelStreamImportTest extends CdmTransactionalIntegrationTest{
56
	@SuppressWarnings("unused")
57
	private static final Logger logger = Logger.getLogger(ExcelStreamImportTest.class);
58

    
59
	@SpringBeanByName
60
	CdmApplicationAwareDefaultImport defaultImport;
61

    
62
	@SpringBeanByType
63
	INameService nameService;
64
	
65
	@SpringBeanByType
66
	ITaxonService taxonService;
67
	
68
	@SpringBeanByType
69
	ITermService termService;
70
	
71
	@SpringBeanByType
72
	IClassificationService classificationService;
73
	
74
	@SpringBeanByType
75
	IDescriptionService descriptionService;
76

    
77
	private IImportConfigurator configurator;
78
	private IImportConfigurator uuidConfigurator;
79
	
80
	@Before
81
	public void setUp() throws URISyntaxException {
82
		//TODO create own test file
83
		String inputFile = "/eu/etaxonomy/cdm/io/excel/stream/ExcelStreamImport-TestInput.xls";
84
		URL url = this.getClass().getResource(inputFile);
85
	 	assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
86
		configurator = ExcelStreamImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
87
		assertNotNull("Configurator could not be created", configurator);
88
		configurator.addObserver(new LoggingIoObserver());
89
		
90
		inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest.testUuid-input.xls";
91
		url = this.getClass().getResource(inputFile);
92
	 	assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
93
		uuidConfigurator = ExcelStreamImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
94
		assertNotNull("Configurator could not be created", configurator);
95
		
96
	}
97
	
98
	@Test
99
	public void testInit() {
100
		assertNotNull("normalExplicitImport should not be null", defaultImport);
101
		assertNotNull("nameService should not be null", nameService);
102
	}
103
	
104
	@Test
105
	@DataSet
106
	public void testDoInvoke() {
107
//		printDataSet(System.out, new String[]{"ANNOTATION"});
108
		boolean result = defaultImport.invoke(configurator);
109
		assertTrue("Return value for import.invoke should be true", result);
110
		commitAndStartNewTransaction(new String[]{"TAXONNAMEBASE", "ANNOTATION"});
111
		assertEquals("Number of TaxonNames should be 10", 10 /*TODO */, nameService.count(null));
112
		List<Classification> treeList = classificationService.list(null, null,null,null,null);
113
		assertEquals("Number of classifications should be 1", 1, treeList.size());
114
		Classification tree = treeList.get(0);
115
		List<TaxonNode> rootNodes = tree.getChildNodes();
116
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
117
		TaxonNode rootNode = rootNodes.iterator().next();
118
		
119
//		printDataSet(System.out, new String[]{"ANNOTATION"});
120
		Set<Annotation> annotationSet = rootNode.getTaxon().getAnnotations();
121
		assertTrue("Annotation is set for first Taxon", !annotationSet.isEmpty()); 
122

    
123
		assertEquals("Root taxon name should be Metazoa", "Metazoa", rootNode.getTaxon().getName().getTitleCache());
124
		TaxonNode secondTaxon = rootNode.getChildNodes().iterator().next();
125

    
126
		Set<Annotation> secondAnnotation = secondTaxon.getTaxon().getAnnotations();
127
		assertTrue("Annotation is empty for second Taxon", secondAnnotation.isEmpty()); 
128
		
129
		Set<TaxonDescription> taxonSet = secondTaxon.getTaxon().getDescriptions();
130
		assertTrue("Description is not Empty", !taxonSet.isEmpty());
131
		assertEquals("Number of Distributions should be 7", 7, descriptionService.listDescriptionElements(null, null, Distribution.class, null, null, null).size());
132
//		assertEquals("Number of Vernecular Names should be 7", 6, descriptionService.listDescriptionElements(null, null, CommonTaxonName.class, null, null, null).size());
133
		List<DescriptionElementBase> list = descriptionService.listDescriptionElements(null, null, CommonTaxonName.class, null, null, null);
134
		for(DescriptionElementBase db : list){
135
			System.out.println(db.toString());
136
		}
137
		//TODO: write test for Refences Sources
138
		Set<IdentifiableSource> sourcesSet = secondTaxon.getTaxon().getSources();
139
		assertTrue("Sources are set for second Taxon", !sourcesSet.isEmpty());
140
		for(IdentifiableSource s : sourcesSet){
141
			System.out.println("ID: "+ s.getId());
142
			System.out.println("External ID: " +s.getIdInSource());
143
			System.out.println("Namespace: " + s.getIdNamespace());
144
			System.out.println("Reference: " + s.getCitation().toString());
145
		}
146
		
147
		//		assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
148
//		TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
149
//		TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
150
//		TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
151
//		TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
152
//		assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
153
//		
154
//		Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
155
//		TaxonNode childNode1 = it.next();
156
//		TaxonNode childNode2 = it.next();
157
//		
158
//		TaxonNode noctuaPronubaNode;
159
//		if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
160
//			noctuaPronubaNode = childNode1;
161
//		}else{
162
//			noctuaPronubaNode = childNode2;
163
//		}
164
//		
165
//		assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
166
//		Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
167
//		Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
168
//		assertEquals("Number of synonyms should be 1", 3 /*TODO*/, synonyms.size());
169
//		Synonym synonym = synonyms.iterator().next();
170
//		assertEquals("Synonym name should be ", "Noctua atlantica", (CdmBase.deproxy(synonym.getName(), NonViralName.class )).getNameCache());
171
//		Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
172
//		Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
173
//		TaxonDescription taxonDescription = descriptions.iterator().next();
174
//		Set<DescriptionElementBase> elements = taxonDescription.getElements();
175
//		List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
176
//		for (DescriptionElementBase element : elements){
177
//			if (element.isInstanceOf(CommonTaxonName.class)){
178
//				commonNames.add((CommonTaxonName)element);
179
//			}
180
//		}
181
//		Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
182
//		Set<String> commonNameStrings = new HashSet<String>();
183
//		commonNameStrings.add(commonNames.get(0).getName());
184
//		commonNameStrings.add(commonNames.get(1).getName());
185
//		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
186
//		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
187
	}
188
	
189
//	@Test
190
//	@DataSet(value="NormalExplicitImportTest.testUuid.xml")
191
//	public void testUUID() throws URISyntaxException{
192
//		UUID taxonUuid = UUID.fromString("aafce7fe-0c5f-42ed-814b-4c7c2c715660");
193
//		UUID synonymUuid = UUID.fromString("fc4a995b-37a9-4984-afe6-e352c6c04d92");
194
//		
195
//		
196
//		//test data set
197
//		assertEquals("Number of taxon bases should be 2", 2, taxonService.count(null));
198
//		Taxon taxon = (Taxon)taxonService.find(taxonUuid);
199
//		assertNotNull("Taxon with given uuid should exist", taxon);
200
//		assertEquals("Taxon should have no description", 0, taxon.getDescriptions().size());
201
//		Synonym synonym = (Synonym)taxonService.find(synonymUuid);
202
//		assertNotNull("Synonym with given uuid should exist", synonym);
203
//		assertEquals("Synonym should have 1 accepted taxon", 1, synonym.getAcceptedTaxa().size());
204
//		
205
//		//import
206
//		boolean result = defaultImport.invoke(uuidConfigurator);
207
//		//test result
208
//		assertTrue("Return value for import.invoke should be true", result);
209
//		assertEquals("Number of taxon names should be 2", 2, nameService.count(null));
210
//		assertEquals("Number of taxa should be 2", 2, taxonService.count(null));
211
//		taxon = (Taxon)taxonService.find(taxonUuid);
212
//		assertEquals("Taxon should have 1 description", 1, taxon.getDescriptions().size());
213
//		TaxonDescription description = taxon.getDescriptions().iterator().next();
214
//		assertEquals("Number of description elements should be 2", 2, description.getElements().size());
215
//		
216
//		String expectedText = "Description for the first taxon";
217
//		TextData textData = getTextElement(description, expectedText);
218
//		assertNotNull("The element should exists", textData);
219
//		Feature feature = textData.getFeature();
220
//		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
221
//		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
222
//		Language language = textData.getMultilanguageText().keySet().iterator().next();
223
//		assertEquals("Language should be German", Language.GERMAN(), language);
224
//		String 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
//		DescriptionElementSource source = textData.getSources().iterator().next();
228
//		Reference ref = source.getCitation();
229
//		assertNotNull("Citation should not be null", ref);
230
//		assertNotNull("Authorship should not be null", ref.getAuthorship());
231
//		assertEquals("Source author should be 'Meyer et. al.'", "Meyer et. al.",ref.getAuthorTeam().getTitleCache());
232
//		assertEquals("Publication title should be 'My first book'", "My first book", ref.getTitle());
233
//		assertEquals("Publication year should be '1987'", "1987", ref.getYear());
234
//		TaxonNameBase nameUsedInSource = source.getNameUsedInSource();
235
//		assertNotNull("Name used in source should not be null", nameUsedInSource);
236
//		assertEquals("Name used in source title should be ", "Abies", nameUsedInSource.getTitleCache());
237
//		
238
//		
239
//		//synonym
240
//		expectedText = "A synonym description";
241
//		textData = getTextElement(description, expectedText);
242
//		assertNotNull("The element should exists", textData);
243
//		feature = textData.getFeature();
244
//		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
245
//		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
246
//		language = textData.getMultilanguageText().keySet().iterator().next();
247
//		assertEquals("Language should be Spanish", Language.SPANISH_CASTILIAN(), language);
248
//		text = textData.getText(language);
249
//		assertEquals("Unexpected description text", expectedText, text);
250
//		assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
251
//		source = textData.getSources().iterator().next();
252
//		ref = source.getCitation();
253
//		assertNotNull("Citation should not be null", ref);
254
//		assertNotNull("Authorship should not be null", ref.getAuthorship());
255
//		assertEquals("Source author should be 'Theys, A.'", "Theys, A.",ref.getAuthorTeam().getTitleCache());
256
//		assertEquals("Publication title should be 'The ultimate book'", "The ultimate book", ref.getTitle());
257
//		assertEquals("Publication year should be '2011'", "2011", ref.getYear());
258
//		nameUsedInSource = source.getNameUsedInSource();
259
//		assertNotNull("Name used in source should not be null", nameUsedInSource);
260
//		assertEquals("Name used in source title should be Pinus", "Pinus", nameUsedInSource.getTitleCache());
261
//		
262
//	}
263

    
264
	/**
265
	 * Returns description element for record id 1
266
	 * @param description
267
	 * @return
268
	 */
269
	private TextData getTextElement(TaxonDescription description, String descriptionText) {
270
		for (DescriptionElementBase element : description.getElements()){
271
			if (element.isInstanceOf(TextData.class)){
272
				TextData textData = CdmBase.deproxy(element, TextData.class);
273
				for (LanguageString ls :textData.getMultilanguageText().values()){
274
					if (ls.getText().equals(descriptionText)){
275
						return textData;
276
					}
277
				}
278
			}
279
		}
280
		return null;
281
	}
282

    
283
}
    (1-1/1)