use of org.neo4j.ogm.domain.filesystem.Document in project neo4j-ogm by neo4j.
the class DirectRelationshipsTest method shouldNotBeAbleToCreateDuplicateRelationship.
@Test
public void shouldNotBeAbleToCreateDuplicateRelationship() {
Folder folder = new Folder();
Document document = new Document();
document.setFolder(folder);
// we try to store two identical references to the document object. Although this
// is supported by the graph, it isn't currently supported by the OGM,
// therefore we expect only one relationship to be persisted
folder.getDocuments().add(document);
folder.getDocuments().add(document);
assertThat(folder.getDocuments()).hasSize(2);
// save folder
Compiler compiler = mapper.map(folder).getCompiler();
compiler.useStatementFactory(new RowStatementFactory());
List<Statement> statements = compiler.createNodesStatements();
List<String> createNodeStatements = cypherStatements(statements);
assertThat(createNodeStatements).hasSize(2);
assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
for (Statement statement : statements) {
List rows = (List) statement.getParameters().get("rows");
assertThat(rows).hasSize(1);
}
statements = compiler.createRelationshipsStatements();
List<String> createRelStatements = cypherStatements(statements);
assertThat(createRelStatements).hasSize(1);
assertThat(createRelStatements.get(0)).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
List rows = (List) statements.get(0).getParameters().get("rows");
assertThat(rows).hasSize(1);
// save document
compiler = mapper.map(document).getCompiler();
compiler.useStatementFactory(new RowStatementFactory());
statements = compiler.createNodesStatements();
createNodeStatements = cypherStatements(statements);
assertThat(createNodeStatements).hasSize(2);
assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Folder`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
assertThat(createNodeStatements.contains("UNWIND $rows as row CREATE (n:`Document`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type")).isTrue();
for (Statement statement : statements) {
rows = (List) statement.getParameters().get("rows");
assertThat(rows).hasSize(1);
}
statements = compiler.createRelationshipsStatements();
createRelStatements = cypherStatements(statements);
assertThat(createRelStatements).hasSize(1);
assertThat(createRelStatements.get(0)).isEqualTo("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`CONTAINS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
rows = (List) statements.get(0).getParameters().get("rows");
assertThat(rows).hasSize(1);
}
Aggregations