Switch to unified view

a b/src/test/java/org/sbaresearch/owl/AlternativesBuilderTest.java
1
/**
2
 * Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
3
 * The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
4
 * TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
5
 * development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
8
 * the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
9
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
11
 * limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
12
 * PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
13
 * unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
14
 * any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
15
 * License or out of the use or inability to use the Work.
16
 * See the License for the specific language governing permissions and limitation under the License.
17
 */
18
package org.sbaresearch.owl;
19
20
import junitparams.JUnitParamsRunner;
21
import junitparams.Parameters;
22
import org.junit.Test;
23
import org.junit.runner.RunWith;
24
import org.semanticweb.owlapi.model.*;
25
26
import java.io.ByteArrayInputStream;
27
import java.net.URISyntaxException;
28
import java.util.HashMap;
29
import java.util.HashSet;
30
import java.util.Set;
31
32
import static junit.framework.Assert.assertFalse;
33
import static junit.framework.Assert.assertTrue;
34
import static org.hamcrest.CoreMatchers.equalTo;
35
import static org.hamcrest.CoreMatchers.is;
36
import static org.hamcrest.Matchers.containsInAnyOrder;
37
import static org.junit.Assert.assertThat;
38
39
@RunWith(JUnitParamsRunner.class)
40
public class AlternativesBuilderTest {
41
42
    @Test
43
    @Parameters({"IndivD, http://localhost/test_ab3.owl#IndivD"})
44
    public void testFindIndividual_indivInBaseOntology_shouldReturnIRI(String indivName, String expectedIRI) throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
45
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
46
        IRI iri = alternativesBuilder.findIndividualByFragmentAndLabel(indivName);
47
        assertThat(iri.toString(), equalTo(expectedIRI));
48
    }
49
50
    @Test
51
    @Parameters({
52
            "IndivA, http://localhost/test_ab1.owl#IndivA",
53
            "IndivB, http://localhost/test_ab2.owl#IndivB"
54
    })
55
    public void testFindIndividual_indivInImportedOntology_shouldReturnIRI(String indivName, String expectedIRI) throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
56
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
57
        IRI iri = alternativesBuilder.findIndividualByFragmentAndLabel(indivName);
58
        assertThat(iri.toString(), equalTo(expectedIRI));
59
    }
60
61
    @Test
62
    @Parameters({
63
            "IndivE, http://localhost/test_ab4_usingLabel.owl#bf342340921",
64
            "IndivA, http://localhost/test_ab1_usingLabel.owl#ab1232323"
65
    })
66
    public void testFindIndividual_byLabel_shouldReturnIRI(String indivName, String expectedIRI) throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
67
        AlternativesBuilder alternativesBuilder = createAlternativesBuilderModelUsingLabel();
68
        IRI actualIRI = alternativesBuilder.findIndividualByFragmentAndLabel(indivName);
69
        assertThat(actualIRI.toString(), equalTo(expectedIRI));
70
    }
71
72
    @Test
73
    public void testCopyOntology_validSource_shouldContainSameNumberOfAxioms() throws OWLOntologyCreationException, URISyntaxException {
74
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
75
        OWLOntology actual = alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1"));
76
        assertThat(actual.getAxiomCount(), is(alternativesBuilder.getModel().getOntology().getAxiomCount()));
77
    }
78
79
    @Test
80
    public void testCopyOntology_validSource_shouldHaveNewIRI() throws OWLOntologyCreationException, URISyntaxException {
81
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
82
        IRI targetIRI = IRI.create("http://localhost/abtest1");
83
        OWLOntology actual = alternativesBuilder.copyOntology(targetIRI);
84
        assertThat(actual.getOntologyID().getOntologyIRI(), equalTo(targetIRI));
85
    }
86
87
    @Test
88
    public void testCopyOntology_validSourceExistingManager_shouldNotCreateNewManager() throws OWLOntologyCreationException, URISyntaxException {
89
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
90
        IRI targetIRI = IRI.create("http://localhost/abtest1");
91
        alternativesBuilder.copyOntology(targetIRI, alternativesBuilder.getModel().getOntology().getOWLOntologyManager());
92
        assertTrue(alternativesBuilder.getModel().getOntology().containsIndividualInSignature(IRI.create("http://localhost/abtest1#IndivD")));
93
    }
94
95
    @Test
96
    public void testCopyOntology_validSource_shouldCreateNewManager() throws OWLOntologyCreationException, URISyntaxException {
97
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
98
        IRI targetIRI = IRI.create("http://localhost/abtest1");
99
        alternativesBuilder.copyOntology(targetIRI);
100
        assertFalse(alternativesBuilder.getModel().getOntology().containsIndividualInSignature(IRI.create("http://localhost/abtest1#IndivD")));
101
    }
102
103
    @Test
104
    public void testCopyOntology_validSource_shouldRenameIndivs() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
105
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
106
107
        final OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
108
        Set<OWLNamedIndividual> actualIndivs = newOntology.getOntology().getIndividualsInSignature();
109
110
        final Set<OWLNamedIndividual> expectedIndivs = new HashSet<OWLNamedIndividual>() {{
111
            add(newOntology.getDataFactory().getOWLNamedIndividual(IRI.create("http://localhost/abtest1.owl#IndivD")));
112
        }};
113
        assertThat(expectedIndivs, containsInAnyOrder(actualIndivs.toArray()));
114
    }
115
116
    @Test
117
    public void testCopyOntology_validSource_shouldRenameClasses() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
118
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
119
120
        final OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
121
        Set<OWLClass> actualClasses = newOntology.getOntology().getClassesInSignature();
122
123
        final Set<OWLClass> expectedClasses = new HashSet<OWLClass>() {{
124
            add(newOntology.getDataFactory().getOWLClass(IRI.create("http://localhost/abtest1.owl#ClassD")));
125
        }};
126
        assertThat(expectedClasses, containsInAnyOrder(actualClasses.toArray()));
127
    }
128
129
    @Test
130
    public void testCopyOntology_validSourceWithoutImports_shouldContainNoImports() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
131
        OwlApiFacade model = TestUtils.createDefaultOwlApiFacade();
132
        model.load(TestUtils.getUri("/test_ab1.owl"));
133
        AlternativesBuilder alternativesBuilder = new AlternativesBuilder(model);
134
135
        OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
136
        Set<IRI> actualImports = newOntology.getOntology().getDirectImportsDocuments();
137
138
        assertThat(actualImports.size(), is(0));
139
    }
140
141
    @Test
142
    public void testCopyOntology_validSourceWithImports_shouldContainAllImports() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
143
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
144
        Set<IRI> expectedImports = alternativesBuilder.getModel().getOntology().getDirectImportsDocuments();
145
        OwlApiFacade newOntology = new OwlApiFacade(alternativesBuilder.copyOntology(IRI.create("http://localhost/abtest1.owl")));
146
147
        Set<IRI> actualImports = newOntology.getOntology().getDirectImportsDocuments();
148
149
        assertThat(actualImports, containsInAnyOrder(expectedImports.toArray()));
150
    }
151
152
    @Test
153
    public void testRenameIndividualByFragment_validFragment_shouldRenameIndividual() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
154
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
155
        alternativesBuilder.renameIndividualByIRI(alternativesBuilder.getModel().createIri("IndivD"), "xIndivD");
156
        assertThat(alternativesBuilder.getModel().getIndividualByUnqualifiedName("xIndivD", false).getIRI().toString(), equalTo("http://localhost/test_ab3.owl#xIndivD"));
157
    }
158
159
    @Test (expected = OwlElementNotFoundException.class)
160
    public void testRenameIndividualByFragment_inexistingFragment_shouldThrowException() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
161
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
162
        alternativesBuilder.renameIndividualByIRI(alternativesBuilder.getModel().createIri("yIndivD"), "xIndivD");
163
    }
164
165
    @Test
166
    public void testRenameIndividualByFragment_indivHasLabel_shouldRenameLabelAlso() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
167
        AlternativesBuilder alternativesBuilder = createAlternativesBuilderModelUsingLabel();
168
        alternativesBuilder.renameIndividualByIRI(alternativesBuilder.getModel().createIri("bf342340921"), "xIndivD");
169
        assertThat(alternativesBuilder.getModel().getIndividualByLabel("xIndivD").getIRI().toString(), equalTo("http://localhost/test_ab4_usingLabel.owl#xIndivD"));
170
    }
171
172
    @Test
173
    public void testRenameIndividualByLabel_validLabel_shouldRenameLabelButNotTheFragment() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
174
        AlternativesBuilder alternativesBuilder = createAlternativesBuilderModelUsingLabel();
175
        alternativesBuilder.renameIndividualByLabel("IndivE", "xIndivE");
176
        assertThat(alternativesBuilder.getModel().getIndividualByLabel("xIndivE").getIRI().toString(), equalTo("http://localhost/test_ab4_usingLabel.owl#bf342340921"));
177
    }
178
179
    @Test (expected = OwlElementNotFoundException.class)
180
    public void testRenameIndividualByLabel_inexistingLabel_shouldThrowException() throws OWLOntologyCreationException, URISyntaxException, OwlElementNotFoundException {
181
        AlternativesBuilder alternativesBuilder = createDefaultAlternativesBuilder();
182
        alternativesBuilder.renameIndividualByLabel("yIndivD", "xIndivD");
183
    }
184
185
    @Test
186
    public void testToXmlString_validOntology_shouldReturnXmlString() throws OWLOntologyCreationException, URISyntaxException {
187
        OwlApiFacade source = TestUtils.createDefaultOwlApiFacade();
188
        source.load(TestUtils.getUri("/test_ab1.owl"));
189
        String actual = AlternativesBuilder.toXMLString(source.getOntology());
190
191
        OwlApiFacade ontology = TestUtils.createDefaultOwlApiFacade();
192
        ontology.load(new ByteArrayInputStream(actual.getBytes()));
193
        assertThat(ontology.getOntology().getOntologyID().getOntologyIRI().toString(), equalTo("http://localhost/test_ab1.owl"));
194
    }
195
196
    private AlternativesBuilder createDefaultAlternativesBuilder() throws URISyntaxException, OWLOntologyCreationException {
197
        OwlApiFacade model = TestUtils.createDefaultOwlApiFacade();
198
        model.load(TestUtils.getUri("/test_ab3.owl"), new HashMap<String, String>() {{
199
            put("http://localhost/test_ab1.owl", TestUtils.getUri("/test_ab1.owl"));
200
            put("http://localhost/test_ab2.owl", TestUtils.getUri("/test_ab2.owl"));
201
        }});
202
        return new AlternativesBuilder(model);
203
    }
204
205
    private AlternativesBuilder createAlternativesBuilderModelUsingLabel() throws URISyntaxException, OWLOntologyCreationException {
206
        OwlApiFacade model = TestUtils.createDefaultOwlApiFacade();
207
        model.load(TestUtils.getUri("/test_ab4_usingLabel.owl"), new HashMap<String, String>() {{
208
            put("http://localhost/test_ab1_usingLabel.owl", TestUtils.getUri("/test_ab1_usingLabel.owl"));
209
            put("http://localhost/test_ab2.owl", TestUtils.getUri("/test_ab2.owl"));
210
        }});
211
        return new AlternativesBuilder(model);
212
    }
213
}