Search in sources :

Example 11 with Neo4jSession

use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.

the class SaveCapabilityTest method shouldSaveOnlyModifiedNodes.

@Test
public void shouldSaveOnlyModifiedNodes() {
    int depth = 1;
    Neo4jSession neo4jSession = (Neo4jSession) session;
    CompileContext context = null;
    Artist leann = new Artist("Leann Rimes");
    Album lost = new Album("Lost Highway");
    lost.setArtist(bonJovi);
    lost.setGuestArtist(leann);
    context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(lost, depth);
    assertThat(context.registry()).as("Should save 3 nodes and 2 relations (5 items)").hasSize(5);
    session.save(lost);
    context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(lost, depth);
    assertThat(context.registry()).as("Should have nothing to save").isEmpty();
    session.clear();
    Artist loadedLeann = session.load(Artist.class, leann.getId(), depth);
    context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(loadedLeann, depth);
    assertThat(context.registry()).as("Should have nothing to save").isEmpty();
    loadedLeann.setName("New name");
    context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(loadedLeann, depth);
    assertThat(context.registry()).as("Should have one node to save").hasSize(1);
    loadedLeann.getGuestAlbums().iterator().next().setName("New Album Name");
    context = new EntityGraphMapper(neo4jSession.metaData(), neo4jSession.context()).map(loadedLeann, depth);
    assertThat(context.registry()).as("Should have two node to save").hasSize(2);
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) EntityGraphMapper(org.neo4j.ogm.context.EntityGraphMapper) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Album(org.neo4j.ogm.domain.music.Album) CompileContext(org.neo4j.ogm.cypher.compiler.CompileContext) Test(org.junit.Test)

Example 12 with Neo4jSession

use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.

the class LoadCapabilityTest method shouldNotBeDirtyOnLoadEntityThenSaveThenReload.

// GH-177
@Test
public void shouldNotBeDirtyOnLoadEntityThenSaveThenReload() {
    MappingContext context = ((Neo4jSession) session).context();
    Artist pinkFloyd = new Artist("Pink Floyd");
    // new object not saved is always dirty
    assertThat(context.isDirty(pinkFloyd)).isTrue();
    session.save(pinkFloyd);
    // object hash updated by save.
    assertThat(context.isDirty(pinkFloyd)).isFalse();
    // forget everything we've done
    session.clear();
    pinkFloyd = session.load(Artist.class, pinkFloyd.getId());
    // a freshly loaded object is never dirty
    assertThat(context.isDirty(pinkFloyd)).isFalse();
    pinkFloyd.setName("Purple Floyd");
    // we changed the name so it is now dirty
    assertThat(context.isDirty(pinkFloyd)).isTrue();
    session.save(pinkFloyd);
    // object saved, no longer dirty
    assertThat(context.isDirty(pinkFloyd)).isFalse();
    // load the same identity, but to a copy ref
    Artist purpleFloyd = session.load(Artist.class, pinkFloyd.getId());
    // nothing has changed, so it should not be dirty
    assertThat(context.isDirty(purpleFloyd)).isFalse();
    // two refs pointing to the same object
    assertThat(pinkFloyd == purpleFloyd).isTrue();
}
Also used : MappingContext(org.neo4j.ogm.context.MappingContext) Artist(org.neo4j.ogm.domain.music.Artist) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Test(org.junit.Test)

Example 13 with Neo4jSession

use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.

the class LoadCapabilityTest method shouldNotBeDirtyOnLoadRelationshipEntityThenSaveThenReload.

// GH-177
@Test
public void shouldNotBeDirtyOnLoadRelationshipEntityThenSaveThenReload() {
    MappingContext context = ((Neo4jSession) session).context();
    Artist pinkFloyd = new Artist("Pink Floyd");
    Album divisionBell = new Album("The Division Bell");
    divisionBell.setArtist(pinkFloyd);
    Studio studio = new Studio("Britannia Row Studios");
    Recording recording = new Recording(divisionBell, studio, 1994);
    divisionBell.setRecording(recording);
    pinkFloyd.addAlbum(divisionBell);
    // new object not saved is always dirty
    assertThat(context.isDirty(recording)).isTrue();
    session.save(recording);
    // object hash updated by save.
    assertThat(context.isDirty(recording)).isFalse();
    // forget everything we've done
    session.clear();
    recording = session.load(Recording.class, recording.getId(), 2);
    // a freshly loaded object is never dirty
    assertThat(context.isDirty(recording)).isFalse();
    recording.setYear(1995);
    // we changed the year so it is now dirty
    assertThat(context.isDirty(recording)).isTrue();
    session.save(recording);
    // object saved, no longer dirty
    assertThat(context.isDirty(recording)).isFalse();
    Recording recording1995 = session.load(Recording.class, recording.getId(), // load the same identity, but to a copy ref
    2);
    // nothing has changed, so it should not be dirty
    assertThat(context.isDirty(recording1995)).isFalse();
    // two refs pointing to the same object
    assertThat(recording == recording1995).isTrue();
}
Also used : MappingContext(org.neo4j.ogm.context.MappingContext) Artist(org.neo4j.ogm.domain.music.Artist) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) Album(org.neo4j.ogm.domain.music.Album) Recording(org.neo4j.ogm.domain.music.Recording) Studio(org.neo4j.ogm.domain.music.Studio) Test(org.junit.Test)

Example 14 with Neo4jSession

use of org.neo4j.ogm.session.Neo4jSession 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

Neo4jSession (org.neo4j.ogm.session.Neo4jSession)14 Test (org.junit.Test)11 Neo4JSessionExt (com.pamirs.attach.plugin.neo4j.config.Neo4JSessionExt)3 ArrayList (java.util.ArrayList)3 DriverConfiguration (org.neo4j.ogm.config.DriverConfiguration)3 MappingContext (org.neo4j.ogm.context.MappingContext)3 Artist (org.neo4j.ogm.domain.music.Artist)3 User (org.neo4j.ogm.domain.social.User)3 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)2 DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)2 AuthTokenCredentials (org.neo4j.ogm.authentication.AuthTokenCredentials)2 Credentials (org.neo4j.ogm.authentication.Credentials)2 UsernamePasswordCredentials (org.neo4j.ogm.authentication.UsernamePasswordCredentials)2 Album (org.neo4j.ogm.domain.music.Album)2 Driver (org.neo4j.ogm.driver.Driver)2 MetaData (org.neo4j.ogm.metadata.MetaData)2 Result (org.neo4j.ogm.model.Result)2 Session (org.neo4j.ogm.session.Session)2 DriverConfig (com.pamirs.attach.plugin.neo4j.config.DriverConfig)1 Neo4JSessionOperation (com.pamirs.attach.plugin.neo4j.config.Neo4JSessionOperation)1