@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 ); } }
private Stream<RowResult> getRowResultStream(boolean virtual, Session session, Map<String, Object> params, String statement, boolean read) { Map<Long, Object> nodesCache = new HashMap<>(); return StreamSupport.stream(Spliterators.spliteratorUnknownSize(runStatement(statement, session, params, read), 0), true) .map(record -> new RowResult(record.asMap(value -> { Object entity = value.asObject(); if (entity instanceof Node) return toNode(entity, virtual, nodesCache); if (entity instanceof Relationship) return toRelationship(entity, virtual, nodesCache); if (entity instanceof Path) return toPath(entity, virtual, nodesCache); return entity; }))); }
recordMap.put("node", nodeVal); List<String> keys = asList("rel", "node"); when(record.keys()).thenReturn(keys); when(record.get(eq("rel"))).thenReturn(relVal); when(record.get(eq("node"))).thenReturn(nodeVal); when(record.<Value>asMap(anyObject())).thenReturn(recordMap); when(record.values()).thenReturn(asList(relVal, nodeVal));
public static <T> Map<String, T> map( Record record, Function<Value, T> mapFunction ) { int size = record.size(); switch ( size ) { case 0: return emptyMap(); case 1: return singletonMap( record.keys().get( 0 ), mapFunction.apply( record.get( 0 ) ) ); default: Map<String,T> map = Iterables.newLinkedHashMapWithSize( size ); List<String> keys = record.keys(); for ( int i = 0; i < size; i++ ) { map.put( keys.get( i ), mapFunction.apply( record.get( i ) ) ); } return unmodifiableMap( map ); } }
public static <V> List<Pair<String, V>> fields( final Record map, final Function<Value, V> mapFunction ) { int size = map.keys().size(); switch ( size ) { case 0: return emptyList(); case 1: { String key = map.keys().iterator().next(); Value value = map.get( key ); return singletonList( InternalPair.of( key, mapFunction.apply( value ) ) ); } default: { List<Pair<String, V>> list = new ArrayList<>( size ); List<String> keys = map.keys(); for ( int i = 0; i < size; i++ ) { String key = keys.get( i ); Value value = map.get( i ); list.add( InternalPair.of( key, mapFunction.apply( value ) ) ); } return unmodifiableList( list ); } } }
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)); }
@Override public List<String> getColumns() { List<String> columns = new ArrayList<String>(); if (this.records.size() > 0) { Record rec = this.records.get(0); columns.addAll(rec.keys()); } return columns; }
@Nonnull private String formatRecord(@Nonnull final Record record) { return record.values().stream().map(this::formatValue).collect(Collectors.joining(COMMA_SEPARATOR)); }
private Entity getPropertiesObject(long id, int rowIndex, ElemType typ) { Record rec = this.records.get(rowIndex); List<Pair<String, Value>> flds = rec.fields(); for (Pair<String, Value> pair : flds) { if (typ == ElemType.NODE && pair.value() instanceof NodeValue) { Node nd = pair.value().asNode(); if (nd.id() == id) return nd; } else if (typ == ElemType.RELATION && pair.value() instanceof RelationshipValue) { Relationship rel = pair.value().asRelationship(); if (rel.id() == id) return rel; } } // element with id may not have been loaded return this.reloaded.getEntity(id, typ); }
@Test public void prettyPrintList() throws Exception { // given BoltResult result = mock(BoltResult.class); Record record1 = mock(Record.class); Record record2 = mock(Record.class); Value value1 = mock(Value.class); Value value2 = mock(Value.class); when(value1.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.LIST()); when(value2.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.LIST()); when(value1.asList(Matchers.any(Function.class))).thenReturn(asList("val1_1", "val1_2")); when(value2.asList(Matchers.any(Function.class))).thenReturn(asList("val2_1")); when(record1.keys()).thenReturn(asList("col1", "col2")); when(record1.values()).thenReturn(asList(value1, value2)); when(record2.values()).thenReturn(asList(value2)); when(result.getRecords()).thenReturn(asList(record1, record2)); when(result.getSummary()).thenReturn(mock(ResultSummary.class)); // when String actual = plainPrinter.format(result); // then assertThat(actual, is("col1, col2\n[val1_1, val1_2], [val2_1]\n[val2_1]")); }
@Override public List<String> getColumns() { List<String> columns = new ArrayList<String>(); if (this.records.size() > 0) { Record rec = this.records.get(0); columns.addAll(rec.keys()); } return columns; }
@Test public void shouldDiffGetTheDiffBetweenTwoStatesCorrectly() { // 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 (s:State:From {keep:'keep', update:'old', delete:'delete'})"); session.run("CREATE (s:State:To {keep:'keep', update:'new', new:'new'})"); // When StatementResult result = session .run("MATCH (stateTo:State:To), (stateFrom:State:From) WITH stateFrom, stateTo CALL graph.versioner.diff(stateFrom, stateTo) YIELD operation, label, oldValue, newValue RETURN operation, label, oldValue, newValue"); // Then assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_REMOVE, "delete", "delete", "null")); assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "update", "old", "new")); assertThat(result.next().values().stream().map(Value::asString).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_ADD, "new", "null", "new")); assertThat(result.hasNext(), is(false)); } }
private Entity getPropertiesObject(long id, int rowIndex, ElemType typ) { Record rec = this.records.get(rowIndex); List<Pair<String, Value>> flds = rec.fields(); for (Pair<String, Value> pair : flds) { if (typ == ElemType.NODE && pair.value() instanceof NodeValue) { Node nd = pair.value().asNode(); if (nd.id() == id) return nd; } else if (typ == ElemType.RELATION && pair.value() instanceof RelationshipValue) { Relationship rel = pair.value().asRelationship(); if (rel.id() == id) return rel; } } // element with id may not have been loaded return this.reloaded.getEntity(id, typ); }
@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()); } }
private Map<String, Object> buildModel() { Map<String, Object> row = new LinkedHashMap<>(); if (resultProjection.hasNext()) { row = restModelAdapter.adapt(resultProjection.next().asMap()); } return row; }
@Test public void prettyPrintRelationships() throws Exception { // given BoltResult result = mock(BoltResult.class); Record record = mock(Record.class); Value value = mock(Value.class); Relationship relationship = mock(Relationship.class); HashMap<String, Object> propertiesAsMap = new HashMap<>(); propertiesAsMap.put("prop1", "prop1_value"); propertiesAsMap.put("prop2", "prop2_value"); when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.RELATIONSHIP()); when(value.asRelationship()).thenReturn(relationship); when(relationship.type()).thenReturn("RELATIONSHIP_TYPE"); when(relationship.asMap(anyObject())).thenReturn(unmodifiableMap(propertiesAsMap)); when(record.keys()).thenReturn(asList("rel")); 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("rel\n[:RELATIONSHIP_TYPE {prop2: prop2_value, prop1: prop1_value}]")); }
@Override @Nonnull public String format(@Nonnull final BoltResult result) { StringBuilder sb = new StringBuilder(); List<Record> records = result.getRecords(); if (!records.isEmpty()) { sb.append(records.get(0).keys().stream().collect(Collectors.joining(COMMA_SEPARATOR))); sb.append("\n"); sb.append(records.stream().map(this::formatRecord).collect(Collectors.joining("\n"))); } return sb.toString(); }
@Test public void shouldDiffGetTheDiffBetweenTwoStatesCorrectlyWithDifferentObjectTypes() { // 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 (s:State:From {a:1, b:1, c:'stringA', d:'string', e: [1, 2], f: [1, 2]})"); session.run("CREATE (s:State:To {a:2, b:1, c:'stringB', d:'string', e:[1, 2], f: [1, 3]})"); // When StatementResult result = session .run("MATCH (stateTo:State:To), (stateFrom:State:From) WITH stateFrom, stateTo CALL graph.versioner.diff(stateFrom, stateTo) YIELD operation, label, oldValue, newValue RETURN operation, label, oldValue, newValue"); // Then assertThat(result.next().values().stream().map(value -> value.asObject().toString()).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "a", "1", "2")); assertThat(result.next().values().stream().map(value -> value.asObject().toString()).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "c", "stringA", "stringB")); assertThat(result.next().values().stream().map(value -> value.asObject().toString()).collect(Collectors.toList()), contains(Utility.DIFF_OPERATION_UPDATE, "f", "[1, 2]", "[1, 3]")); assertThat(result.hasNext(), is(false)); } }
private CypherResult buildResult(StatementResult response) { CypherResult result = new CypherResult(); while (response.hasNext()) { Record record = response.next(); ResultRow resultRow = new ResultRow(); for (Pair<String, Value> fieldInRecord : record.fields()) { resultRow.add(fieldInRecord.key(), fieldInRecord.value()); } result.addRow(resultRow); } return result; }