@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 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); } } }
public long extractResultAvailableAfter( Map<String,Value> metadata ) { Value resultAvailableAfterValue = metadata.get( resultAvailableAfterMetadataKey ); if ( resultAvailableAfterValue != null ) { return resultAvailableAfterValue.asLong(); } return -1; }
private static long extractResultConsumedAfter( Map<String,Value> metadata, String key ) { Value resultConsumedAfterValue = metadata.get( key ); if ( resultConsumedAfterValue != null ) { return resultConsumedAfterValue.asLong(); } return -1; } }
private long get( Value value, long defaultValue ) { if( value.equals( Values.NULL ) ) { return defaultValue; } else { return value.asLong(); } }
private static long expirationTimestamp( long now, Record record ) { long ttl = record.get( "ttl" ).asLong(); long expirationTimestamp = now + ttl * 1000; if ( ttl < 0 || ttl >= MAX_TTL || expirationTimestamp < 0 ) { expirationTimestamp = Long.MAX_VALUE; } return expirationTimestamp; }
@Override public ProfiledPlan create( String operatorType, Map<String,Value> arguments, List<String> identifiers, List<ProfiledPlan> children, Value originalPlanValue ) { return new InternalProfiledPlan( operatorType, arguments, identifiers, children, originalPlanValue.get( "dbHits" ).asLong(), originalPlanValue.get( "rows" ).asLong() ); } };
public static final Object formatValue(Class<?> clazz, Value value) { if(value.isNull()) return null; if(clazz.isAssignableFrom(Integer.class)) return (Integer)value.asInt(); if(clazz.isAssignableFrom(Short.class)) return Short.valueOf(value.asString()); if(clazz.isAssignableFrom(Character.class)) return (Character)value.asString().charAt(0); if(clazz.isAssignableFrom(Float.class)) return (Float)value.asFloat(); if(clazz.isAssignableFrom(Double.class)) return (Double)value.asDouble(); if(clazz.isAssignableFrom(Long.class)) return (Long)value.asLong(); if(clazz.isAssignableFrom(Number.class)) return value.asNumber(); if(clazz.isAssignableFrom(Boolean.class)) return (Boolean)value.asBoolean(); if(clazz.isAssignableFrom(byte[].class)) return value.asByteArray(); if(clazz.isAssignableFrom(LocalDate.class)) return value.asLocalDate(); if(clazz.isAssignableFrom(LocalTime.class)) return value.asLocalTime(); if(clazz.isAssignableFrom(LocalDateTime.class)) return value.asLocalDateTime(); if(clazz.isAssignableFrom(OffsetTime.class)) return value.asOffsetTime(); if(clazz.isAssignableFrom(IsoDuration.class)) return value.asIsoDuration(); if(clazz.isAssignableFrom(ZonedDateTime.class)) return value.asZonedDateTime(); if(clazz.isAssignableFrom(Date.class)) return LocalDateTimeOperation.toDate().convert(value.asLocalDateTime()); if(clazz.isAssignableFrom(String.class)) return formatToString(value); if(clazz.isAssignableFrom(Enum.class)) return MethodOperation.invoked(null , MethodOperation.getMethod(clazz , "valueOf" , String.class), value.asString()); throw new NotSupportConvertException(); }
@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); } }
@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(); } }
@Test public void shouldReportTotalDBHits() { Value labelScan = buildOperator( "NodeByLabelScan", 1002L, 1001L, null ); Value filter = buildOperator( "Filter", 1402, 280, labelScan ); Value planMap = buildOperator( "ProduceResults", 0, 280, filter ); ProfiledPlan plan = PROFILED_PLAN_FROM_VALUE.apply( planMap ); ResultSummary summary = new InternalResultSummary( new Statement( "PROFILE MATCH (n:LABEL) WHERE 20 < n.age < 35 return n" ), new InternalServerInfo( new BoltServerAddress( "localhost:7687" ), ServerVersion.vInDev ), StatementType.READ_ONLY, null, plan, plan, Collections.emptyList(), 39, 55 ); // When Map<String,Value> info = OutputFormatter.info( summary ); //Then assertThat( info.get( "DbHits" ).asLong(), equalTo( 2404L ) ); }
@Test public void shouldCreateAnEntityWithPropertiesWithAStateAndItsProperties() 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', {key:'value'}, {key:'value'})"); StatementResult entityResult = session.run("MATCH (e:Entity) RETURN properties(e) as props"); StatementResult stateResult = session.run("MATCH (s:State) RETURN s"); StatementResult stateProps = session.run("MATCH (s:State) RETURN properties(s) as props"); StatementResult currentResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) RETURN id(e) as id"); StatementResult hasStatusResult = session.run("MATCH (e:Entity)-[:HAS_STATE]->(s:State) RETURN id(e) as id"); // Then assertThat(result.single().get("node").asNode().id(), equalTo(0L)); assertThat(entityResult.single().get("props").asMap().isEmpty(), equalTo(false)); assertThat(stateResult.single().get("s").asNode().id(), equalTo(1L)); assertThat(stateProps.single().get("props").asMap().isEmpty(), equalTo(false)); assertThat(currentResult.single().get("id").asLong(), equalTo(0L)); assertThat(hasStatusResult.single().get("id").asLong(), equalTo(0L)); } }
@Test public void shouldCreateAnEntityWithPropertiesWithAStateAndItsPropertiesWithAdditionalLabelAndDate() 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', {key:'value'}, {key:'value'}, 'Error', localdatetime('1988-10-27T02:46:40'))"); StatementResult entityResult = session.run("MATCH (e:Entity) RETURN properties(e) as props"); StatementResult stateResult = session.run("MATCH (s:State) RETURN s"); Node state = stateResult.single().get("s").asNode(); StatementResult stateProps = session.run("MATCH (s:State) RETURN properties(s) as props"); StatementResult currentResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s:State) RETURN id(e) as id"); StatementResult hasStatusResult = session.run("MATCH (e:Entity)-[:HAS_STATE]->(s:State) RETURN id(e) as id"); StatementResult hasStatusDateResult = session.run("MATCH (e:Entity)-[rel:CURRENT]->(s:State) RETURN rel.date as date"); // Then assertThat(result.single().get("node").asNode().id(), equalTo(0L)); assertThat(entityResult.single().get("props").asMap().isEmpty(), equalTo(false)); assertThat(state.id(), equalTo(1L)); assertThat(stateProps.single().get("props").asMap().isEmpty(), equalTo(false)); assertThat(currentResult.single().get("id").asLong(), equalTo(0L)); assertThat(hasStatusResult.single().get("id").asLong(), equalTo(0L)); assertThat(state.hasLabel("Error"), equalTo(true)); assertThat(hasStatusDateResult.single().get("date").asLocalDateTime(), equalTo(convertEpochToLocalDateTime(593920000000L))); } }
@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 public void shouldCreateAnEntityWithPropertiesWithAState() 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', {key:'value'})"); StatementResult entityResult = session.run("MATCH (e:Entity) RETURN properties(e) as props"); 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(entityResult.single().get("props").asMap().isEmpty(), equalTo(false)); assertThat(state.id(), equalTo(1L)); assertThat(currentResult.single().get("id").asLong(), equalTo(0L)); } }
@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 shouldCreateANewStateWithoutAdditionalLabelAndDate() 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)-[:CURRENT]->(s:State) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.update(e, {key:'newValue'}) YIELD node RETURN node"); StatementResult countStateResult = session.run("MATCH (s:State) RETURN count(s) as s"); StatementResult nextResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) return s2"); StatementResult correctStateResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) WITH s1 MATCH (e:Entity)-[:CURRENT]->(s1) return e"); // Then assertThat(countStateResult.single().get("s").asLong(), equalTo(2L)); assertThat(nextResult.single().get("s2").asNode().id(), equalTo(1L)); assertThat(correctStateResult.single().get("e").asNode().id(), equalTo(0L)); } }
@Test public void shouldCreateANewStateWithAdditionalLabelButWithoutDate() 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)-[:CURRENT]->(s:State) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.update(e, {key:'newValue'}, 'Error') YIELD node RETURN node"); StatementResult countStateResult = session.run("MATCH (s:State) RETURN count(s) as s"); StatementResult nextResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) return s2"); StatementResult correctStateResult = session.run("MATCH (s1:State)-[:PREVIOUS]->(s2:State) WITH s1 MATCH (e:Entity)-[:CURRENT]->(s1) return e"); StatementResult currentStateResult = session.run("MATCH (e:Entity)-[:CURRENT]->(s) return s"); // Then assertThat(countStateResult.single().get("s").asLong(), equalTo(2L)); assertThat(nextResult.single().get("s2").asNode().id(), equalTo(1L)); assertThat(correctStateResult.single().get("e").asNode().id(), equalTo(0L)); assertThat(currentStateResult.single().get("s").asNode().hasLabel("Error"), equalTo(true)); } }