-
Notifications
You must be signed in to change notification settings - Fork 14
/
NamedParameterSparqlQueryHolderTest.java
177 lines (155 loc) · 7.42 KB
/
NamedParameterSparqlQueryHolderTest.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* 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.jopa.query.sparql;
import cz.cvut.kbss.jopa.model.query.Parameter;
import cz.cvut.kbss.jopa.query.QueryParameter;
import org.junit.Before;
import org.junit.Test;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
public class NamedParameterSparqlQueryHolderTest {
private static final String QUERY = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" +
"SELECT ?craft\n" +
"{\n" +
"?craft foaf:name \"Apollo 7\" .\n" +
"?craft foaf:homepage ?homepage .\n" +
"}";
private static final List<String> PARTS = Arrays.asList("PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" +
"SELECT ", "\n{\n", " foaf:name \"Apollo 7\" .\n", " foaf:homepage ", " .\n}");
private static final List<String> PARAMS = Arrays.asList("craft", "craft", "craft", "homepage");
private static final Set<String> PARAM_NAMES = new HashSet<>(Arrays.asList("craft", "homepage"));
private SparqlQueryHolder holder;
@Before
public void setUp() throws Exception {
final Map<String, QueryParameter<?>> paramsByName = new HashMap<>();
for (String n : PARAM_NAMES) {
paramsByName.put(n, new QueryParameter<>(n));
}
final List<QueryParameter<?>> parameters = PARAMS.stream().map(paramsByName::get).collect(Collectors.toList());
this.holder = new SparqlQueryHolder(QUERY, PARTS, parameters);
}
@Test
public void testGetQuery() throws Exception {
final String original = holder.getQuery();
assertEquals(QUERY, original);
}
@Test
public void testGetParameters() throws Exception {
final Collection<Parameter<?>> res = holder.getParameters();
assertEquals(PARAM_NAMES.size(), res.size());
for (Parameter<?> p : res) {
assertTrue(PARAM_NAMES.contains(p.getName()));
}
}
@Test
public void testGetParameterByName() throws Exception {
final String name = PARAM_NAMES.iterator().next();
final Parameter<?> p = holder.getParameter(name);
assertNotNull(p);
assertEquals(name, p.getName());
}
@Test(expected = IllegalArgumentException.class)
public void getParameterByUnknownNameThrowsIllegalArgument() throws Exception {
holder.getParameter("stupidUnknownParameter");
}
@Test(expected = IllegalArgumentException.class)
public void getValueOfUnknownParameterThrowsIllegalArgumentException() throws Exception {
final Parameter<Object> p = new QueryParameter<>("homepage");
holder.setParameter(p, 1);
holder.getParameterValue(new QueryParameter<>("blabla"));
}
@Test
public void testSetParameter() throws Exception {
final String value = "http://kbss.felk.cvut.cz";
holder.setParameter(new QueryParameter<>("homepage"), value);
assertEquals(value, holder.getParameterValue(holder.getParameter("homepage")));
}
@Test(expected = NullPointerException.class)
public void setParameterToNullThrowsException() throws Exception {
holder.setParameter(new QueryParameter<>("homepage"), null);
}
@Test(expected = NullPointerException.class)
public void setNullParameterThrowsException() throws Exception {
holder.setParameter(null, "Whatever");
}
@Test(expected = IllegalArgumentException.class)
public void setUnknownParameterThrowsException() throws Exception {
holder.setParameter(new QueryParameter<>("unknown"), "Whatever");
}
@Test
public void clearParameterRemovesParameterValue() throws Exception {
final QueryParameter<?> qp = new QueryParameter<>("homepage");
holder.setParameter(qp, URI.create("http://kbss.felk.cvut.cz"));
assertNotNull(holder.getParameterValue(qp));
holder.clearParameter(qp);
assertNull(holder.getParameterValue(qp));
}
@Test
public void clearParametersRemovesAllParameterValues() throws Exception {
final QueryParameter<?> qp = new QueryParameter<>("homepage");
holder.setParameter(qp, URI.create("http://kbss.felk.cvut.cz"));
final QueryParameter<?> qpTwo = new QueryParameter<>("craft");
holder.setParameter(qpTwo, "Programming");
holder.getParameters().forEach(param -> assertNotNull(holder.getParameterValue(param)));
holder.clearParameters();
holder.getParameters().forEach(param -> assertNull(holder.getParameterValue(param)));
}
@Test
public void assembleQueryWithUri() throws Exception {
final QueryParameter<?> qp = new QueryParameter<>("homepage");
holder.setParameter(qp, URI.create("http://kbss.felk.cvut.cz"));
final String expected = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" +
"SELECT ?craft\n" +
"{\n" +
"?craft foaf:name \"Apollo 7\" .\n" +
"?craft foaf:homepage <http://kbss.felk.cvut.cz> .\n" +
"}";
assertEquals(expected, holder.assembleQuery());
}
@Test
public void assembleQueryWithLiteral() throws Exception {
final QueryParameter<?> qp = new QueryParameter<>("homepage");
holder.setParameter(qp, "http://kbss.felk.cvut.cz", null);
final String expected = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" +
"SELECT ?craft\n" +
"{\n" +
"?craft foaf:name \"Apollo 7\" .\n" +
"?craft foaf:homepage \"http://kbss.felk.cvut.cz\" .\n" +
"}";
assertEquals(expected, holder.assembleQuery());
}
@Test
public void setParametersAndAssembleQueryWithMultipleParamsNextToEachOther() throws Exception {
final String query = "SELECT ?y ?z WHERE { <http://krizik.felk.cvut.cz/ontologies/jopa#entityA> ?y ?z . }";
final List<QueryParameter<?>> params = Arrays
.asList(new QueryParameter<>("y"), new QueryParameter<>("z"), new QueryParameter<>("x"),
new QueryParameter<>("y"), new QueryParameter<>("z"));
final List<String> parts = Arrays.asList("SELECT ", " ", " WHERE { ", " ", " ", " . }");
this.holder = new SparqlQueryHolder(query, parts, params);
holder.setParameter(new QueryParameter<>("x"),
URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#entityA"));
final String result = holder.assembleQuery();
assertEquals(query, result);
}
@Test
public void setParameterReplacesAllOccurrencesOfVariable() throws Exception {
final QueryParameter<?> qp = new QueryParameter<>("craft");
holder.setParameter(qp, URI.create("http://kbss.felk.cvut.cz/apollo7"));
final String result = holder.assembleQuery();
assertFalse(result.contains("?craft"));
}
}