Project

General

Profile

Download (14 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.io.excel.stream;
11

    
12
import static org.junit.Assert.assertEquals;
13
import static org.junit.Assert.assertNotNull;
14
import static org.junit.Assert.assertTrue;
15

    
16
import java.io.FileNotFoundException;
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.dbunit.annotation.DataSets;
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.io.stream.excel.ExcelStreamImportConfigurator;
39
import eu.etaxonomy.cdm.model.common.Annotation;
40
import eu.etaxonomy.cdm.model.common.CdmBase;
41
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
42
import eu.etaxonomy.cdm.model.common.LanguageString;
43
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
44
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
45
import eu.etaxonomy.cdm.model.description.Distribution;
46
import eu.etaxonomy.cdm.model.description.TaxonDescription;
47
import eu.etaxonomy.cdm.model.description.TextData;
48
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
49
import eu.etaxonomy.cdm.model.taxon.Classification;
50
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
51
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
52

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

    
61
	@SpringBeanByName
62
	CdmApplicationAwareDefaultImport<?> defaultImport;
63

    
64
	@SpringBeanByType
65
	INameService nameService;
66

    
67
	@SpringBeanByType
68
	ITaxonService taxonService;
69

    
70
	@SpringBeanByType
71
	ITermService termService;
72

    
73
	@SpringBeanByType
74
	IClassificationService classificationService;
75

    
76
	@SpringBeanByType
77
	IDescriptionService descriptionService;
78

    
79
	private IImportConfigurator configurator;
80
	private IImportConfigurator uuidConfigurator;
81

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

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

    
98
	}
99

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

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

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

    
128
		assertEquals("Root taxon name should be Metazoa", "Metazoa", rootNode.getTaxon().getName().getTitleCache());
129
		TaxonNode secondTaxon = rootNode.getChildNodes().iterator().next();
130

    
131
		Set<Annotation> secondAnnotation = secondTaxon.getTaxon().getAnnotations();
132
		assertTrue("Annotation is empty for second Taxon", secondAnnotation.isEmpty());
133

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

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

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

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

    
288

    
289
    @Override
290
    public void createTestDataSet() throws FileNotFoundException {}
291
}
    (1-1/1)