Search in sources :

Example 1 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class SesameBoundVariablesTest method checkGetAsStringCanConvertAllTypesToTheirStringForm.

@Test
public void checkGetAsStringCanConvertAllTypesToTheirStringForm() {
    String turtle = "@prefix ex: <http://example.com/ontology#> . " + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . " + "<http://example.com/1> ex:is _:foo . " + "<http://example.com/2> ex:is \"true\"^^xsd:boolean . " + "<http://example.com/3> ex:is \"3\"^^xsd:integer . " + "<http://example.com/4> ex:is \"ho hum\"^^xsd:string . " + "<http://example.com/5> ex:is <http://yadda/> . ";
    graph.addTriplesFromTurtle(turtle);
    String sparqlSelect = "PREFIX ex: <http://example.com/ontology#> " + "SELECT ?blankNode ?boolean ?int ?string ?uri ?unbound WHERE { " + "<http://example.com/1> ex:is ?blankNode . " + "<http://example.com/2> ex:is ?boolean . " + "<http://example.com/3> ex:is ?int . " + "<http://example.com/4> ex:is ?string . " + "<http://example.com/5> ex:is ?uri . " + "OPTIONAL { <http://example.com/6> ex:is ?unbound }" + "}";
    BoundVariables bv = graph.selectFirst(sparqlSelect);
    assertNotNull(bv.getAsString("blankNode"));
    assertEquals("true", bv.getAsString("boolean"));
    assertEquals("3", bv.getAsString("int"));
    assertEquals("ho hum", bv.getAsString("string"));
    assertEquals("http://yadda/", bv.getAsString("uri"));
    assertNull(bv.getAsString("unbound"));
    try {
        bv.getAsString("baz");
        fail("Illegal argument exception expected.");
    } catch (IllegalArgumentException e) {
        assertEquals("boundName 'baz' is not in the list of possible values ('blankNode', 'boolean', 'int', 'string', 'uri', 'unbound')", e.getMessage());
    }
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) Test(org.junit.Test)

Example 2 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class SesameBoundVariablesTest method checkGetURIReference.

@Test
public void checkGetURIReference() {
    String turtle = "@prefix ex: <http://example.com/ontology#> . " + "<http://example.com/1> ex:is <http://hello> . ";
    graph.addTriplesFromTurtle(turtle);
    String sparqlSelect = "PREFIX ex: <http://example.com/ontology#> " + "SELECT ?one ?two WHERE { " + "<http://example.com/1> ex:is ?one . " + "OPTIONAL { [] ex:nope ?two } " + "}";
    BoundVariables bv = graph.selectFirst(sparqlSelect);
    assertEquals("http://hello", bv.getURIReference("one").getURIText());
    assertNull(bv.getURIReference("two"));
    try {
        bv.getURIReference("baz");
        fail("Illegal argument exception expected.");
    } catch (IllegalArgumentException e) {
        assertEquals("boundName 'baz' is not in the list of possible values ('one', 'two')", e.getMessage());
    }
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) Test(org.junit.Test)

Example 3 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class SesameBoundVariablesTest method checkGetString.

@Test
public void checkGetString() {
    String turtle = "@prefix ex: <http://example.com/ontology#> . " + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . " + "<http://example.com/1> ex:is \"won\"^^xsd:string . " + "<http://example.com/2> ex:is \"too\"^^xsd:string . ";
    graph.addTriplesFromTurtle(turtle);
    String sparqlSelect = "PREFIX ex: <http://example.com/ontology#> " + "SELECT ?one ?two ?three WHERE { " + "<http://example.com/1> ex:is ?one . " + "<http://example.com/2> ex:is ?two . " + "OPTIONAL { <http://example.com/3> ex:is ?three } " + "}";
    BoundVariables bv = graph.selectFirst(sparqlSelect);
    assertEquals("won", bv.getString("one"));
    assertEquals("too", bv.getString("two"));
    assertNull(bv.getString("three"));
    try {
        bv.getString("baz");
        fail("Illegal argument exception expected.");
    } catch (IllegalArgumentException e) {
        assertEquals("boundName 'baz' is not in the list of possible values ('one', 'two', 'three')", e.getMessage());
    }
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) Test(org.junit.Test)

Example 4 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class SesameBoundVariablesTest method checkGetInt.

@Test
public void checkGetInt() {
    String turtle = "@prefix ex: <http://example.com/ontology#> . " + "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . " + "<http://example.com/1> ex:is \"1\"^^xsd:integer . " + "<http://example.com/2> ex:is \"2\"^^xsd:integer . ";
    graph.addTriplesFromTurtle(turtle);
    String sparqlSelect = "PREFIX ex: <http://example.com/ontology#> " + "SELECT ?one ?two ?three WHERE { " + "<http://example.com/1> ex:is ?one . " + "<http://example.com/2> ex:is ?two . " + "OPTIONAL { <http://example.com/3> ex:is ?three } " + "}";
    BoundVariables bv = graph.selectFirst(sparqlSelect);
    assertEquals((Integer) 1, bv.getInt("one"));
    assertEquals((Integer) 2, bv.getInt("two"));
    assertNull(bv.getInt("three"));
    try {
        bv.getInt("baz");
        fail("Illegal argument exception expected.");
    } catch (IllegalArgumentException e) {
        assertEquals("boundName 'baz' is not in the list of possible values ('one', 'two', 'three')", e.getMessage());
    }
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) Test(org.junit.Test)

Example 5 with BoundVariables

use of com.thoughtworks.studios.shine.semweb.BoundVariables in project gocd by gocd.

the class SesameBoundVariablesTest method shouldBeSerializable.

@Test
public void shouldBeSerializable() throws Exception {
    String turtle = "<http://s1> <http://p1> <http://o1> . ";
    graph.addTriplesFromTurtle(turtle);
    String sparqlSelect = "SELECT ?s ?p ?o { ?s ?p ?o . }";
    List<BoundVariables> bvs = graph.select(sparqlSelect);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ObjectOutputStream objectOS = new ObjectOutputStream(os);
    objectOS.writeObject(bvs);
    ObjectInputStream objectIS = new ObjectInputStream(new ByteArrayInputStream(os.toByteArray()));
    List<BoundVariables> bvsFromInputStream = (List<BoundVariables>) objectIS.readObject();
    assertEquals(1, bvsFromInputStream.size());
    assertEquals("http://s1", bvsFromInputStream.get(0).getAsString("s"));
    assertEquals("http://p1", bvsFromInputStream.get(0).getAsString("p"));
    assertEquals("http://o1", bvsFromInputStream.get(0).getAsString("o"));
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

BoundVariables (com.thoughtworks.studios.shine.semweb.BoundVariables)16 Test (org.junit.Test)10 StageIdentifier (com.thoughtworks.go.domain.StageIdentifier)3 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)3 Graph (com.thoughtworks.studios.shine.semweb.Graph)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)2 TupleQueryResult (org.openrdf.query.TupleQueryResult)2 MoreThanOneResultFoundException (com.thoughtworks.studios.shine.semweb.MoreThanOneResultFoundException)1 InMemoryTempGraphFactory (com.thoughtworks.studios.shine.semweb.sesame.InMemoryTempGraphFactory)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 LinkedList (java.util.LinkedList)1