Search in sources :

Example 1 with Course

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

the class EntityGraphMapperTest method shouldCorrectlyRemoveRelationshipWhenItemIsRemovedFromCollection.

@Test
public void shouldCorrectlyRemoveRelationshipWhenItemIsRemovedFromCollection() {
    // simple music course with three students
    Iterable<Map<String, Object>> executionResult = session.query("CREATE (c:Course {name:'GCSE Music'}), " + "(c)-[:STUDENTS]->(x:Student:DomainObject {name:'Xavier'}), " + "(c)-[:STUDENTS]->(y:Student:DomainObject {name:'Yvonne'}), " + "(c)-[:STUDENTS]->(z:Student:DomainObject {name:'Zack'}) " + "RETURN id(c) AS course_id, id(x) AS xid, id(y) AS yid, id(z) AS zid", emptyMap()).queryResults();
    Map<String, ?> results = executionResult.iterator().next();
    session.clear();
    Long mid = (Long) results.get("course_id");
    Long xid = (Long) results.get("xid");
    Long yid = (Long) results.get("yid");
    Long zid = (Long) results.get("zid");
    Course music = session.load(Course.class, mid);
    Student xavier = session.load(Student.class, xid);
    Student yvonne = session.load(Student.class, yid);
    Student zack = session.load(Student.class, zid);
    music.setStudents(Arrays.asList(yvonne, xavier, zack));
    // object is now "loaded"
    // now, update the domain model, setting yvonne as the only music student (i.e remove zack and xavier)
    music.setStudents(Arrays.asList(yvonne));
    session.save(music);
    session.clear();
    assertThat(session.query("MATCH (a:Student:DomainObject {name:'Xavier'}), " + "(b:Student:DomainObject {name:'Zack'}), " + "(c:Course {name:'GCSE Music'})-[:STUDENTS]->(:Student:DomainObject {name:'Yvonne'}) return a,b,c", emptyMap()).queryResults()).hasSize(1);
}
Also used : Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Course

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

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

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

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

the class EntityGraphMapperTest method shouldThrowInvalidRelationshipTargetExceptionOnNullElements.

// GH-781
@Test
public void shouldThrowInvalidRelationshipTargetExceptionOnNullElements() {
    Course course = new Course("Some course");
    Student student1 = new Student("A student");
    Student student2 = new Student("Another student");
    course.setStudents(Arrays.asList(student1, null, student2));
    assertThatExceptionOfType(InvalidRelationshipTargetException.class).isThrownBy(() -> session.save(course)).withMessage("The relationship 'STUDENTS' from 'org.neo4j.ogm.domain.education.Course' to 'org.neo4j.ogm.domain.education.Student' stored on '#students' contains 'null', which is an invalid target for this relationship.'");
}
Also used : Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) 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