/
JQLConverterIT.java
345 lines (298 loc) · 17.6 KB
/
JQLConverterIT.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
344
345
/**
* 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;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.fcrepo.kernel.rdf.impl.DefaultIdentifierTranslator;
import org.fcrepo.transform.sparql.JQLConverter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.api.JcrTools;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import static org.junit.Assert.assertEquals;
/**
* <p>JQLConverterIT class.</p>
*
* @author cbeer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/spring-test/master.xml"})
public class JQLConverterIT {
@Inject
Repository repo;
private Session session;
private IdentifierTranslator subjects;
@Before
public void setUp() throws Exception {
session = repo.login();
subjects = new DefaultIdentifierTranslator();
}
@Test
public void testSimpleQuery() {
}
@Test
public void testSimpleFilterReturningJcrSubject() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?subject WHERE { ?subject " +
"dc:title \"xyz\"}";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals(
"SELECT [fedoraResource_subject].[jcr:path] AS subject FROM [fedora:resource] AS " +
"[fedoraResource_subject] WHERE [fedoraResource_subject].[dc:title] = 'xyz'",
testObj.getStatement());
}
@Test
public void testSimpleMixinRdfTypeFilter() throws RepositoryException {
final String sparql = "SELECT ?subject WHERE { ?subject a <http://fedora" +
".info/definitions/v4/rest-api#datastream>}";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals(
"SELECT [fedoraResource_subject].[jcr:path] AS subject FROM [fedora:resource] AS " +
"[fedoraResource_subject] INNER JOIN [fedora:datastream] AS [ref_type_fedora_datastream] ON " +
"ISSAMENODE([fedoraResource_subject],[ref_type_fedora_datastream],'.')",
testObj.getStatement());
}
@Test
public void testSimplePropertyRdfTypeFilter() throws RepositoryException {
final String sparql = "SELECT ?subject WHERE { ?subject a <http://some/other/uri>}";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals(
"SELECT [fedoraResource_subject].[jcr:path] AS subject FROM [fedora:resource] AS " +
"[fedoraResource_subject] WHERE [fedoraResource_subject].[rdf:type] = CAST" +
"('http://some/other/uri' AS URI)",
testObj.getStatement());
}
@Test
public void testDistinctFilterReturningJcrSubject() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT DISTINCT ?subject WHERE { " +
"?subject dc:title \"xyz\"}";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals(
"SELECT DISTINCT [fedoraResource_subject].[jcr:path] AS subject FROM [fedora:resource] AS " +
"[fedoraResource_subject] WHERE [fedoraResource_subject].[dc:title] = 'xyz'",
testObj.getStatement());
}
@Test
public void testSimpleFilterReturningJcrPropertyValue() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?title WHERE { ?subject " +
"dc:title ?title }";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals(
"SELECT [fedoraResource_subject].[dc:title] AS title FROM [fedora:resource] AS " +
"[fedoraResource_subject] WHERE [fedoraResource_subject].[dc:title] IS NOT NULL",
testObj.getStatement());
}
@Test
public void testSimpleFilterReturningJcrSubjectAndPropertyValue() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?subject ?title WHERE { " +
"?subject dc:title ?title }";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals(
"SELECT [fedoraResource_subject].[jcr:path] AS subject, [fedoraResource_subject].[dc:title] AS title " +
"FROM [fedora:resource] AS [fedoraResource_subject] WHERE [fedoraResource_subject].[dc:title]" +
" IS NOT NULL",
testObj.getStatement());
}
@Test
public void testSimpleFilterReturningJcrSubjectAndOptionalPropertyValue() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?subject ?title WHERE { " +
"OPTIONAL { ?subject dc:title ?title } }";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_subject].[jcr:path] AS subject, [fedoraResource_subject].[dc:title] AS " +
"title FROM [fedora:resource] AS [fedoraResource_subject]",
testObj.getStatement());
}
@Test
public void testSimpleFilterReturningJcrPropertyValueWithCondition() throws RepositoryException {
final Node orCreateNode = new JcrTools().findOrCreateNode(session, "/xyz", "nt:folder");
orCreateNode.addMixin("fedora:resource");
session.save();
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/> " +
"PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#>" +
"SELECT ?title WHERE { ?subject dc:title ?title . ?subject fedorarelsext:isPartOf <" +
subjects.getSubject("/xyz") + "> }";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_subject].[dc:title] AS title FROM [fedora:resource] AS " +
"[fedoraResource_subject] WHERE ([fedoraResource_subject].[dc:title] IS NOT NULL AND " +
"[fedoraResource_subject].[fedorarelsext:isPartOf] = CAST('" +
orCreateNode.getIdentifier() + "' AS REFERENCE))",
testObj.getStatement());
}
@Test
public void testSecondOrderReturnValues() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/>" +
"PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#>" +
"SELECT ?relatedTitle WHERE { " +
"?subject fedorarelsext:hasPart ?part . ?part dc:title ?relatedTitle }";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_part].[dc:title] AS relatedTitle FROM [fedora:resource] AS " +
"[fedoraResource_subject] LEFT OUTER JOIN [fedora:resource] AS " +
"[fedoraResource_part] ON [fedoraResource_subject].[fedorarelsext:hasPart] = " +
"[fedoraResource_part].[jcr:uuid] WHERE (" +
"[fedoraResource_subject].[fedorarelsext:hasPart] IS NOT NULL AND " +
"[fedoraResource_part].[dc:title] IS NOT NULL)",
testObj.getStatement());
}
@Test
public void testJoinAndSecondOrderReturnValues() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/>" +
"PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#>" +
"SELECT ?subject ?relatedTitle WHERE { " +
"?subject fedorarelsext:hasPart ?part . ?part dc:title ?relatedTitle }";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_subject].[jcr:path] AS subject, [fedoraResource_part].[dc:title] AS " +
"relatedTitle FROM [fedora:resource] AS [fedoraResource_subject] LEFT OUTER JOIN " +
"[fedora:resource] AS [fedoraResource_part] ON " +
"[fedoraResource_subject].[fedorarelsext:hasPart] = " +
"[fedoraResource_part].[jcr:uuid] WHERE (" +
"[fedoraResource_subject].[fedorarelsext:hasPart] IS NOT NULL AND " +
"[fedoraResource_part].[dc:title] IS NOT NULL)",
testObj.getStatement());
}
@Test
public void testRegexFilter() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
"SELECT ?title \n" +
"WHERE { ?x dc:title ?title\n" +
" FILTER regex(?title, \"^SPARQL\")\n" +
"}";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_x].[dc:title] AS title FROM [fedora:resource] AS " +
"[fedoraResource_x] WHERE ([fedoraResource_x].[dc:title] IS NOT NULL AND " +
"[fedoraResource_x].[dc:title] LIKE '^SPARQL')",
testObj.getStatement());
}
@Test
public void testContainsFilter() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
"SELECT ?title \n" +
"WHERE { ?x dc:title ?title\n" +
" FILTER contains(?title, \"SPARQL\")\n" +
"}";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_x].[dc:title] AS title FROM [fedora:resource] AS " +
"[fedoraResource_x] WHERE ([fedoraResource_x].[dc:title] IS NOT NULL AND " +
"[fedoraResource_x].[dc:title] LIKE '%SPARQL%')",
testObj.getStatement());
}
@Test
public void testStrstartsFilter() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
"SELECT ?title \n" +
"WHERE { ?x dc:title ?title\n" +
" FILTER strStarts(?title, \"SPARQL\")\n" +
"}";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_x].[dc:title] AS title FROM [fedora:resource] AS " +
"[fedoraResource_x] WHERE ([fedoraResource_x].[dc:title] IS NOT NULL AND " +
"[fedoraResource_x].[dc:title] LIKE 'SPARQL%')",
testObj.getStatement());
}
@Test
public void testStrendsFilter() throws RepositoryException {
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
"SELECT ?title \n" +
"WHERE { ?x dc:title ?title\n" +
" FILTER strEnds(?title, \"SPARQL\")\n" +
"}";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals("SELECT [fedoraResource_x].[dc:title] AS title FROM [fedora:resource] AS " +
"[fedoraResource_x] WHERE ([fedoraResource_x].[dc:title] IS NOT NULL AND " +
"[fedoraResource_x].[dc:title] LIKE '%SPARQL')",
testObj.getStatement());
}
@Test
public void testComplexQuery() throws RepositoryException {
final String sparql = "PREFIX ns: <http://libraries.ucsd.edu/ark:/20775/>"
+ " SELECT DISTINCT ?subject ?object WHERE {" +
"?subject ns:bb2765355h 'bf2765355h' . ?subject ns:bb3652744n ?object . FILTER regex(" +
"?object, \"r\", \"i\") .FILTER (?object >= 'abc' && ?object < 'efg' || !(?object = 'efg')) } " +
" ORDER BY DESC(?subject) ?object LIMIT 10 OFFSET 20";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
final String statement = testObj.getStatement();
final String namespacePrefix = session.getNamespacePrefix("http://libraries.ucsd.edu/ark:/20775/");
final String expectedQuery =
"SELECT DISTINCT [fedoraResource_subject].[jcr:path] AS subject, " +
"[fedoraResource_subject].[ns001:bb3652744n] AS object FROM [fedora:resource] AS " +
"[fedoraResource_subject] WHERE ((([fedoraResource_subject].[ns001:bb2765355h] = " +
"'bf2765355h' AND [fedoraResource_subject].[ns001:bb3652744n] IS NOT NULL) AND " +
"[fedoraResource_subject].[ns001:bb3652744n] LIKE 'r') AND " +
"(([fedoraResource_subject].[ns001:bb3652744n] >= 'abc' AND " +
"[fedoraResource_subject].[ns001:bb3652744n] < 'efg') OR NOT " +
"([fedoraResource_subject].[ns001:bb3652744n] = 'efg'))) ORDER BY " +
"[fedoraResource_subject].[jcr:path] DESC, " +
"[fedoraResource_subject].[ns001:bb3652744n] ASC LIMIT 10 OFFSET 20";
assertEquals(expectedQuery.replaceAll("ns001", namespacePrefix), statement);
}
@Test
public void testConstantSubjectQuery() throws RepositoryException {
final String path = "/foo";
final String selector = "fedoraResource_" + path.replace("/", "_");
final String baseUri = subjects.getBaseUri();
final String subjectUri = (baseUri.endsWith("/") ? baseUri.substring(0, baseUri.length() - 1) : baseUri) + path;
final String sparql = "PREFIX fcrepo: <http://fedora.info/definitions/v4/repository#> "
+ "select ?date where { <" + subjectUri + "> fcrepo:created ?date }";
final String expectedQuery =
"SELECT [" + selector + "].[jcr:created] AS date " +
"FROM [fedora:resource] AS [" + selector + "] " +
"WHERE ([" + selector + "].[jcr:path] = '" + path + "' AND " +
"[" + selector + "].[jcr:created] IS NOT NULL)";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals(expectedQuery, testObj.getStatement());
}
@Test
public void testConstantSubjectSimpleReferenceQuery() throws RepositoryException {
final String path = "/foo";
final String selector = "fedoraResource_" + path.replace("/", "_");
final String baseUri = subjects.getBaseUri();
final String subjectUri = (baseUri.endsWith("/") ? baseUri.substring(0, baseUri.length() - 1) : baseUri) + path;
final String sparql =
"PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#> " +
"SELECT ?part WHERE { <" + subjectUri + "> fedorarelsext:hasPart ?part }";
final String expectedQuery =
"SELECT [" + selector + "].[fedorarelsext:hasPart] AS part " +
"FROM [fedora:resource] AS [" + selector + "] " +
"WHERE ([" + selector + "].[jcr:path] = '" + path + "' AND " +
"[" + selector + "].[fedorarelsext:hasPart] IS NOT NULL)";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals(expectedQuery, testObj.getStatement());
}
@Test
public void testConstantSubjectReferenceQuery() throws RepositoryException {
final String path = "/foo";
final String selector = "fedoraResource_" + path.replace("/", "_");
final String baseUri = subjects.getBaseUri();
final String subjectUri = (baseUri.endsWith("/") ? baseUri.substring(0, baseUri.length() - 1) : baseUri) + path;
final String sparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/>" +
"PREFIX fedorarelsext: <http://fedora.info/definitions/v4/rels-ext#>" +
"SELECT ?title WHERE { <" + subjectUri + "> fedorarelsext:hasPart ?part . " +
"?part dc:title ?title }";
final String expectedQuery =
"SELECT [fedoraResource_part].[dc:title] AS title FROM [fedora:resource] AS " +
"[" + selector + "] LEFT OUTER JOIN [fedora:resource] AS [fedoraResource_part] ON " +
"[" + selector + "].[fedorarelsext:hasPart] = [fedoraResource_part].[jcr:uuid] " +
"WHERE (([" + selector + "].[jcr:path] = '" + path + "' AND " +
"[" + selector + "].[fedorarelsext:hasPart] IS NOT NULL) AND " +
"[fedoraResource_part].[dc:title] IS NOT NULL)";
final JQLConverter testObj = new JQLConverter(session, subjects, sparql);
assertEquals(expectedQuery, testObj.getStatement());
}
}