moved taxonNode specific code to TaxonTreeService. Cleaned up the datasource classes
[cdmlib.git] / cdmlib-io / src / test / java / eu / etaxonomy / cdm / io / excel / taxa / NormalExplicitImportTest.java
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.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
24 import junit.framework.Assert;
25
26 import org.apache.log4j.Logger;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.unitils.dbunit.annotation.DataSet;
31 import org.unitils.spring.annotation.SpringBeanByName;
32 import org.unitils.spring.annotation.SpringBeanByType;
33
34 import eu.etaxonomy.cdm.api.service.INameService;
35 import eu.etaxonomy.cdm.api.service.ITaxonService;
36 import eu.etaxonomy.cdm.api.service.ITaxonTreeService;
37 import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
38 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
39 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
40 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
41 import eu.etaxonomy.cdm.model.description.TaxonDescription;
42 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
43 import eu.etaxonomy.cdm.model.name.NonViralName;
44 import eu.etaxonomy.cdm.model.taxon.Synonym;
45 import eu.etaxonomy.cdm.model.taxon.Taxon;
46 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
47 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
48 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
49
50 /**
51 * @author a.mueller
52 * @created 26.08.2009
53 * @version 1.0
54 */
55 public class NormalExplicitImportTest extends CdmTransactionalIntegrationTest{
56 @SuppressWarnings("unused")
57 private static final Logger logger = Logger.getLogger(NormalExplicitImportTest.class);
58
59 @SpringBeanByName
60 CdmApplicationAwareDefaultImport defaultImport;
61
62 @SpringBeanByType
63 INameService nameService;
64
65 @SpringBeanByType
66 ITaxonService taxonService;
67
68 @SpringBeanByType
69 ITaxonTreeService taxonTreeService;
70
71 private IImportConfigurator configurator;
72
73 @Before
74 public void setUp() {
75 String inputFile = "/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImportTest-input.xls";
76 URL url = this.getClass().getResource(inputFile);
77 assertNotNull("URL for the test file '" + inputFile + "' does not exist", url);
78 configurator = NormalExplicitImportConfigurator.NewInstance(url.toString(), null, NomenclaturalCode.ICBN);
79 assertNotNull("Configurator could not be created", configurator);
80 }
81
82 @Test
83 public void testInit() {
84 assertNotNull("normalExplicitImport should not be null", defaultImport);
85 assertNotNull("nameService should not be null", nameService);
86 }
87
88 @Test
89 @DataSet
90 @Ignore //does run standalone, but not in suite (maven)
91 public void testDoInvoke() {
92 //printDataSet(System.out);
93 boolean result = defaultImport.invoke(configurator);
94 assertTrue("Return value for import.invoke should be true", result);
95 assertEquals("Number of TaxonNames should be 9", 9, nameService.count());
96 List<TaxonomicTree> treeList = taxonTreeService.list(null, null).getRecords();
97 assertEquals("Number of taxonomic trees should be 1", 1, treeList.size());
98 TaxonomicTree tree = treeList.get(0);
99 Set<TaxonNode> rootNodes = tree.getRootNodes();
100 assertEquals("Number of root nodes should be 1", 1, rootNodes.size());
101 TaxonNode rootNode = rootNodes.iterator().next();
102 assertEquals("Root taxon name should be Animalia", "Animalia", rootNode.getTaxon().getName().getTitleCache());
103 TaxonNode arthropodaNode = rootNode.getChildNodes().iterator().next();
104 assertEquals("Arthropoda node taxon name should be Arthropoda", "Arthropoda", arthropodaNode.getTaxon().getName().getTitleCache());
105 TaxonNode insectaNode = arthropodaNode.getChildNodes().iterator().next();
106 TaxonNode lepidopteraNode = insectaNode.getChildNodes().iterator().next();
107 TaxonNode noctuidaeNode = lepidopteraNode.getChildNodes().iterator().next();
108 TaxonNode noctuaNode = noctuidaeNode.getChildNodes().iterator().next();
109 assertEquals("Number of child nodes of noctuca should be 2", 2, noctuaNode.getChildNodes().size());
110
111 Iterator<TaxonNode> it = noctuaNode.getChildNodes().iterator();
112 TaxonNode childNode1 = it.next();
113 TaxonNode childNode2 = it.next();
114
115 TaxonNode noctuaPronubaNode;
116 if (childNode1.getTaxon().getName().getTitleCache().startsWith("Noctua pronuba")){
117 noctuaPronubaNode = childNode1;
118 }else{
119 noctuaPronubaNode = childNode2;
120 }
121
122 assertEquals("Noctua pronuba taxon name should be ", "Noctua pronuba", noctuaPronubaNode.getTaxon().getName().getTitleCache());
123 Taxon noctuaPronubaTaxon = noctuaPronubaNode.getTaxon();
124 Set<Synonym> synonyms = noctuaPronubaTaxon.getSynonyms();
125 assertEquals("Number of synonyms should be 1", 1, synonyms.size());
126 Synonym synonym = synonyms.iterator().next();
127 assertEquals("Synonym name should be ", "Noctua atlantica", ((NonViralName)synonym.getName()).getNameCache());
128 Set<TaxonDescription> descriptions = noctuaPronubaTaxon.getDescriptions();
129 Assert.assertEquals("Number of descriptions should be 1", 1, descriptions.size());
130 TaxonDescription taxonDescription = descriptions.iterator().next();
131 Set<DescriptionElementBase> elements = taxonDescription.getElements();
132 List<CommonTaxonName> commonNames = new ArrayList<CommonTaxonName>();
133 for (DescriptionElementBase element : elements){
134 if (element.isInstanceOf(CommonTaxonName.class)){
135 commonNames.add((CommonTaxonName)element);
136 }
137 }
138 Assert.assertEquals("Number of common names should be 2", 2, commonNames.size());
139 Set<String> commonNameStrings = new HashSet<String>();
140 commonNameStrings.add(commonNames.get(0).getName());
141 commonNameStrings.add(commonNames.get(1).getName());
142 Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Large Sunshine Underwing"));
143 Assert.assertTrue("Common names must include Yellow Underwing", commonNameStrings.contains("Yellow Underwing"));
144 }
145 }