Search in sources :

Example 1 with Path

use of org.neo4j.driver.types.Path in project neo4j by neo4j.

the class TableOutputFormatterTest method prettyPrintPath.

@Test
public void prettyPrintPath() {
    // given
    List<String> keys = asList("path");
    Node n1 = mock(Node.class);
    when(n1.id()).thenReturn(1L);
    List<String> labels = asList("L1");
    when(n1.labels()).thenReturn(labels);
    when(n1.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Relationship r1 = mock(Relationship.class);
    when(r1.startNodeId()).thenReturn(2L);
    when(r1.type()).thenReturn("R1");
    when(r1.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Node n2 = mock(Node.class);
    when(n2.id()).thenReturn(2L);
    when(n2.labels()).thenReturn(asList("L2"));
    when(n2.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Relationship r2 = mock(Relationship.class);
    when(r2.startNodeId()).thenReturn(2L);
    when(r2.type()).thenReturn("R2");
    when(r2.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Node n3 = mock(Node.class);
    when(n3.id()).thenReturn(3L);
    when(n3.labels()).thenReturn(asList("L3"));
    when(n3.asMap(anyObject())).thenReturn(Collections.emptyMap());
    Path.Segment s1 = mock(Path.Segment.class);
    when(s1.relationship()).thenReturn(r1);
    when(s1.start()).thenReturn(n1);
    when(s1.end()).thenReturn(n2);
    Path.Segment s2 = mock(Path.Segment.class);
    when(s2.relationship()).thenReturn(r2);
    when(s2.start()).thenReturn(n2);
    when(s2.end()).thenReturn(n3);
    List<Path.Segment> segments = asList(s1, s2);
    List<Node> nodes = asList(n1, n2);
    List<Relationship> relationships = asList(r1);
    InternalPath internalPath = new InternalPath(segments, nodes, relationships);
    Value value = new PathValue(internalPath);
    Record record = new InternalRecord(keys, new Value[] { value });
    // when
    String actual = verbosePrinter.format(new ListBoltResult(asList(record), mock(ResultSummary.class)));
    // then
    assertThat(actual, containsString("| (:L1)<-[:R1]-(:L2)-[:R2]->(:L3) |"));
}
Also used : Path(org.neo4j.driver.types.Path) InternalPath(org.neo4j.driver.internal.InternalPath) PathValue(org.neo4j.driver.internal.value.PathValue) Node(org.neo4j.driver.types.Node) InternalNode(org.neo4j.driver.internal.InternalNode) InternalPath(org.neo4j.driver.internal.InternalPath) StringContains.containsString(org.hamcrest.core.StringContains.containsString) InternalRecord(org.neo4j.driver.internal.InternalRecord) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Relationship(org.neo4j.driver.types.Relationship) InternalRelationship(org.neo4j.driver.internal.InternalRelationship) Value(org.neo4j.driver.Value) RelationshipValue(org.neo4j.driver.internal.value.RelationshipValue) NodeValue(org.neo4j.driver.internal.value.NodeValue) DurationValue(org.neo4j.driver.internal.value.DurationValue) PathValue(org.neo4j.driver.internal.value.PathValue) PointValue(org.neo4j.driver.internal.value.PointValue) InternalRecord(org.neo4j.driver.internal.InternalRecord) Record(org.neo4j.driver.Record) Test(org.junit.Test)

Example 2 with Path

use of org.neo4j.driver.types.Path in project neo4j by neo4j.

the class PrettyPrinterTest method prettyPrintThreeSegmentPath.

@Test
public void prettyPrintThreeSegmentPath() {
    // given
    Record record = mock(Record.class);
    Value value = mock(Value.class);
    Node start = mock(Node.class);
    when(start.labels()).thenReturn(asList("start"));
    when(start.id()).thenReturn(1L);
    Node second = mock(Node.class);
    when(second.labels()).thenReturn(asList("second"));
    when(second.id()).thenReturn(2L);
    Node third = mock(Node.class);
    when(third.labels()).thenReturn(asList("third"));
    when(third.id()).thenReturn(3L);
    Node end = mock(Node.class);
    when(end.labels()).thenReturn(asList("end"));
    when(end.id()).thenReturn(4L);
    Path path = mock(Path.class);
    when(path.start()).thenReturn(start);
    Relationship relationship = mock(Relationship.class);
    when(relationship.type()).thenReturn("RELATIONSHIP_TYPE");
    when(relationship.startNodeId()).thenReturn(1L).thenReturn(3L).thenReturn(3L);
    Path.Segment segment1 = mock(Path.Segment.class);
    when(segment1.start()).thenReturn(start);
    when(segment1.end()).thenReturn(second);
    when(segment1.relationship()).thenReturn(relationship);
    Path.Segment segment2 = mock(Path.Segment.class);
    when(segment2.start()).thenReturn(second);
    when(segment2.end()).thenReturn(third);
    when(segment2.relationship()).thenReturn(relationship);
    Path.Segment segment3 = mock(Path.Segment.class);
    when(segment3.start()).thenReturn(third);
    when(segment3.end()).thenReturn(end);
    when(segment3.relationship()).thenReturn(relationship);
    when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.PATH());
    when(value.asPath()).thenReturn(path);
    when(path.iterator()).thenReturn(asList(segment1, segment2, segment3).iterator());
    when(record.keys()).thenReturn(asList("path"));
    when(record.values()).thenReturn(asList(value));
    BoltResult result = new ListBoltResult(singletonList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("path" + NEWLINE + "(:start)-[:RELATIONSHIP_TYPE]->" + "(:second)<-[:RELATIONSHIP_TYPE]-(:third)-[:RELATIONSHIP_TYPE]->(:end)" + NEWLINE));
}
Also used : Path(org.neo4j.driver.types.Path) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Node(org.neo4j.driver.types.Node) Relationship(org.neo4j.driver.types.Relationship) Value(org.neo4j.driver.Value) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Example 3 with Path

use of org.neo4j.driver.types.Path in project neo4j by neo4j.

the class SnapshotExecutionTest method testEntityVirtualTypes.

// Since most test work with primitives (storable values), this is a smoke test that
// entity types work as result of Snapshot EE
@Test
void testEntityVirtualTypes() {
    var query = joinAsLines("CREATE (n1:LABEL_1), (n2:LABEL_2)", "MERGE (n1) - [:TYPE_1] -> (n2)", "RETURN () - [] -> ()[0] AS p");
    Path p = driver.session().run(query).stream().map(r -> r.get("p")).findFirst().get().asPath();
    assertThat(p.start().labels()).containsExactly("LABEL_1");
    assertThat(p.end().labels()).containsExactly("LABEL_2");
}
Also used : Path(org.neo4j.driver.types.Path) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Mode(org.neo4j.procedure.Mode) ConnectorPortRegister(org.neo4j.configuration.connectors.ConnectorPortRegister) Context(org.neo4j.procedure.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Path(org.neo4j.driver.types.Path) Transaction(org.neo4j.driver.Transaction) GlobalProcedures(org.neo4j.kernel.api.procedure.GlobalProcedures) ExtensionCallback(org.neo4j.test.extension.ExtensionCallback) AfterAll(org.junit.jupiter.api.AfterAll) Value(org.neo4j.driver.Value) Values(org.neo4j.driver.Values) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) TestInstance(org.junit.jupiter.api.TestInstance) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Inject(org.neo4j.test.extension.Inject) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) GraphDatabaseInternalSettings(org.neo4j.configuration.GraphDatabaseInternalSettings) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Strings.joinAsLines(org.neo4j.internal.helpers.Strings.joinAsLines) Procedure(org.neo4j.procedure.Procedure) TransientException(org.neo4j.driver.exceptions.TransientException) ClientException(org.neo4j.driver.exceptions.ClientException) Driver(org.neo4j.driver.Driver) SyntaxError(org.neo4j.kernel.api.exceptions.Status.Statement.SyntaxError) Executors(java.util.concurrent.Executors) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) KernelException(org.neo4j.exceptions.KernelException) BoltDbmsExtension(org.neo4j.test.extension.BoltDbmsExtension) Name(org.neo4j.procedure.Name) ArithmeticError(org.neo4j.kernel.api.exceptions.Status.Statement.ArithmeticError) Test(org.junit.jupiter.api.Test)

Example 4 with Path

use of org.neo4j.driver.types.Path in project neo4j by neo4j.

the class OutputFormatter method pathAsString.

@Nonnull
default String pathAsString(@Nonnull Path path) {
    List<String> list = new ArrayList<>(path.length());
    Node lastTraversed = path.start();
    if (lastTraversed != null) {
        list.add(nodeAsString(lastTraversed));
        for (Path.Segment segment : path) {
            Relationship relationship = segment.relationship();
            if (relationship.startNodeId() == lastTraversed.id()) {
                list.add("-" + relationshipAsString(relationship) + "->");
            } else {
                list.add("<-" + relationshipAsString(relationship) + "-");
            }
            list.add(nodeAsString(segment.end()));
            lastTraversed = segment.end();
        }
    }
    return String.join("", list);
}
Also used : Path(org.neo4j.driver.types.Path) Node(org.neo4j.driver.types.Node) Relationship(org.neo4j.driver.types.Relationship) ArrayList(java.util.ArrayList) Nonnull(javax.annotation.Nonnull)

Example 5 with Path

use of org.neo4j.driver.types.Path in project neo4j by neo4j.

the class PrettyPrinterTest method prettyPrintSingleNodePath.

@Test
public void prettyPrintSingleNodePath() {
    // given
    Record record = mock(Record.class);
    Value value = mock(Value.class);
    Node start = mock(Node.class);
    when(start.labels()).thenReturn(asList("start"));
    when(start.id()).thenReturn(1L);
    Node end = mock(Node.class);
    when(end.labels()).thenReturn(asList("end"));
    when(end.id()).thenReturn(2L);
    Path path = mock(Path.class);
    when(path.start()).thenReturn(start);
    Relationship relationship = mock(Relationship.class);
    when(relationship.type()).thenReturn("RELATIONSHIP_TYPE");
    when(relationship.startNodeId()).thenReturn(1L);
    Path.Segment segment1 = mock(Path.Segment.class);
    when(segment1.start()).thenReturn(start);
    when(segment1.end()).thenReturn(end);
    when(segment1.relationship()).thenReturn(relationship);
    when(value.type()).thenReturn(InternalTypeSystem.TYPE_SYSTEM.PATH());
    when(value.asPath()).thenReturn(path);
    when(path.iterator()).thenReturn(asList(segment1).iterator());
    when(record.keys()).thenReturn(asList("path"));
    when(record.values()).thenReturn(asList(value));
    BoltResult result = new ListBoltResult(asList(record), mock(ResultSummary.class));
    // when
    String actual = plainPrinter.format(result);
    // then
    assertThat(actual, is("path" + NEWLINE + "(:start)-[:RELATIONSHIP_TYPE]->(:end)" + NEWLINE));
}
Also used : Path(org.neo4j.driver.types.Path) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Node(org.neo4j.driver.types.Node) Relationship(org.neo4j.driver.types.Relationship) Value(org.neo4j.driver.Value) ResultSummary(org.neo4j.driver.summary.ResultSummary) Record(org.neo4j.driver.Record) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BoltResult(org.neo4j.shell.state.BoltResult) ListBoltResult(org.neo4j.shell.state.ListBoltResult) Test(org.junit.Test)

Aggregations

Path (org.neo4j.driver.types.Path)6 Value (org.neo4j.driver.Value)5 Node (org.neo4j.driver.types.Node)5 Relationship (org.neo4j.driver.types.Relationship)5 Test (org.junit.Test)4 Record (org.neo4j.driver.Record)4 ListBoltResult (org.neo4j.shell.state.ListBoltResult)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 ResultSummary (org.neo4j.driver.summary.ResultSummary)3 BoltResult (org.neo4j.shell.state.BoltResult)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Executors (java.util.concurrent.Executors)1 Nonnull (javax.annotation.Nonnull)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 StringContains.containsString (org.hamcrest.core.StringContains.containsString)1 AfterAll (org.junit.jupiter.api.AfterAll)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1