public BoltNeo4jMapsTupleIterator(StatementResult statementResult) { super( statementResult, statementResult.keys() ); }
@Nonnull public List<String> getKeys() { return statementResult.keys(); }
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; }
@Override public Response<RowModel> execute(DefaultRequest query) { final List<RowModel> rowModels = new ArrayList<>(); String[] columns = null; for (Statement statement : query.getStatements()) { StatementResult result = executeRequest(statement); if (columns == null) { try { List<String> columnSet = result.keys(); columns = columnSet.toArray(new String[columnSet.size()]); } catch (ClientException e) { throw new CypherException(e.code(), e.getMessage(), e); } } try (RowModelResponse rowModelResponse = new RowModelResponse(result, transactionManager)) { RowModel model; while ((model = rowModelResponse.next()) != null) { rowModels.add(model); } result.consume(); } } return new MultiStatementBasedResponse(columns, rowModels); }
@Override public Response<RowModel> execute(DefaultRequest query) { final List<RowModel> rowModels = new ArrayList<>(); String[] columns = null; for (Statement statement : query.getStatements()) { StatementResult result = executeRequest(statement); if (columns == null) { try { List<String> columnSet = result.keys(); columns = columnSet.toArray(new String[columnSet.size()]); } catch (ClientException e) { throw new CypherException(e.code(), e.getMessage(), e); } } try (RowModelResponse rowModelResponse = new RowModelResponse(result, transactionManager, entityAdapter)) { RowModel model; while ((model = rowModelResponse.next()) != null) { rowModels.add(model); } result.consume(); } } return new MultiStatementBasedResponse(columns, rowModels); }
@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\"}) |")); }
@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 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.keys()).thenReturn(keys);
when(statementResult.keys()).thenReturn(keys);
@Override public GraphQueryResult execute(String query, Map<String, Object> statementParameters) { try { Driver driver = GraphDatabase.driver(url, auth); try { try (Session session = driver.session()) { Neo4jBoltBuffer buffer = new Neo4jBoltBuffer(); long startTime = System.currentTimeMillis(); StatementResult statementResult = session.run(query, statementParameters); buffer.addColumns(statementResult.keys()); for (Record record : statementResult.list()) { // Add row buffer.addRow(record.asMap()); } buffer.addResultSummary(statementResult.consume()); long endTime = System.currentTimeMillis(); return new Neo4jBoltQueryResult(endTime - startTime, buffer); } } finally { driver.closeAsync(); } } catch (UnresolvedAddressException e) { throw new ClientException(e.getMessage()); } }