use of org.neo4j.ogm.domain.social.Person in project neo4j-ogm by neo4j.
the class BasicDriverTest method customWriteProtectionStrategyShouldBeApplied.
@Test
public void customWriteProtectionStrategyShouldBeApplied() {
Predicate<Object> alwaysWriteProtect = o -> true;
Predicate<Object> protectAvon = o -> ((User) o).getName().startsWith("Avon");
WriteProtectionStrategy customStrategy = () -> (target, clazz) -> {
// You than can omit the class check in the predicate
if (clazz == Immortal.class) {
return alwaysWriteProtect;
} else if (clazz == User.class) {
return protectAvon;
} else {
// Person.class in the example
return null;
}
};
User avon = new User("Avon Barksdale");
session.save(avon);
User stringer = new User("Stringer Bell");
session.save(stringer);
Immortal connor = new Immortal("Connor", "MacLeod");
session.save(connor);
Person person = new Person("A person");
session.save(person);
session.clear();
try {
((Neo4jSession) session).setWriteProtectionStrategy(customStrategy);
stringer.setName("Avon Something");
avon.befriend(stringer);
session.save(avon);
connor.setFirstName("Duncan");
session.save(connor);
person.setName("John Reese");
session.save(person);
session.clear();
Collection<User> users = session.loadAll(User.class);
assertThat(users).hasSize(2).extracting(User::getName).containsExactlyInAnyOrder("Avon Barksdale", "Stringer Bell");
Collection<Immortal> immortals = session.loadAll(Immortal.class);
assertThat(immortals).hasSize(1).extracting(Immortal::getFirstName).containsExactlyInAnyOrder("Connor");
Collection<Person> personsOfInterest = session.loadAll(Person.class);
assertThat(personsOfInterest).hasSize(1).extracting(Person::getName).containsExactlyInAnyOrder("John Reese");
} finally {
((Neo4jSession) session).setWriteProtectionStrategy(null);
}
}
Aggregations