Project

General

Profile

Download (18.1 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.taxa;
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.ArrayList;
20
import java.util.HashSet;
21
import java.util.Iterator;
22
import java.util.List;
23
import java.util.Set;
24
import java.util.UUID;
25

    
26
import org.apache.log4j.Logger;
27
import org.junit.Assert;
28
import org.junit.Before;
29
import org.junit.Test;
30
import org.unitils.dbunit.annotation.DataSet;
31
import org.unitils.dbunit.annotation.DataSets;
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.TaxonName;
52
import eu.etaxonomy.cdm.model.reference.Reference;
53
import eu.etaxonomy.cdm.model.taxon.Classification;
54
import eu.etaxonomy.cdm.model.taxon.Synonym;
55
import eu.etaxonomy.cdm.model.taxon.Taxon;
56
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
57
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
58
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
59

    
60
/**
61
 * @author a.mueller
62
 * @since 26.08.2009
63
 */
64
public class NormalExplicitImportTest extends CdmTransactionalIntegrationTest{
65
	@SuppressWarnings("unused")
66
	private static final Logger logger = Logger.getLogger(NormalExplicitImportTest.class);
67

    
68
	@SpringBeanByName
69
	CdmApplicationAwareDefaultImport<?> defaultImport;
70

    
71
	@SpringBeanByType
72
	INameService nameService;
73

    
74
	@SpringBeanByType
75
	ITaxonService taxonService;
76

    
77
	@SpringBeanByType
78
	ITermService termService;
79

    
80
	@SpringBeanByType
81
	IClassificationService classificationService;
82

    
83
	private IImportConfigurator configurator;
84
	private IImportConfigurator uuidConfigurator;
85
	private IImportConfigurator configuratorXslx;
86
	private IImportConfigurator configuratorTropicos;
87

    
88
	@Before
89
	public void setUp() throws URISyntaxException {
90
		String inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest-input.xls";
91
		URL url = this.getClass().getResource(inputFile);
92
	 	assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
93
		configurator = NormalExplicitImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
94
		assertNotNull("Configurator could not be created", configurator);
95

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

    
102
		String inputFileXslx = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest-input.xlsx";
103
		url = this.getClass().getResource(inputFileXslx);
104
	 	assertNotNull("URL for the test file '" + inputFileXslx + "' does not exist", url);
105
		configuratorXslx = NormalExplicitImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
106
		assertNotNull("Configurator could not be created", configuratorXslx);
107

    
108
		String inputFileTropicos = "/eu/etaxonomy/cdm/io/excel/taxa/ExcelTropicosImportExampleTest-input.xlsx";
109
        url = this.getClass().getResource(inputFileTropicos);
110
        assertNotNull("URL for the test file '" + inputFileTropicos + "' does not exist", url);
111
        configuratorTropicos = NormalExplicitImportConfigurator.NewInstance(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
112
        assertNotNull("Configurator could not be created", configuratorTropicos);
113
	}
114

    
115
	@Test
116
	public void testInit() {
117
		assertNotNull("normalExplicitImport should not be null", defaultImport);
118
		assertNotNull("nameService should not be null", nameService);
119
	}
120

    
121
	@Test
122
    @DataSets({
123
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
124
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
125
        @DataSet(value="NormalExplicitImportTest.xml")
126
    })
127
	public void testDoInvoke() {
128
		//printDataSet(System.out);
129
		boolean result = defaultImport.invoke(configurator).isSuccess();
130
		assertTrue("Return value for import.invoke should be true", result);
131
		assertEquals("Number of TaxonNames should be 9", 9, nameService.count(null));
132
		List<Classification> treeList = classificationService.list(null, null,null,null,null);
133
		assertEquals("Number of classifications should be 1", 1, treeList.size());
134
		Classification tree = treeList.get(0);
135
		List<TaxonNode> rootNodes = tree.getChildNodes();
136
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
137
		TaxonNode rootNode = rootNodes.iterator().next();
138
		assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
139
		TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
140
		assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
141
		TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
142
		TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
143
		TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
144
		TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
145
		assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
146

    
147
		Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
148
		TaxonNode childNode1 = it.next();
149
		TaxonNode childNode2 = it.next();
150

    
151
		TaxonNode noctuaPronubaNode;
152
		if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
153
			noctuaPronubaNode = childNode1;
154
		}else{
155
			noctuaPronubaNode = childNode2;
156
		}
157

    
158
		assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
159
		Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
160
		Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
161
		assertEquals("Number of synonyms should be 1", 1, synonyms.size());
162
		Synonym synonym = synonyms.iterator().next();
163
		assertEquals("Synonym name should be ", "Noctua atlantica", synonym.getName().getNameCache());
164
		Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
165
		Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
166
		TaxonDescription taxonDescription = descriptions.iterator().next();
167
		Set<DescriptionElementBase> elements = taxonDescription.getElements();
168
		List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
169
		for (DescriptionElementBase element : elements){
170
			if (element.isInstanceOf(CommonTaxonName.class)){
171
				commonNames.add((CommonTaxonName)element);
172
			}
173
		}
174
		Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
175
		Set<String> commonNameStrings = new HashSet<String>();
176
		commonNameStrings.add(commonNames.get(0).getName());
177
		commonNameStrings.add(commonNames.get(1).getName());
178
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
179
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
180
	}
181

    
182
	@Test
183
    @DataSets({
184
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
185
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
186
        @DataSet(value="NormalExplicitImportTest.xml")
187
    })
188
	public void testDoInvokeXslx() {
189
		//printDataSet(System.out);
190
		boolean result = defaultImport.invoke(configuratorXslx).isSuccess();
191
		assertTrue("Return value for import.invoke should be true", result);
192
		assertEquals("Number of TaxonNames should be 9", 9, nameService.count(null));
193
		List<Classification> treeList = classificationService.list(null, null,null,null,null);
194
		assertEquals("Number of classifications should be 1", 1, treeList.size());
195
		Classification tree = treeList.get(0);
196
		List<TaxonNode> rootNodes = tree.getChildNodes();
197
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
198
		TaxonNode rootNode = rootNodes.iterator().next();
199
		assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
200
		TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
201
		assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
202
		TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
203
		TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
204
		TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
205
		TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
206
		assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
207

    
208
		Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
209
		TaxonNode childNode1 = it.next();
210
		TaxonNode childNode2 = it.next();
211

    
212
		TaxonNode noctuaPronubaNode;
213
		if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
214
			noctuaPronubaNode = childNode1;
215
		}else{
216
			noctuaPronubaNode = childNode2;
217
		}
218

    
219
		assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
220
		Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
221
		Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
222
		assertEquals("Number of synonyms should be 1", 1, synonyms.size());
223
		Synonym synonym = synonyms.iterator().next();
224
		assertEquals("Synonym name should be ", "Noctua atlantica", synonym.getName().getNameCache());
225
		Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
226
		Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
227
		TaxonDescription taxonDescription = descriptions.iterator().next();
228
		Set<DescriptionElementBase> elements = taxonDescription.getElements();
229
		List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
230
		for (DescriptionElementBase element : elements){
231
			if (element.isInstanceOf(CommonTaxonName.class)){
232
				commonNames.add((CommonTaxonName)element);
233
			}
234
		}
235
		Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
236
		Set<String> commonNameStrings = new HashSet<String>();
237
		commonNameStrings.add(commonNames.get(0).getName());
238
		commonNameStrings.add(commonNames.get(1).getName());
239
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
240
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
241
	}
242

    
243
	@Test
244
//	@DataSet(value="NormalExplicitImportTest.testUuid.xml")
245
    @DataSets({
246
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
247
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
248
        @DataSet(value="NormalExplicitImportTest.testUuid.xml"),
249
    })
250
	public void testUUID(){
251
		UUID taxonUuid = UUID.fromString("aafce7fe-0c5f-42ed-814b-4c7c2c715660");
252
		UUID synonymUuid = UUID.fromString("fc4a995b-37a9-4984-afe6-e352c6c04d92");
253

    
254

    
255
		//test data set
256
		assertEquals("Number of taxon bases should be 2", 2, taxonService.count(null));
257
		Taxon taxon = (Taxon)taxonService.find(taxonUuid);
258
		assertNotNull("Taxon with given uuid should exist", taxon);
259
		assertEquals("Taxon should have no description", 0, taxon.getDescriptions().size());
260
		Synonym synonym = (Synonym)taxonService.find(synonymUuid);
261
		assertNotNull("Synonym with given uuid should exist", synonym);
262
		assertNotNull("Synonym should have 1 accepted taxon", synonym.getAcceptedTaxon());
263

    
264
		//import
265
		boolean result = defaultImport.invoke(uuidConfigurator).isSuccess();
266
		//test result
267
		assertTrue("Return value for import.invoke should be true", result);
268
		assertEquals("Number of taxon names should be 2", 2, nameService.count(null));
269
		assertEquals("Number of taxa should be 2", 2, taxonService.count(null));
270
		taxon = (Taxon)taxonService.find(taxonUuid);
271
		assertEquals("Taxon should have 1 description", 1, taxon.getDescriptions().size());
272
		TaxonDescription description = taxon.getDescriptions().iterator().next();
273
		assertEquals("Number of description elements should be 2", 2, description.getElements().size());
274

    
275
		String expectedText = "Description for the first taxon";
276
		TextData textData = getTextElement(description, expectedText);
277
		assertNotNull("The element should exists", textData);
278
		Feature feature = textData.getFeature();
279
		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
280
		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
281
		Language language = textData.getMultilanguageText().keySet().iterator().next();
282
		assertEquals("Language should be German", Language.GERMAN(), language);
283
		String text = textData.getText(language);
284
		assertEquals("Unexpected description text", expectedText, text);
285
		assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
286
		DescriptionElementSource source = textData.getSources().iterator().next();
287
		Reference ref = source.getCitation();
288
		assertNotNull("Citation should not be null", ref);
289
		assertNotNull("Authorship should not be null", ref.getAuthorship());
290
		assertEquals("Source author should be 'Meyer et. al.'", "Meyer et. al.",ref.getAuthorship().getTitleCache());
291
		assertEquals("Publication title should be 'My first book'", "My first book", ref.getTitle());
292
		assertEquals("Publication year should be '1987'", "1987", ref.getYear());
293
		TaxonName nameUsedInSource = source.getNameUsedInSource();
294
		assertNotNull("Name used in source should not be null", nameUsedInSource);
295
		assertEquals("Name used in source title should be ", "Abies", nameUsedInSource.getTitleCache());
296

    
297

    
298
		//synonym
299
		expectedText = "A synonym description";
300
		textData = getTextElement(description, expectedText);
301
		assertNotNull("The element should exists", textData);
302
		feature = textData.getFeature();
303
		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
304
		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
305
		language = textData.getMultilanguageText().keySet().iterator().next();
306
		assertEquals("Language should be Spanish", Language.SPANISH_CASTILIAN(), language);
307
		text = textData.getText(language);
308
		assertEquals("Unexpected description text", expectedText, text);
309
		assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
310
		source = textData.getSources().iterator().next();
311
		ref = source.getCitation();
312
		assertNotNull("Citation should not be null", ref);
313
		assertNotNull("Authorship should not be null", ref.getAuthorship());
314
		assertEquals("Source author should be 'Theys, A.'", "Theys, A.",ref.getAuthorship().getTitleCache());
315
		assertEquals("Publication title should be 'The ultimate book'", "The ultimate book", ref.getTitle());
316
		assertEquals("Publication year should be '2011'", "2011", ref.getYear());
317
		nameUsedInSource = source.getNameUsedInSource();
318
		assertNotNull("Name used in source should not be null", nameUsedInSource);
319
		assertEquals("Name used in source title should be Pinus", "Pinus", nameUsedInSource.getTitleCache());
320

    
321
	}
322

    
323
	/**
324
	 * Returns description element for record id 1
325
	 * @param description
326
	 * @return
327
	 */
328
	private TextData getTextElement(TaxonDescription description, String descriptionText) {
329
		for (DescriptionElementBase element : description.getElements()){
330
			if (element.isInstanceOf(TextData.class)){
331
				TextData textData = CdmBase.deproxy(element, TextData.class);
332
				for (LanguageString ls :textData.getMultilanguageText().values()){
333
					if (ls.getText().equals(descriptionText)){
334
						return textData;
335
					}
336
				}
337
			}
338
		}
339
		return null;
340
	}
341

    
342
    @Override
343
    public void createTestDataSet() throws FileNotFoundException {}
344

    
345
    @Test
346
    @DataSets({
347
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
348
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
349
        @DataSet(value="NormalExplicitImportTest.xml")
350
    })
351
    public void testDoInvokeTropicos() {
352
        //printDataSet(System.out);
353
        boolean result = defaultImport.invoke(configuratorTropicos).isSuccess();
354
        assertTrue("Return value for import.invoke should be true", result);
355
        int count = nameService.count(null);
356

    
357
        assertEquals("Number of TaxonNames should be 19", 19, count);
358
        int countTaxa = taxonService.count(Taxon.class);
359
        assertEquals("Number of Taxa should be 18", 18, countTaxa);
360
    }
361

    
362
}
    (1-1/1)