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.io.FileNotFoundException;
18
import java.net.URISyntaxException;
19
import java.net.URL;
20
import java.util.List;
21
import java.util.Set;
22

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

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

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

    
60
	@SpringBeanByName
61
	CdmApplicationAwareDefaultImport defaultImport;
62

    
63
	@SpringBeanByType
64
	INameService nameService;
65

    
66
	@SpringBeanByType
67
	ITaxonService taxonService;
68

    
69
	@SpringBeanByType
70
	ITermService termService;
71

    
72
	@SpringBeanByType
73
	IClassificationService classificationService;
74

    
75
	@SpringBeanByType
76
	IDescriptionService descriptionService;
77

    
78
	private IImportConfigurator configurator;
79
	private IImportConfigurator uuidConfigurator;
80

    
81
	@Before
82
	public void setUp() throws URISyntaxException {
83
		//TODO create own test file
84
		String inputFile = "/eu/etaxonomy/cdm/io/excel/stream/ExcelStreamImport-TestInput.xls";
85
		URL url = this.getClass().getResource(inputFile);
86
	 	assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
87
		configurator = ExcelStreamImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
88
		assertNotNull("Configurator could not be created", configurator);
89
		configurator.addObserver(new LoggingIoObserver());
90

    
91
		inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest.testUuid-input.xls";
92
		url = this.getClass().getResource(inputFile);
93
	 	assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
94
		uuidConfigurator = ExcelStreamImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
95
		assertNotNull("Configurator could not be created", configurator);
96

    
97
	}
98

    
99
	@Test
100
	public void testInit() {
101
		assertNotNull("normalExplicitImport should not be null", defaultImport);
102
		assertNotNull("nameService should not be null", nameService);
103
	}
104

    
105
	@Test
106
	@DataSet
107
	public void testDoInvoke() {
108
//		printDataSet(System.out, new String[]{"ANNOTATION"});
109
		boolean result = defaultImport.invoke(configurator).isSuccess();
110
		assertTrue("Return value for import.invoke should be true", result);
111
		commitAndStartNewTransaction(new String[]{"TAXONNAMEBASE", "ANNOTATION"});
112
		assertEquals("Number of TaxonNames should be 10", 10 /*TODO */, nameService.count(null));
113
		List<Classification> treeList = classificationService.list(null, null,null,null,null);
114
		assertEquals("Number of classifications should be 1", 1, treeList.size());
115
		Classification tree = treeList.get(0);
116
		List<TaxonNode> rootNodes = tree.getChildNodes();
117
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
118
		TaxonNode rootNode = rootNodes.iterator().next();
119

    
120
//		printDataSet(System.out, new String[]{"ANNOTATION"});
121
		Set<Annotation> annotationSet = rootNode.getTaxon().getAnnotations();
122
		assertTrue("Annotation is set for first Taxon", !annotationSet.isEmpty());
123

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

    
127
		Set<Annotation> secondAnnotation = secondTaxon.getTaxon().getAnnotations();
128
		assertTrue("Annotation is empty for second Taxon", secondAnnotation.isEmpty());
129

    
130
		Set<TaxonDescription> taxonSet = secondTaxon.getTaxon().getDescriptions();
131
		assertTrue("Description is not Empty", !taxonSet.isEmpty());
132
		assertEquals("Number of Distributions should be 7", 7, descriptionService.listDescriptionElements(null, null, Distribution.class, null, null, null).size());
133
//		assertEquals("Number of Vernecular Names should be 7", 6, descriptionService.listDescriptionElements(null, null, CommonTaxonName.class, null, null, null).size());
134
		List<CommonTaxonName> list = descriptionService.listDescriptionElements(null, null, CommonTaxonName.class, null, null, null);
135
		for(DescriptionElementBase db : list){
136
			System.out.println(db.toString());
137
		}
138
		//TODO: write test for Refences Sources
139
		Set<IdentifiableSource> sourcesSet = secondTaxon.getTaxon().getSources();
140
		assertTrue("Sources are set for second Taxon", !sourcesSet.isEmpty());
141
		for(IdentifiableSource s : sourcesSet){
142
			System.out.println("ID: "+ s.getId());
143
			System.out.println("External ID: " +s.getIdInSource());
144
			System.out.println("Namespace: " + s.getIdNamespace());
145
			System.out.println("Reference: " + s.getCitation().toString());
146
		}
147

    
148
		//		assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
149
//		TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
150
//		TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
151
//		TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
152
//		TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
153
//		assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
154
//
155
//		Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
156
//		TaxonNode childNode1 = it.next();
157
//		TaxonNode childNode2 = it.next();
158
//
159
//		TaxonNode noctuaPronubaNode;
160
//		if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
161
//			noctuaPronubaNode = childNode1;
162
//		}else{
163
//			noctuaPronubaNode = childNode2;
164
//		}
165
//
166
//		assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
167
//		Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
168
//		Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
169
//		assertEquals("Number of synonyms should be 1", 3 /*TODO*/, synonyms.size());
170
//		Synonym synonym = synonyms.iterator().next();
171
//		assertEquals("Synonym name should be ", "Noctua atlantica", (CdmBase.deproxy(synonym.getName(), NonViralName.class )).getNameCache());
172
//		Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
173
//		Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
174
//		TaxonDescription taxonDescription = descriptions.iterator().next();
175
//		Set<DescriptionElementBase> elements = taxonDescription.getElements();
176
//		List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
177
//		for (DescriptionElementBase element : elements){
178
//			if (element.isInstanceOf(CommonTaxonName.class)){
179
//				commonNames.add((CommonTaxonName)element);
180
//			}
181
//		}
182
//		Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
183
//		Set<String> commonNameStrings = new HashSet<String>();
184
//		commonNameStrings.add(commonNames.get(0).getName());
185
//		commonNameStrings.add(commonNames.get(1).getName());
186
//		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
187
//		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
188
	}
189

    
190
//	@Test
191
//	@DataSet(value="NormalExplicitImportTest.testUuid.xml")
192
//	public void testUUID() throws URISyntaxException{
193
//		UUID taxonUuid = UUID.fromString("aafce7fe-0c5f-42ed-814b-4c7c2c715660");
194
//		UUID synonymUuid = UUID.fromString("fc4a995b-37a9-4984-afe6-e352c6c04d92");
195
//
196
//
197
//		//test data set
198
//		assertEquals("Number of taxon bases should be 2", 2, taxonService.count(null));
199
//		Taxon taxon = (Taxon)taxonService.find(taxonUuid);
200
//		assertNotNull("Taxon with given uuid should exist", taxon);
201
//		assertEquals("Taxon should have no description", 0, taxon.getDescriptions().size());
202
//		Synonym synonym = (Synonym)taxonService.find(synonymUuid);
203
//		assertNotNull("Synonym with given uuid should exist", synonym);
204
//		assertEquals("Synonym should have 1 accepted taxon", 1, synonym.getAcceptedTaxa().size());
205
//
206
//		//import
207
//		boolean result = defaultImport.invoke(uuidConfigurator);
208
//		//test result
209
//		assertTrue("Return value for import.invoke should be true", result);
210
//		assertEquals("Number of taxon names should be 2", 2, nameService.count(null));
211
//		assertEquals("Number of taxa should be 2", 2, taxonService.count(null));
212
//		taxon = (Taxon)taxonService.find(taxonUuid);
213
//		assertEquals("Taxon should have 1 description", 1, taxon.getDescriptions().size());
214
//		TaxonDescription description = taxon.getDescriptions().iterator().next();
215
//		assertEquals("Number of description elements should be 2", 2, description.getElements().size());
216
//
217
//		String expectedText = "Description for the first taxon";
218
//		TextData textData = getTextElement(description, expectedText);
219
//		assertNotNull("The element should exists", textData);
220
//		Feature feature = textData.getFeature();
221
//		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
222
//		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
223
//		Language language = textData.getMultilanguageText().keySet().iterator().next();
224
//		assertEquals("Language should be German", Language.GERMAN(), language);
225
//		String 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
//		DescriptionElementSource source = textData.getSources().iterator().next();
229
//		Reference 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 'Meyer et. al.'", "Meyer et. al.",ref.getAuthorship().getTitleCache());
233
//		assertEquals("Publication title should be 'My first book'", "My first book", ref.getTitle());
234
//		assertEquals("Publication year should be '1987'", "1987", ref.getYear());
235
//		TaxonNameBase nameUsedInSource = source.getNameUsedInSource();
236
//		assertNotNull("Name used in source should not be null", nameUsedInSource);
237
//		assertEquals("Name used in source title should be ", "Abies", nameUsedInSource.getTitleCache());
238
//
239
//
240
//		//synonym
241
//		expectedText = "A synonym description";
242
//		textData = getTextElement(description, expectedText);
243
//		assertNotNull("The element should exists", textData);
244
//		feature = textData.getFeature();
245
//		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
246
//		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
247
//		language = textData.getMultilanguageText().keySet().iterator().next();
248
//		assertEquals("Language should be Spanish", Language.SPANISH_CASTILIAN(), language);
249
//		text = textData.getText(language);
250
//		assertEquals("Unexpected description text", expectedText, text);
251
//		assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
252
//		source = textData.getSources().iterator().next();
253
//		ref = source.getCitation();
254
//		assertNotNull("Citation should not be null", ref);
255
//		assertNotNull("Authorship should not be null", ref.getAuthorship());
256
//		assertEquals("Source author should be 'Theys, A.'", "Theys, A.",ref.getAuthorship().getTitleCache());
257
//		assertEquals("Publication title should be 'The ultimate book'", "The ultimate book", ref.getTitle());
258
//		assertEquals("Publication year should be '2011'", "2011", ref.getYear());
259
//		nameUsedInSource = source.getNameUsedInSource();
260
//		assertNotNull("Name used in source should not be null", nameUsedInSource);
261
//		assertEquals("Name used in source title should be Pinus", "Pinus", nameUsedInSource.getTitleCache());
262
//
263
//	}
264

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

    
284
    /* (non-Javadoc)
285
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
286
     */
287
    @Override
288
    public void createTestDataSet() throws FileNotFoundException {
289
        // TODO Auto-generated method stub
290

    
291
    }
292

    
293
}
    (1-1/1)