@Test public void shouldStart() { boolean actual = neo4jContainer.isRunning(); assertThat(actual).isTrue(); try (Driver driver = GraphDatabase .driver(neo4jContainer.getBoltUrl(), AuthTokens.basic("neo4j", "password")); Session session = driver.session() ) { long one = session.run("RETURN 1", Collections.emptyMap()).next().get(0).asLong(); assertThat(one).isEqualTo(1L); } catch (Exception e) { fail(e.getMessage()); } }
@Test public void shouldDisableAuthentication() { try ( Neo4jContainer neo4jContainer = new Neo4jContainer().withAdminPassword(null); ) { neo4jContainer.start(); try (Driver driver = getDriver(neo4jContainer); Session session = driver.session() ) { long one = session.run("RETURN 1", Collections.emptyMap()).next().get(0).asLong(); assertThat(one).isEqualTo(1L); } } }
@Test public void shouldRunEnterprise() { assumeThat(Neo4jContainerTest.class.getResource(ACCEPTANCE_FILE_LOCATION)).isNotNull(); try ( Neo4jContainer neo4jContainer = new Neo4jContainer() .withEnterpriseEdition() .withAdminPassword("Picard123") ) { neo4jContainer.start(); try ( Driver driver = getDriver(neo4jContainer); Session session = driver.session() ) { String edition = session .run("CALL dbms.components() YIELD edition RETURN edition", Collections.emptyMap()) .next().get(0).asString(); assertThat(edition).isEqualTo("enterprise"); } } }
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) ); Session session = driver.session(); StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" ); while ( result.hasNext() ) { Record record = result.next(); gson.toJson(record.asMap()); } session.close(); driver.close();
StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" ); while ( result.hasNext() ) { Record record = result.next(); System.out.println( record.get( "title" ).asString() + " " + record.get("name").asString() ); }
@Override public RowModel fetchNext() { if (result.hasNext()) { return adapter.adapt(result.next().asMap()); } return null; } }
@Override public GraphModel fetchNext() { if (result.hasNext()) { return adapter.adapt(result.next().asMap()); } return null; } }
@Override public RowModel fetchNext() { if (result.hasNext()) { return adapter.adapt(result.next().asMap()); } return null; } }
private Relationship relationship(StatementResult result) { if ( result.hasNext() ) { return result.next().get( 0 ).asRelationship(); } return null; }
@Override public GraphModel fetchNext() { if (result.hasNext()) { return adapter.adapt(result.next().asMap()); } return null; } }
Session session = driver.session(); session.run( "CREATE (a:Person {name:'Arthur', title:'King'})" ); StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" ); while ( result.hasNext() ) { Record record = result.next(); System.out.println( record.get("title").asString() + " " + record.get("name").asString() ); } session.close(); driver.close();
@Override public QueryResult getNodes(Query query, QueryOptions queryOptions) throws BioNetDBException { Session session = this.driver.session(); long startTime = System.currentTimeMillis(); String nodeName = "n"; String myQuery = "MATCH " + Neo4JQueryParser.parse(nodeName, query, queryOptions) + " RETURN " + nodeName; System.out.println("Query: " + myQuery); long stopTime = System.currentTimeMillis(); // TODO: Build new Network with the result StatementResult run = session.run(myQuery); while (run.hasNext()) { System.out.println(run.next().asMap()); } int time = (int) (stopTime - startTime) / 1000; session.close(); return new QueryResult("get", time, 0, 0, null, null, Arrays.asList(new Network())); }
Neo4jIndexManagement(Neo4jTransaction transaction, String namespace, Set<String> managedDomains) { currentIndexes = new LinkedHashSet<>(); StatementResult statementResult = transaction.executeCypher(String.format("CALL db.indexes")); while (statementResult.hasNext()) { Record record = statementResult.next(); Value labelValue = record.get("label"); Value properties = record.get("properties"); String label = labelValue.asString(); List<String> propertyList = properties.asList(Value::asString); currentIndexes.add(new Index(label, propertyList, !label.endsWith("_E"))); } wantedIndexes = new LinkedHashSet<>(); for (String managedDomain : managedDomains) { wantedIndexes.add(new Index(managedDomain, List.of("id"), true)); wantedIndexes.add(new Index(managedDomain + "_E", List.of("path", "hashOrValue"), false)); } createMissingIndices(transaction); }
@Test public void shouldDiffGetTheDiffBetweenTwoStatesCorrectly() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (s:State:From {keep:'keep', update:'old', delete:'delete'})"); session.run("CREATE (s:State:To {keep:'keep', update:'new', new:'new'})"); // When StatementResult result = session .run("MATCH (stateTo:State:To), (stateFrom:State:From) WITH stateFrom, stateTo CALL graph.versioner.diff(stateFrom, stateTo) YIELD operation, label, oldValue, newValue RETURN operation, label, oldValue, newValue"); // Then assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_REMOVE, "delete", "delete", "null")); assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "update", "old", "new")); assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_ADD, "new", "null", "new")); assertThat(result.hasNext(), is(false)); } }
@Test public void shouldDiffGetTheDiffBetweenTwoStatesCorrectlyWithDifferentObjectTypes() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (s:State:From {a:1, b:1, c:'stringA', d:'string', e: [1, 2], f: [1, 2]})"); session.run("CREATE (s:State:To {a:2, b:1, c:'stringB', d:'string', e:[1, 2], f: [1, 3]})"); // When StatementResult result = session .run("MATCH (stateTo:State:To), (stateFrom:State:From) WITH stateFrom, stateTo CALL graph.versioner.diff(stateFrom, stateTo) YIELD operation, label, oldValue, newValue RETURN operation, label, oldValue, newValue"); // Then assertThat(result.next().values().stream().map(value -> value.asObject().toString()).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "a", "1", "2")); assertThat(result.next().values().stream().map(value -> value.asObject().toString()).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "c", "stringA", "stringB")); assertThat(result.next().values().stream().map(value -> value.asObject().toString()).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "f", "[1, 2]", "[1, 3]")); assertThat(result.hasNext(), is(false)); } }
@Test public void shouldDiffFromPreviousGetTheDiffFromPreviousCorrectly() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (s:State:From {keep:'keep', update:'old', delete:'delete'})"); session.run("CREATE (s:State:To {keep:'keep', update:'new', new:'new'})"); session.run("MATCH (from:From), (to:To) CREATE (from)<-[:PREVIOUS]-(to)"); // When StatementResult result = session .run("MATCH (stateTo:State:To) WITH stateTo CALL graph.versioner.diff.from.previous(stateTo) YIELD operation, label, oldValue, newValue RETURN operation, label, oldValue, newValue"); // Then assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_REMOVE, "delete", "delete", "null")); assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "update", "old", "new")); assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_ADD, "new", "null", "new")); assertThat(result.hasNext(), is(false)); } }
@Test public void shouldDiffFromCurrentGetTheDiffFromCurrentCorrectly() { // This is in a try-block, to make sure we close the driver after the test try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given session.run("CREATE (:Entity)"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE]->(s:State:From {keep:'keep', update:'old', delete:'delete'})"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE]->(s:State:To {keep:'keep', update:'new', new:'new'})"); session.run("MATCH (to:To)-[:HAS_STATE]-(e:Entity) CREATE (e)-[:CURRENT]->(to)"); // When StatementResult result = session .run("MATCH (stateFrom:State:From) WITH stateFrom CALL graph.versioner.diff.from.current(stateFrom) YIELD operation, label, oldValue, newValue RETURN operation, label, oldValue, newValue"); // Then assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_REMOVE, "delete", "delete", "null")); assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "update", "old", "new")); assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_ADD, "new", "null", "new")); assertThat(result.hasNext(), is(false)); } }
private CypherResult buildResult(StatementResult response) { CypherResult result = new CypherResult(); while (response.hasNext()) { Record record = response.next(); ResultRow resultRow = new ResultRow(); for (Pair<String, Value> fieldInRecord : record.fields()) { resultRow.add(fieldInRecord.key(), fieldInRecord.value()); } result.addRow(resultRow); } return result; }