-
Notifications
You must be signed in to change notification settings - Fork 14
/
SesameValueConverterTest.java
101 lines (87 loc) · 4.21 KB
/
SesameValueConverterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.sesame;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import org.junit.Before;
import org.junit.Test;
import org.openrdf.model.Literal;
import org.openrdf.model.Value;
import org.openrdf.model.impl.ValueFactoryImpl;
import java.net.URI;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class SesameValueConverterTest {
private static final String LANG = "en";
private static final URI PROPERTY = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#property");
private SesameValueConverter converter;
@Before
public void setUp() {
this.converter = new SesameValueConverter(new ValueFactoryImpl(), LANG);
}
@Test
public void convertsObjectPropertyNamedResourceToSesameUri() throws Exception {
final Value res = converter.toSesameValue(assertion(Assertion.AssertionType.OBJECT_PROPERTY),
value(NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa#individual")));
assertTrue(res instanceof org.openrdf.model.URI);
}
private Assertion assertion(Assertion.AssertionType type) {
switch (type) {
case CLASS:
return Assertion.createClassAssertion(false);
case PROPERTY:
return Assertion.createPropertyAssertion(PROPERTY, false);
case OBJECT_PROPERTY:
return Assertion.createObjectPropertyAssertion(PROPERTY, false);
case DATA_PROPERTY:
return Assertion.createDataPropertyAssertion(PROPERTY, false);
case ANNOTATION_PROPERTY:
return Assertion.createAnnotationPropertyAssertion(PROPERTY, false);
default:
throw new IllegalArgumentException("Unknown assertion type: " + type);
}
}
private <T> cz.cvut.kbss.ontodriver.model.Value<T> value(T val) {
return new cz.cvut.kbss.ontodriver.model.Value<>(val);
}
@Test
public void convertsDataPropertyValueToSesameLiteral() throws Exception {
final int val = 117;
final Value res = converter.toSesameValue(assertion(Assertion.AssertionType.DATA_PROPERTY), value(val));
assertTrue(res instanceof Literal);
assertEquals(val, ((Literal) res).intValue());
}
@Test
public void convertsAnnotationPropertyLiteralValueToSesameLiteral() throws Exception {
final String val = "AnnotationValue";
final Value res = converter.toSesameValue(assertion(Assertion.AssertionType.ANNOTATION_PROPERTY), value(val));
assertTrue(res instanceof Literal);
assertEquals(val, ((Literal) res).getLabel());
assertEquals(LANG, ((Literal) res).getLanguage());
}
@Test
public void convertsAnnotationPropertyNamedResourceToSesameUri() throws Exception {
final NamedResource val = NamedResource
.create("http://krizik.felk.cvut.cz/ontologies/jopa#individualAnnotation");
final Value res = converter.toSesameValue(assertion(Assertion.AssertionType.ANNOTATION_PROPERTY), value(val));
assertTrue(res instanceof org.openrdf.model.URI);
assertEquals(val.toString(), res.toString());
}
@Test(expected = SesameDriverException.class)
public void conversionThrowsExceptionWhenObjectPropertyValueIsNotUri() throws Exception {
converter.toSesameValue(assertion(Assertion.AssertionType.OBJECT_PROPERTY), value(117));
}
}