@Test public void calls_procedures_with_simple_input_type_returning_record_with_primitive_fields() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput11('string') YIELD field04 AS p RETURN p" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput12(42)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput13(42)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput14(4.2)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput15(true)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput16(false)" ).single() ).isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput17({foo:'bar'})" ).single() ) .isNotNull(); assertThat( session.run( "CALL " + procedureNamespace + ".simpleInput21()" ).single() ).isNotNull(); } }
@Test public void calls_simplistic_procedure() { try ( Driver driver = GraphDatabase.driver( graphDb.boltURI(), configuration() ); Session session = driver.session() ) { StatementResult result = session.run( "CALL " + procedureNamespace + ".theAnswer()" ); assertThat( result.single().get( "value" ).asLong() ).isEqualTo( 42L ); } }
@Test(expected = NoSuchRecordException.class) public void singleZero() { StatementResult statementResult = new GremlinServerStatementResult(serverInfo, statement, new ArrayList<Map<String, Object>>().iterator(), converter); statementResult.single(); }
/** * Generate the next value in a sequence for a given {@link IdSourceKey}. * * @return the next value in a sequence */ @Override protected Number nextValue(RemoteStatements remoteStatements) { List<Statement> statements = convert( remoteStatements ); StatementResult result = execute( statements ); Number nextValue = result.single().get( 0 ).asNumber(); return nextValue; }
@Test public void single() { List<Map<String, Object>> results = singletonList(getRow(1)); StatementResult statementResult = new GremlinServerStatementResult(serverInfo, statement, results.iterator(), converter); assertThat(statementResult.single().get(KEY1).asInt()).isEqualTo(1); }
public Node findAssociatedEntity(Transaction tx, Object[] keyValues, String associationrole) { Map<String, Object> params = params( keyValues ); String query = getFindAssociatedEntityQuery( associationrole ); if ( query != null ) { StatementResult statementResult = tx.run( query, params ); if ( statementResult.hasNext() ) { return statementResult.single().get( 0 ).asNode(); } } return null; }
@Test(expected = NoSuchRecordException.class) public void singleMore() { List<Map<String, Object>> results = asList(getRow(1), getRow(2)); StatementResult statementResult = new GremlinServerStatementResult(serverInfo, statement, results.iterator(), converter); statementResult.single(); }
protected static Node initEntity(Session session) { return session.run("CREATE (:R)-[:FOR]->(e:Entity)-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(:State) RETURN e").single().get("e").asNode(); } }
@Test public void simple() { Driver adam = GremlinDatabase.driver("//localhost:" + server.getPort()); try (Session session = adam.session()) { StatementResult result = session.run("MATCH (n:person) RETURN count(n) as count"); int count = result.single().get("count").asInt(); assertThat(count).isEqualTo(4); } }
@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"); } }
@Test public void inMemory() throws Exception { // freshReadmeSnippet: inMemory TinkerGraph graph = TinkerFactory.createModern(); GraphTraversalSource traversal = graph.traversal(); Driver driver = GremlinDatabase.driver(traversal); // freshReadmeSnippet: inMemory try (Session session = driver.session()) { StatementResult result = session.run("RETURN 'Hello'"); String message = result.single().get(0).asString(); assertThat(message).isEqualTo("Hello"); } }
@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); } }
@Test public void shouldCreateAnEntityAndAStateNodeWithoutPropsIfEmptyMapIsPassed() throws Throwable { // 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()) { // When StatementResult result = session.run("CALL graph.versioner.init('Entity')"); StatementResult stateResult = session.run("MATCH (s:State) RETURN s"); StatementResult currentResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) RETURN id(e) as id"); Node state = stateResult.single().get("s").asNode(); // Then assertThat(result.single().get("node").asNode().id(), equalTo(0L)); assertThat(state.id(), equalTo(1L)); assertThat(currentResult.single().get("id").asLong(), equalTo(0L)); } }
@Test public void shouldGetCurrentStateByGivenEntity() { // 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 (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); Node state = session.run("MATCH (s:State) RETURN s").single().get("s").asNode(); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.get.current.state(e) YIELD node RETURN node"); // Then assertThat(result.single().get("node").asNode(), equalTo(state)); } }
@Test public void shouldCreateAnRNodeConnectedToTheEntity() throws Throwable { try (Driver driver = GraphDatabase.driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { StatementResult result = session.run("CALL graph.versioner.init('Entity')"); StatementResult rPath = session.run("MATCH rPath = (:R)-[:FOR]->(:Entity) RETURN rPath"); Assertions.assertThat(result.single().get("node").asNode().id()).isEqualTo(0L); Assertions.assertThat(rPath) .hasSize(1) .allMatch(path -> path.get("rPath").asPath().length() == 1); } } }
@Test public void shouldNotCreateTheRelationshipIfSourceIsNotAnEntity() throws Throwable { try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given Node entityA = session.run("CREATE (e:Entity) RETURN e").single().get("e").asNode(); //Not an entity because missing states and R Node entityB = initEntity(session); String testType = "testType"; // When String query = "MATCH (a:Entity), (b:Entity) WHERE id(a) = %d AND id(b) = %d WITH a, b CALL graph.versioner.relationship.create(a, b, '%s') YIELD relationship RETURN relationship"; assertThat(session.run(String.format(query, entityA.id(), entityB.id(), testType))); Throwable thrown = catchThrowable(() -> session.run(String.format(query, entityA.id(), entityB.id(), testType))); //Then assertThat(thrown).hasMessageContaining("The given node is not a Versioner Core Entity"); } }
@Test public void shouldNotCreateTheRelationshipIfDestinationIsNotAnEntity() throws Throwable { try (Driver driver = GraphDatabase .driver(neo4j.boltURI(), Config.build().withEncryption().toConfig()); Session session = driver.session()) { // Given Node entityA = initEntity(session); Node entityB = session.run("CREATE (e:Entity) RETURN e").single().get("e").asNode(); //Not an entity because missing states and R String testType = "testType"; // When String query = "MATCH (a:Entity), (b:Entity) WHERE id(a) = %d AND id(b) = %d WITH a, b CALL graph.versioner.relationship.create(a, b, '%s') YIELD relationship RETURN relationship"; assertThat(session.run(String.format(query, entityA.id(), entityB.id(), testType))); Throwable thrown = catchThrowable(() -> session.run(String.format(query, entityA.id(), entityB.id(), testType))); //Then assertThat(thrown).hasMessageContaining("The given node is not a Versioner Core Entity"); } }
@Test public void shouldCreateANewStateFromAnEntityWithoutAState() throws Throwable { // 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 (e:Entity {key:'immutableValue'})"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.update(e, {key:'newValue'}, 'Error', localdatetime('1988-10-27T02:46:40')) YIELD node RETURN node"); StatementResult correctResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) RETURN id(s) as stateId"); // Then assertThat(correctResult.single().get("stateId").asLong(), equalTo(20L)); } }
@Test public void shouldCreateANewStateFromAnEntityWithoutAStateUsingPatch() throws Throwable { // 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 (e:Entity {key:'immutableValue'})"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.patch(e, {key:'newValue'}, 'Error', localdatetime('1988-10-27T02:46:40')) YIELD node RETURN node"); StatementResult correctResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) RETURN id(s) as stateId"); // Then assertThat(correctResult.single().get("stateId").asLong(), equalTo(20L)); } }
@Test (expected = ClientException.class) public void shouldNotCreateACopyOfTheGivenStateSinceItsADifferentEntityState() throws Throwable { // 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 (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-26T00:00:00'), endDate:localdatetime('1988-10-27T00:00:00')}]->(s:State:Test {key:'initialValue'})"); session.run("MATCH (sc:State)<-[:CURRENT]-(e:Entity)-[:HAS_STATE]->(s:Test) CREATE (sc)-[:PREVIOUS {date:localdatetime('1988-10-26T00:00:00')}]->(s)"); session.run("CREATE (e:EntityBis {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:EntityBis)-[:CURRENT]->(s:State) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); StatementResult result = session.run("MATCH (e:Entity), (:EntityBis)-[:HAS_STATE]->(s:State) WITH e, s CALL graph.versioner.patch.from(e, s) YIELD node RETURN node"); Node currentState = result.single().get("node").asNode(); } } }