Search in sources :

Example 11 with Statement

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

the class DirectRelationshipsTest method shouldBeAbleToRemoveAnyRegisteredRelationship.

@Test
public void shouldBeAbleToRemoveAnyRegisteredRelationship() {
    // given
    Folder folder = new Folder();
    Document doc1 = new Document();
    Document doc2 = new Document();
    folder.getDocuments().add(doc1);
    folder.getDocuments().add(doc2);
    doc1.setFolder(folder);
    doc2.setFolder(folder);
    folder.setId(0L);
    doc1.setId(1L);
    doc2.setId(2L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(doc1);
    mappingContext.addNodeEntity(doc2);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc1.getId(), null, Folder.class, Document.class));
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    // when
    doc2.setFolder(null);
    folder.getDocuments().remove(doc2);
    // then
    assertThat(folder.getDocuments()).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");
    // we need to re-establish the relationship in the mapping context for this expectation, otherwise
    // the previous save will have de-registered the relationship.
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(doc1).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    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");
    // we need to re-establish the relationship in the mapping context for this expectation, otherwise
    // the previous save will have de-registered the relationship.
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(doc2).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    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");
}
Also used : 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 12 with Statement

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

the class CompilerTest method shouldCorrectlyRemoveRelationshipWhenItemIsMovedToDifferentCollection.

@Test
public void shouldCorrectlyRemoveRelationshipWhenItemIsMovedToDifferentCollection() {
    Long teacherId = 0L;
    Long businessStudiesCourseId = 1L;
    Long designTechnologyCourseId = 2L;
    Long shivaniId = 3L;
    Course designTech = new Course("GCSE Design & Technology");
    designTech.setId(designTechnologyCourseId);
    Course businessStudies = new Course("GNVQ Business Studies");
    businessStudies.setId(businessStudiesCourseId);
    Teacher msThompson = new Teacher();
    msThompson.setId(teacherId);
    msThompson.setName("Ms Thompson");
    msThompson.setCourses(Arrays.asList(businessStudies, designTech));
    Student shivani = new Student("Shivani");
    shivani.setId(shivaniId);
    mappingContext.addNodeEntity(msThompson);
    mappingContext.addNodeEntity(businessStudies);
    mappingContext.addNodeEntity(designTech);
    mappingContext.addNodeEntity(shivani);
    mappingContext.addRelationship(new MappedRelationship(teacherId, "COURSES", businessStudiesCourseId, null, Teacher.class, Course.class));
    mappingContext.addRelationship(new MappedRelationship(teacherId, "COURSES", designTechnologyCourseId, null, Teacher.class, Course.class));
    mappingContext.addRelationship(new MappedRelationship(businessStudiesCourseId, "STUDENTS", shivaniId, null, Teacher.class, Student.class));
    // move shivani from one course to the other
    businessStudies.setStudents(Collections.emptyList());
    designTech.setStudents(Arrays.asList(shivani));
    // Save msThomson
    // we expect a new relationship to be created, and an old one deleted
    Compiler compiler = mapAndCompile(msThompson, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).isEmpty();
    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 MERGE (startNode)-[rel:`STUDENTS`]->(endNode) 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);
    }
    List<Statement> deleteRelsStatements = compiler.deleteRelationshipStatements();
    assertThat(deleteRelsStatements).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:`STUDENTS`]->(endNode) DELETE rel");
    assertThat(((List) deleteRelsStatements.get(0).getParameters().get("rows"))).hasSize(1);
// fixme: these other tests now need to be in their own test method, because
// a bug fix to the deletion code means that a second deletion won't (correctly) fire again
// expect a delete, but don't expect the new relationship to be created, because the fact of it
// is inaccessible from the businessStudies object
// expectOnSave(businessStudies,
// "MATCH ($1)-[_0:STUDENTS]->($3) WHERE id($1)=1 AND id($3)=3 DELETE _0");
// 
// // expect the new relationship, but don't expect the old one to be deleted, because the fact
// // of it is inaccessible from the designTech object
// expectOnSave(designTech,
// "MATCH ($2) WHERE id($2)=2 MATCH ($3) WHERE id($3)=3 MERGE ($2)-[_0:`STUDENTS`]->($3) RETURN id(_0) AS _0");
// 
// // we can't explore the object model from shivani at all, so no changes.
// expectOnSave(shivani, "");
}
Also used : Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Teacher(org.neo4j.ogm.domain.education.Teacher) ArrayList(java.util.ArrayList) List(java.util.List) Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Example 13 with Statement

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

the class CompilerTest method shouldDeleteExistingRelationshipEntity.

@Test
public void shouldDeleteExistingRelationshipEntity() {
    Long forumId = 0L;
    Long topicId = 1L;
    Long linkId = 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(linkId);
    link.setForum(forum);
    link.setTopic(topic);
    forum.setTopicsInForum(Arrays.asList(link));
    mappingContext.addNodeEntity(forum);
    mappingContext.addNodeEntity(topic);
    mappingContext.addRelationshipEntity(link, linkId);
    // the mapping context remembers the relationship between the forum and the topic in the graph
    mappingContext.addRelationship(new MappedRelationship(forumId, "HAS_TOPIC", topicId, null, Forum.class, ForumTopicLink.class));
    // unlink the objects manually
    forum.setTopicsInForum(null);
    link.setTopic(null);
    // expect the delete to be recognised when the forum is saved
    Compiler compiler = mapAndCompile(forum, -1);
    List<Statement> 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:`HAS_TOPIC`]->(endNode) DELETE rel");
    assertThat(((List) statements.get(0).getParameters().get("rows"))).hasSize(1);
// expect the delete to be recognised if the RE is saved
// expectOnSave(link, "MATCH ($0)-[_0:HAS_TOPIC]->($1) WHERE id($0)=0 AND id($1)=1 DELETE _0");
// 
// // expect nothing to happen if the topic is saved, because the domain model does not
// // permit navigation from the topic to the RE (topic has no reference to it)
// expectOnSave(topic, "");
// todo: more tests re saving deletes from REs marked as incoming relationships
}
Also used : Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) 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 14 with Statement

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

the class CompilerTest method createOutgoingRelationWhenUnmarkedRelationIsSpecified.

// DATAGRAPH-594
@Test
public void createOutgoingRelationWhenUnmarkedRelationIsSpecified() {
    Individual adam = new Individual();
    adam.setName("Adam");
    Individual vince = new Individual();
    vince.setName("Vince");
    adam.setFriends(Collections.singletonList(vince));
    Compiler compiler = mapAndCompile(adam, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Individual`) 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(2);
    }
    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 MERGE (startNode)-[rel:`FRIENDS`]->(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);
    Map row = (Map) rows.get(0);
    assertThat(row.get("startNodeId")).isEqualTo(mappingContext.nativeId(adam));
    assertThat(row.get("endNodeId")).isEqualTo(mappingContext.nativeId(vince));
}
Also used : Individual(org.neo4j.ogm.domain.social.Individual) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 15 with Statement

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

the class CompilerTest method shouldMergeNewRelationshipEntity.

@Test
public void shouldMergeNewRelationshipEntity() throws Exception {
    Person frantisek = new Person("Frantisek");
    Place scotland = new Place("Scotland");
    Visit visit = frantisek.addVisit(scotland, "Holiday");
    Compiler compiler = mapAndCompile(frantisek, -1);
    List<Statement> 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 " + "MERGE (startNode)-[rel:`VISITED` {`identifier`: row.props.`identifier`}]->(endNode) " + "SET rel += row.props " + "RETURN row.relRef as ref, ID(rel) as id, $type as type");
}
Also used : Visit(org.neo4j.ogm.domain.travel.Visit) Statement(org.neo4j.ogm.request.Statement) Person(org.neo4j.ogm.domain.travel.Person) Place(org.neo4j.ogm.domain.travel.Place) 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