-
Notifications
You must be signed in to change notification settings - Fork 14
/
QueryTestBase.java
216 lines (191 loc) · 8.67 KB
/
QueryTestBase.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* 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.
* <p>
* 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.model;
import cz.cvut.kbss.jopa.exceptions.NoResultException;
import cz.cvut.kbss.jopa.model.query.Parameter;
import cz.cvut.kbss.jopa.model.query.Query;
import cz.cvut.kbss.jopa.query.sparql.SparqlQueryFactory;
import cz.cvut.kbss.jopa.sessions.ConnectionWrapper;
import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
import cz.cvut.kbss.jopa.utils.Procedure;
import cz.cvut.kbss.ontodriver.ResultSet;
import cz.cvut.kbss.ontodriver.Statement;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyVararg;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
public abstract class QueryTestBase {
static final String UPDATE_QUERY = "INSERT DATA { ?inst a ?type . }";
@Rule
public ExpectedException thrown = ExpectedException.none();
@Mock
ConnectionWrapper connectionWrapperMock;
@Mock
UnitOfWorkImpl uowMock;
@Mock
Statement statementMock;
@Mock
ResultSet resultSetMock;
@Mock
Procedure handler;
SparqlQueryFactory queryFactory;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(connectionWrapperMock.createStatement()).thenReturn(statementMock);
when(statementMock.executeQuery(anyString(), anyVararg())).thenReturn(resultSetMock);
doAnswer((invocationOnMock) -> {
resultSetMock.close();
return null;
}).when(statementMock).close();
this.queryFactory = new SparqlQueryFactory(uowMock, connectionWrapperMock);
}
abstract Query createQuery(String query, Class<?> resultType);
@Test
public void getResultListWithoutParameterSettingJustPassesTheOriginalQuery() throws Exception {
final String query = "SELECT ?x ?y ?z WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
q.getResultList();
verify(connectionWrapperMock).createStatement();
verify(statementMock).executeQuery(query);
}
@Test
public void setParameterByNameSetsAllOccurrencesOfVariableInQuery() throws Exception {
final String query = "SELECT ?y ?z WHERE { ?x ?y ?z . ?z ?y ?x . }";
final Query q = createQuery(query, Object.class);
q.setParameter("x", "Individual");
q.getResultList();
verify(connectionWrapperMock).createStatement();
verify(statementMock).executeQuery("SELECT ?y ?z WHERE { \"Individual\" ?y ?z . ?z ?y \"Individual\" . }");
}
@Test
public void setParameterByParameterSetsValueWithCorrectType() throws Exception {
final String query = "SELECT ?x ?y WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
final Parameter<URI> p = (Parameter<URI>) q.getParameter("z");
q.setParameter(p, URI.create("http://krizik.felk.cvut.cz"));
q.getResultList();
verify(statementMock).executeQuery("SELECT ?x ?y WHERE { ?x ?y <http://krizik.felk.cvut.cz> .}");
}
@Test
public void testSetStringParameterWithLanguageTag() throws Exception {
final String query = "SELECT ?x ?y WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
q.setParameter("z", "Object", "en");
assertEquals("Object", q.getParameterValue("z"));
q.getResultList();
verify(statementMock).executeQuery("SELECT ?x ?y WHERE { ?x ?y \"Object\"@en .}");
}
@Test
public void testSetStringParameterWithLanguageTagUsingParameterMethod() throws Exception {
final String query = "SELECT ?x ?y WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
final Parameter<String> p = (Parameter<String>) q.getParameter("z");
q.setParameter(p, "Object", "cs");
q.getResultList();
verify(statementMock).executeQuery("SELECT ?x ?y WHERE { ?x ?y \"Object\"@cs .}");
}
@Test(expected = IllegalStateException.class)
public void getParameterValueThrowsIllegalStateForUnboundParam() throws Exception {
final String query = "SELECT ?x ?y WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
q.getParameterValue("z");
}
@Test
public void isBoundIndicatesWhetherVariableHasBeenBound() throws Exception {
final String query = "SELECT ?x ?y WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
final Parameter<?> p = q.getParameter("z");
assertFalse(q.isBound(p));
q.setParameter(p.getName(), "Test");
assertTrue(q.isBound(p));
assertEquals("Test", q.getParameterValue(p.getName()));
}
@Test(expected = IllegalArgumentException.class)
public void setMaxResultsToLessThanZeroThrowsIllegalArgument() throws Exception {
final String query = "SELECT ?x ?y WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
q.setMaxResults(-1);
}
@Test
public void addingContextsPassesThemToQueryEvaluation() throws Exception {
final String query = "SELECT ?x ?y WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
q.addContext(URI.create("http://contextOne"));
q.addContexts(Arrays.asList(URI.create("http://contextTwo"), URI.create("http://contextThree")));
q.getResultList();
final ArgumentCaptor<URI[]> captor = ArgumentCaptor.forClass(URI[].class);
verify(statementMock).executeQuery(eq(query), captor.capture());
final List<URI[]> contexts = captor.getAllValues();
assertEquals(3, contexts.size());
// Mockito interprets the varargs as separate arguments
assertTrue(contexts.contains(URI.create("http://contextOne")));
assertTrue(contexts.contains(URI.create("http://contextTwo")));
assertTrue(contexts.contains(URI.create("http://contextThree")));
}
@Test(expected = NoResultException.class)
public void getSingleResultWithoutResultThrowsNoResultException() throws Exception {
final String query = "SELECT ?x ?y WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
q.getSingleResult();
}
@Test
public void setPositionalParameterSetsValueAtCorrectPosition() throws Exception {
final String query = "SELECT ?x ?z WHERE { ?x $1 ?z . }";
final Query q = createQuery(query, Object.class);
final URI paramValue = URI.create("http://krizik.felk.cvut.cz/jopa#property");
q.setParameter(1, paramValue);
q.getResultList();
verify(statementMock).executeQuery(query.replace("$1", "<" + paramValue.toString() + ">"));
}
@Test
public void setPositionalParameterWithLanguageTag() throws Exception {
final String query = "SELECT ?x WHERE { ?x rdfs:label $ . }";
final Query q = createQuery(query, Object.class);
final String value = "Hooray";
q.setParameter(1, value, "en");
assertEquals(value, q.getParameterValue(1));
final Parameter<?> p = q.getParameter(1);
assertEquals(value, q.getParameterValue(p));
q.getResultList();
verify(statementMock).executeQuery(query.replace("$", "\"Hooray\"@en"));
}
@Test
public void closesStatementAndResultSetUponSelectFinish() throws Exception {
final String query = "SELECT ?x ?y ?z WHERE { ?x ?y ?z .}";
final Query q = createQuery(query, Object.class);
q.getResultList();
verify(statementMock).close();
verify(resultSetMock).close();
}
@Test
public void closesStatementUponUpdateFinish() throws Exception {
final String query = "INSERT DATA { ?x ?y ?z .}";
final Query q = createQuery(query, Void.class);
q.executeUpdate();
verify(statementMock).close();
}
}