SoqlQueryParser implements QueryParser { @Override public QueryHolder parseQuery(String query) { CharStream cs = CharStreams.fromString(query); SoqlLexer lexer = new SoqlLexer(cs); final CommonTokenStream tokens = new CommonTokenStream(lexer); SoqlParser parser = new SoqlParser(tokens); final ParseTree tree = parser.querySentence(); final SoqlQueryListener listener = new SoqlQueryListener(this.metamodel); final ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(listener, tree); return sparqlParser.parseQuery(listener.getSoqlQuery()); } SoqlQueryParser(QueryParser sparqlParser, MetamodelImpl metamodel); @Override QueryHolder parseQuery(String query);  }
@Test public void testParseFindAllQuery(){ final String jpqlQuery = "SELECT p FROM Person p"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(1, holder.getParameters().size()); }
@Test public void testParseDistinctFindAllQuery(){ final String jpqlQuery = "SELECT DISTINCT p FROM Person p"; final String expectedSparqlQuery = "SELECT DISTINCT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(1, holder.getParameters().size()); }
@Test public void testParseCountQuery(){ final String jpqlQuery = "SELECT COUNT(p) FROM Person p"; final String expectedSparqlQuery = "SELECT (COUNT(?x) AS ?count) WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(2, holder.getParameters().size()); }
@Test public void testParseDistinctCountQuery(){ final String jpqlQuery = "SELECT DISTINCT COUNT(p) FROM Person p"; final String expectedSparqlQuery = "SELECT (COUNT(distinct ?x) AS ?count) WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(2, holder.getParameters().size()); }
@Test public void testParseFindAllOWLClassAQuery(){ final String jpqlQuery = "SELECT a FROM OWLClassA a"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(1, holder.getParameters().size()); }
@Test public void testParseFindOneQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.username = :username"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(2, holder.getParameters().size()); }
@Test public void testParseFindOneLikeQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.username LIKE :username"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(3, holder.getParameters().size()); }
@Test public void testParseFindOneJoinedQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.phone.number = :phoneNumber"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(3, holder.getParameters().size()); }
@Test public void testParseFindMutipleJoinedQuery(){ final String jpqlQuery = "SELECT a FROM OWLClassA a WHERE a.OWLClassB.OWLClassC.OWLClassD = :d"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindMutipleJoinedQueryFilter(){ final String jpqlQuery = "SELECT a FROM OWLClassA a WHERE a.OWLClassB.OWLClassC.OWLClassD.intAttribute > :intAttribute"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(6, holder.getParameters().size()); }
@Test public void testParseFindOneOrderByQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.age > :age ORDER BY p.age DESC"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(3, holder.getParameters().size()); }
@Test public void testParseFindOneOrderByNotInWhereQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.age > :age ORDER BY p.username DESC"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindOneGroupByQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.age > :age GROUP BY p.age"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(3, holder.getParameters().size()); }
@Test public void testParseFindOneGroupByNotInWhereQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.age > :age GROUP BY p.gender"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindByOneNotQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(2, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.username = :username AND p.age > :age"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotAndQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username AND p.age > :age"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndNotQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.username = :username AND NOT p.age > :age"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotAndNotQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username AND NOT p.age > :age"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindByMultipleOrQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.phone.number = :phoneNumber OR p.age > :age"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotOrQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username OR p.age > :age"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindByMultipleOrNotQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE p.username = :username OR NOT p.age > :age"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotOrNotQuery(){ final String jpqlQuery = "SELECT p FROM Person p WHERE NOT p.username = :username OR NOT p.age > :age"; final String expectedSparqlQuery = "SELECT ?x WHERE { ?x a <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(4, holder.getParameters().size()); }
@Test public void testParseFindByMultipleOrOrderByNotInWhereQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleOrGroupByNotInWhereQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndOrQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(6, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotAndOrQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(6, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndNotOrQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(6, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndOrNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(6, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotAndNotOrQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndNotOrNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotAndOrNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndAndQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotAndAndQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndNotAndQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndAndNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotAndNotAndQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleAndNotAndNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotAndAndNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotAndNotAndNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleOrOrQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotOrOrQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleOrNotOrQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleOrOrNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotOrNotOrQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleOrNotOrNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotOrOrNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }
@Test public void testParseFindByMultipleNotOrNotOrNotQuery(){ final String jpqlQuery = "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 <http: final QueryHolder holder = sut.parseQuery(jpqlQuery); assertEquals(expectedSparqlQuery, holder.getQuery()); assertEquals(5, holder.getParameters().size()); }