Search in sources :

Example 21 with Document

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

the class DirectRelationshipsTest method shouldBeAbleToCreateDifferentRelationshipsToTheSameDocument.

@Test
public void shouldBeAbleToCreateDifferentRelationshipsToTheSameDocument() {
    Folder folder = new Folder();
    Document document = new Document();
    document.setFolder(folder);
    folder.getDocuments().add(document);
    folder.getArchived().add(document);
    // 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(2);
    assertThat(createRelStatements.contains("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")).isTrue();
    assertThat(createRelStatements.contains("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`ARCHIVED`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type")).isTrue();
    boolean archivedType = false;
    boolean containsType = false;
    for (Statement statement : statements) {
        if (statement.getStatement().contains("ARCHIVED")) {
            archivedType = true;
        }
        if (statement.getStatement().contains("CONTAINS")) {
            containsType = true;
        }
    }
    assertThat(archivedType).isTrue();
    assertThat(containsType).isTrue();
    // 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();
    statements = compiler.createRelationshipsStatements();
    createRelStatements = cypherStatements(statements);
    assertThat(createRelStatements).hasSize(2);
    assertThat(createRelStatements.contains("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")).isTrue();
    assertThat(createRelStatements.contains("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`ARCHIVED`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type")).isTrue();
    archivedType = false;
    containsType = false;
    for (Statement statement : statements) {
        if (statement.getStatement().contains("ARCHIVED")) {
            archivedType = true;
        }
        if (statement.getStatement().contains("CONTAINS")) {
            containsType = true;
        }
    }
    assertThat(archivedType).isTrue();
    assertThat(containsType).isTrue();
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) ArrayList(java.util.ArrayList) List(java.util.List) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 22 with Document

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

the class DirectRelationshipsTest method shouldSaveNewFolderDocumentPair.

@Test
public void shouldSaveNewFolderDocumentPair() {
    Folder folder = new Folder();
    Document document = new Document();
    folder.getDocuments().add(document);
    document.setFolder(folder);
    Compiler compiler = mapper.map(folder).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    List<String> createNodeStatements = cypherStatements(compiler.createNodesStatements());
    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();
    List<String> createRelStatements = cypherStatements(compiler.createRelationshipsStatements());
    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");
    compiler = mapper.map(document).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    createNodeStatements = cypherStatements(compiler.createNodesStatements());
    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();
    createRelStatements = cypherStatements(compiler.createRelationshipsStatements());
    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");
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) 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 23 with Document

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

the class DirectRelationshipsTest method shouldBeAbleToRemoveTheOnlyRegisteredRelationship.

@Test
public void shouldBeAbleToRemoveTheOnlyRegisteredRelationship() {
    Folder folder = new Folder();
    Document document = new Document();
    folder.getDocuments().add(document);
    document.setFolder(folder);
    folder.setId(0L);
    document.setId(1L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(document);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", document.getId(), null, Folder.class, Document.class));
    document.setFolder(null);
    folder.getDocuments().clear();
    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", document.getId(), null, Folder.class, Document.class));
    compiler = mapper.map(document).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 24 with Document

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

the class DirectRelationshipsTest method shouldSaveNewFolderWithTwoDocuments.

@Test
public void shouldSaveNewFolderWithTwoDocuments() {
    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);
    // 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");
        if (statement.getStatement().contains("Folder")) {
            assertThat(rows).hasSize(1);
        }
        if (statement.getStatement().contains("Document")) {
            assertThat(rows).hasSize(2);
        }
    }
    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(2);
    // Save doc1
    compiler = mapper.map(doc1).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");
        if (statement.getStatement().contains("Folder")) {
            assertThat(rows).hasSize(1);
        }
        if (statement.getStatement().contains("Document")) {
            assertThat(rows).hasSize(2);
        }
    }
    // Save doc2
    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(2);
    compiler = mapper.map(doc2).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");
        if (statement.getStatement().contains("Folder")) {
            assertThat(rows).hasSize(1);
        }
        if (statement.getStatement().contains("Document")) {
            assertThat(rows).hasSize(2);
        }
    }
    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(2);
}
Also used : Compiler(org.neo4j.ogm.cypher.compiler.Compiler) Statement(org.neo4j.ogm.request.Statement) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) ArrayList(java.util.ArrayList) List(java.util.List) Folder(org.neo4j.ogm.domain.filesystem.Folder) Document(org.neo4j.ogm.domain.filesystem.Document) Test(org.junit.Test)

Example 25 with Document

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

the class DirectRelationshipsTest method shouldBeAbleToRemoveContainedRelationshipOnly.

@Test
public void shouldBeAbleToRemoveContainedRelationshipOnly() {
    // given
    Folder folder = new Folder();
    Document doc1 = new Document();
    folder.getDocuments().add(doc1);
    folder.getArchived().add(doc1);
    doc1.setFolder(folder);
    folder.setId(0L);
    doc1.setId(1L);
    mappingContext.addNodeEntity(folder);
    mappingContext.addNodeEntity(doc1);
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "CONTAINS", doc1.getId(), null, Folder.class, Document.class));
    mappingContext.addRelationship(new MappedRelationship(folder.getId(), "ARCHIVED", doc1.getId(), null, Folder.class, Document.class));
    // when
    folder.getDocuments().remove(doc1);
    doc1.setFolder(null);
    // then
    assertThat(folder.getDocuments()).isEmpty();
    assertThat(folder.getArchived()).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");
    mapper = new EntityGraphMapper(mappingMetadata, mappingContext);
    // There are no more changes to the graph
    compiler = mapper.map(doc1).getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    statements = compiler.deleteRelationshipStatements();
    assertThat(statements).isEmpty();
}
Also used : EntityGraphMapper(org.neo4j.ogm.context.EntityGraphMapper) 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)

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