private Object toRelationship(Object value, boolean virtual, Map<Long, Object> nodesCache) { Value internalValue = ((InternalEntity) value).asValue(); Relationship relationship = internalValue.asRelationship(); if (virtual) { VirtualNode start = (VirtualNode) nodesCache.getOrDefault(relationship.startNodeId(), new VirtualNode(relationship.startNodeId(), db)); VirtualNode end = (VirtualNode) nodesCache.getOrDefault(relationship.endNodeId(), new VirtualNode(relationship.endNodeId(), db)); VirtualRelationship virtualRelationship = new VirtualRelationship(relationship.id(), start, end, RelationshipType.withName(relationship.type()), relationship.asMap()); return virtualRelationship; } else return Util.map("entityType", internalValue.type().name(), "type", relationship.type(), "id", relationship.id(), "start", relationship.startNodeId(), "end", relationship.endNodeId(), "properties", relationship.asMap()); }
public String relationshipType(Object relationship) { return ((Relationship) relationship).type(); }
public String relationshipType(Object relationship) { return ((Relationship) relationship).type(); }
@Override public String getRelationType(long relationId, int rowIndex) { if (rowIndex >= 0) { Relationship rel = (Relationship) getPropertiesObject(relationId, rowIndex, ElemType.RELATION); return rel.type(); } return null; }
@Override public String getRelationType(long relationId, int rowIndex) { if (rowIndex >= 0) { Relationship rel = (Relationship) getPropertiesObject(relationId, rowIndex, ElemType.RELATION); return rel.type(); } return null; }
@Nonnull default String relationshipAsString(@Nonnull Relationship relationship) { List<String> relationshipAsString = new ArrayList<>(); relationshipAsString.add(COLON + escape(relationship.type())); relationshipAsString.add(mapAsStringWithEmpty(relationship.asMap(this::formatValue))); return "[" + joinWithSpace(relationshipAsString) + "]"; }
public Neo4jBoltRelationship(Relationship rel) { this.id = String.valueOf(rel.id()); this.types = Collections.singletonList(rel.type()); this.propertyContainer = new Neo4jBoltPropertyContainer(rel.asMap()); this.startNodeId = String.valueOf(rel.startNodeId()); this.endNodeId = String.valueOf(rel.endNodeId()); }
@Override public String toString() { return String.format( relationship.startNodeId() == start.id() ? "(%s)-[%s:%s]->(%s)" : "(%s)<-[%s:%s]-(%s)", start.id(), relationship.id(), relationship.type(), end.id() ); } }
@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}]")); }
when(relationship.type()).thenReturn("RELATIONSHIP_TYPE"); when(relationship.startNodeId()).thenReturn(1l);
when(relationship.type()).thenReturn("RELATIONSHIP,TYPE"); when(relationship.asMap(anyObject())).thenReturn(unmodifiableMap(relProp));
when(relationship.type()).thenReturn("RELATIONSHIP_TYPE"); when(relationship.startNodeId()).thenReturn(1l).thenReturn(3l);
when(relationship.type()).thenReturn("RELATIONSHIP_TYPE"); when(relationship.startNodeId()).thenReturn(1l).thenReturn(3l).thenReturn(3l);
@Test public void shouldGetCurrentPathByGivenEntity() { // 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 (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); Node entity = session.run("MATCH (e:Entity) RETURN e").single().get("e").asNode(); Node state = session.run("MATCH (s:State) RETURN s").single().get("s").asNode(); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.get.current.path(e) YIELD path RETURN path"); Path current = result.single().get("path").asPath(); Iterator<Relationship> relsIterator = current.relationships().iterator(); Map<String, Object> rels = new HashMap<>(); while (relsIterator.hasNext()) { Relationship support = relsIterator.next(); rels.put(support.type(), support); } // Then assertThat(current.contains(entity), equalTo(true)); assertThat(rels.containsKey(Utility.CURRENT_TYPE), equalTo(true)); assertThat(current.contains(state), equalTo(true)); } }
@Test public void shouldGetAllStateNodesByGivenEntityWithOnlyOneCurrentState() { // 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 (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); Node entity = session.run("MATCH (e:Entity) RETURN e").single().get("e").asNode(); Node stateNew = session.run("MATCH (s:State {key:'initialValue'}) RETURN s").single().get("s").asNode(); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.get.all(e) YIELD path RETURN path"); Path current = result.single().get("path").asPath(); Iterator<Relationship> relsIterator = current.relationships().iterator(); Map<String, Object> rels = new HashMap<>(); while (relsIterator.hasNext()) { Relationship support = relsIterator.next(); rels.put(support.type(), support); } // Then assertThat(current.contains(entity), equalTo(true)); assertThat(current.contains(stateNew), equalTo(true)); } }
@Test public void shouldGetAllStateNodesByGivenEntity() { // 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 (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'})"); session.run("MATCH (e:Entity {key:'immutableValue'})-[:CURRENT {date:localdatetime('1988-10-27T00:00:00')}]->(s:State {key:'initialValue'}) CREATE (e)-[:HAS_STATE {startDate:localdatetime('1988-10-27T00:00:00')}]->(s)"); session.run("MATCH (e)-[hs:HAS_STATE]->(s) CREATE (e)-[:HAS_STATE {startDate: localdatetime('1988-10-26T00:00:00'), endDate: hs.startDate}]->(:State{key:'oldState'})"); session.run("MATCH (s1:State {key:'oldState'}), (s2:State {key:'initialValue'}) CREATE (s1)<-[:PREVIOUS {date: localdatetime('1988-10-26T00:00:00')}]-(s2) "); Node entity = session.run("MATCH (e:Entity) RETURN e").single().get("e").asNode(); Node stateNew = session.run("MATCH (s:State {key:'initialValue'}) RETURN s").single().get("s").asNode(); Node stateOld = session.run("MATCH (s:State {key:'oldState'}) RETURN s").single().get("s").asNode(); // When StatementResult result = session.run("MATCH (e:Entity) WITH e CALL graph.versioner.get.all(e) YIELD path RETURN path"); Path current = result.single().get("path").asPath(); Iterator<Relationship> relsIterator = current.relationships().iterator(); Map<String, Object> rels = new HashMap<>(); while (relsIterator.hasNext()) { Relationship support = relsIterator.next(); rels.put(support.type(), support); } // Then assertThat(current.contains(entity), equalTo(true)); assertThat(current.contains(stateNew), equalTo(true)); assertThat(rels.containsKey(Utility.PREVIOUS_TYPE), equalTo(true)); assertThat(current.contains(stateOld), equalTo(true)); } }