Search in sources :

Example 6 with Topic

use of org.neo4j.ogm.domain.forum.Topic in project neo4j-ogm by neo4j.

the class EntityGraphMapperTest method shouldProduceCypherForSavingNewRichRelationshipBetweenNodes.

@Test
public void shouldProduceCypherForSavingNewRichRelationshipBetweenNodes() {
    Forum forum = new Forum();
    forum.setName("SDN FAQs");
    Topic topic = new Topic();
    ForumTopicLink link = new ForumTopicLink();
    link.setForum(forum);
    link.setTopic(topic);
    link.setTimestamp(1647209L);
    forum.setTopicsInForum(Arrays.asList(link));
    session.save(forum);
    session.clear();
    assertThat(session.query("MATCH (f:Forum {name:'SDN FAQs'})-[:HAS_TOPIC {timestamp:1647209}]->(t:Topic) return f", emptyMap()).queryResults()).hasSize(1);
}
Also used : ForumTopicLink(org.neo4j.ogm.domain.forum.ForumTopicLink) Topic(org.neo4j.ogm.domain.forum.Topic) Forum(org.neo4j.ogm.domain.forum.Forum) Test(org.junit.Test)

Example 7 with Topic

use of org.neo4j.ogm.domain.forum.Topic in project neo4j-ogm by neo4j.

the class CompilerTest method shouldCreateRelationshipWithPropertiesFromRelationshipEntity.

@Test
public void shouldCreateRelationshipWithPropertiesFromRelationshipEntity() {
    Forum forum = new Forum();
    forum.setName("SDN FAQs");
    Topic topic = new Topic();
    ForumTopicLink link = new ForumTopicLink();
    link.setForum(forum);
    link.setTopic(topic);
    link.setTimestamp(1647209L);
    forum.setTopicsInForum(Arrays.asList(link));
    // the entire object tree is accessible from the forum
    // Note that a relationshipEntity has a direction by default (srcNode -> tgtNode)
    // because it has an annotation, so we should not create an inverse relationship.
    Compiler compiler = mapAndCompile(forum, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Forum`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`Topic`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId CREATE (startNode)-[rel:`HAS_TOPIC`]->(endNode) SET rel += row.props RETURN row.relRef as ref, ID(rel) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    // the entire object tree is accessible from the link
    compiler = mapAndCompile(link, -1);
    statements = compiler.createNodesStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Forum`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`Topic`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId CREATE (startNode)-[rel:`HAS_TOPIC`]->(endNode) SET rel += row.props RETURN row.relRef as ref, ID(rel) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    // the related entity is not visible from the Topic object.
    compiler = mapAndCompile(topic, -1);
    statements = compiler.createNodesStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Topic`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    assertThat(statements).isEmpty();
}
Also used : Statement(org.neo4j.ogm.request.Statement) ForumTopicLink(org.neo4j.ogm.domain.forum.ForumTopicLink) ArrayList(java.util.ArrayList) List(java.util.List) Topic(org.neo4j.ogm.domain.forum.Topic) Forum(org.neo4j.ogm.domain.forum.Forum) Test(org.junit.Test)

Example 8 with Topic

use of org.neo4j.ogm.domain.forum.Topic in project neo4j-ogm by neo4j.

the class CompilerTest method shouldUpdatingExistingRelationshipEntity.

@Test
public void shouldUpdatingExistingRelationshipEntity() {
    Long forumId = 0L;
    Long topicId = 1L;
    Long relationshipId = 2L;
    Forum forum = new Forum();
    forum.setId(forumId);
    forum.setName("Spring Data Neo4j");
    Topic topic = new Topic();
    topic.setTopicId(topicId);
    topic.setInActive(Boolean.FALSE);
    ForumTopicLink link = new ForumTopicLink();
    link.setId(relationshipId);
    link.setForum(forum);
    link.setTopic(topic);
    forum.setTopicsInForum(Arrays.asList(link));
    mappingContext.addNodeEntity(forum);
    mappingContext.addNodeEntity(topic);
    mappingContext.addRelationshipEntity(link, relationshipId);
    MappedRelationship mappedRelationship = new MappedRelationship(forumId, "HAS_TOPIC", topicId, relationshipId, Forum.class, ForumTopicLink.class);
    mappingContext.addRelationship(mappedRelationship);
    // change the timestamp
    link.setTimestamp(327790L);
    // expect the property on the relationship entity to be updated on the graph relationship
    Compiler compiler = mapAndCompile(link, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).isEmpty();
    statements = compiler.updateRelationshipStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows AS row MATCH ()-[r]->() WHERE ID(r) = row.relId SET r += row.props RETURN ID(r) as ref, ID(r) as id, $type as type");
    assertThat((List) statements.get(0).getParameters().get("rows")).hasSize(1);
}
Also used : Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) ForumTopicLink(org.neo4j.ogm.domain.forum.ForumTopicLink) ArrayList(java.util.ArrayList) List(java.util.List) Topic(org.neo4j.ogm.domain.forum.Topic) Forum(org.neo4j.ogm.domain.forum.Forum) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 Topic (org.neo4j.ogm.domain.forum.Topic)8 ForumTopicLink (org.neo4j.ogm.domain.forum.ForumTopicLink)6 Forum (org.neo4j.ogm.domain.forum.Forum)5 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)3 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)3 Statement (org.neo4j.ogm.request.Statement)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)2 Map (java.util.Map)1 Member (org.neo4j.ogm.domain.forum.Member)1 Comment (org.neo4j.ogm.domain.forum.activity.Comment)1 Post (org.neo4j.ogm.domain.forum.activity.Post)1 Satellite (org.neo4j.ogm.domain.satellites.Satellite)1