forked from fcrepo/fcrepo
/
FedoraSparqlIT.java
343 lines (251 loc) · 13.5 KB
/
FedoraSparqlIT.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* Copyright 2014 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.integration.transform.http;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFactory;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.fcrepo.integration.AbstractResourceIT;
import org.fcrepo.kernel.impl.FedoraResourceImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jcr.api.JcrConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import static org.apache.jena.riot.WebContent.contentTypeHTMLForm;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.ws.rs.core.Response.Status;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* <p>FedoraSparqlIT class.</p>
*
* @author cbeer
*/
@ContextConfiguration({"/spring-test/master.xml", "/spring-test/test-container.xml"})
public class FedoraSparqlIT extends AbstractResourceIT {
@Autowired
Repository repo;
private Session session;
@Before
public void setUpTestData() throws RepositoryException {
session = repo.login();
session.setNamespacePrefix("zz", "http://zz.com/");
final ValueFactory valueFactory = session.getValueFactory();
final FedoraResourceImpl fedoraResource = new FedoraResourceImpl(session, "/abc", JcrConstants.NT_FOLDER);
final FedoraResourceImpl fedoraResource2 = new FedoraResourceImpl(session, "/xyz", JcrConstants.NT_FOLDER);
final FedoraResourceImpl fedoraResource3 = new FedoraResourceImpl(session, "/anobject", JcrConstants.NT_FOLDER);
fedoraResource.getNode().setProperty("dc:title", new Value[] { valueFactory.createValue("xyz") });
fedoraResource.getNode().setProperty("fedorarelsext:hasPart",
new Value[] { valueFactory.createValue(fedoraResource2.getNode()) });
fedoraResource2.getNode().setProperty("fedorarelsext:isPartOf",
new Value[] { valueFactory.createValue(fedoraResource.getNode()) });
fedoraResource3.getNode().setProperty("zz:name", new Value[] { valueFactory.createValue("junk") });
fedoraResource3.getNode().setProperty("rdf:type", new Value[] { valueFactory.createValue("info:some-type") });
session.save();
}
@After
public void destroy() {
session.logout();
}
@Test
public void itShouldHaveAnHtmlView() throws IOException {
final HttpGet request = new HttpGet(serverAddress + "/fcr:sparql");
request.addHeader("Accept", "text/html");
final HttpResponse response = client.execute(request);
assertEquals(200, response.getStatusLine().getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
assertTrue(content.contains("SPARQL"));
assertTrue(content.contains("PREFIX dc: <http://purl.org/dc/elements/1.1/>"));
}
@Test
public void itShouldWorkWithSimpleProperties() throws IOException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> " +
"SELECT ?subject WHERE { ?subject dc:title \"xyz\"}";
final String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("subject", resultSet.getResultVars().get(0));
assertEquals(serverAddress + "/abc", resultSet.next().get("subject").toString());
}
@Test
public void itShouldWorkWithRdfTypeMixins() throws IOException {
final String sparql =
"PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT " +
"?subject WHERE { " +
"?subject a <http://fedora.info/definitions/v4/rest-api#resource> . ?subject dc:title \"xyz\"}";
final String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("subject", resultSet.getResultVars().get(0));
assertEquals(serverAddress + "/abc", resultSet.next().get("subject").toString());
}
@Test
public void itShouldWorkWithRdfTypeProperties() throws IOException {
final String sparql = "SELECT ?subject WHERE { ?subject a <info:some-type> }";
final String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("subject", resultSet.getResultVars().get(0));
assertEquals(serverAddress + "/anobject", resultSet.next().get("subject").toString());
}
@Test
public void itShouldWorkWithReferenceProperties() throws IOException {
final String sparql =
"PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#> SELECT " +
"?subject ?part WHERE { ?subject fedorarelsext:hasPart ?part }";
final String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("subject", resultSet.getResultVars().get(0));
final QuerySolution row = resultSet.next();
assertEquals(serverAddress + "/abc", row.get("subject").toString());
assertEquals(serverAddress + "/xyz", row.get("part").toString());
}
@Test
public void itShouldWorkWithJoinedQueries() throws IOException {
final String sparql = "PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#>\n" +
"PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
"SELECT ?part ?collectionTitle WHERE { ?part fedorarelsext:isPartOf ?collection .\n" +
" ?collection dc:title ?collectionTitle }";
final String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("part", resultSet.getResultVars().get(0));
final QuerySolution row = resultSet.next();
assertEquals(serverAddress + "/xyz", row.get("part").toString());
assertEquals("xyz", row.get("collectionTitle").asLiteral().getLexicalForm());
}
@Test
public void itShouldIndexCustomProperties() throws Exception {
final String sparql = "PREFIX zz: <http://zz.com/> SELECT ?subject WHERE { ?subject zz:name \"junk\"}";
final String content = getResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("subject", resultSet.getResultVars().get(0));
assertEquals(serverAddress + "/anobject", resultSet.next().get("subject").toString());
}
private String getResponseContent(final String sparql) throws IOException {
final HttpPost sparqlRequest = new HttpPost(serverAddress + "/fcr:sparql");
final BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(IOUtils.toInputStream(sparql));
sparqlRequest.setEntity(entity);
final HttpResponse response = client.execute(sparqlRequest);
assertEquals(200, response.getStatusLine().getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved sparql feed:\n" + content);
return content;
}
private String getFormRequestResponseContent(final String sparql)
throws IOException {
final HttpPost request = getFormRequest (sparql, "query");
final HttpResponse response = client.execute(request);
assertEquals(200, response.getStatusLine().getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
logger.trace("Retrieved sparql feed:\n" + content);
return content;
}
private HttpPost getFormRequest (final String sparql, final String paramName)
throws UnsupportedEncodingException {
final HttpPost post = new HttpPost(serverAddress + "/fcr:sparql");
post.addHeader("Content-Type", contentTypeHTMLForm);
final List<BasicNameValuePair> nvps = new ArrayList<>();
if (sparql != null) {
nvps.add(new BasicNameValuePair(
paramName != null && paramName.length() > 0 ? paramName : "sparql", sparql));
}
final HttpEntity formEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
post.setEntity(formEntity);
return post;
}
@Test
public void formRequestShouldWorkWithSimpleProperties() throws IOException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> " +
"SELECT ?subject WHERE { ?subject dc:title \"xyz\"}";
final String content = getFormRequestResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("subject", resultSet.getResultVars().get(0));
assertEquals(serverAddress + "/abc", resultSet.next().get("subject").toString());
}
@Test
public void formRequestShouldWorkWithRdfTypeMixins() throws IOException {
final String sparql =
"PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT " +
"?subject WHERE { " +
"?subject a <http://fedora.info/definitions/v4/rest-api#resource> . ?subject dc:title \"xyz\"}";
final String content = getFormRequestResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("subject", resultSet.getResultVars().get(0));
assertEquals(serverAddress + "/abc", resultSet.next().get("subject").toString());
}
@Test
public void formRequestShouldWorkWithReferenceProperties() throws IOException {
final String sparql =
"PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#> SELECT " +
"?subject ?part WHERE { ?subject fedorarelsext:hasPart ?part }";
final String content = getFormRequestResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("subject", resultSet.getResultVars().get(0));
final QuerySolution row = resultSet.next();
assertEquals(serverAddress + "/abc", row.get("subject").toString());
assertEquals(serverAddress + "/xyz", row.get("part").toString());
}
@Test
public void formRequestShouldWorkWithJoinedQueries() throws IOException {
final String sparql = "PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#>\n" +
"PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
"SELECT ?part ?collectionTitle WHERE { ?part fedorarelsext:isPartOf ?collection .\n" +
" ?collection dc:title ?collectionTitle }";
final String content = getFormRequestResponseContent(sparql);
final ResultSet resultSet = ResultSetFactory.fromTSV(IOUtils.toInputStream(content));
assertTrue(resultSet.hasNext());
assertEquals("part", resultSet.getResultVars().get(0));
final QuerySolution row = resultSet.next();
assertEquals(serverAddress + "/xyz", row.get("part").toString());
assertEquals("xyz", row.get("collectionTitle").asLiteral().getLexicalForm());
}
@Test
public void testBadFormRequest() throws IOException {
String sparql = "";
HttpPost badRequest = getFormRequest(sparql, null);
HttpResponse response = client.execute(badRequest);
assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatusLine().getStatusCode());
sparql = null;
badRequest = getFormRequest(sparql, null);
response = client.execute(badRequest);
assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatusLine().getStatusCode());
}
}