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());
}
}
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);
}
}
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");
}
}
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();
}
}
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);
}
}
Aggregations