@Procedure() @Description("apoc.bolt.load(url-or-key, kernelTransaction, params, config) - access to other databases via bolt for read") public Stream<RowResult> load(@Name("url") String url, @Name("kernelTransaction") String statement, @Name(value = "params", defaultValue = "{}") Map<String, Object> params, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) throws URISyntaxException { if (params == null) params = Collections.emptyMap(); if (config == null) config = Collections.emptyMap(); boolean virtual = (boolean) config.getOrDefault("virtual", false); boolean addStatistics = (boolean) config.getOrDefault("statistics", false); boolean readOnly = (boolean) config.getOrDefault("readOnly", true); Config driverConfig = toDriverConfig(config.getOrDefault("driverConfig", map())); UriResolver uri = new UriResolver(url, "bolt"); uri.initialize(); try (Driver driver = GraphDatabase.driver(uri.getConfiguredUri(), uri.getToken(), driverConfig); Session session = driver.session()) { if (addStatistics) return Stream.of(new RowResult(toMap(runStatement(statement, session, params, readOnly).summary().counters()))); else return getRowResultStream(virtual, session, params, statement, readOnly); } catch (Exception e) { throw new RuntimeException("It's not possible to create a connection due to: " + e.getMessage()); } }
@Nonnull public ResultSummary getSummary() { return statementResult.summary(); } }
private Driver stubVersionInAnOpenSession(StatementResult versionMock, Session sessionMock, String value) { Driver driverMock = mock(Driver.class); ResultSummary resultSummary = mock(ResultSummary.class); ServerInfo serverInfo = mock(ServerInfo.class); when(resultSummary.server()).thenReturn(serverInfo); when(serverInfo.version()).thenReturn(value); when(versionMock.summary()).thenReturn(resultSummary); when(sessionMock.isOpen()).thenReturn(true); when(sessionMock.run("RETURN 1")).thenReturn(versionMock); when(driverMock.session(any(), anyString())).thenReturn(sessionMock); return driverMock; }
private StatementResult mockResult(List<String> cols, Object... data) { StatementResult result = mock(StatementResult.class); Statement statement = mock(Statement.class); ResultSummary summary = mock(ResultSummary.class); when(summary.statement()).thenReturn(statement); when(result.keys()).thenReturn(cols); List<Record> records = new ArrayList<>(); List<Object> input = asList(data); int width = cols.size(); for (int row = 0; row < input.size() / width; row++) { records.add(record(cols, input.subList(row * width, (row + 1) * width))); } when(result.list()).thenReturn(records); when(result.consume()).thenReturn(summary); when(result.summary()).thenReturn(summary); return result; }
@Test public void prettyPrintDuration() throws Exception { // given StatementResult statementResult = mock(StatementResult.class); List<String> keys = asList("d"); when(statementResult.summary()).thenReturn(mock(ResultSummary.class)); when(statementResult.keys()).thenReturn(keys); Value duration = new DurationValue(new InternalIsoDuration(1, 2, 3, 4)); Record record = new InternalRecord(keys, new Value[]{duration}); // when String actual = verbosePrinter.format(new BoltResult(asList(record), statementResult)); // then assertThat(actual, containsString("| P1M2DT3.000000004S |")); }
@Test public void prettyPrintDurationWithNoTrailingZeroes() throws Exception { // given StatementResult statementResult = mock(StatementResult.class); List<String> keys = asList("d"); when(statementResult.summary()).thenReturn(mock(ResultSummary.class)); when(statementResult.keys()).thenReturn(keys); Value duration = new DurationValue(new InternalIsoDuration(1, 2, 3, 0)); Record record = new InternalRecord(keys, new Value[]{duration}); // when String actual = verbosePrinter.format(new BoltResult(asList(record), statementResult)); // then assertThat(actual, containsString("| P1M2DT3S |")); }
@Test public void prettyPrintNode() throws Exception { // given StatementResult statementResult = mock(StatementResult.class); List<String> labels = asList("label1", "label2"); Map<String, Value> propertiesAsMap = new HashMap<>(); propertiesAsMap.put("prop1", Values.value("prop1_value")); propertiesAsMap.put("prop2", Values.value("prop2_value")); List<String> keys = asList("col1", "col2"); when(statementResult.summary()).thenReturn(mock(ResultSummary.class)); when(statementResult.keys()).thenReturn(keys); Value value = new NodeValue(new InternalNode(1, labels, propertiesAsMap)); Record record = new InternalRecord(keys, new Value[]{value}); // when String actual = verbosePrinter.format(new BoltResult(asList(record), statementResult)); // then assertThat(actual, containsString("| (:label1:label2 {prop2: \"prop2_value\", prop1: \"prop1_value\"}) |")); }
private void reconnect() { String bookmark = null; if (session != null) { bookmark = session.lastBookmark(); session.close(); } session = driver.session(AccessMode.WRITE, bookmark); StatementResult run = session.run("RETURN 1"); this.version = run.summary().server().version(); run.consume(); }
@Test public void prettyPrintRelationships() throws Exception { // given StatementResult statementResult = mock(StatementResult.class); List<String> keys = asList("rel"); Map<String, Value> propertiesAsMap = new HashMap<>(); propertiesAsMap.put("prop1", Values.value("prop1_value")); propertiesAsMap.put("prop2", Values.value("prop2_value")); RelationshipValue relationship = new RelationshipValue(new InternalRelationship(1, 1, 2, "RELATIONSHIP_TYPE", propertiesAsMap)); Record record = new InternalRecord(keys, new Value[]{relationship}); when(statementResult.summary()).thenReturn(mock(ResultSummary.class)); when(statementResult.keys()).thenReturn(keys); // when String actual = verbosePrinter.format(new BoltResult(asList(record), statementResult)); // then assertThat(actual, containsString("| [:RELATIONSHIP_TYPE {prop2: \"prop2_value\", prop1: \"prop1_value\"}] |")); }
@Test public void prettyPrintPoint() throws Exception { // given StatementResult statementResult = mock(StatementResult.class); List<String> keys = asList("p1", "p2"); when(statementResult.summary()).thenReturn(mock(ResultSummary.class)); when(statementResult.keys()).thenReturn(keys); Value point2d = new PointValue(new InternalPoint2D(4326, 42.78, 56.7)); Value point3d = new PointValue(new InternalPoint3D(4326, 1.7, 26.79, 34.23)); Record record = new InternalRecord(keys, new Value[]{point2d, point3d}); // when String actual = verbosePrinter.format(new BoltResult(asList(record), statementResult)); // then assertThat(actual, containsString("| point({srid:4326, x:42.78, y:56.7}) |")); assertThat(actual, containsString("| point({srid:4326, x:1.7, y:26.79, z:34.23}) |")); }
@Test public void create() { List<Map<String, Object>> results = asList( getRow(1), getRow(2), getRow(3) ); StatementResult statementResult = new GremlinServerStatementResult(serverInfo, statement, results.iterator(), converter); assertThat(statementResult.keys()).containsExactly(KEY1, KEY2); assertThat(statementResult.peek().get(KEY1).asInt()).isEqualTo(1); assertThat(statementResult.hasNext()).isTrue(); assertThat(statementResult.next().get(KEY1).asInt()).isEqualTo(1); assertThat(statementResult.hasNext()).isTrue(); assertThat(statementResult.next().get(KEY1).asInt()).isEqualTo(2); assertThat(statementResult.hasNext()).isTrue(); //see org.neo4j.driver.internal.InternalStatementResult#list() assertThat(statementResult.list()).hasSize(1); assertThat(statementResult.hasNext()).isFalse(); assertThat(statementResult.summary().server()).isEqualTo(serverInfo); }
when(statementResult.summary()).thenReturn(mock(ResultSummary.class)); when(statementResult.keys()).thenReturn(keys);
when(statementResult.summary()).thenReturn(mock(ResultSummary.class)); when(statementResult.keys()).thenReturn(keys);