2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.owl
.in
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertNotNull
;
14 import static org
.junit
.Assert
.assertTrue
;
16 import java
.io
.FileNotFoundException
;
18 import java
.net
.URISyntaxException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Arrays
;
22 import java
.util
.List
;
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
;
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
;
59 public class StructureTreeOwlImportTest
extends CdmTransactionalIntegrationTest
{
62 private CdmApplicationAwareDefaultImport
<?
> defaultImport
;
65 private ITermService termService
;
68 private IFeatureTreeService featureTreeService
;
71 private IVocabularyService vocabularyService
;
78 public void testInit() {
79 assertNotNull("import should not be null", defaultImport
);
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();
88 StructureTreeOwlImportConfigurator configurator
= StructureTreeOwlImportConfigurator
.NewInstance(uri
);
90 boolean result
= defaultImport
.invoke(configurator
).isSuccess();
91 assertTrue("Return value for import.invoke should be true", result
);
93 this.endTransaction();
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
);
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());
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()));
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();
129 assertEquals("wrong id in vocabulary", "inflorescence", inflorescence
.getIdInVocabulary());
130 assertEquals("wrong symbol", "infloSymbol", inflorescence
.getSymbol());
131 assertEquals("wrong symbol2", "infloSymbol2", inflorescence
.getSymbol2());
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());
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());
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());
155 assertNotNull("term is null", inflorescence
);
156 assertEquals("Wrong term type", TermType
.Structure
, inflorescence
.getTermType());
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
));
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();
175 StructureTreeOwlImportConfigurator configurator
= StructureTreeOwlImportConfigurator
.NewInstance(uri
);
177 boolean result
= defaultImport
.invoke(configurator
).isSuccess();
178 assertTrue("Return value for import.invoke should be true", result
);
180 this.endTransaction();
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
);
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();
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());
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())));
212 public void createTestDataSet() throws FileNotFoundException
{}