Project

General

Profile

Download (11 KB) Statistics
| Branch: | Tag: | Revision:
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
package eu.etaxonomy.cdm.io.owl.in;
10

    
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertNotNull;
13
import static org.junit.Assert.assertTrue;
14

    
15
import java.io.FileNotFoundException;
16
import java.net.URISyntaxException;
17
import java.net.URL;
18
import java.util.ArrayList;
19
import java.util.Arrays;
20
import java.util.Collection;
21
import java.util.List;
22
import java.util.Set;
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.ITermService;
31
import eu.etaxonomy.cdm.api.service.ITermTreeService;
32
import eu.etaxonomy.cdm.api.service.IVocabularyService;
33
import eu.etaxonomy.cdm.common.CdmUtils;
34
import eu.etaxonomy.cdm.common.URI;
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.description.Feature;
40
import eu.etaxonomy.cdm.model.media.Media;
41
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
42
import eu.etaxonomy.cdm.model.media.MediaUtils;
43
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
44
import eu.etaxonomy.cdm.model.term.DefinedTerm;
45
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
46
import eu.etaxonomy.cdm.model.term.Representation;
47
import eu.etaxonomy.cdm.model.term.TermNode;
48
import eu.etaxonomy.cdm.model.term.TermTree;
49
import eu.etaxonomy.cdm.model.term.TermType;
50
import eu.etaxonomy.cdm.model.term.TermVocabulary;
51
import eu.etaxonomy.cdm.persistence.dto.TermDto;
52
import eu.etaxonomy.cdm.persistence.query.MatchMode;
53
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
54

    
55
/**
56
 * @author pplitzner
57
 * @since Apr 24, 2019
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 ITermTreeService 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 = URI.fromUrl(url);
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<TermTree> trees = featureTreeService.listByTitle(TermTree.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
        TermTree<Feature> 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.getDistinctTerms().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 TermNode);
109
        assertEquals("Root node has wrong term type", TermType.Structure, ((TermNode)entirePlant).getTermType());
110
        TermNode<DefinedTerm> entirePlantNode = (TermNode<DefinedTerm>) entirePlant;
111
        List<TermNode<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 (TermNode<DefinedTerm> termNode : childNodes) {
122
            assertTrue("Child node not found. Found node with term: "+termNode.getTerm().getLabel(),
123
                    termNode.getTerm().getUuid().equals(inflorescence.getUuid())
124
                    || termNode.getTerm().getLabel(Language.ENGLISH()).equals("Flower"));
125
            if(termNode.getTerm().getUuid().equals(inflorescence.getUuid())){
126
                assertEquals("Term mismatch", inflorescence, termNode.getTerm());
127
                inflorescence = termNode.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
        Collection<TermDto> topLevelTerms = vocabularyService.getTopLevelTerms(termVoc.getUuid());
164
        assertEquals("wrong number of top level terms", 4, topLevelTerms.size());
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 = URI.fromUrl(url);
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<TermTree> trees = featureTreeService.listByTitle(TermTree.class, treeLabel, MatchMode.EXACT, null, null, null, null, null);
184
        List<String> nodeProperties = new ArrayList<>();
185
        nodeProperties.add("term");
186
        TermTree 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.getDistinctTerms().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
}
    (1-1/1)