Project

General

Profile

Download (18 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
package eu.etaxonomy.cdm.io.excel.taxa;
10

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

    
15
import java.io.FileNotFoundException;
16
import java.net.URISyntaxException;
17
import java.net.URL;
18
import java.util.ArrayList;
19
import java.util.HashSet;
20
import java.util.Iterator;
21
import java.util.List;
22
import java.util.Set;
23
import java.util.UUID;
24

    
25
import org.apache.log4j.Logger;
26
import org.junit.Assert;
27
import org.junit.Before;
28
import org.junit.Test;
29
import org.unitils.dbunit.annotation.DataSet;
30
import org.unitils.dbunit.annotation.DataSets;
31
import org.unitils.spring.annotation.SpringBeanByName;
32
import org.unitils.spring.annotation.SpringBeanByType;
33

    
34
import eu.etaxonomy.cdm.api.service.IClassificationService;
35
import eu.etaxonomy.cdm.api.service.INameService;
36
import eu.etaxonomy.cdm.api.service.ITaxonService;
37
import eu.etaxonomy.cdm.api.service.ITermService;
38
import eu.etaxonomy.cdm.common.URI;
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

    
66
    @SuppressWarnings("unused")
67
	private static final Logger logger = Logger.getLogger(NormalExplicitImportTest.class);
68

    
69
	@SpringBeanByName
70
	CdmApplicationAwareDefaultImport<?> defaultImport;
71

    
72
	@SpringBeanByType
73
	INameService nameService;
74

    
75
	@SpringBeanByType
76
	ITaxonService taxonService;
77

    
78
	@SpringBeanByType
79
	ITermService termService;
80

    
81
	@SpringBeanByType
82
	IClassificationService classificationService;
83

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
296

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

    
320
	}
321

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

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

    
351
        assertEquals("Number of TaxonNames should be 19", 19, count);
352
        int countTaxa = taxonService.count(Taxon.class);
353
        assertEquals("Number of Taxa should be 18", 18, countTaxa);
354
    }
355

    
356
    @Override
357
    public void createTestDataSet() throws FileNotFoundException {}
358

    
359
}
    (1-1/1)