-
Notifications
You must be signed in to change notification settings - Fork 14
/
SoqlQueryParserTest.java
751 lines (678 loc) · 42.9 KB
/
SoqlQueryParserTest.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/**
* Copyright (C) 2023 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.soql;
import cz.cvut.kbss.jopa.environment.Vocabulary;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.model.MetamodelImpl;
import cz.cvut.kbss.jopa.query.QueryHolder;
import cz.cvut.kbss.jopa.query.QueryParser;
import cz.cvut.kbss.jopa.query.parameter.ParameterValueFactory;
import cz.cvut.kbss.jopa.query.sparql.SparqlQueryParser;
import cz.cvut.kbss.jopa.sessions.MetamodelProvider;
import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class SoqlQueryParserTest {
@Mock
private MetamodelImpl metamodel;
private QueryParser sut;
@BeforeEach
void setUp() throws Exception {
MetamodelMocks mocks = new MetamodelMocks();
mocks.setMocks(metamodel);
final MetamodelProvider mpp = mock(MetamodelProvider.class);
when(mpp.getMetamodel()).thenReturn(metamodel);
when(mpp.isEntityType(any())).thenAnswer(inv -> metamodel.isEntityType(inv.getArgument(0)));
final SparqlQueryParser qp = new SparqlQueryParser(new ParameterValueFactory(mpp));
this.sut = new SoqlQueryParser(qp, metamodel);
}
@Test
public void testParseFindAllQuery() {
final String soqlQuery = "SELECT p FROM Person p";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(1, holder.getParameters().size());
}
@Test
public void testParseDistinctFindAllQuery() {
final String soqlQuery = "SELECT DISTINCT p FROM Person p";
final String expectedSparqlQuery =
"SELECT DISTINCT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(1, holder.getParameters().size());
}
@Test
public void testParseCountQuery() {
final String soqlQuery = "SELECT COUNT(p) FROM Person p";
final String expectedSparqlQuery =
"SELECT (COUNT(?x) AS ?count) WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(2, holder.getParameters().size());
}
@Test
public void testParseDistinctCountQuery() {
final String soqlQuery = "SELECT DISTINCT COUNT(p) FROM Person p";
final String expectedSparqlQuery =
"SELECT (COUNT(DISTINCT ?x) AS ?count) WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(2, holder.getParameters().size());
}
@Test
public void testParseFindAllOWLClassAQuery() {
final String soqlQuery = "SELECT a FROM OWLClassA a";
final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <" + Vocabulary.c_OwlClassA + "> . }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(1, holder.getParameters().size());
}
@Test
public void testParseFindOneQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.username = :uname";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?uname . }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(2, holder.getParameters().size());
}
@Test
public void testParseFindOneLikeQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.username LIKE :username";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?pUsername . FILTER (REGEX(?pUsername, ?username)) }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(3, holder.getParameters().size());
}
@Test
public void testParseFindOneJoinedQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.phone.number = :phoneNumber";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x <" + Vocabulary.p_p_hasPhone + "> ?phone . ?phone <" + Vocabulary.p_p_phoneNumber + "> ?phoneNumber . }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(3, holder.getParameters().size());
}
@Test
public void testParseFindMultipleJoinedQuery() {
final String soqlQuery = "SELECT g FROM OWLClassG g WHERE g.owlClassH.owlClassA.stringAttribute = :d";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a <" + Vocabulary.c_OwlClassG + "> . ?x <" + Vocabulary.p_g_hasH + "> ?owlClassH . ?owlClassH <" + Vocabulary.p_h_hasA + "> ?owlClassA . ?owlClassA <" + Vocabulary.p_a_stringAttribute + "> ?d . }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindMultipleJoinedQueryFilter() {
final String soqlQuery = "SELECT g FROM OWLClassG g WHERE g.owlClassH.owlClassA.stringAttribute > :str";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a <" + Vocabulary.c_OwlClassG + "> . ?x <" + Vocabulary.p_g_hasH + "> ?owlClassH . ?owlClassH <" + Vocabulary.p_h_hasA + "> ?owlClassA . ?owlClassA <" + Vocabulary.p_a_stringAttribute + "> ?gOwlClassHOwlClassAStringAttribute . FILTER (?gOwlClassHOwlClassAStringAttribute > ?str) }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindOneOrderByQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.age > :age ORDER BY p.age DESC";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } ORDER BY DESC(?pAge) ";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(3, holder.getParameters().size());
}
@Test
public void testParseFindOneOrderByNotInWhereQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.age > :age ORDER BY p.username DESC";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } ORDER BY DESC(?username) ";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindOneGroupByQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.age > :age GROUP BY p.age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } GROUP BY ?pAge ";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(3, holder.getParameters().size());
}
@Test
public void testParseFindOneGroupByNotInWhereQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.age > :age GROUP BY p.gender";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x <" + Vocabulary.p_p_gender + "> ?gender . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } GROUP BY ?gender ";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindByOneNotQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(2, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.username = :username AND p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotAndQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username AND p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndNotQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.username = :username AND NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?username . FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotAndNotQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username AND NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleOrQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.phone.number = :phoneNumber OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x <" + Vocabulary.p_p_hasPhone + "> ?phone . ?phone <" + Vocabulary.p_p_phoneNumber + "> ?phoneNumber . } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotOrQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleOrNotQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.username = :username OR NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotOrNotQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username OR NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(4, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleOrOrderByNotInWhereQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.age > :age OR p.gender = :gender ORDER BY p.username DESC";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?username . { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } UNION { ?x <" + Vocabulary.p_p_gender + "> ?gender . } } ORDER BY DESC(?username) ";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleOrGroupByNotInWhereQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.age > :age OR p.gender = :gender GROUP BY p.username DESC";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?username . { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } UNION { ?x <" + Vocabulary.p_p_gender + "> ?gender . } } GROUP BY ?username ";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndOrQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.phone.number = :phoneNumber AND p.gender = :gender OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x <" + Vocabulary.p_p_hasPhone + "> ?phone . ?phone <" + Vocabulary.p_p_phoneNumber + "> ?phoneNumber . ?x <" + Vocabulary.p_p_gender + "> ?gender . } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(6, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotAndOrQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.phone.number = :phoneNumber AND p.gender = :gender OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x <" + Vocabulary.p_p_gender + "> ?gender . FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_hasPhone + "> ?phone . ?phone <" + Vocabulary.p_p_phoneNumber + "> ?phoneNumber . } } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(6, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndNotOrQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.phone.number = :phoneNumber AND NOT p.gender = :gender OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x <" + Vocabulary.p_p_hasPhone + "> ?phone . ?phone <" + Vocabulary.p_p_phoneNumber + "> ?phoneNumber . FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_gender + "> ?gender . } } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(6, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndOrNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.phone.number = :phoneNumber AND p.gender = :gender OR NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x <" + Vocabulary.p_p_hasPhone + "> ?phone . ?phone <" + Vocabulary.p_p_phoneNumber + "> ?phoneNumber . ?x <" + Vocabulary.p_p_gender + "> ?gender . } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(6, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotAndNotOrQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username AND NOT p.gender = :gender OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_gender + "> ?gender . } } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndNotOrNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.username = :username AND NOT p.gender = :gender OR NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x " + strUri(Vocabulary.p_p_username) + " ?username . FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_gender + "> ?gender . } } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotAndOrNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username AND p.gender = :gender OR NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x <" + Vocabulary.p_p_gender + "> ?gender . FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndAndQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.username = :username AND p.gender = :gender AND p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_gender + "> ?gender . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotAndAndQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username AND p.gender = :gender AND p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x <" + Vocabulary.p_p_gender + "> ?gender . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndNotAndQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.username = :username AND NOT p.gender = :gender AND p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_gender + "> ?gender . } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndAndNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.username = :username AND p.gender = :gender AND NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_gender + "> ?gender . FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotAndNotAndQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username AND NOT p.gender = :gender AND p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_age) + " ?pAge . FILTER (?pAge > ?age) FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_gender + "> ?gender . } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleAndNotAndNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.username = :username AND NOT p.gender = :gender AND NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?username . FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_gender + "> ?gender . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotAndAndNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username AND p.gender = :gender AND NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x <" + Vocabulary.p_p_gender + "> ?gender . FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotAndNotAndNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username AND NOT p.gender = :gender AND NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . ?x <" + Vocabulary.p_p_gender + "> ?gender . ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleOrOrQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.username = :username OR p.gender = :gender OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } UNION { ?x <" + Vocabulary.p_p_gender + "> ?gender . } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotOrOrQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username OR p.gender = :gender OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } } UNION { ?x <" + Vocabulary.p_p_gender + "> ?gender . } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleOrNotOrQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.username = :username OR NOT p.gender = :gender OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_gender + "> ?gender . } } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleOrOrNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.username = :username OR p.gender = :gender OR NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } UNION { ?x <" + Vocabulary.p_p_gender + "> ?gender . } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotOrNotOrQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username OR NOT p.gender = :gender OR p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_gender + "> ?gender . } } UNION { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleOrNotOrNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE p.username = :username OR NOT p.gender = :gender OR NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_gender + "> ?gender . } } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotOrOrNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username OR p.gender = :gender OR NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } } UNION { ?x <" + Vocabulary.p_p_gender + "> ?gender . } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
public void testParseFindByMultipleNotOrNotOrNotQuery() {
final String soqlQuery =
"SELECT p FROM Person p WHERE NOT p.username = :username OR NOT p.gender = :gender OR NOT p.age > :age";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . { FILTER NOT EXISTS { ?x " + strUri(Vocabulary.p_p_username) + " ?username . } } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_gender + "> ?gender . } } UNION { FILTER NOT EXISTS { ?x <" + Vocabulary.p_p_age + "> ?pAge . FILTER (?pAge > ?age) } } }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(5, holder.getParameters().size());
}
@Test
void testParseFindByAttributeValueInVariable() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.username IN :authorizedUsers";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?pUsername . FILTER (?pUsername IN (?authorizedUsers)) }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertNotNull(holder.getParameter("authorizedUsers"));
}
@Test
void testParseFindByAttributeValueNotInVariable() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.username NOT IN :authorizedUsers";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?pUsername . FILTER (?pUsername NOT IN (?authorizedUsers)) }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertNotNull(holder.getParameter("authorizedUsers"));
}
@Test
void testParseFindByAttributeValueInVariableWrappedInParentheses() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.username IN (:authorizedUsers)";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?pUsername . FILTER (?pUsername IN (?authorizedUsers)) }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertNotNull(holder.getParameter("authorizedUsers"));
}
@Test
public void testParseFindOneNotLikeQuery() {
final String soqlQuery = "SELECT p FROM Person p WHERE p.username NOT LIKE :username";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?pUsername . FILTER (!REGEX(?pUsername, ?username)) }";
final QueryHolder holder = sut.parseQuery(soqlQuery);
assertEquals(expectedSparqlQuery, holder.getQuery());
assertEquals(3, holder.getParameters().size());
assertNotNull(holder.getParameter("username"));
}
@Test
public void testParseInequalityOperatorQuery() {
final String standardSoql = "SELECT p FROM Person p WHERE p.username <> :username";
final String javaLikeSoql = "SELECT p FROM Person p WHERE p.username != :username";
final String expectedSparqlQuery =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . ?x " + strUri(Vocabulary.p_p_username) + " ?pUsername . FILTER (?pUsername != ?username) }";
parseAndAssertEquality(standardSoql, expectedSparqlQuery);
parseAndAssertEquality(javaLikeSoql, expectedSparqlQuery);
}
private void parseAndAssertEquality(String soql, String expectedSparql) {
final QueryHolder holder = sut.parseQuery(soql);
assertEquals(expectedSparql, holder.getQuery());
}
@Test
void testParseQueryWithIdentifierVariableInEqualityExpression() {
final String soql = "SELECT p FROM Person p WHERE p.uri = :pUri";
final String expectedSparql =
"SELECT ?pUri WHERE { ?pUri a " + strUri(Vocabulary.c_Person) + " . }";
parseAndAssertEquality(soql, expectedSparql);
}
@Test
void testParseQueryWithIdentifierVariableInInExpression() {
final String soql = "SELECT p FROM Person p WHERE p.uri IN :uris";
final String expectedSparql = "SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . FILTER (?x IN (?uris)) }";
parseAndAssertEquality(soql, expectedSparql);
}
@Test
void testParseQueryWithIdentifierVariableInInExpressionAccessedViaAttributeChain() {
final String soql = "SELECT p FROM Person p WHERE p.phone.uri IN :uris";
final String expectedSparql =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . " +
"?x <" + Vocabulary.p_p_hasPhone + "> ?phone . FILTER (?phone IN (?uris)) }";
parseAndAssertEquality(soql, expectedSparql);
}
@Test
void testParseQueryWithIdentifierVariableAccessViaAttributeChain() {
final String soql = "SELECT p FROM Person p WHERE p.phone.uri = :phoneUri";
final String expectedSparql =
"SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_Person) + " . " +
"?x <" + Vocabulary.p_p_hasPhone + "> ?phoneUri . }";
parseAndAssertEquality(soql, expectedSparql);
}
@ParameterizedTest
@CsvSource({
"UPPER, UCASE",
"LOWER, LCASE"
})
void testParseQueryWithStringCapitalizationFunctionsGeneratesFilterWithSparqlFunctions(String soqlFunction,
String sparqlFunction) {
final String soql = "SELECT p FROM Person p WHERE " + soqlFunction + "(p.username) = :username";
final String expectedSparql = "SELECT ?x WHERE { " +
"?x a " + strUri(Vocabulary.c_Person) + " . " +
"?x " + strUri(Vocabulary.p_p_username) + " ?pUsername . " +
"FILTER (" + sparqlFunction + "(?pUsername) = ?username) }";
parseAndAssertEquality(soql, expectedSparql);
}
@Test
void testParseQueryWithUpperAndLikeOperators() {
final String soql = "SELECT p FROM Person p WHERE UPPER(p.username) LIKE :value";
final String expectedSparql = "SELECT ?x WHERE { " +
"?x a " + strUri(Vocabulary.c_Person) + " . " +
"?x " + strUri(Vocabulary.p_p_username) + " ?pUsername . " +
"FILTER (REGEX(UCASE(?pUsername), ?value)) }";
parseAndAssertEquality(soql, expectedSparql);
}
@Test
void testParseQueryWithMultipleInequalityOperatorsOnSingleAttribute() {
final String soql = "SELECT p FROM Person p WHERE p.age >= :minAge AND p.age < :maxAge";
final String expectedSparql = "SELECT ?x WHERE { " +
"?x a " + strUri(Vocabulary.c_Person) + " . " +
"?x <" + Vocabulary.p_p_age + "> ?pAge . " +
"FILTER (?pAge >= ?minAge && ?pAge < ?maxAge) }";
parseAndAssertEquality(soql, expectedSparql);
}
@Test
void testParseQueryWithLength() {
final String soql = "SELECT p FROM Person p WHERE LENGTH(p.username) <= :minLength";
final String expectedSparql = "SELECT ?x WHERE { " +
"?x a " + strUri(Vocabulary.c_Person) + " . " +
"?x " + strUri(Vocabulary.p_p_username) + " ?pUsername . " +
"FILTER (STRLEN(?pUsername) <= ?minLength) }";
parseAndAssertEquality(soql, expectedSparql);
}
@ParameterizedTest
@CsvSource({
SoqlConstants.Functions.ABS + ", ABS",
SoqlConstants.Functions.CEIL + ", CEIL",
SoqlConstants.Functions.FLOOR + ", FLOOR",
})
void testParseQueryWithNumericFunction(String soqlFunction, String sparqlFunction) {
final String soql = "SELECT m FROM OWLClassM m WHERE " + soqlFunction + "(m.doubleAttribute) = :value";
final String expectedSparql = "SELECT ?x WHERE { " +
"?x a " + strUri(Vocabulary.c_OwlClassM) + " . " +
"?x " + strUri(Vocabulary.p_m_doubleAttribute) + " ?mDoubleAttribute . " +
"FILTER (" + sparqlFunction + "(?mDoubleAttribute) = ?value) }";
parseAndAssertEquality(soql, expectedSparql);
}
private static String strUri(String uri) {
return IdentifierTransformer.stringifyIri(uri);
}
@Test
void parseQueryWithIdentifierAndAttributeIsCommutative() {
final String soqlIdFirst = "SELECT p FROM Person p WHERE p.uri = :uri AND p.username = :username";
final String soqlIdSecond = "SELECT p FROM Person p WHERE p.username = :username AND p.uri = :uri";
final String expectedSparql = "SELECT ?uri WHERE { ?uri a " + strUri(Vocabulary.c_Person) + " . " +
"?uri " + strUri(Vocabulary.p_p_username) + " ?username . }";
parseAndAssertEquality(soqlIdFirst, expectedSparql);
parseAndAssertEquality(soqlIdSecond, expectedSparql);
}
/**
* Bug #178
*/
@Test
void parseQueryWithRelatedAttributeAndIdentifierIsCommutative() {
final String soqlIdFirst = "SELECT d FROM OWLClassD d WHERE d.uri = :uri AND d.owlClassA.stringAttribute = :stringAtt";
final String soqlIdSecond = "SELECT d FROM OWLClassD d WHERE d.owlClassA.stringAttribute = :stringAtt AND d.uri = :uri";
final String expectedSparql = "SELECT ?uri WHERE { ?uri a " + strUri(Vocabulary.c_OwlClassD) + " . " +
"?uri " + strUri(Vocabulary.p_h_hasA) + " ?owlClassA . " +
"?owlClassA " + strUri(Vocabulary.p_a_stringAttribute) + " ?stringAtt . }";
parseAndAssertEquality(soqlIdFirst, expectedSparql);
parseAndAssertEquality(soqlIdSecond, expectedSparql);
}
@Test
void parseQuerySupportsLangExtractionAndComparison() {
final String soql = "SELECT u FROM OWLClassU u WHERE LANG(u.singularStringAtt) = :language";
final String expectedSparql = "SELECT ?x WHERE { ?x a " + strUri(Vocabulary.c_OwlClassU) + " . " +
"?x " + strUri(Vocabulary.P_U_SINGULAR_MULTILINGUAL_ATTRIBUTE) + " ?uSingularStringAtt . " +
"FILTER (lang(?uSingularStringAtt) = ?language) }";
parseAndAssertEquality(soql, expectedSparql);
}
}