Search in sources :

Example 11 with Document

use of org.neo4j.ogm.domain.filesystem.Document in project neo4j-ogm by neo4j.

the class DirtyObjectsTest method reloadingAnObjectReturnsTheCachedInstanceEvenIfItIsChangedInTheDatabase.

@Test
public void reloadingAnObjectReturnsTheCachedInstanceEvenIfItIsChangedInTheDatabase() {
    Document d = new Document();
    session.save(d);
    // perform an out-of-session update on the object
    sessionFactory.openSession().query("MATCH (n) SET n.name='Document'", Collections.emptyMap());
    // get a copy of the document into a different session
    Document d2 = sessionFactory.openSession().load(Document.class, d.getId());
    assertThat(d2.getName()).isEqualTo("Document");
    // now get a copy of the object from the original session
    Document d3 = session.load(Document.class, d.getId());
    // it does not reflect the changes made in the database
    assertThat(d3.getName()).isEqualTo(null);
    // and in fact, d3 and d are the same object.
    assertThat(d3).isSameAs(d);
}
Also used : Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 12 with Document

use of org.neo4j.ogm.domain.filesystem.Document in project neo4j-ogm by neo4j.

the class StaleObjectTest method testSaveDegenerateFolder.

@Test
public void testSaveDegenerateFolder() {
    // note that we don't clear any of the document object's folder references.
    f.getDocuments().clear();
    session.save(f);
    assertThat(f.toString()).isEqualTo("Folder{name='f', documents=0, archived=0}");
    assertThat(a.toString()).isEqualTo("Document{folder=Folder{name='f', documents=0, archived=0}, name='a'}");
    assertThat(b.toString()).isEqualTo("Document{folder=Folder{name='f', documents=0, archived=0}, name='b'}");
    Document aa = session.load(Document.class, a.getId());
    Document bb = session.load(Document.class, b.getId());
    assertThat(aa.toString()).isEqualTo("Document{folder=null, name='a'}");
    assertThat(bb.toString()).isEqualTo("Document{folder=null, name='b'}");
}
Also used : Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 13 with Document

use of org.neo4j.ogm.domain.filesystem.Document in project neo4j-ogm by neo4j.

the class DirectRelationshipsTest method shouldBeAbleToRemoveAnyRegisteredRelationship.

@Test
public void shouldBeAbleToRemoveAnyRegisteredRelationship() {
    // given
    Folder folder = new Folder();
    Document doc1 = new Document();
    Document doc2 = new Document();
    folder.getDocuments().add(doc1);
    folder.getDocuments().add(doc2);
    doc1.setFolder(folder);
    doc2.setFolder(folder);
    folder.setId(0L);
    doc1.setId(1L);
    doc2.setId(2L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(doc1);
    mappingContext.addNodeEntity(doc2);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc1.getId(), null, Folder.class, Document.class));
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    // when
    doc2.setFolder(null);
    folder.getDocuments().remove(doc2);
    // then
    assertThat(folder.getDocuments()).hasSize(1);
    Compiler compiler = mapper.map(folder).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<Statement> statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
    // we need to re-establish the relationship in the mapping context for this expectation, otherwise
    // the previous save will have de-registered the relationship.
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(doc1).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
    // we need to re-establish the relationship in the mapping context for this expectation, otherwise
    // the previous save will have de-registered the relationship.
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc2.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(doc2).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.deleteRelationshipStatements();
    assertThat(statements).hasSize(1);
    assertThat(statements.get(0).getStatement()).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`CONTAINS`]->(endNode) DELETE rel");
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 14 with Document

use of org.neo4j.ogm.domain.filesystem.Document in project neo4j-ogm by neo4j.

the class LifecycleTest method shouldNotSaveObjectUpdatedByPreDeleteEventHandler.

@Test
public void shouldNotSaveObjectUpdatedByPreDeleteEventHandler() {
    // not interested in the default one for this suite
    session.dispose(eventListener);
    session.register(uuidEventListener);
    Folder f = new Folder();
    Document d = new Document();
    f.setName("folder");
    d.setFolder(f);
    f.getDocuments().add(d);
    assertThat(f.getUuid()).isNull();
    session.save(f);
    assertThat(f.getUuid()).isNotNull();
    // 
    assertThat(((Neo4jSession) session).context().isDirty(f)).isFalse();
    session.delete(d);
    assertThat(f.getName()).isEqualTo("updated by pre-delete");
    assertThat(((Neo4jSession) session).context().isDirty(f)).isTrue();
    session.clear();
    Folder f2 = session.load(Folder.class, f.getId());
    assertThat(f2.getDocuments().isEmpty()).isTrue();
    assertThat(f2.getName()).isEqualTo("folder");
}
Also used : Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 15 with Document

use of org.neo4j.ogm.domain.filesystem.Document in project neo4j-ogm by neo4j.

the class NodeEntityTest method shouldFireEventsWhenCreatingNewEntity.

@Test
public void shouldFireEventsWhenCreatingNewEntity() {
    Document e = new Document();
    e.setName("e");
    session.save(e);
    assertThat(eventListener.count()).isEqualTo(2);
    Event captured = eventListener.get(e, Event.TYPE.PRE_SAVE);
    assertThat(captured).isNotNull();
    assertThat(((PreSaveEvent) captured).isNew()).isTrue();
    captured = eventListener.get(e, Event.TYPE.POST_SAVE);
    assertThat(captured).isNotNull();
    assertThat(((PostSaveEvent) captured).wasNew()).isTrue();
}
Also used : PostSaveEvent(org.neo4j.ogm.session.event.PostSaveEvent) Event(org.neo4j.ogm.session.event.Event) PreSaveEvent(org.neo4j.ogm.session.event.PreSaveEvent) PostSaveEvent(org.neo4j.ogm.session.event.PostSaveEvent) PreSaveEvent(org.neo4j.ogm.session.event.PreSaveEvent) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Aggregations

Document (org.neo4j.ogm.domain.filesystem.Document)26 Test (org.junit.Test)23 Folder (org.neo4j.ogm.domain.filesystem.Folder)11 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)7 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)7 Statement (org.neo4j.ogm.request.Statement)6 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Before (org.junit.Before)3 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)3 Date (java.util.Date)1 Map (java.util.Map)1 EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)1 Actor (org.neo4j.ogm.domain.cineasts.annotated.Actor)1 Knows (org.neo4j.ogm.domain.cineasts.annotated.Knows)1 SessionFactory (org.neo4j.ogm.session.SessionFactory)1 Event (org.neo4j.ogm.session.event.Event)1 PostSaveEvent (org.neo4j.ogm.session.event.PostSaveEvent)1 PreSaveEvent (org.neo4j.ogm.session.event.PreSaveEvent)1