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 ); } }
badUsage( "you must provide a command" ); return; success(); return; commandFailed( new CommandFailed( blocker.explanation() ) ); badUsage( format( "unrecognized command: %s", name ) ); return; badUsage( format( "unrecognized command: %s", name ) ); return; failure(); success(); badUsage( provider, e ); commandFailed( e ); unexpected( e );
public AdminTool( CommandLocator commandLocator, BlockerLocator blockerLocator, OutsideWorld outsideWorld, boolean debug ) { this.commandLocator = CommandLocator.withAdditionalCommand( help(), commandLocator ); this.blockerLocator = blockerLocator; this.outsideWorld = outsideWorld; this.debug = debug; this.usage = new Usage( scriptName, this.commandLocator ); }
@Test public void shouldOverwriteIfSetPasswordAgain() throws Throwable { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "abc" ); assertAuthIniFile( "abc" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "muchBetter" ); assertAuthIniFile( "muchBetter" ); verify( out, times( 2 ) ).stdOutLine( "Changed password for user 'neo4j'." ); }
@Before public void setup() { File graphDir = new File( GraphDatabaseSettings.DEFAULT_DATABASE_NAME ); confDir = new File( graphDir, "conf" ); homeDir = new File( graphDir, "home" ); out = mock( OutsideWorld.class ); resetOutsideWorldMock(); tool = new AdminTool( CommandLocator.fromServiceLocator(), BlockerLocator.fromServiceLocator(), out, true ); }
private void unexpected( RuntimeException e ) { failure( "unexpected error", e ); }
@Test public void shouldWorkWithSamePassword() throws Throwable { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "neo4j" ); assertAuthIniFile( "neo4j" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "neo4j" ); assertAuthIniFile( "neo4j" ); verify( out, times( 2 ) ).stdOutLine( "Changed password for user 'neo4j'." ); }
@Before public void setup() { File graphDir = new File( GraphDatabaseSettings.DEFAULT_DATABASE_NAME ); confDir = new File( graphDir, "conf" ); homeDir = new File( graphDir, "home" ); out = mock( OutsideWorld.class ); resetOutsideWorldMock(); tool = new AdminTool( CommandLocator.fromServiceLocator(), BlockerLocator.fromServiceLocator(), out, true ); }
private void failure( String message, Exception e ) { failure( message, e, STATUS_ERROR ); }
@Test void shouldNotPrintTheStacktraceWhenTheCommandThrowsARuntimeExceptionIfTheDebugFlagIsNotSet() { OutsideWorld outsideWorld = mock( OutsideWorld.class ); RuntimeException exception = new RuntimeException( "" ); AdminCommand command = args -> { throw exception; }; new AdminTool( cannedCommand( "exception", command ), new NullBlockerLocator(), outsideWorld, false ) .execute( null, null, "exception" ); verify( outsideWorld, never() ).printStacktrace( exception ); }
@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() ); }
private void failure( String message, Exception e, int code ) { if ( debug ) { outsideWorld.printStacktrace( e ); } failure( format( "%s: %s", message, e.getMessage() ), code ); }
@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 shouldSetPassword() throws Throwable { tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "abc" ); assertAuthIniFile( "abc" ); verify( out ).stdOutLine( "Changed password for user 'neo4j'." ); }
private void commandFailed( CommandFailed e ) { failure( "command failed", e, e.code() ); }
@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 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 ); }
private void badUsage( String message ) { outsideWorld.stdErrLine( message ); usage.print( outsideWorld::stdErrLine ); failure(); }
@Test void shouldNotBlockIfNoneOfTheBlockersBlock() throws CommandFailed, IncorrectUsage { AdminCommand command = mock( AdminCommand.class ); 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, falseBlocker, falseBlocker ) ); new AdminTool( cannedCommand( "command", command ), blockerLocator, new NullOutsideWorld(), false ) .execute( null, null, "command", "the", "other", "args" ); verify( command ).execute( new String[]{"the", "other", "args"} ); }
@Test public void shouldOverwrite() throws Throwable { insertUser( "jane", false ); insertUser( "janette", false ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "jane" ); assertAdminIniFile( "jane" ); tool.execute( homeDir.toPath(), confDir.toPath(), SET_ADMIN, "janette" ); assertAdminIniFile( "janette" ); verify( out ).stdOutLine( "default admin user set to 'jane'" ); verify( out ).stdOutLine( "default admin user set to 'janette'" ); }