Search in sources :

Example 1 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class ImportToolTest method shouldNotTrimStringsByDefault.

@Test
public void shouldNotTrimStringsByDefault() throws Exception {
    // GIVEN
    String name = "  This is a line with leading and trailing whitespaces   ";
    File data = data(":ID,name", "1,\"" + name + "\"");
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--nodes", data.getAbsolutePath());
    // THEN
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        ResourceIterator<Node> allNodes = db.getAllNodes().iterator();
        Node node = Iterators.single(allNodes);
        allNodes.close();
        assertEquals(name, node.getProperty("name"));
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 2 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class ImportToolTest method shouldAcceptRawAsciiCharacterCodeAsQuoteConfiguration.

@Test
public void shouldAcceptRawAsciiCharacterCodeAsQuoteConfiguration() throws Exception {
    // GIVEN
    // not '1', just the character represented with code 1, which seems to be SOH
    char weirdDelimiter = 1;
    String name1 = weirdDelimiter + "Weird" + weirdDelimiter;
    String name2 = "Start " + weirdDelimiter + "middle thing" + weirdDelimiter + " end!";
    File data = data(":ID,name", "1," + name1, "2," + name2);
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--nodes", data.getAbsolutePath(), "--quote", String.valueOf(weirdDelimiter));
    // THEN
    Set<String> names = asSet("Weird", name2);
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        for (Node node : db.getAllNodes()) {
            String name = (String) node.getProperty("name");
            assertTrue("Didn't expect node with name '" + name + "'", names.remove(name));
        }
        assertTrue(names.isEmpty());
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 3 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class ImportToolTest method verifyData.

private void verifyData(Validator<Node> nodeAdditionalValidation, Validator<Relationship> relationshipAdditionalValidation) {
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        int nodeCount = 0, relationshipCount = 0;
        for (Node node : db.getAllNodes()) {
            assertTrue(node.hasProperty("name"));
            nodeAdditionalValidation.validate(node);
            nodeCount++;
        }
        assertEquals(NODE_COUNT, nodeCount);
        for (Relationship relationship : db.getAllRelationships()) {
            assertTrue(relationship.hasProperty("created"));
            relationshipAdditionalValidation.validate(relationship);
            relationshipCount++;
        }
        assertEquals(RELATIONSHIP_COUNT, relationshipCount);
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 4 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class ImportToolTest method shouldSkipDuplicateNodesIfToldTo.

@Test
public void shouldSkipDuplicateNodesIfToldTo() throws Exception {
    // GIVEN
    List<String> nodeIds = asList("a", "b", "c", "d", "e", "f", "a", "g");
    Configuration config = Configuration.COMMAS;
    File nodeHeaderFile = nodeHeader(config);
    File nodeData1 = nodeData(false, config, nodeIds, lines(0, 4));
    File nodeData2 = nodeData(false, config, nodeIds, lines(4, nodeIds.size()));
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--skip-duplicate-nodes", "--nodes", nodeHeaderFile.getAbsolutePath() + MULTI_FILE_DELIMITER + nodeData1.getAbsolutePath() + MULTI_FILE_DELIMITER + nodeData2.getAbsolutePath());
    // THEN
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        // there should not be duplicates of any node
        Iterator<Node> nodes = db.getAllNodes().iterator();
        Iterator<String> expectedIds = FilteringIterator.noDuplicates(nodeIds.iterator());
        while (expectedIds.hasNext()) {
            assertTrue(nodes.hasNext());
            assertEquals(expectedIds.next(), nodes.next().getProperty("id"));
        }
        assertFalse(nodes.hasNext());
        // also all nodes in the label index should exist
        for (int i = 0; i < MAX_LABEL_ID; i++) {
            Label label = Label.label(labelName(i));
            try (ResourceIterator<Node> nodesByLabel = db.findNodes(label)) {
                while (nodesByLabel.hasNext()) {
                    assertTrue(nodesByLabel.next().hasLabel(label));
                }
            }
        }
        tx.success();
    } finally {
        db.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Configuration(org.neo4j.unsafe.impl.batchimport.input.csv.Configuration) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) DynamicLabel(org.neo4j.graphdb.DynamicLabel) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 5 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class ImportToolTest method shouldImportGroupsOfOverlappingIds.

@Test
public void shouldImportGroupsOfOverlappingIds() throws Exception {
    // GIVEN
    List<String> groupOneNodeIds = asList("1", "2", "3");
    List<String> groupTwoNodeIds = asList("4", "5", "2");
    List<RelationshipDataLine> rels = asList(relationship("1", "4", "TYPE"), relationship("2", "5", "TYPE"), relationship("3", "2", "TYPE"));
    Configuration config = Configuration.COMMAS;
    String groupOne = "Actor";
    String groupTwo = "Movie";
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--nodes", nodeHeader(config, groupOne) + MULTI_FILE_DELIMITER + nodeData(false, config, groupOneNodeIds, TRUE), "--nodes", nodeHeader(config, groupTwo) + MULTI_FILE_DELIMITER + nodeData(false, config, groupTwoNodeIds, TRUE), "--relationships", relationshipHeader(config, groupOne, groupTwo, true) + MULTI_FILE_DELIMITER + relationshipData(false, config, rels.iterator(), TRUE, true));
    // THEN
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        int nodeCount = 0;
        for (Node node : db.getAllNodes()) {
            assertTrue(node.hasProperty("name"));
            nodeCount++;
            assertEquals(1, Iterables.count(node.getRelationships()));
        }
        assertEquals(6, nodeCount);
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Configuration(org.neo4j.unsafe.impl.batchimport.input.csv.Configuration) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)322 Test (org.junit.Test)225 Transaction (org.neo4j.graphdb.Transaction)182 Node (org.neo4j.graphdb.Node)142 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)77 File (java.io.File)70 Relationship (org.neo4j.graphdb.Relationship)49 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)32 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)17 Result (org.neo4j.graphdb.Result)14 Label (org.neo4j.graphdb.Label)13 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)12 HashMap (java.util.HashMap)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 PageCache (org.neo4j.io.pagecache.PageCache)10 DbRepresentation (org.neo4j.test.DbRepresentation)10 GraphDatabaseFactory (org.neo4j.graphdb.factory.GraphDatabaseFactory)9 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)8