private void success() { outsideWorld.exit( STATUS_SUCCESS ); } }
private void failure() { outsideWorld.exit( 1 ); }
private void failure( String message, int code ) { outsideWorld.stdErrLine( message ); outsideWorld.exit( code ); }
@Test public void shouldErrorWithNoSuchUser() { tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "bob" ); verify( out ).stdErrLine( "command failed: no such user: 'bob'" ); verify( out ).exit( 1 ); verify( out, never() ).stdOutLine( anyString() ); }
@Test void shouldBlockDumpIfABlockerSaysSo() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = mock( AdminCommand.class ); AdminCommand.Blocker blocker = mock( AdminCommand.Blocker.class ); when( blocker.doesBlock( any(), any() ) ).thenReturn( true ); when( blocker.commands() ).thenReturn( Collections.singleton( "command" ) ); when( blocker.explanation() ).thenReturn( "the explanation" ); BlockerLocator blockerLocator = mock( BlockerLocator.class ); when( blockerLocator.findBlockers( "command" ) ).thenReturn( Collections.singletonList( blocker ) ); new AdminTool( cannedCommand( "command", command ), blockerLocator, outsideWorld, false ) .execute( null, null, "command" ); verify( outsideWorld ).stdErrLine( "command failed: the explanation" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test void shouldBlockDumpIfOneBlockerOutOfManySaysSo() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = mock( AdminCommand.class ); AdminCommand.Blocker trueBlocker = mock( AdminCommand.Blocker.class ); when( trueBlocker.doesBlock( any(), any() ) ).thenReturn( true ); when( trueBlocker.explanation() ).thenReturn( "trueBlocker explanation" ); AdminCommand.Blocker falseBlocker = mock( AdminCommand.Blocker.class ); when( falseBlocker.doesBlock( any(), any() ) ).thenReturn( false ); when( falseBlocker.explanation() ).thenReturn( "falseBlocker explanation" ); BlockerLocator blockerLocator = mock( BlockerLocator.class ); when( blockerLocator.findBlockers( "command" ) ) .thenReturn( Arrays.asList( falseBlocker, trueBlocker, falseBlocker ) ); new AdminTool( cannedCommand( "command", command ), blockerLocator, outsideWorld, false ) .execute( null, null, "command" ); verify( outsideWorld ).stdErrLine( "command failed: trueBlocker explanation" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test public void shouldIgnoreInitialUserIfUsersExist() throws Throwable { insertUser( "jane", false ); insertUser( "janette", true ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "jane" ); assertAdminIniFile( "jane" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "janette" ); verify( out ).stdOutLine( "default admin user set to 'jane'" ); verify( out ).stdErrLine( "command failed: no such user: 'janette'" ); verify( out ).exit( 1 ); }
@Test public void shouldGetUsageOnWrongArguments1() { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD ); assertNoAuthIniFile(); verify( out ).stdErrLine( "not enough arguments" ); verify( out, times( 3 ) ).stdErrLine( "" ); verify( out ).stdErrLine( "usage: neo4j-admin set-initial-password <password>" ); verify( out ).stdErrLine( String.format( "environment variables:" ) ); verify( out ).stdErrLine( String.format( " NEO4J_CONF Path to directory which contains neo4j.conf." ) ); verify( out ).stdErrLine( String.format( " NEO4J_DEBUG Set to anything to enable debug output." ) ); verify( out ).stdErrLine( String.format( " NEO4J_HOME Neo4j home directory." ) ); verify( out ).stdErrLine( String.format( " HEAP_SIZE Set JVM maximum heap size during command execution." ) ); verify( out ).stdErrLine( String.format( " Takes a number and a unit, for example 512m." ) ); verify( out ).stdErrLine( "Sets the initial password of the initial admin user ('neo4j')." ); verify( out ).exit( 1 ); verifyNoMoreInteractions( out ); verify( out, never() ).stdOutLine( anyString() ); }
@Test void helpArgumentPrintsHelp() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "--help" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdErrLine( "unrecognized command: --help" ); verify( outsideWorld ).stdErrLine( "usage: neo4j-admin <command>" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test public void shouldGetUsageOnWrongArguments2() { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "foo", "bar" ); assertNoAuthIniFile(); verify( out ).stdErrLine( "unrecognized arguments: 'bar'" ); verify( out, times( 3 ) ).stdErrLine( "" ); verify( out ).stdErrLine( "usage: neo4j-admin set-initial-password <password>" ); verify( out ).stdErrLine( String.format( "environment variables:" ) ); verify( out ).stdErrLine( String.format( " NEO4J_CONF Path to directory which contains neo4j.conf." ) ); verify( out ).stdErrLine( String.format( " NEO4J_DEBUG Set to anything to enable debug output." ) ); verify( out ).stdErrLine( String.format( " NEO4J_HOME Neo4j home directory." ) ); verify( out ).stdErrLine( String.format( " HEAP_SIZE Set JVM maximum heap size during command execution." ) ); verify( out ).stdErrLine( String.format( " Takes a number and a unit, for example 512m." ) ); verify( out ).stdErrLine( "Sets the initial password of the initial admin user ('neo4j')." ); verify( out ).exit( 1 ); verifyNoMoreInteractions( out ); verify( out, never() ).stdOutLine( anyString() ); }
@Test void helpArgumentPrintsHelpForCommand() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "command", "--help" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdErrLine( "unknown argument: --help" ); verify( outsideWorld ).stdErrLine( "usage: neo4j-admin command " ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test public void shouldErrorIfRealUsersAlreadyExistCommunity() throws Throwable { // Given File authFile = getAuthFile( "auth" ); fileSystem.mkdirs( authFile.getParentFile() ); fileSystem.create( authFile ); // When tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "will-be-ignored" ); // Then assertNoAuthIniFile(); verify( out, times( 1 ) ) .stdErrLine( "command failed: the provided initial password was not set because existing Neo4j users were " + "detected at `" + authFile.getAbsolutePath() + "`. Please remove the existing `auth` file if you " + "want to reset your database to only have a default user with the provided password." ); verify( out ).exit( 1 ); verify( out, times( 0 ) ).stdOutLine( anyString() ); }
@Test void versionArgumentPrintsVersionEvenWithCommand() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "command", "--version" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdOutLine( "neo4j-admin " + neo4jVersion() ); verify( outsideWorld ).exit( STATUS_SUCCESS ); }
@Test void versionArgumentPrintsVersion() { AdminCommand command = mock( AdminCommand.class ); OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( cannedCommand( "command", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "--version" ); verifyNoMoreInteractions( command ); verify( outsideWorld ).stdOutLine( "neo4j-admin " + neo4jVersion() ); verify( outsideWorld ).exit( STATUS_SUCCESS ); }
@Test void shouldProvideFeedbackIfTheCommandThrowsARuntimeException() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = args -> { throw new RuntimeException( "the-exception-message" ); }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); verify( outsideWorld ).stdErrLine( "unexpected error: the-exception-message" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test void shouldProvideFeedbackIfTheCommandReportsAUsageProblem() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = args -> { throw new IncorrectUsage( "the-usage-message" ); }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); InOrder inOrder = inOrder( outsideWorld ); inOrder.verify( outsideWorld ).stdErrLine( "the-usage-message" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test void shouldProvideFeedbackWhenNoCommandIsProvided() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( new NullCommandLocator(), new NullBlockerLocator(), outsideWorld, false ).execute( null, null ); verify( outsideWorld ).stdErrLine( "you must provide a command" ); verify( outsideWorld ).stdErrLine( "usage: neo4j-admin <command>" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test public void shouldErrorIfRealUsersAlreadyExistEnterprise() throws Throwable { // Given File authFile = getAuthFile( "auth" ); File rolesFile = getAuthFile( "roles" ); fileSystem.mkdirs( authFile.getParentFile() ); fileSystem.create( authFile ); fileSystem.create( rolesFile ); // When tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "will-be-ignored" ); // Then assertNoAuthIniFile(); verify( out, times( 1 ) ) .stdErrLine( "command failed: the provided initial password was not set because existing Neo4j users were " + "detected at `" + authFile.getAbsolutePath() + "`. Please remove the existing `auth` and `roles` files if you " + "want to reset your database to only have a default user with the provided password." ); verify( out ).exit( 1 ); }
@Test void shouldProvideFeedbackIfTheCommandFails() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); AdminCommand command = args -> { throw new CommandFailed( "the-failure-message" ); }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); verify( outsideWorld ).stdErrLine( "command failed: the-failure-message" ); verify( outsideWorld ).exit( STATUS_ERROR ); }
@Test void shouldExit0WhenEverythingWorks() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); new AdminTool( new CannedLocator( new NullCommandProvider() ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "null" ); verify( outsideWorld ).exit( STATUS_SUCCESS ); }