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