@Nonnull private String formatRecord(@Nonnull final Record record) { return record.values().stream().map(this::formatValue).collect(Collectors.joining(COMMA_SEPARATOR)); }
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)); }
@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]")); }
@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}]")); }
@Test public void prettyPrintNode() throws Exception { // given BoltResult result = mock(BoltResult.class); Record record = mock(Record.class); Value value = mock(Value.class); Node node = mock(Node.class); HashMap<String, Object> propertiesAsMap = new HashMap<>(); propertiesAsMap.put("prop1", "prop1_value"); propertiesAsMap.put("prop2", "prop2_value"); when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.NODE()); when(value.asNode()).thenReturn(node); when(node.labels()).thenReturn(asList("label1", "label2")); when(node.asMap(anyObject())).thenReturn(unmodifiableMap(propertiesAsMap)); when(record.keys()).thenReturn(asList("col1", "col2")); 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("col1, col2\n" + "(:label1:label2 {prop2: prop2_value, prop1: prop1_value})")); }
@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)); } }
when(record.values()).thenReturn(asList(relVal, nodeVal));
@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)); } }
when(record.values()).thenReturn(asList(value));
@Test public void shouldDiffFromPreviousGetTheDiffFromPreviousCorrectly() { // 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'})"); session.run("MATCH (from:From), (to:To) CREATE (from)<-[:PREVIOUS]-(to)"); // When StatementResult result = session .run("MATCH (stateTo:State:To) WITH stateTo CALL graph.versioner.diff.from.previous(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)); } }
@Test public void shouldDiffFromCurrentGetTheDiffFromCurrentCorrectly() { // 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 (:Entity)"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE]->(s:State:From {keep:'keep', update:'old', delete:'delete'})"); session.run("MATCH (e:Entity) CREATE (e)-[:HAS_STATE]->(s:State:To {keep:'keep', update:'new', new:'new'})"); session.run("MATCH (to:To)-[:HAS_STATE]-(e:Entity) CREATE (e)-[:CURRENT]->(to)"); // When StatementResult result = session .run("MATCH (stateFrom:State:From) WITH stateFrom CALL graph.versioner.diff.from.current(stateFrom) 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)); } }
when(record.values()).thenReturn(asList(value));
when(record.values()).thenReturn(asList(value));
when(record.values()).thenReturn(asList(relVal, nodeVal));