Search in sources :

Example 11 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class ElectionTest method shouldAllowVoterToChangeHerMind.

@Test
public void shouldAllowVoterToChangeHerMind() {
    Candidate a = new Candidate("A");
    Candidate b = new Candidate("B");
    Voter v = new Voter("V");
    v.candidateVotedFor = b;
    session.save(a);
    session.save(v);
    MappingContext context = ((Neo4jSession) session).context();
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
    session.clear();
    a = session.load(Candidate.class, a.getId());
    v = session.load(Voter.class, v.getId());
    assertThat(v.candidateVotedFor.getId()).isEqualTo(b.getId());
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
    v.candidateVotedFor = a;
    session.save(v);
    session.clear();
    session.load(Candidate.class, b.getId());
    session.load(Voter.class, v.getId());
    assertThat(v.candidateVotedFor.getId()).isEqualTo(a.getId());
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", a.getId(), null, Voter.class, Candidate.class))).isTrue();
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isFalse();
    session.clear();
}
Also used : Candidate(org.neo4j.ogm.domain.election.Candidate) MappingContext(org.neo4j.ogm.context.MappingContext) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Voter(org.neo4j.ogm.domain.election.Voter) Test(org.junit.Test)

Example 12 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldBeAbleToRemoveTheOnlyRegisteredRelationship.

@Test
public void shouldBeAbleToRemoveTheOnlyRegisteredRelationship() {
    Folder folder = new Folder();
    Document document = new Document();
    folder.getDocuments().add(document);
    document.setFolder(folder);
    folder.setId(0L);
    document.setId(1L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(document);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", document.getId(), null, Folder.class, Document.class));
    document.setFolder(null);
    folder.getDocuments().clear();
    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", document.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(document).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 13 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship 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 14 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship 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 15 with MappedRelationship

use of org.neo4j.ogm.context.MappedRelationship in project neo4j-ogm by neo4j.

the class CompilerTest method expectNoChangesWhenDomainUnchanged.

@Test
public void expectNoChangesWhenDomainUnchanged() {
    // create
    Long wallerId = 0L;
    Long maryId = 1L;
    School waller = new School("Waller");
    waller.setId(wallerId);
    Teacher mary = new Teacher("Mary");
    mary.setId(maryId);
    // relate
    mary.setSchool(waller);
    // validate the domain model
    assertThat(mary.getSchool().equals(waller)).isTrue();
    assertThat(waller.getTeachers().contains(mary)).isTrue();
    assertThat(waller.getTeachers().size() == 1).isTrue();
    // set the mapping context accordingly
    mappingContext.addNodeEntity(mary);
    mappingContext.addNodeEntity(waller);
    mappingContext.addRelationship(new MappedRelationship(maryId, "SCHOOL", wallerId, null, Teacher.class, School.class));
    mappingContext.addRelationship(new MappedRelationship(wallerId, "TEACHERS", maryId, null, School.class, Teacher.class));
    Compiler compiler = mapAndCompile(waller, -1);
    compiler.useStatementFactory(new RowStatementFactory());
    assertThat(compiler.createNodesStatements()).isEmpty();
    assertThat(compiler.updateNodesStatements()).isEmpty();
    assertThat(compiler.createRelationshipsStatements()).isEmpty();
    assertThat(compiler.updateRelationshipStatements()).isEmpty();
    compiler = mapAndCompile(mary, -1);
    assertThat(compiler.createNodesStatements()).isEmpty();
    assertThat(compiler.updateNodesStatements()).isEmpty();
    assertThat(compiler.createRelationshipsStatements()).isEmpty();
    assertThat(compiler.updateRelationshipStatements()).isEmpty();
}
Also used : School(org.neo4j.ogm.domain.education.School) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Teacher(org.neo4j.ogm.domain.education.Teacher) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Test(org.junit.Test)

Aggregations

MappedRelationship (org.neo4j.ogm.context.MappedRelationship)18 Test (org.junit.Test)11 Statement (org.neo4j.ogm.request.Statement)9 Teacher (org.neo4j.ogm.domain.education.Teacher)4 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)3 School (org.neo4j.ogm.domain.education.School)3 Document (org.neo4j.ogm.domain.filesystem.Document)3 Folder (org.neo4j.ogm.domain.filesystem.Folder)3 HashSet (java.util.HashSet)2 Course (org.neo4j.ogm.domain.education.Course)2 Student (org.neo4j.ogm.domain.education.Student)2 Forum (org.neo4j.ogm.domain.forum.Forum)2 ForumTopicLink (org.neo4j.ogm.domain.forum.ForumTopicLink)2 Topic (org.neo4j.ogm.domain.forum.Topic)2 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)2 Direction (org.neo4j.ogm.annotation.Relationship.Direction)1 EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)1