Search in sources :

Example 1 with School

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

the class EntityGraphMapperTest method testVariablePersistenceToDepthTwo.

@Test
public void testVariablePersistenceToDepthTwo() {
    School coalHillSchool = new School("Coal Hill");
    Teacher claraOswald = new Teacher("Clara Oswald");
    Teacher dannyPink = new Teacher("Danny Pink");
    Course english = new Course("English");
    Course maths = new Course("Maths");
    // do we need to set both sides?
    coalHillSchool.setTeachers(Arrays.asList(claraOswald, dannyPink));
    // do we need to set both sides?
    claraOswald.setCourses(Arrays.asList(english));
    dannyPink.setCourses(Arrays.asList(maths));
    session.save(coalHillSchool, 2);
    // we expect the school its teachers and the teachers courses to be persisted when persisting the school to depth 2
    session.clear();
    assertThat(session.query("MATCH" + "(school:School:DomainObject {name:'Coal Hill'}), " + "(clara:Teacher {name:'Clara Oswald'}), " + "(danny:Teacher {name:'Danny Pink'}), " + "(english:Course {name:'English'}), " + "(maths:Course {name:'Maths'}) " + "WHERE (school)-[:TEACHERS]->(clara)-[:SCHOOL]->(school) and " + "(school)-[:TEACHERS]->(danny)-[:SCHOOL]->(school) and " + "(danny)-[:COURSES]->(maths) and " + "(clara)-[:COURSES]->(english) return school, clara, danny, english, maths", emptyMap()).queryResults()).hasSize(1);
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) Course(org.neo4j.ogm.domain.education.Course) Test(org.junit.Test)

Example 2 with School

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

the class EntityGraphMapperTest method collectedListOfNodesFromPathsShouldNotCollapse.

// GH-902
@Test
@SuppressWarnings("unchecked")
public void collectedListOfNodesFromPathsShouldNotCollapse() {
    Teacher t = new Teacher("T0");
    School s = new School("Sß");
    Course c = new Course("C0");
    t.setSchool(s);
    t.setCourses(singletonList(c));
    Session writingSession = sessionFactory.openSession();
    writingSession.save(t);
    Result result = session.query("MATCH p=(:School) -[*]->(:Course) RETURN COLLECT (DISTINCT nodes(p)) AS paths", Collections.emptyMap());
    assertThat(result).hasSize(1).first().satisfies(row -> {
        Object nestedLists = row.get("paths");
        assertThat(nestedLists).isInstanceOf(List.class);
        assertThat((List<?>) nestedLists).hasSize(1).first().satisfies(v -> {
            assertThat(v).isInstanceOf(List.class);
            assertThat((List<?>) v).extracting(Object::getClass).extracting(Class::getSimpleName).containsExactly("School", "Teacher", "Course");
        });
    });
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) List(java.util.List) Course(org.neo4j.ogm.domain.education.Course) Session(org.neo4j.ogm.session.Session) Result(org.neo4j.ogm.model.Result) Test(org.junit.Test)

Example 3 with School

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

the class EntityGraphMapperTest method testVariablePersistenceToDepthOne.

@Test
public void testVariablePersistenceToDepthOne() {
    School coalHillSchool = new School("Coal Hill");
    Teacher claraOswald = new Teacher("Clara Oswald");
    Teacher dannyPink = new Teacher("Danny Pink");
    Course english = new Course("English");
    Course maths = new Course("Maths");
    // do we need to set both sides?
    coalHillSchool.setTeachers(Arrays.asList(claraOswald, dannyPink));
    // do we need to set both sides?
    claraOswald.setCourses(Arrays.asList(english));
    dannyPink.setCourses(Arrays.asList(maths));
    session.save(coalHillSchool, 1);
    // we ONLY expect the school and its teachers to be persisted when persisting the school to depth 1
    session.clear();
    assertThat(session.query("MATCH " + "(s:School:DomainObject {name:'Coal Hill'}), " + "(c:Teacher {name:'Clara Oswald'}), " + "(d:Teacher {name:'Danny Pink'}) " + "WHERE (s)-[:TEACHERS]->(c) and " + "(s)-[:TEACHERS]->(d) return s,c,d", emptyMap())).hasSize(1);
    assertThat(session.query("MATCH (c:Course) return c", emptyMap())).hasSize(0);
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) Course(org.neo4j.ogm.domain.education.Course) Test(org.junit.Test)

Example 4 with School

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

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

the class EntityGraphMapperTest method persistManyToOneObjectFromSingletonSide.

@Test
public void persistManyToOneObjectFromSingletonSide() {
    Iterable<Map<String, Object>> executionResult = session.query("CREATE (s:School:DomainObject {name:'Waller'})-[:TEACHERS]->(t:Teacher {name:'Mary'})-[:SCHOOL]->(s) " + "RETURN id(s) AS school_id, id(t) AS teacher_id", emptyMap()).queryResults();
    Map<String, Object> resultSetRow = executionResult.iterator().next();
    Long wallerId = Long.valueOf(resultSetRow.get("school_id").toString());
    Long maryId = Long.valueOf(resultSetRow.get("teacher_id").toString());
    session.clear();
    School waller = session.load(School.class, wallerId);
    Teacher mary = session.load(Teacher.class, maryId);
    mary.setId(maryId);
    // create a new teacher and add him to the school
    Teacher jim = new Teacher("Jim");
    jim.setSchool(waller);
    // ensure that the domain objects are mutually established by the code
    assertThat(waller.getTeachers().contains(jim)).isTrue();
    session.save(jim);
    session.clear();
    assertThat(session.query("MATCH " + "(s:School:DomainObject {name:'Waller'}), " + "(m:Teacher {name:'Mary'}), " + "(j:Teacher {name:'Jim'}) " + "WHERE (j)-[:SCHOOL]->(s) and " + "(m)-[:SCHOOL]->(s) and " + "(s)-[:TEACHERS]->(j) and " + "(s)-[:TEACHERS]->(m) return s, m, j", emptyMap()).queryResults()).hasSize(1);
}
Also used : School(org.neo4j.ogm.domain.education.School) Teacher(org.neo4j.ogm.domain.education.Teacher) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)13 School (org.neo4j.ogm.domain.education.School)13 Teacher (org.neo4j.ogm.domain.education.Teacher)12 Course (org.neo4j.ogm.domain.education.Course)4 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)3 List (java.util.List)2 Student (org.neo4j.ogm.domain.education.Student)2 Statement (org.neo4j.ogm.request.Statement)2 Session (org.neo4j.ogm.session.Session)2 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DomainObject (org.neo4j.ogm.domain.education.DomainObject)1 Result (org.neo4j.ogm.model.Result)1 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)1 SessionFactory (org.neo4j.ogm.session.SessionFactory)1