/** * Converts values to {@link Map}, with the map values further converted using * the provided converter. * * @param valueConverter converter to use for the values of the map * @param <T> the type of values in the returned map * @return a function that returns {@link Value#asMap(Function)} of a {@link Value} */ public static <T> Function<Value,Map<String,T>> ofMap( final Function<Value,T> valueConverter ) { return val -> val.asMap( valueConverter ); }
@Override public <T> Map<String,T> asMap( Function<Value,T> mapFunction, Map<String,T> defaultValue ) { return computeOrDefault( value -> value.asMap( mapFunction ), defaultValue ); }
private <T>Map<String, T> get( Value value, Map<String, T> defaultValue, Function<Value, T> mapFunc ) { if( value.equals( Values.NULL ) ) { return defaultValue; } else { return value.asMap( mapFunc ); } }
private Map<String, Object> get( Value value, Map<String, Object> defaultValue ) { if( value.equals( Values.NULL ) ) { return defaultValue; } else { return value.asMap(); } }
@Override public StatementResult run(String statementTemplate, Value parameters) { return run(statementTemplate, parameters.asMap()); }
private void addTargetEmbeddedProperties(Map<String, Object> targetNode, Record row) { if ( !row.get( 1 ).isNull() ) { List<Object> pathToNode = row.get( 1 ).asList(); Map<String, Object> embeddedNodeProperties = (Map<String, Object>) row.get( 3 ).asMap(); String path = concat( pathToNode ); for ( Map.Entry<String, Object> entry : embeddedNodeProperties.entrySet() ) { targetNode.put( path + "." + entry.getKey(), entry.getValue() ); } } }
@Override public StatementResult run(Statement statement) { HashMap<String, Object> serializableMap = new HashMap<>(statement.parameters().asMap()); Iterator<Map<String, Object>> iterator = client.submit(statement.text(), serializableMap).iterator(); return new GremlinServerStatementResult(serverInfo, statement, iterator, converter); }
static Object convert(Value value) { switch (value.type().name()) { case "PATH": return value.asList(BoltCypherExecutor::convert); case "NODE": case "RELATIONSHIP": return value.asMap(); } return value.asObject(); }
private void checkMapForPrettyPrint(Map<String, String> map, String expectedResult) { // given BoltResult result = mock(BoltResult.class); Record record = mock(Record.class); Value value = mock(Value.class); when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.MAP()); when(value.asMap((Function<Value, String>) anyObject())).thenReturn(map); when(record.keys()).thenReturn(asList("map")); when(record.values()).thenReturn(asList(value)); when(result.getRecords()).thenReturn(asList(record)); when(result.getSummary()).thenReturn(mock(ResultSummary.class)); // when String actual = plainPrinter.format(result); // then assertThat(actual, is(expectedResult)); }
private static CompletionStage<InternalStatementResultCursor> runStatement( Connection connection, Statement statement, ExplicitTransaction tx, boolean waitForRunResponse ) { String query = statement.text(); Map<String,Value> params = statement.parameters().asMap( ofValue() ); CompletableFuture<Void> runCompletedFuture = new CompletableFuture<>(); RunResponseHandler runHandler = new RunResponseHandler( runCompletedFuture, METADATA_EXTRACTOR ); PullAllResponseHandler pullAllHandler = newPullAllHandler( statement, runHandler, connection, tx ); connection.writeAndFlush( new RunMessage( query, params ), runHandler, PullAllMessage.PULL_ALL, pullAllHandler ); if ( waitForRunResponse ) { // wait for response of RUN before proceeding return runCompletedFuture.thenApply( ignore -> new InternalStatementResultCursor( runHandler, pullAllHandler ) ); } else { return completedFuture( new InternalStatementResultCursor( runHandler, pullAllHandler ) ); } }
private static CompletionStage<InternalStatementResultCursor> runStatement( Connection connection, Statement statement, BookmarksHolder bookmarksHolder, ExplicitTransaction tx, TransactionConfig config, boolean waitForRunResponse ) { String query = statement.text(); Map<String,Value> params = statement.parameters().asMap( ofValue() ); CompletableFuture<Void> runCompletedFuture = new CompletableFuture<>(); Message runMessage = new RunWithMetadataMessage( query, params, bookmarksHolder.getBookmarks(), config ); RunResponseHandler runHandler = new RunResponseHandler( runCompletedFuture, METADATA_EXTRACTOR ); PullAllResponseHandler pullAllHandler = newPullAllHandler( statement, runHandler, connection, bookmarksHolder, tx ); connection.writeAndFlush( runMessage, runHandler, PULL_ALL, pullAllHandler ); if ( waitForRunResponse ) { // wait for response of RUN before proceeding return runCompletedFuture.thenApply( ignore -> new InternalStatementResultCursor( runHandler, pullAllHandler ) ); } else { return completedFuture( new InternalStatementResultCursor( runHandler, pullAllHandler ) ); } }
@Test public void prettyPrintPlanInformation() throws Exception { // given ResultSummary resultSummary = mock(ResultSummary.class); ProfiledPlan plan = mock(ProfiledPlan.class); when(plan.dbHits()).thenReturn(1000L); when(plan.records()).thenReturn(20L); when(resultSummary.hasPlan()).thenReturn(true); when(resultSummary.hasProfile()).thenReturn(true); when(resultSummary.plan()).thenReturn(plan); when(resultSummary.profile()).thenReturn(plan); when(resultSummary.resultAvailableAfter(anyObject())).thenReturn(5L); when(resultSummary.resultConsumedAfter(anyObject())).thenReturn(7L); when(resultSummary.statementType()).thenReturn(StatementType.READ_ONLY); Map<String, Value> argumentMap = Values.parameters("Version", "3.1", "Planner", "COST", "Runtime", "INTERPRETED").asMap(v -> v); when(plan.arguments()).thenReturn(argumentMap); BoltResult result = mock(BoltResult.class); when(result.getRecords()).thenReturn(Collections.emptyList()); when(result.getSummary()).thenReturn(resultSummary); // when String actual = verbosePrinter.format(result); // then argumentMap.forEach((k, v) -> { assertThat(actual, CoreMatchers.containsString("| " + k)); assertThat(actual, CoreMatchers.containsString("| " + v.toString())); }); }
@Override public T apply( Value plan ) { final String operatorType = plan.get( "operatorType" ).asString(); final Value argumentsValue = plan.get( "args" ); final Map<String, Value> arguments = argumentsValue.isNull() ? Collections.<String, Value>emptyMap() : argumentsValue.asMap( Values.ofValue() ); final Value identifiersValue = plan.get( "identifiers" ); final List<String> identifiers = identifiersValue.isNull() ? Collections.<String>emptyList() : identifiersValue.asList( ofString() ); final Value childrenValue = plan.get( "children" ); final List<T> children = childrenValue.isNull() ? Collections.<T>emptyList() : childrenValue.asList( this ); return planCreator.create( operatorType, arguments, identifiers, children, plan ); } }
when(resultSummary.resultConsumedAfter(anyObject())).thenReturn(7L); when(resultSummary.statementType()).thenReturn(StatementType.READ_ONLY); Map<String, Value> argumentMap = Values.parameters("Version", "3.1", "Planner", "COST", "Runtime", "INTERPRETED").asMap(v -> v); when(plan.arguments()).thenReturn(argumentMap);
@Test public void prettyPrintExplainInformation() throws Exception { // given ResultSummary resultSummary = mock(ResultSummary.class); ProfiledPlan plan = mock(ProfiledPlan.class); when(plan.dbHits()).thenReturn(1000L); when(plan.records()).thenReturn(20L); when(resultSummary.hasPlan()).thenReturn(true); when(resultSummary.hasProfile()).thenReturn(false); when(resultSummary.plan()).thenReturn(plan); when(resultSummary.resultAvailableAfter(anyObject())).thenReturn(5L); when(resultSummary.resultConsumedAfter(anyObject())).thenReturn(7L); when(resultSummary.statementType()).thenReturn(StatementType.READ_ONLY); Map<String, Value> argumentMap = Values.parameters("Version", "3.1", "Planner", "COST", "Runtime", "INTERPRETED").asMap(v -> v); when(plan.arguments()).thenReturn(argumentMap); BoltResult result = mock(BoltResult.class); when(result.getRecords()).thenReturn(Collections.emptyList()); when(result.getSummary()).thenReturn(resultSummary); // when String actual = plainPrinter.format(result); // then String expected = "Plan: \"EXPLAIN\"\n" + "Statement: \"READ_ONLY\"\n" + "Version: \"3.1\"\n" + "Planner: \"COST\"\n" + "Runtime: \"INTERPRETED\"\n" + "Time: 12"; Stream.of(expected.split("\n")).forEach(e -> assertThat(actual, containsString(e))); }
@Test public void shouldCreateAnEntityWithPropertiesWithAStateAndItsPropertiesWithAdditionalLabelButNoDate() 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')"); 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"); // 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)); } }
@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 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)); } }