Search in sources :

Example 6 with Student

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

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

the class CompilerTest method doNothingIfNothingHasChanged.

@Test
public void doNothingIfNothingHasChanged() {
    Long existingNodeId = 0L;
    Student sheila = new Student();
    sheila.setId(existingNodeId);
    sheila.setName("Sheila Smythe");
    mappingContext.addNodeEntity(sheila);
    Compiler compiler = mapAndCompile(sheila, -1);
    compiler.useStatementFactory(new RowStatementFactory());
    assertThat(compiler.createNodesStatements()).isEmpty();
    assertThat(compiler.updateNodesStatements()).isEmpty();
}
Also used : RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Example 8 with Student

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

the class CompilerTest method shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep.

@Test
public void shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep() {
    Student sheila = new Student();
    sheila.setName("Sheila Smythe");
    Student gary = new Student();
    gary.setName("Gary Jones");
    Student winston = new Student();
    winston.setName("Winston Charles");
    Course physics = new Course();
    physics.setName("GCSE Physics");
    physics.setStudents(Arrays.asList(gary, sheila));
    Course maths = new Course();
    maths.setName("A-Level Mathematics");
    maths.setStudents(Arrays.asList(sheila, winston));
    Teacher teacher = new Teacher();
    teacher.setName("Mrs Kapoor");
    teacher.setCourses(Arrays.asList(physics, maths));
    // Save teacher
    Compiler compiler = mapAndCompile(teacher, -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", "UNWIND $rows as row CREATE (n:`Course`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`DomainObject`:`Student`) 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");
        if (statement.getStatement().contains("Teacher")) {
            assertThat(rows).hasSize(1);
        }
        if (statement.getStatement().contains("Student")) {
            assertThat(rows).hasSize(3);
        }
        if (statement.getStatement().contains("Course")) {
            assertThat(rows).hasSize(2);
        }
    }
    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:`COURSES`]->(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:`STUDENTS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    for (Statement statement : createRelsStatements) {
        List rows = (List) statement.getParameters().get("rows");
        if (statement.getStatement().contains("STUDENTS")) {
            assertThat(rows).hasSize(4);
        }
        if (statement.getStatement().contains("COURSES")) {
            assertThat(rows).hasSize(2);
        }
    }
}
Also used : Statement(org.neo4j.ogm.request.Statement) Teacher(org.neo4j.ogm.domain.education.Teacher) ArrayList(java.util.ArrayList) List(java.util.List) Student(org.neo4j.ogm.domain.education.Student) Course(org.neo4j.ogm.domain.education.Course) Test(org.junit.Test)

Example 9 with Student

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

the class EducationIntegrationTest method loadingASchoolWithNegativeDepthShouldLoadAllConnectedEntities.

/**
 * @see DATAGRAPH-595
 */
@Test
public void loadingASchoolWithNegativeDepthShouldLoadAllConnectedEntities() {
    // Create students, teachers, courses and a school
    School hogwarts = new School("Hogwarts");
    Student harry = new Student("Harry Potter");
    Student ron = new Student("Ron Weasley");
    Student hermione = new Student("Hermione Granger");
    Course transfiguration = new Course("Transfiguration");
    transfiguration.setStudents(Arrays.asList(harry, hermione, ron));
    Course potions = new Course("Potions");
    potions.setStudents(Arrays.asList(ron, hermione));
    Course dark = new Course("Defence Against The Dark Arts");
    dark.setStudents(Collections.singletonList(harry));
    Teacher minerva = new Teacher("Minerva McGonagall");
    minerva.setCourses(Collections.singletonList(transfiguration));
    minerva.setSchool(hogwarts);
    Teacher severus = new Teacher("Severus Snape");
    severus.setCourses(Arrays.asList(potions, dark));
    severus.setSchool(hogwarts);
    hogwarts.setTeachers(Arrays.asList(minerva, severus));
    session.save(hogwarts);
    session.clear();
    // Load the school with depth -1
    hogwarts = session.load(School.class, hogwarts.getId(), -1);
    assertThat(hogwarts.getTeachers()).hasSize(2);
    for (Teacher teacher : hogwarts.getTeachers()) {
        if (teacher.getName().equals("Severus Snape")) {
            assertThat(teacher.getCourses()).hasSize(2);
            for (Course course : teacher.getCourses()) {
                if (course.getName().equals("Potions")) {
                    assertThat(course.getStudents()).hasSize(2);
                } else {
                    assertThat(course.getStudents()).hasSize(1);
                }
            }
        } else {
            assertThat(teacher.getCourses()).hasSize(1);
            assertThat(teacher.getCourses().get(0).getStudents()).hasSize(3);
        }
    }
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) Student(org.neo4j.ogm.domain.education.Student) Course(org.neo4j.ogm.domain.education.Course) Test(org.junit.Test)

Example 10 with Student

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

the class EducationTest method testFetchCoursesTaughtByAllTeachers.

@Test
public void testFetchCoursesTaughtByAllTeachers() throws Exception {
    // note: idempotent!
    Map<String, Teacher> teachers = loadTeachers();
    // this response is for an imagined request: "match p = (c:COURSE)--(o) where id(c) in [....] RETURN p"
    // i.e. we have a set of partially loaded courses attached to our teachers which we now want to
    // hydrate by getting all their relationships
    hydrateCourses(teachers.values());
    Set<Course> courses = new HashSet<>();
    for (Teacher teacher : teachers.values()) {
        for (Course course : teacher.getCourses()) {
            if (!courses.contains(course)) {
                List<Student> students = course.getStudents();
                switch(course.getName()) {
                    case "Maths":
                        checkMaths(students);
                        break;
                    case "Physics":
                        checkPhysics(students);
                        break;
                    case "Philosophy and Ethics":
                        checkPhilosophyAndEthics(students);
                        break;
                    case "PE":
                        checkPE(students);
                        break;
                    case "History":
                        checkHistory(students);
                        break;
                    case "Geography":
                        checkGeography(students);
                        break;
                    default:
                        checkEnglish(students);
                        break;
                }
                courses.add(course);
            }
        }
    }
    assertThat(courses).hasSize(7);
}
Also used : Teacher(org.neo4j.ogm.domain.education.Teacher) Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 Student (org.neo4j.ogm.domain.education.Student)18 Course (org.neo4j.ogm.domain.education.Course)11 Teacher (org.neo4j.ogm.domain.education.Teacher)6 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Statement (org.neo4j.ogm.request.Statement)3 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)2 School (org.neo4j.ogm.domain.education.School)2 Session (org.neo4j.ogm.session.Session)2 SessionFactory (org.neo4j.ogm.session.SessionFactory)2 HashSet (java.util.HashSet)1 Filter (org.neo4j.ogm.cypher.Filter)1 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)1 DomainObject (org.neo4j.ogm.domain.education.DomainObject)1 Statements (org.neo4j.ogm.request.Statements)1 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)1