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