Search in sources :

Example 81 with Session

use of org.neo4j.driver.v1.Session in project rdf2neo by Rothamsted.

the class CypherHandlersIT method testRelations.

/**
 * Tests {@link CyRelationLoadingHandler} to see if relations are mapped from RDF and loaded into Neo4J.
 */
@Test
public void testRelations() throws Exception {
    try (Driver neoDriver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "test"));
        CyRelationLoadingHandler handler = new CyRelationLoadingHandler();
        RdfDataManager rdfMgr = new RdfDataManager(RdfDataManagerTest.TDB_PATH);
        Neo4jDataManager neoMgr = new Neo4jDataManager(neoDriver)) {
        handler.setRdfDataManager(rdfMgr);
        handler.setNeo4jDataManager(neoMgr);
        handler.setRelationTypesSparql(RdfDataManagerTest.SPARQL_REL_TYPES);
        handler.setRelationPropsSparql(RdfDataManagerTest.SPARQL_REL_PROPS);
        Set<QuerySolution> relSparqlRows = new HashSet<>();
        Dataset dataSet = rdfMgr.getDataSet();
        Txn.executeRead(dataSet, () -> SparqlUtils.select(RdfDataManagerTest.SPARQL_REL_TYPES, rdfMgr.getDataSet().getDefaultModel()).forEachRemaining(row -> relSparqlRows.add(row)));
        handler.accept(relSparqlRows);
        Session session = neoDriver.session(AccessMode.READ);
        StatementResult cursor = session.run("MATCH ()-[r]->() RETURN COUNT ( r ) AS ct");
        Assert.assertEquals("Wrong count for relations", 3, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH p = (:TestNode{ iri:$iri1 })-[:relatedTo]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) AS ct", parameters("iri1", iri("ex:1"), "iri2", iri("ex:2")));
        Assert.assertEquals("Wrong count for {1 relatedTo 2}!", 1, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH p = (:SuperTestNode{ iri:$iri1 })-[:derivedFrom]->(:TestNode{ iri:$iri2 }) RETURN COUNT ( p ) AS ct", parameters("iri1", iri("ex:3"), "iri2", iri("ex:1")));
        Assert.assertEquals("Wrong count for {3 derivedFrom 1}!", 1, cursor.next().get("ct").asLong());
        cursor = session.run("MATCH (:TestNode{ iri:$iri1 })-[r:relatedTo]->(:AdditionalLabel{ iri:$iri2 }) RETURN r.note AS note", parameters("iri1", iri("ex:2"), "iri2", iri("ex:3")));
        assertTrue("{2 relatedTo 3} not found!", cursor.hasNext());
        Set<String> values = cursor.next().get("note").asList().stream().map(v -> (String) v).collect(Collectors.toSet());
        Set<String> refValues = new HashSet<>(Arrays.asList(new String[] { "Reified Relation", "Another Note" }));
        assertTrue("reified relation, wrong property value for 'note'!", Sets.difference(values, refValues).isEmpty());
    }
}
Also used : SparqlUtils(info.marcobrandizi.rdfutils.jena.SparqlUtils) Arrays(java.util.Arrays) Driver(org.neo4j.driver.v1.Driver) BeforeClass(org.junit.BeforeClass) AccessMode(org.neo4j.driver.v1.AccessMode) LoggerFactory(org.slf4j.LoggerFactory) HashSet(java.util.HashSet) QuerySolution(org.apache.jena.query.QuerySolution) Session(org.neo4j.driver.v1.Session) Txn(org.apache.jena.system.Txn) Values.parameters(org.neo4j.driver.v1.Values.parameters) Resource(org.apache.jena.rdf.model.Resource) Map(java.util.Map) GraphDatabase(org.neo4j.driver.v1.GraphDatabase) NamespaceUtils.iri(info.marcobrandizi.rdfutils.namespaces.NamespaceUtils.iri) Dataset(org.apache.jena.query.Dataset) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) AuthTokens(org.neo4j.driver.v1.AuthTokens) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Stream(java.util.stream.Stream) StatementResult(org.neo4j.driver.v1.StatementResult) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) StatementResult(org.neo4j.driver.v1.StatementResult) Dataset(org.apache.jena.query.Dataset) Driver(org.neo4j.driver.v1.Driver) QuerySolution(org.apache.jena.query.QuerySolution) HashSet(java.util.HashSet) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 82 with Session

use of org.neo4j.driver.v1.Session in project cypher-for-gremlin by opencypher.

the class GremlinNeo4jDriverTest method translating.

@Test
public void translating() {
    Config config = Config.build().withTranslation(TranslatorFlavor.gremlinServer()).toConfig();
    Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort(), config);
    try (Session session = driver.session()) {
        StatementResult result = session.run("MATCH (n:person) RETURN count(n) as count");
        int count = result.single().get("count").asInt();
        assertThat(count).isEqualTo(4);
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Config(org.opencypher.Config) Driver(org.neo4j.driver.v1.Driver) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 83 with Session

use of org.neo4j.driver.v1.Session in project cypher-for-gremlin by opencypher.

the class GremlinNeo4jDriverTest method withParameter.

@Test
public void withParameter() {
    Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort());
    try (Session session = driver.session()) {
        StatementResult result = session.run("CREATE (a:Greeting) " + "SET a.message = $message " + "RETURN a.message", parameters("message", "Hello"));
        String message = result.single().get(0).asString();
        assertThat(message).isEqualTo("Hello");
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Driver(org.neo4j.driver.v1.Driver) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 84 with Session

use of org.neo4j.driver.v1.Session in project cypher-for-gremlin by opencypher.

the class GremlinNeo4jDriverTest method returnNodeAndRelationship.

@Test
public void returnNodeAndRelationship() {
    Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort());
    try (Session session = driver.session()) {
        StatementResult result = session.run("CREATE (n1:Person {name: 'Marko'})-[r:knows {since:1999}]->(n2:Person)" + "RETURN n1,r,n2", parameters("message", "Hello"));
        Record record = result.single();
        Node n1 = record.get("n1").asNode();
        Relationship r = record.get("r").asRelationship();
        Node n2 = record.get("n2").asNode();
        assertThat(n1.hasLabel("Person")).isTrue();
        assertThat(n1.get("name").asString()).isEqualTo("Marko");
        assertThat(r.hasType("knows")).isTrue();
        assertThat(r.startNodeId()).isEqualTo(n1.id());
        assertThat(r.endNodeId()).isEqualTo(n2.id());
        assertThat(r.get("since").asLong()).isEqualTo(1999L);
        assertThat(n2.hasLabel("Person")).isTrue();
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Node(org.neo4j.driver.v1.types.Node) Relationship(org.neo4j.driver.v1.types.Relationship) Driver(org.neo4j.driver.v1.Driver) Record(org.neo4j.driver.v1.Record) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 85 with Session

use of org.neo4j.driver.v1.Session in project cypher-for-gremlin by opencypher.

the class GremlinNeo4jDriverTest method withMultipleParameters.

@Test
public void withMultipleParameters() {
    Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort());
    Map<String, Object> properties = new HashMap<>();
    properties.put("p1", 1L);
    properties.put("p2", 2L);
    try (Session session = driver.session()) {
        StatementResult result = session.run("CREATE (a:Greeting) " + "SET a.p1 = $p1, " + "a.p2 = $p2 " + "RETURN a.p1, a.p2", properties);
        Record record = result.single();
        assertThat(record.get("a.p1").asLong()).isEqualTo(1L);
        assertThat(record.get("a.p2").asLong()).isEqualTo(2L);
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) HashMap(java.util.HashMap) Driver(org.neo4j.driver.v1.Driver) Record(org.neo4j.driver.v1.Record) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Aggregations

Session (com.trilead.ssh2.Session)43 Session (org.neo4j.driver.v1.Session)38 Connection (com.trilead.ssh2.Connection)32 IOException (java.io.IOException)30 Test (org.junit.Test)30 Driver (org.neo4j.driver.v1.Driver)29 InputStream (java.io.InputStream)28 StatementResult (org.neo4j.driver.v1.StatementResult)20 Record (org.neo4j.driver.v1.Record)15 Session (iaik.pkcs.pkcs11.Session)10 TokenException (iaik.pkcs.pkcs11.TokenException)10 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)10 P11TokenException (org.xipki.security.exception.P11TokenException)10 RoutingNetworkSession (org.neo4j.driver.internal.RoutingNetworkSession)9 Session (ch.ethz.ssh2.Session)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 HttpException (org.apache.commons.httpclient.HttpException)8 Transaction (org.neo4j.driver.v1.Transaction)8 SCPClient (com.trilead.ssh2.SCPClient)6