Project

General

Profile

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

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

    
36
import eu.etaxonomy.cdm.api.service.IClassificationService;
37
import eu.etaxonomy.cdm.api.service.INameService;
38
import eu.etaxonomy.cdm.api.service.ITaxonService;
39
import eu.etaxonomy.cdm.api.service.ITermService;
40
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
41
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
42
import eu.etaxonomy.cdm.model.common.CdmBase;
43
import eu.etaxonomy.cdm.model.common.Language;
44
import eu.etaxonomy.cdm.model.common.LanguageString;
45
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
46
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
47
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
48
import eu.etaxonomy.cdm.model.description.Feature;
49
import eu.etaxonomy.cdm.model.description.TaxonDescription;
50
import eu.etaxonomy.cdm.model.description.TextData;
51
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
52
import eu.etaxonomy.cdm.model.name.NonViralName;
53
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
54
import eu.etaxonomy.cdm.model.reference.Reference;
55
import eu.etaxonomy.cdm.model.taxon.Classification;
56
import eu.etaxonomy.cdm.model.taxon.Synonym;
57
import eu.etaxonomy.cdm.model.taxon.Taxon;
58
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
59
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
60
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
61

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

    
70
	@SpringBeanByName
71
	CdmApplicationAwareDefaultImport<?> defaultImport;
72

    
73
	@SpringBeanByType
74
	INameService nameService;
75

    
76
	@SpringBeanByType
77
	ITaxonService taxonService;
78

    
79
	@SpringBeanByType
80
	ITermService termService;
81

    
82
	@SpringBeanByType
83
	IClassificationService classificationService;
84

    
85
	private IImportConfigurator configurator;
86
	private IImportConfigurator uuidConfigurator;
87
	private IImportConfigurator configuratorXslx;
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(url.toURI(), 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(url.toURI(), 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(url.toURI(), null, NomenclaturalCode.ICNAFP, null);
107
		assertNotNull("Configurator could not be created", configuratorXslx);
108

    
109

    
110
	}
111

    
112
	@Test
113
	public void testInit() {
114
		assertNotNull("normalExplicitImport should not be null", defaultImport);
115
		assertNotNull("nameService should not be null", nameService);
116
	}
117

    
118
	@Test
119
	@DataSet
120
	public void testDoInvoke() {
121
		//printDataSet(System.out);
122
		boolean result = defaultImport.invoke(configurator).isSuccess();
123
		assertTrue("Return value for import.invoke should be true", result);
124
		assertEquals("Number of TaxonNames should be 9", 9, nameService.count(null));
125
		List<Classification> treeList = classificationService.list(null, null,null,null,null);
126
		assertEquals("Number of classifications should be 1", 1, treeList.size());
127
		Classification tree = treeList.get(0);
128
		List<TaxonNode> rootNodes = tree.getChildNodes();
129
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
130
		TaxonNode rootNode = rootNodes.iterator().next();
131
		assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
132
		TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
133
		assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
134
		TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
135
		TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
136
		TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
137
		TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
138
		assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
139

    
140
		Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
141
		TaxonNode childNode1 = it.next();
142
		TaxonNode childNode2 = it.next();
143

    
144
		TaxonNode noctuaPronubaNode;
145
		if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
146
			noctuaPronubaNode = childNode1;
147
		}else{
148
			noctuaPronubaNode = childNode2;
149
		}
150

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

    
175
	@Test
176
	@DataSet
177
	public void testDoInvokeXslx() {
178
		//printDataSet(System.out);
179
		boolean result = defaultImport.invoke(configuratorXslx).isSuccess();
180
		assertTrue("Return value for import.invoke should be true", result);
181
		assertEquals("Number of TaxonNames should be 9", 9, nameService.count(null));
182
		List<Classification> treeList = classificationService.list(null, null,null,null,null);
183
		assertEquals("Number of classifications should be 1", 1, treeList.size());
184
		Classification tree = treeList.get(0);
185
		List<TaxonNode> rootNodes = tree.getChildNodes();
186
		assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
187
		TaxonNode rootNode = rootNodes.iterator().next();
188
		assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
189
		TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
190
		assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
191
		TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
192
		TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
193
		TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
194
		TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
195
		assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
196

    
197
		Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
198
		TaxonNode childNode1 = it.next();
199
		TaxonNode childNode2 = it.next();
200

    
201
		TaxonNode noctuaPronubaNode;
202
		if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
203
			noctuaPronubaNode = childNode1;
204
		}else{
205
			noctuaPronubaNode = childNode2;
206
		}
207

    
208
		assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
209
		Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
210
		Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
211
		assertEquals("Number of synonyms should be 1", 1, synonyms.size());
212
		Synonym synonym = synonyms.iterator().next();
213
		assertEquals("Synonym name should be ", "Noctua atlantica", ((NonViralName<?>)synonym.getName()).getNameCache());
214
		Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
215
		Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
216
		TaxonDescription taxonDescription = descriptions.iterator().next();
217
		Set<DescriptionElementBase> elements = taxonDescription.getElements();
218
		List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
219
		for (DescriptionElementBase element : elements){
220
			if (element.isInstanceOf(CommonTaxonName.class)){
221
				commonNames.add((CommonTaxonName)element);
222
			}
223
		}
224
		Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
225
		Set<String> commonNameStrings = new HashSet<String>();
226
		commonNameStrings.add(commonNames.get(0).getName());
227
		commonNameStrings.add(commonNames.get(1).getName());
228
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
229
		Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
230
	}
231

    
232
	@Test
233
//	@DataSet(value="NormalExplicitImportTest.testUuid.xml")
234
    @DataSets({
235
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/io/ClearDB_with_Terms_DataSet.xml"),
236
        @DataSet(value="/eu/etaxonomy/cdm/io/TermsDataSet-with_auditing_info.xml"),
237
        @DataSet(value="NormalExplicitImportTest.testUuid.xml"),
238
    })
239
	public void testUUID(){
240
		UUID taxonUuid = UUID.fromString("aafce7fe-0c5f-42ed-814b-4c7c2c715660");
241
		UUID synonymUuid = UUID.fromString("fc4a995b-37a9-4984-afe6-e352c6c04d92");
242

    
243

    
244
		//test data set
245
		assertEquals("Number of taxon bases should be 2", 2, taxonService.count(null));
246
		Taxon taxon = (Taxon)taxonService.find(taxonUuid);
247
		assertNotNull("Taxon with given uuid should exist", taxon);
248
		assertEquals("Taxon should have no description", 0, taxon.getDescriptions().size());
249
		Synonym synonym = (Synonym)taxonService.find(synonymUuid);
250
		assertNotNull("Synonym with given uuid should exist", synonym);
251
		assertEquals("Synonym should have 1 accepted taxon", 1, synonym.getAcceptedTaxa().size());
252

    
253
		//import
254
		boolean result = defaultImport.invoke(uuidConfigurator).isSuccess();
255
		//test result
256
		assertTrue("Return value for import.invoke should be true", result);
257
		assertEquals("Number of taxon names should be 2", 2, nameService.count(null));
258
		assertEquals("Number of taxa should be 2", 2, taxonService.count(null));
259
		taxon = (Taxon)taxonService.find(taxonUuid);
260
		assertEquals("Taxon should have 1 description", 1, taxon.getDescriptions().size());
261
		TaxonDescription description = taxon.getDescriptions().iterator().next();
262
		assertEquals("Number of description elements should be 2", 2, description.getElements().size());
263

    
264
		String expectedText = "Description for the first taxon";
265
		TextData textData = getTextElement(description, expectedText);
266
		assertNotNull("The element should exists", textData);
267
		Feature feature = textData.getFeature();
268
		assertEquals("Unexpected feature", Feature.DESCRIPTION(), feature);
269
		assertEquals("There should be exactly 1 language", 1,textData.getMultilanguageText().size());
270
		Language language = textData.getMultilanguageText().keySet().iterator().next();
271
		assertEquals("Language should be German", Language.GERMAN(), language);
272
		String text = textData.getText(language);
273
		assertEquals("Unexpected description text", expectedText, text);
274
		assertEquals("Number of source elements should be 1", 1, textData.getSources().size());
275
		DescriptionElementSource source = textData.getSources().iterator().next();
276
		Reference<?> ref = source.getCitation();
277
		assertNotNull("Citation should not be null", ref);
278
		assertNotNull("Authorship should not be null", ref.getAuthorship());
279
		assertEquals("Source author should be 'Meyer et. al.'", "Meyer et. al.",ref.getAuthorship().getTitleCache());
280
		assertEquals("Publication title should be 'My first book'", "My first book", ref.getTitle());
281
		assertEquals("Publication year should be '1987'", "1987", ref.getYear());
282
		TaxonNameBase<?,?> nameUsedInSource = source.getNameUsedInSource();
283
		assertNotNull("Name used in source should not be null", nameUsedInSource);
284
		assertEquals("Name used in source title should be ", "Abies", nameUsedInSource.getTitleCache());
285

    
286

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

    
310
	}
311

    
312
	/**
313
	 * Returns description element for record id 1
314
	 * @param description
315
	 * @return
316
	 */
317
	private TextData getTextElement(TaxonDescription description, String descriptionText) {
318
		for (DescriptionElementBase element : description.getElements()){
319
			if (element.isInstanceOf(TextData.class)){
320
				TextData textData = CdmBase.deproxy(element, TextData.class);
321
				for (LanguageString ls :textData.getMultilanguageText().values()){
322
					if (ls.getText().equals(descriptionText)){
323
						return textData;
324
					}
325
				}
326
			}
327
		}
328
		return null;
329
	}
330

    
331
    /* (non-Javadoc)
332
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
333
     */
334
    @Override
335
    public void createTestDataSet() throws FileNotFoundException {
336
        // TODO Auto-generated method stub
337

    
338
    }
339

    
340
}
    (1-1/1)