ref #8248, #6774 Add plural to import/export
[cdmlib.git] / cdmlib-io / src / test / java / eu / etaxonomy / cdm / io / owl / in / StructureTreeOwlImportTest.java
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.owl.in;
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.URI;
18 import java.net.URISyntaxException;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.unitils.dbunit.annotation.DataSet;
27 import org.unitils.spring.annotation.SpringBeanByName;
28 import org.unitils.spring.annotation.SpringBeanByType;
29
30 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
31 import eu.etaxonomy.cdm.api.service.ITermService;
32 import eu.etaxonomy.cdm.api.service.IVocabularyService;
33 import eu.etaxonomy.cdm.common.CdmUtils;
34 import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
35 import eu.etaxonomy.cdm.io.descriptive.owl.in.StructureTreeOwlImportConfigurator;
36 import eu.etaxonomy.cdm.model.common.Language;
37 import eu.etaxonomy.cdm.model.term.DefinedTerm;
38 import eu.etaxonomy.cdm.model.term.FeatureNode;
39 import eu.etaxonomy.cdm.model.term.FeatureTree;
40 import eu.etaxonomy.cdm.model.term.Representation;
41 import eu.etaxonomy.cdm.model.term.TermType;
42 import eu.etaxonomy.cdm.model.term.TermVocabulary;
43 import eu.etaxonomy.cdm.persistence.query.MatchMode;
44 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
45
46 /**
47 *
48 * @author pplitzner
49 * @since Apr 24, 2019
50 *
51 */
52 public class StructureTreeOwlImportTest extends CdmTransactionalIntegrationTest {
53
54 @SpringBeanByName
55 private CdmApplicationAwareDefaultImport<?> defaultImport;
56
57 @SpringBeanByType
58 private ITermService termService;
59
60 @SpringBeanByType
61 private IFeatureTreeService featureTreeService;
62
63 @SpringBeanByType
64 private IVocabularyService vocabularyService;
65
66 @Before
67 public void setUp() {
68 }
69
70 @Test
71 public void testInit() {
72 assertNotNull("import should not be null", defaultImport);
73 }
74
75 @Test
76 @DataSet(value="/eu/etaxonomy/cdm/database/BlankDataSet.xml")
77 public void testImportStructureTree() throws URISyntaxException {
78 URL url = this.getClass().getResource("/eu/etaxonomy/cdm/io/owl/in/test_structures.owl");
79 URI uri = url.toURI();
80 assertNotNull(url);
81 StructureTreeOwlImportConfigurator configurator = StructureTreeOwlImportConfigurator.NewInstance(uri);
82
83 boolean result = defaultImport.invoke(configurator).isSuccess();
84 assertTrue("Return value for import.invoke should be true", result);
85 this.setComplete();
86 this.endTransaction();
87
88 String treeLabel = "test_structures";
89 List<FeatureTree> trees = featureTreeService.listByTitle(FeatureTree.class, treeLabel, MatchMode.EXACT, null, null, null, null, null);
90 List<String> nodeProperties = new ArrayList<>();
91 nodeProperties.add("term");
92 FeatureTree tree = featureTreeService.loadWithNodes(trees.iterator().next().getUuid(), null, nodeProperties);
93 assertNotNull("featureTree should not be null", tree);
94
95 assertEquals("Tree has wrong term type", TermType.Structure, tree.getTermType());
96 assertEquals("Wrong number of distinct features", 4, tree.getDistinctFeatures().size());
97 List rootChildren = tree.getRootChildren();
98 assertEquals("Wrong number of root children", 1, rootChildren.size());
99 Object entirePlant = rootChildren.iterator().next();
100 assertTrue("Root is no feature node", entirePlant instanceof FeatureNode);
101 assertEquals("Root node has wrong term type", TermType.Structure, ((FeatureNode)entirePlant).getTermType());
102 FeatureNode<DefinedTerm> entirePlantNode = (FeatureNode<DefinedTerm>) entirePlant;
103 List<FeatureNode<DefinedTerm>> childNodes = entirePlantNode.getChildNodes();
104 assertEquals("Wrong number of children", 2, childNodes.size());
105
106 String inflorescenceLabel = "inflorescence";
107 String inflorescenceDescription = " the part of the plant that bears the flowers, including all its bracts branches and flowers but excluding unmodified leaves ";
108 List<DefinedTerm> records = termService.findByRepresentationText(inflorescenceDescription, DefinedTerm.class, null, null).getRecords();
109 assertEquals("wrong number of terms found for \"inflorescence\"", 1, records.size());
110 DefinedTerm inflorescence = records.iterator().next();
111 assertEquals(inflorescenceLabel, inflorescence.getLabel(Language.ENGLISH()));
112
113 for (FeatureNode<DefinedTerm> featureNode : childNodes) {
114 assertTrue("Child node not found. Found node with term: "+featureNode.getTerm().getLabel(),
115 featureNode.getTerm().getUuid().equals(inflorescence.getUuid())
116 || featureNode.getTerm().getLabel(Language.ENGLISH()).equals("Flower"));
117 if(featureNode.getTerm().getUuid().equals(inflorescence.getUuid())){
118 assertEquals("Term mismatch", inflorescence, featureNode.getTerm());
119 inflorescence = featureNode.getTerm();
120
121 assertEquals("wrong id in vocabulary", "inflorescence", inflorescence.getIdInVocabulary());
122 assertEquals("wrong symbol", "infloSymbol", inflorescence.getSymbol());
123 assertEquals("wrong symbol2", "infloSymbol2", inflorescence.getSymbol2());
124
125 Representation englishRepresentation = inflorescence.getRepresentation(Language.ENGLISH());
126 assertTrue("Description not found", CdmUtils.isNotBlank(englishRepresentation.getDescription()));
127 assertEquals("Description wrong", inflorescenceDescription, englishRepresentation.getDescription());
128 assertEquals("wrong plural", "inflorescences", englishRepresentation.getPlural());
129 assertEquals("wrong label abbrev", "inflo", englishRepresentation.getAbbreviatedLabel());
130
131 // german representation
132 assertEquals("wrong number of representations", 2, inflorescence.getRepresentations().size());
133 Representation germanRepresentation = inflorescence.getRepresentation(Language.GERMAN());
134 assertNotNull("Representation is null for "+Language.GERMAN(), germanRepresentation);
135 assertEquals("wrong description", "Der Teil der Pflanze, der die Bluete traegt", germanRepresentation.getDescription());
136 assertEquals("wrong label", "Infloreszenz", germanRepresentation.getLabel());
137 }
138 }
139 assertNotNull("term is null", inflorescence);
140 assertEquals("Wrong term type", TermType.Structure, inflorescence.getTermType());
141
142 String vocLabel = "03 Generative Structures";
143 List<TermVocabulary> vocs = vocabularyService.findByTitle(TermVocabulary.class, vocLabel, MatchMode.EXACT, null, null, null, null, Arrays.asList("terms")).getRecords();
144 assertEquals("wrong number of vocabularies", 1, vocs.size());
145 TermVocabulary termVoc = vocs.iterator().next();
146 assertEquals("Wrong vocabulary label", vocLabel, termVoc.getTitleCache());
147 assertEquals(3, termVoc.getTerms().size());
148 assertTrue("Term not included in vocabulary", termVoc.getTerms().contains(inflorescence));
149
150 }
151
152
153 @Test
154 @DataSet(value="/eu/etaxonomy/cdm/database/BlankDataSet.xml")
155 public void testImportPropertyTreeAndVocabulary() throws URISyntaxException {
156 URL url = this.getClass().getResource("/eu/etaxonomy/cdm/io/owl/in/properties.owl");
157 URI uri = url.toURI();
158 assertNotNull(url);
159 StructureTreeOwlImportConfigurator configurator = StructureTreeOwlImportConfigurator.NewInstance(uri);
160
161 boolean result = defaultImport.invoke(configurator).isSuccess();
162 assertTrue("Return value for import.invoke should be true", result);
163 this.setComplete();
164 this.endTransaction();
165
166 String treeLabel = "properties 1.0";
167 List<FeatureTree> trees = featureTreeService.listByTitle(FeatureTree.class, treeLabel, MatchMode.EXACT, null, null, null, null, null);
168 List<String> nodeProperties = new ArrayList<>();
169 nodeProperties.add("term");
170 FeatureTree tree = featureTreeService.loadWithNodes(trees.iterator().next().getUuid(), null, nodeProperties);
171 assertNotNull("featureTree should not be null", tree);
172
173 assertEquals("Tree has wrong term type", TermType.Property, tree.getTermType());
174 assertEquals("Wrong number of distinct features", 12, tree.getDistinctFeatures().size());
175 List rootChildren = tree.getRootChildren();
176
177 String vocLabel = "Plant Glossary Properties";
178 List<TermVocabulary> vocs = vocabularyService.findByTitle(TermVocabulary.class, vocLabel, MatchMode.EXACT, null, null, null, null, Arrays.asList("terms")).getRecords();
179 assertEquals("wrong number of vocabularies", 1, vocs.size());
180 TermVocabulary termVoc = vocs.iterator().next();
181 assertEquals("Wrong vocabulary label", vocLabel, termVoc.getTitleCache());
182 assertEquals(82, termVoc.getTerms().size());
183
184 }
185 @Override
186 public void createTestDataSet() throws FileNotFoundException {}
187
188
189 }