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);
}
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);
}
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");
});
});
}
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);
}
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.'");
}
Aggregations