public static void main( String[] args ) throws IOException { Path homeDir = Paths.get( NEO4J_HOME ); Path configDir = Paths.get( NEO4J_CONF ); boolean debug = NEO4J_DEBUG != null; try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) { new AdminTool( CommandLocator.fromServiceLocator(), BlockerLocator.fromServiceLocator(), outsideWorld, debug ).execute( homeDir, configDir, args ); } }
@Test void acceptsNodeMetadata() throws Exception { File homeDir = testDir.directory( "home" ); ImporterFactory mockImporterFactory = mock( ImporterFactory.class ); Importer importer = mock( Importer.class ); when( mockImporterFactory .getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ) ) .thenReturn( importer ); ImportCommand importCommand = new ImportCommand( homeDir.toPath(), testDir.directory( "conf" ).toPath(), new RealOutsideWorld( System.out, System.err, new ByteArrayInputStream( new byte[0] ) ), mockImporterFactory ); String[] arguments = {"--database=foo", "--from=bar", "--nodes:PERSON:FRIEND=mock.csv"}; importCommand.execute( arguments ); verify( mockImporterFactory ) .getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ); }
@Test void acceptsRelationshipsMetadata() throws Exception { File homeDir = testDir.directory( "home" ); ImporterFactory mockImporterFactory = mock( ImporterFactory.class ); Importer importer = mock( Importer.class ); when( mockImporterFactory .getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ) ) .thenReturn( importer ); ImportCommand importCommand = new ImportCommand( homeDir.toPath(), testDir.directory( "conf" ).toPath(), new RealOutsideWorld( System.out, System.err, new ByteArrayInputStream( new byte[0] ) ), mockImporterFactory ); String[] arguments = {"--database=foo", "--from=bar", "--relationships:LIKES:HATES=mock.csv"}; importCommand.execute( arguments ); verify( mockImporterFactory ) .getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ); }
@Test public void closeFileSystemOnClose() throws Exception { RealOutsideWorld outsideWorld = new RealOutsideWorld(); FileSystemAbstraction fileSystemMock = mock( FileSystemAbstraction.class ); outsideWorld.fileSystemAbstraction = fileSystemMock; outsideWorld.close(); verify( fileSystemMock ).close(); }
@Test void defaultsToCsvWhenModeNotSpecified() throws Exception { File homeDir = testDir.directory( "home" ); ImporterFactory mockImporterFactory = mock( ImporterFactory.class ); Importer importer = mock( Importer.class ); when( mockImporterFactory .getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ) ) .thenReturn( importer ); try ( RealOutsideWorld outsideWorld = new RealOutsideWorld( System.out, System.err, new ByteArrayInputStream( new byte[0] ) ) ) { ImportCommand importCommand = new ImportCommand( homeDir.toPath(), testDir.directory( "conf" ).toPath(), outsideWorld, mockImporterFactory ); String[] arguments = {"--database=foo", "--from=bar"}; importCommand.execute( arguments ); verify( mockImporterFactory ).getImporterForMode( eq( "csv" ), any( Args.class ), any( Config.class ), any( OutsideWorld.class ) ); } }
@Test void letImporterDecideAboutDatabaseExistence() throws Exception { Path homeDir = testDir.directory( "home" ).toPath(); PrintStream nullOutput = new PrintStream( NULL_OUTPUT_STREAM ); OutsideWorld outsideWorld = new RealOutsideWorld( nullOutput, nullOutput, new ByteArrayInputStream( new byte[0] ) ); Path confPath = testDir.directory( "conf" ).toPath(); ImportCommand importCommand = new ImportCommand( homeDir, confPath, outsideWorld ); File nodesFile = createTextFile( "nodes.csv", ":ID", "1", "2" ); String[] arguments = {"--mode=csv", "--database=existing.db", "--nodes=" + nodesFile.getAbsolutePath()}; // First run an import so that a database gets created importCommand.execute( arguments ); // When ImporterFactory importerFactory = mock( ImporterFactory.class ); Importer importer = mock( Importer.class ); when( importerFactory.getImporterForMode( any(), any(), any(), any() ) ).thenReturn( importer ); new ImportCommand( homeDir, confPath, outsideWorld, importerFactory ).execute( arguments ); // Then no exception about database existence should be thrown }
@Test public void closeFilesystemOnExit() throws IOException { RealOutsideWorld outsideWorld = new RealOutsideWorld(); FileSystemAbstraction fileSystemMock = mock( FileSystemAbstraction.class ); outsideWorld.fileSystemAbstraction = fileSystemMock; systemExitRule.expectExit( 0 ); outsideWorld.exit( 0 ); verify( fileSystemMock ).close(); } }
@Test void exitIfConfigFileIsMissing() throws IOException { Files.delete( configFile ); String[] args = {"--list"}; try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) { DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> diagnosticsReportCommand.execute( args ) ); assertThat( commandFailed.getMessage(), containsString( "Unable to find config file, tried: " ) ); } }
try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() )
@Test void overrideDestination() throws Exception { // because of https://bugs.openjdk.java.net/browse/JDK-8202127 and current surefire behaviour we need to have custom value for JRE >= 11 String toArgument = JRE.JAVA_11.isCurrentVersion() ? "--to=" + System.getProperty( "user.dir" ) + "/other/" : "--to=other/"; String[] args = {toArgument, "all"}; try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) { DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); diagnosticsReportCommand.execute( args ); File other = testDirectory.directory( "other" ); FileSystemAbstraction fs = outsideWorld.fileSystem(); assertThat( fs.fileExists( other ), is( true ) ); assertThat( fs.listFiles( other ).length, is( 1 ) ); // Default should be empty File reports = new File( testDirectory.directory(), "reports" ); assertThat( fs.fileExists( reports ), is( false ) ); } }
@Test void writesReportToSpecifiedReportFile() throws Exception { File dbDir = testDir.directory( "db" ); File logDir = testDir.directory( "logs" ); File reportLocation = testDir.file( "the_report" ); File inputFile = testDir.file( "foobar.csv" ); List<String> lines = Collections.singletonList( "foo\\tbar\\tbaz" ); Files.write( inputFile.toPath(), lines, Charset.defaultCharset() ); try ( RealOutsideWorld outsideWorld = new RealOutsideWorld( System.out, System.err, new ByteArrayInputStream( new byte[0] ) ) ) { Config config = Config.builder() .withSettings( additionalConfig() ) .withSetting( GraphDatabaseSettings.database_path, dbDir.getAbsolutePath() ) .withSetting( GraphDatabaseSettings.logs_directory, logDir.getAbsolutePath() ).build(); CsvImporter csvImporter = new CsvImporter( Args.parse( String.format( "--report-file=%s", reportLocation.getAbsolutePath() ), String.format( "--nodes=%s", inputFile.getAbsolutePath() ), "--delimiter=TAB" ), config, outsideWorld ); csvImporter.doImport(); } assertTrue( reportLocation.exists() ); }
@Test void shouldUseArgumentsFoundInside_f_Argument() throws FileNotFoundException, CommandFailed, IncorrectUsage { // given File report = testDir.file( "report" ); ImportCommand importCommand = new ImportCommand( testDir.directory( "home" ).toPath(), testDir.directory( "conf" ).toPath(), new RealOutsideWorld( System.out, System.err, new ByteArrayInputStream( new byte[0] ) ) ); File nodesFile = createTextFile( "nodes.csv", ":ID", "1", "2" ); String pathWithEscapedSpaces = escapeSpaces( nodesFile.getAbsolutePath() ); String reportEscapedPath = escapeSpaces( report.getAbsolutePath() ); File argFile = createTextFile( "args.txt", "--database=foo", "--nodes=" + pathWithEscapedSpaces, "--report-file=" + reportEscapedPath ); String[] arguments = {"-f", argFile.getAbsolutePath()}; // when importCommand.execute( arguments ); // then assertTrue( suppressOutput.getOutputVoice().containsMessage( "IMPORT DONE" ) ); assertTrue( suppressOutput.getErrorVoice().containsMessage( nodesFile.getAbsolutePath() ) ); assertTrue( suppressOutput.getOutputVoice().containsMessage( "2 nodes" ) ); }
@Test void printUnrecognizedClassifiers() throws Exception { String[] args = {"logs", "tx", "invalid"}; try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) { DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> diagnosticsReportCommand.execute( args ) ); assertEquals( "Unknown classifier: invalid", incorrectUsage.getMessage() ); } }
@Test void allHasToBeOnlyClassifier() throws Exception { String[] args = {"all", "logs", "tx"}; try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) { DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); IncorrectUsage incorrectUsage = assertThrows( IncorrectUsage.class, () -> diagnosticsReportCommand.execute( args ) ); assertEquals( "If you specify 'all' this has to be the only classifier. Found ['logs','tx'] as well.", incorrectUsage.getMessage() ); } }
@Test void errorOnInvalidPid() throws Exception { String[] args = {"--pid=a", "all"}; try ( RealOutsideWorld outsideWorld = new RealOutsideWorld() ) { DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand( homeDir, configDir, outsideWorld ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> diagnosticsReportCommand.execute( args ) ); assertEquals( "Unable to parse --pid", commandFailed.getMessage() ); } } }