Search in sources :

Example 6 with Course

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

the class EntityGraphMapperTest 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));
    session.save(teacher);
    session.clear();
    assertThat(session.query("MATCH (t:Teacher {name:'Mrs Kapoor'}), " + "(p:Course {name:'GCSE Physics'}), (m:Course {name:'A-Level Mathematics'}), " + "(s:Student:DomainObject {name:'Sheila Smythe'}), " + "(g:Student:DomainObject {name:'Gary Jones'}), " + "(w:Student:DomainObject {name:'Winston Charles'}) " + "WHERE (t)-[:COURSES]->(p)-[:STUDENTS]->(s) and (t)-[:COURSES]->(m)-[:STUDENTS]->(s) and " + "(p)-[:STUDENTS]->(g) and (m)-[:STUDENTS]->(w) return t, p, m, s, g, w", emptyMap()).queryResults()).hasSize(1);
}
Also used : 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 7 with Course

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

the class EducationIntegrationTest method loadingCourseByPropertyShouldNotLoadOtherEntitiesWithSamePropertyValue.

@Test
public void loadingCourseByPropertyShouldNotLoadOtherEntitiesWithSamePropertyValue() {
    // create a course
    Course course = new Course("CompSci");
    // create a student with the same name as the course
    Student student = new Student("CompSci");
    // relate them so they're both in the mappingContext
    course.setStudents(Collections.singletonList(student));
    session.save(course);
    // fetch Courses by name
    Collection<Course> courses = session.loadAll(Course.class, new Filter("name", ComparisonOperator.EQUALS, "CompSci"));
    assertThat(courses).hasSize(1);
    assertThat(courses.iterator().next()).isEqualTo(course);
    assertThat(courses.iterator().next().getStudents()).hasSize(1);
}
Also used : Filter(org.neo4j.ogm.cypher.Filter) Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Example 8 with Course

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

the class EducationTest method checkCourseNames.

private void checkCourseNames(Teacher teacher, String... courseNames) {
    int n = courseNames.length;
    List<String> test = Arrays.asList(courseNames);
    for (Course course : teacher.getCourses()) {
        if (test.contains(course.getName())) {
            n--;
        }
    }
    assertThat(n).isEqualTo(0);
}
Also used : Course(org.neo4j.ogm.domain.education.Course)

Example 9 with Course

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

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

Aggregations

Course (org.neo4j.ogm.domain.education.Course)15 Test (org.junit.Test)14 Student (org.neo4j.ogm.domain.education.Student)11 Teacher (org.neo4j.ogm.domain.education.Teacher)9 School (org.neo4j.ogm.domain.education.School)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Statement (org.neo4j.ogm.request.Statement)3 ArrayList (java.util.ArrayList)2 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)2 HashSet (java.util.HashSet)1 Filter (org.neo4j.ogm.cypher.Filter)1 Result (org.neo4j.ogm.model.Result)1 Session (org.neo4j.ogm.session.Session)1