QueryInvestigator { public static boolean isInsertWhere(final String sparql) throws MalformedQueryException { requireNonNull(sparql); try { PARSER.parseUpdate(sparql, null); return Pattern.matches("[\\s\\S]*?insert\\s*?\\{[\\s\\S]*?\\}[\\s\\S]*?where\\s*\\{[\\s\\S]*?\\}", sparql.toLowerCase()); } catch(final MalformedQueryException updateE) { try { PARSER.parseQuery(sparql, null); return false; } catch(final MalformedQueryException queryE) { throw updateE; } } } private  QueryInvestigator(); static boolean isConstruct(final String sparql); static boolean isInsertWhere(final String sparql);  }
@Test public void isInsert_true() throws Exception { final String sparql = "PREFIX Sensor: <http: "INSERT { " + "?subject Sensor:test2 ?newValue " + "} WHERE {" + "values (?oldValue ?newValue) {" + "('testValue1' 'newValue1')" + "('testValue2' 'newValue2')" + "}" + "?subject Sensor:test1 ?oldValue" + "}"; assertTrue( QueryInvestigator.isInsertWhere(sparql) ); }
@Test public void isInsert_false_notAnInsert() throws Exception { final String sparql = "PREFIX dc: <http: "PREFIX xsd: <http: "DELETE { " + "?book ?p ?v " + "} WHERE { " + "?book dc:date ?date . " + "FILTER ( ?date < \"2000-01-01T00:00:00\"^^xsd:dateTime ) " + "?book ?p ?v " + "}"; assertFalse( QueryInvestigator.isInsertWhere(sparql) ); }
@Test public void isInsert_false_notAnInsertWithKeywords() throws Exception { final String sparql = "DELETE" + "{ " + " ?bookInsert ?p ?o" + "}" + "WHERE" + "{ " + " ?bookInsert <urn:datePrinted> ?datePrinted ." + " FILTER ( ?datePrinted < \"2018-01-01T00:00:00\"^^xsd:dateTime )" + " ?bookInsert ?p ?o" + "}"; assertFalse( QueryInvestigator.isInsertWhere(sparql) ); }
@Test public void isInsert_false_notAnUpdate() throws Exception { final String sparql = "SELECT * WHERE { ?a ?b ?c . }"; assertFalse( QueryInvestigator.isInsertWhere(sparql) ); }
@Test(expected = MalformedQueryException.class) public void isInsert_false_malformed() throws MalformedQueryException { assertFalse( QueryInvestigator.isInsertWhere("not sparql") ); }