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.common.URI;
36
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
37
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
38
import eu.etaxonomy.cdm.io.common.events.LoggingIoObserver;
39
import eu.etaxonomy.cdm.io.stream.excel.ExcelStreamImportConfigurator;
40
import eu.etaxonomy.cdm.model.common.Annotation;
41
import eu.etaxonomy.cdm.model.common.CdmBase;
42
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
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.Distribution;
47
import eu.etaxonomy.cdm.model.description.TaxonDescription;
48
import eu.etaxonomy.cdm.model.description.TextData;
49
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
50
import eu.etaxonomy.cdm.model.taxon.Classification;
51
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
52
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
53

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

    
62
	@SpringBeanByName
63
	CdmApplicationAwareDefaultImport<?> defaultImport;
64

    
65
	@SpringBeanByType
66
	INameService nameService;
67

    
68
	@SpringBeanByType
69
	ITaxonService taxonService;
70

    
71
	@SpringBeanByType
72
	ITermService termService;
73

    
74
	@SpringBeanByType
75
	IClassificationService classificationService;
76

    
77
	@SpringBeanByType
78
	IDescriptionService descriptionService;
79

    
80
	private IImportConfigurator configurator;
81
	private IImportConfigurator uuidConfigurator;
82

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

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

    
99
	}
100

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

    
107
	@Test
108
    @DataSets({
109
        @DataSet(/*loadStrategy=CleanSweepInsertLoadStrategy.class, */value="/eu/etaxonomy/cdm/database/ClearDBDataSet.xml"),
110
//        @DataSet( value="AbcdGgbnImportTest.testNoAttachDnaSampleToDerivedUnit.xml", loadStrategy=CleanSweepInsertLoadStrategy.class)
111
    })
112
	public void testDoInvoke() {
113
//		printDataSet(System.out, new String[]{"ANNOTATION"});
114
		boolean result = defaultImport.invoke(configurator).isSuccess();
115
		assertTrue("Return value for import.invoke should be true", result);
116
		commitAndStartNewTransaction(new String[]{"TAXONNAME", "ANNOTATION"});
117
		assertEquals("Number of TaxonNames should be 10", 10 /*TODO */, 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
		List<TaxonNode> rootNodes = tree.getChildNodes();
122
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
123
		TaxonNode rootNode = rootNodes.iterator().next();
124

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

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

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

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

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

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

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

    
289

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