@Test public void shouldFailOnInvalidMaxMemoryPercentageSetting() throws Exception { // GIVEN List<String> nodeIds = nodeIds( 10 ); try { // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeData( true, Configuration.COMMAS, nodeIds, TRUE ).getAbsolutePath(), "--max-memory", "110%" ); fail( "Should have failed" ); } catch ( IllegalArgumentException e ) { // THEN good assertThat( e.getMessage(), containsString( "percent" ) ); } }
@Test public void shouldRespectMaxMemoryPercentageSetting() throws Exception { // GIVEN List<String> nodeIds = nodeIds( 10 ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeData( true, Configuration.COMMAS, nodeIds, TRUE ).getAbsolutePath(), "--max-memory", "60%" ); }
@Test public void shouldRespectMaxMemorySuffixedSetting() throws Exception { // GIVEN List<String> nodeIds = nodeIds( 10 ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeData( true, Configuration.COMMAS, nodeIds, TRUE ).getAbsolutePath(), "--max-memory", "100M" ); }
@Test public void shouldFailAndReportStartingLineForUnbalancedQuoteAtEnd() throws Exception { // GIVEN int unbalancedStartLine = 10; // WHEN try { importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeDataWithMissingQuote( unbalancedStartLine, unbalancedStartLine ).getAbsolutePath() ); fail( "Should have failed" ); } catch ( InputException e ) { // THEN assertThat( e.getMessage(), containsString( String.format( "Multi-line fields" ) ) ); } }
@Test public void shouldFailAndReportStartingLineForUnbalancedQuoteInMiddle() throws Exception { // GIVEN int unbalancedStartLine = 10; // WHEN try { importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeDataWithMissingQuote( 2 * unbalancedStartLine, unbalancedStartLine ) .getAbsolutePath() ); fail( "Should have failed" ); } catch ( InputException e ) { // THEN assertThat( e.getMessage(), containsString( String.format( "Multi-line fields are illegal", unbalancedStartLine ) ) ); } }
@Test public void shouldDisallowImportWithoutNodesInput() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; // WHEN try { importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--relationships", relationshipData( true, config, nodeIds, TRUE, true ).getAbsolutePath() ); fail( "Should have failed" ); } catch ( IllegalArgumentException e ) { // THEN assertThat( e.getMessage(), containsString( "No node input" ) ); } }
@Test public void shouldImportFromInputDataEncodedWithSpecificCharset() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; Charset charset = StandardCharsets.UTF_16; // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--input-encoding", charset.name(), "--nodes", nodeData( true, config, nodeIds, TRUE, charset ).getAbsolutePath(), "--relationships", relationshipData( true, config, nodeIds, TRUE, true, charset ) .getAbsolutePath() ); // THEN verifyData(); }
@Test public void shouldBeAbleToMixSpecifiedAndUnspecifiedGroups() throws Exception { // GIVEN List<String> groupOneNodeIds = asList( "1", "2", "3" ); List<String> groupTwoNodeIds = asList( "4", "5", "2" ); Configuration config = Configuration.COMMAS; // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeHeader( config, "MyGroup" ).getAbsolutePath() + MULTI_FILE_DELIMITER + nodeData( false, config, groupOneNodeIds, TRUE ).getAbsolutePath(), "--nodes", nodeHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER + nodeData( false, config, groupTwoNodeIds, TRUE ).getAbsolutePath() ); // THEN verifyData( 6, 0, Validators.emptyValidator(), Validators.emptyValidator() ); }
@Test public void shouldFailOnUnbalancedQuoteWithMultilinesEnabled() throws Exception { // GIVEN int unbalancedStartLine = 10; // WHEN try { importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--multiline-fields", "true", "--nodes", nodeDataWithMissingQuote( 2 * unbalancedStartLine, unbalancedStartLine ).getAbsolutePath() ); fail( "Should have failed" ); } catch ( InputException e ) { // THEN OK } }
@Test public void shouldAcceptSpecialTabCharacterAsDelimiterConfiguration() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.TABS; // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--delimiter", "\\t", "--array-delimiter", String.valueOf( config.arrayDelimiter() ), "--nodes", nodeData( true, config, nodeIds, TRUE ).getAbsolutePath(), "--relationships", relationshipData( true, config, nodeIds, TRUE, true ).getAbsolutePath() ); // THEN verifyData(); }
@Test public void shouldImportWithHeadersBeingInSeparateFiles() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.TABS; // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--delimiter", "TAB", "--array-delimiter", String.valueOf( config.arrayDelimiter() ), "--nodes", nodeHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER + nodeData( false, config, nodeIds, TRUE ).getAbsolutePath(), "--relationships", relationshipHeader( config ).getAbsolutePath() + MULTI_FILE_DELIMITER + relationshipData( false, config, nodeIds, TRUE, true ).getAbsolutePath() ); // THEN verifyData(); }
@Test public void shouldDisallowMultilineFieldsByDefault() throws Exception { // GIVEN File data = data( ":ID,name", "1,\"This is a line with\nnewlines in\"" ); // WHEN try { importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath() ); fail(); } catch ( Exception e ) { // THEN assertExceptionContains( e, "Multi-line", IllegalMultilineFieldException.class ); } }
@Test public void shouldImportWithoutTypeSpecifiedInRelationshipHeaderbutWithDefaultTypeInArgument() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; String type = randomType(); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeData( true, config, nodeIds, TRUE ).getAbsolutePath(), // there will be no :TYPE specified in the header of the relationships below "--relationships:" + type, relationshipData( true, config, nodeIds, TRUE, false ).getAbsolutePath() ); // THEN verifyData(); }
@Test public void shouldSupplyArgumentsAsFile() throws Exception { // given List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; File argumentFile = file( "args" ); String nodesEscapedSpaces = escapePath( nodeData( true, config, nodeIds, TRUE ).getAbsolutePath() ); String relationshipsEscapedSpaced = escapePath( relationshipData( true, config, nodeIds, TRUE, true ).getAbsolutePath() ); String arguments = format( "--into %s%n" + "--nodes %s --relationships %s", escapePath( dbRule.getDatabaseDirAbsolutePath() ), nodesEscapedSpaces, relationshipsEscapedSpaced ); writeToFile( argumentFile, arguments, false ); // when importTool( "-f", argumentFile.getAbsolutePath() ); // then verifyData(); }
@Test public void shouldImportWithAsManyDefaultsAsAvailable() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", nodeData( true, config, nodeIds, TRUE ).getAbsolutePath(), "--relationships", relationshipData( true, config, nodeIds, TRUE, true ).getAbsolutePath() ); // THEN assertTrue( suppressOutput.getOutputVoice().containsMessage( "IMPORT DONE" ) ); verifyData(); }
@Test public void shouldSkipEmptyFiles() throws Exception { // GIVEN File data = data( "" ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath() ); // THEN GraphDatabaseService graphDatabaseService = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = graphDatabaseService.beginTx() ) { ResourceIterator<Node> allNodes = graphDatabaseService.getAllNodes().iterator(); assertFalse( "Expected database to be empty", allNodes.hasNext() ); tx.success(); } }
@Test public void shouldHandleAdditiveLabelsWithSpaces() throws Exception { // GIVEN List<String> nodeIds = nodeIds(); Configuration config = Configuration.COMMAS; final Label label1 = label( "My First Label" ); final Label label2 = label( "My Other Label" ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes:My First Label:My Other Label", nodeData( true, config, nodeIds, TRUE ).getAbsolutePath(), "--relationships", relationshipData( true, config, nodeIds, TRUE, true ).getAbsolutePath() ); // THEN verifyData( node -> { assertTrue( node.hasLabel( label1 ) ); assertTrue( node.hasLabel( label2 ) ); }, Validators.emptyValidator() ); }
@Test public void shouldNotIndexNodesWithWrongLabel() throws Exception { // Given File file = new File( dbRule.getDatabaseDirAbsolutePath() ); BatchInserter inserter = BatchInserters.inserter( file, fileSystemRule.get() ); inserter.createNode( map("name", "Bob"), label( "User" ), label("Admin")); inserter.createDeferredSchemaIndex( label( "Banana" ) ).on( "name" ).create(); // When inserter.shutdown(); // Then GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { assertThat( count( db.findNodes( label( "Banana" ), "name", "Bob" ) ), equalTo( 0L ) ); } finally { db.shutdown(); } }
@Test public void shouldAllowMultilineFieldsWhenEnabled() throws Exception { // GIVEN File data = data( ":ID,name", "1,\"This is a line with\nnewlines in\"" ); // WHEN importTool( "--into", dbRule.getDatabaseDirAbsolutePath(), "--nodes", data.getAbsolutePath(), "--multiline-fields", "true" ); // THEN GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { ResourceIterator<Node> allNodes = db.getAllNodes().iterator(); Node node = Iterators.single( allNodes ); allNodes.close(); assertEquals( "This is a line with\nnewlines in", node.getProperty( "name" ) ); tx.success(); } }
@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.getDatabaseDirAbsolutePath(), "--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(); } }