Search in sources :

Example 31 with Statement

use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldBeAbleToRemoveContainedRelationshipOnly.

@Test
public void shouldBeAbleToRemoveContainedRelationshipOnly() {
    // given
    Folder folder = new Folder();
    Document doc1 = new Document();
    folder.getDocuments().add(doc1);
    folder.getArchived().add(doc1);
    doc1.setFolder(folder);
    folder.setId(0L);
    doc1.setId(1L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(doc1);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc1.getId(), null, Folder.class, Document.class));
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "ARCHIVED", doc1.getId(), null, Folder.class, Document.class));
    // when
    folder.getDocuments().remove(doc1);
    doc1.setFolder(null);
    // then
    assertThat(folder.getDocuments()).isEmpty();
    assertThat(folder.getArchived()).hasSize(1);
    Compiler compiler = mapper.map(folder).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<Statement> statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
    mapper = new EntityGraphMapper(mappingMetadata, mappingContext);
    // There are no more changes to the graph
    compiler = mapper.map(doc1).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.deleteRelationshipStatements();
    assertThat(statements).isEmpty();
}
Also used : EntityGraphMapper(org.neo4j.ogm.context.EntityGraphMapper) Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 32 with Statement

use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldNotBeAbleToCreateDuplicateRelationship.

@Test
public void shouldNotBeAbleToCreateDuplicateRelationship() {
    Folder folder = new Folder();
    Document document = new Document();
    document.setFolder(folder);
    // we try to store two identical references to the document object. Although this
    // is supported by the graph, it isn't currently supported by the OGM,
    // therefore we expect only one relationship to be persisted
    folder.getDocuments().add(document);
    folder.getDocuments().add(document);
    assertThat(folder.getDocuments()).hasSize(2);
    // save folder
    Compiler compiler = mapper.map(folder).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<Statement> statements = compiler.createNodesStatements();
    List<String> createNodeStatements = cypherStatements(statements);
    assertThat(createNodeStatements).hasSize(2);
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    List<String> createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(1);
    assertThat(createRelStatements.get(0)).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    List rows = (List) statements.get(0).getParameters().get("rows");
    assertThat(rows).hasSize(1);
    // save document
    compiler = mapper.map(document).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.createNodesStatements();
    createNodeStatements = cypherStatements(statements);
    assertThat(createNodeStatements).hasSize(2);
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
    for (Statement statement : statements) {
        rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    statements = compiler.createRelationshipsStatements();
    createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(1);
    assertThat(createRelStatements.get(0)).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    rows = (List) statements.get(0).getParameters().get("rows");
    assertThat(rows).hasSize(1);
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) ArrayList(java.util.ArrayList) List(java.util.List) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 33 with Statement

use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.

the class CompilerTest method shouldCorrectlyRemoveRelationshipWhenItemIsDisconnectedFromNonOwningSide.

@Test
public void shouldCorrectlyRemoveRelationshipWhenItemIsDisconnectedFromNonOwningSide() {
    Long schoolId = 0L;
    Long whiteId = 1L;
    Long jonesId = 2L;
    School hillsRoad = new School("Hills Road Sixth Form College");
    hillsRoad.setId(schoolId);
    Teacher mrWhite = new Teacher("Mr White");
    mrWhite.setId(whiteId);
    Teacher missJones = new Teacher("Miss Jones");
    missJones.setId(jonesId);
    hillsRoad.setTeachers(Arrays.asList(missJones, mrWhite));
    assertThat(hillsRoad.getTeachers()).contains(mrWhite);
    assertThat(hillsRoad.getTeachers()).contains(missJones);
    assertThat(mrWhite.getSchool()).isEqualTo(hillsRoad);
    assertThat(missJones.getSchool()).isEqualTo(hillsRoad);
    mappingContext.addNodeEntity(hillsRoad);
    mappingContext.addNodeEntity(mrWhite);
    mappingContext.addNodeEntity(missJones);
    mappingContext.addRelationship(new MappedRelationship(schoolId, "TEACHERS", whiteId, null, School.class, Teacher.class));
    mappingContext.addRelationship(new MappedRelationship(schoolId, "TEACHERS", jonesId, null, School.class, Teacher.class));
    mappingContext.addRelationship(new MappedRelationship(whiteId, "SCHOOL", schoolId, null, Teacher.class, School.class));
    mappingContext.addRelationship(new MappedRelationship(jonesId, "SCHOOL", schoolId, null, Teacher.class, School.class));
    // Fire Mr White:
    mrWhite.setSchool(null);
    // validate model:
    assertThat(mrWhite.getSchool()).isNull();
    assertThat(hillsRoad.getTeachers()).doesNotContain(mrWhite);
    // we expect hillsRoad relationship to mrWhite to be removed.
    // however, the change to MrWhite's relationship is not detected.
    // this is because MrWhite is not "visited" during the traversal of
    // hillsRoad - his reference is now inaccessible. this looks like a FIXME
    Compiler compiler = mapAndCompile(hillsRoad, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).isEmpty();
    statements = compiler.createRelationshipsStatements();
    assertThat(statements).isEmpty();
    statements = compiler.deleteRelationshipStatements();
    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 MATCH (startNode)-[rel:`TEACHERS`]->(endNode) DELETE rel");
    assertThat(((List) statements.get(0).getParameters().get("rows"))).hasSize(1);
    // we expect mrWhite's relationship to hillsRoad to be removed
    // but the change to hillsRoad's relationship with MrWhite is not detected
    // this is because hillsRoad object is no longer directly accessible from MrWhite
    // looks like a FIXME (infer symmetric deletions)
    compiler = mapAndCompile(mrWhite, -1);
    statements = compiler.createNodesStatements();
    assertThat(statements).isEmpty();
    statements = compiler.createRelationshipsStatements();
    assertThat(statements).isEmpty();
    statements = compiler.deleteRelationshipStatements();
    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 MATCH (startNode)-[rel:`SCHOOL`]->(endNode) DELETE rel");
    assertThat(((List) statements.get(0).getParameters().get("rows"))).hasSize(1);
// because missJones has a reference to hillsRoad, we expect an outcome
// the same as if we had saved hillsRoiad directly.
// expectOnSave(missJones,
// "MATCH ($0)-[_2:TEACHERS]->($1) WHERE id($0)=0 AND id($1)=1 DELETE _2");
}
Also used : School(org.neo4j.ogm.domain.education.School) Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Teacher(org.neo4j.ogm.domain.education.Teacher) Test(org.junit.Test)

Example 34 with Statement

use of org.neo4j.ogm.request.Statement 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 35 with Statement

use of org.neo4j.ogm.request.Statement 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

Statement (org.neo4j.ogm.request.Statement)38 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)19 List (java.util.List)15 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)10 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)7 Map (java.util.Map)6 Document (org.neo4j.ogm.domain.filesystem.Document)6 Folder (org.neo4j.ogm.domain.filesystem.Folder)6 RowModel (org.neo4j.ogm.model.RowModel)6 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)5 Teacher (org.neo4j.ogm.domain.education.Teacher)4 HashMap (java.util.HashMap)3 Set (java.util.Set)3 DefaultRowModelRequest (org.neo4j.ogm.cypher.query.DefaultRowModelRequest)3 Forum (org.neo4j.ogm.domain.forum.Forum)3 ForumTopicLink (org.neo4j.ogm.domain.forum.ForumTopicLink)3 Topic (org.neo4j.ogm.domain.forum.Topic)3 Node (org.neo4j.ogm.model.Node)3 Arrays (java.util.Arrays)2