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