Search in sources :

Example 21 with Teacher

use of org.neo4j.ogm.domain.education.Teacher 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)

Example 22 with Teacher

use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.

the class CompilerTest method addObjectToExistingCollection.

@Test
public void addObjectToExistingCollection() {
    // 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()).isEqualTo(waller);
    assertThat(waller.getTeachers()).contains(mary);
    assertThat(waller.getTeachers()).hasSize(1);
    // 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));
    Teacher jim = new Teacher("Jim");
    jim.setSchool(waller);
    assertThat(waller.getTeachers()).contains(jim);
    assertThat(waller.getTeachers()).hasSize(2);
    assertThat(jim.getSchool()).isEqualTo(waller);
    // Save jim
    Compiler compiler = mapAndCompile(jim, -1);
    List<Statement> createNodesStatements = compiler.createNodesStatements();
    assertThat(createNodesStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    assertThat(createNodesStatements).extracting(Statement::getParameters);
    for (Statement statement : createNodesStatements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    List<Statement> createRelsStatements = compiler.createRelationshipsStatements();
    assertThat(createRelsStatements).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:`SCHOOL`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`TEACHERS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    // Save waller
    compiler = mapAndCompile(waller, -1);
    createNodesStatements = compiler.createNodesStatements();
    assertThat(createNodesStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : createNodesStatements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    createRelsStatements = compiler.createRelationshipsStatements();
    assertThat(createRelsStatements).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:`SCHOOL`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`TEACHERS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    // Save mary
    compiler = mapAndCompile(mary, -1);
    createNodesStatements = compiler.createNodesStatements();
    assertThat(createNodesStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : createNodesStatements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    createRelsStatements = compiler.createRelationshipsStatements();
    assertThat(createRelsStatements).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:`SCHOOL`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`TEACHERS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
}
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) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 23 with Teacher

use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.

the class IdentityMapTest method testUnchangedObjectDetected.

@Test
public void testUnchangedObjectDetected() {
    Teacher mrsJones = new Teacher();
    // the id field must not be part of the memoised property list
    mrsJones.setId(115L);
    mappingContext.addNodeEntity(mrsJones);
    assertThat(mappingContext.isDirty(mrsJones)).isFalse();
}
Also used : Teacher(org.neo4j.ogm.domain.education.Teacher) Test(org.junit.Test)

Example 24 with Teacher

use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.

the class IdentityMapTest method testRelatedObjectChangeDoesNotAffectNodeMemoisation.

@Test
public void testRelatedObjectChangeDoesNotAffectNodeMemoisation() {
    Teacher teacher = new Teacher("Miss White");
    // the id field must not be part of the memoised property list
    teacher.setId(115L);
    mappingContext.addNodeEntity(teacher);
    // a related object does not affect the property list.
    teacher.setSchool(new School("Roedean"));
    assertThat(mappingContext.isDirty(teacher)).isFalse();
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) Test(org.junit.Test)

Example 25 with Teacher

use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.

the class IdentityMapTest method testNodeAndRelationshipWithSameId.

// GH-684
@Test
public void testNodeAndRelationshipWithSameId() {
    // Create a Node and set the Id.
    Teacher mrsJones = new Teacher();
    mrsJones.setId(1L);
    // Remember the entity.
    mappingContext.addNodeEntity(mrsJones);
    // Create a Relationship with the same Id.
    TeachesAt teachesAtRelationship = new TeachesAt();
    teachesAtRelationship.setId(1L);
    IdentityMap identityMap = new IdentityMap(metaData);
    identityMap.remember(mrsJones, mrsJones.getId());
    assertThat(identityMap.remembered(mrsJones, mrsJones.getId())).isTrue();
    assertThat(identityMap.remembered(teachesAtRelationship, teachesAtRelationship.getId())).isFalse();
    identityMap.remember(teachesAtRelationship, teachesAtRelationship.getId());
    assertThat(identityMap.remembered(teachesAtRelationship, teachesAtRelationship.getId())).isTrue();
}
Also used : TeachesAt(org.neo4j.ogm.domain.education.TeachesAt) Teacher(org.neo4j.ogm.domain.education.Teacher) Test(org.junit.Test)

Aggregations

Teacher (org.neo4j.ogm.domain.education.Teacher)25 Test (org.junit.Test)24 School (org.neo4j.ogm.domain.education.School)12 Course (org.neo4j.ogm.domain.education.Course)9 List (java.util.List)6 Student (org.neo4j.ogm.domain.education.Student)6 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)4 Result (org.neo4j.ogm.model.Result)4 Statement (org.neo4j.ogm.request.Statement)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Session (org.neo4j.ogm.session.Session)3 Map (java.util.Map)2 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)2 HashSet (java.util.HashSet)1 TeachesAt (org.neo4j.ogm.domain.education.TeachesAt)1