Search in sources :

Example 11 with Person

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);
    }
}
Also used : Arrays(java.util.Arrays) WriteProtectionStrategy(org.neo4j.ogm.session.WriteProtectionStrategy) BeforeClass(org.junit.BeforeClass) User(org.neo4j.ogm.domain.social.User) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Utils(org.neo4j.ogm.session.Utils) ArrayList(java.util.ArrayList) Assertions(org.assertj.core.api.Assertions) Transaction(org.neo4j.ogm.transaction.Transaction) Result(org.neo4j.ogm.model.Result) Before(org.junit.Before) Person(org.neo4j.ogm.domain.social.Person) CypherException(org.neo4j.ogm.exception.CypherException) Session(org.neo4j.ogm.session.Session) WriteProtectionTarget(org.neo4j.ogm.context.WriteProtectionTarget) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ComparisonOperator(org.neo4j.ogm.cypher.ComparisonOperator) TransactionException(org.neo4j.ogm.exception.TransactionException) Test(org.junit.Test) Filter(org.neo4j.ogm.cypher.Filter) List(java.util.List) Stream(java.util.stream.Stream) Ignore(org.junit.Ignore) SessionFactory(org.neo4j.ogm.session.SessionFactory) TestContainersTestBase(org.neo4j.ogm.testutil.TestContainersTestBase) Collections(java.util.Collections) Immortal(org.neo4j.ogm.domain.social.Immortal) WriteProtectionStrategy(org.neo4j.ogm.session.WriteProtectionStrategy) User(org.neo4j.ogm.domain.social.User) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Immortal(org.neo4j.ogm.domain.social.Immortal) Person(org.neo4j.ogm.domain.social.Person) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 Person (org.neo4j.ogm.domain.social.Person)11 Filter (org.neo4j.ogm.cypher.Filter)3 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 Assertions (org.assertj.core.api.Assertions)1 Before (org.junit.Before)1 BeforeClass (org.junit.BeforeClass)1 Ignore (org.junit.Ignore)1 WriteProtectionTarget (org.neo4j.ogm.context.WriteProtectionTarget)1 ComparisonOperator (org.neo4j.ogm.cypher.ComparisonOperator)1 Immortal (org.neo4j.ogm.domain.social.Immortal)1 User (org.neo4j.ogm.domain.social.User)1 CypherException (org.neo4j.ogm.exception.CypherException)1 TransactionException (org.neo4j.ogm.exception.TransactionException)1