Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Update MainTest and CypherShellTest
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Feb 5, 2020
1 parent a18f541 commit a18bcd6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void verifyDelegationOfConnectionMethods() throws CommandException {
CypherShell shell = new CypherShell(logger, mockedBoltStateHandler, mockedPrettyPrinter, new ShellParameterMap());

shell.connect(cc);
verify(mockedBoltStateHandler).connect(cc);
verify(mockedBoltStateHandler).connect(cc, null);

shell.isConnected();
verify(mockedBoltStateHandler).isConnected();
Expand Down
56 changes: 28 additions & 28 deletions cypher-shell/src/test/java/org/neo4j/shell/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,30 @@ public void nonEndedStringFails() throws Exception {
String inputString = "no newline";
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());

doThrow(authException).when(shell).connect(connectionConfig);
doThrow(authException).when(shell).connect(connectionConfig, null);

thrown.expectMessage("No text could be read, exiting");

Main main = new Main(inputStream, out);
main.connectMaybeInteractively(shell, connectionConfig, true, true);
verify(shell, times(1)).connect(connectionConfig);
verify(shell, times(1)).connect(connectionConfig, null);
}

@Test
public void unrelatedErrorDoesNotPrompt() throws Exception {
doThrow(new RuntimeException("bla")).when(shell).connect(connectionConfig);
doThrow(new RuntimeException("bla")).when(shell).connect(connectionConfig, null);

thrown.expect(RuntimeException.class);
thrown.expectMessage("bla");

Main main = new Main(mock(InputStream.class), out);
main.connectMaybeInteractively(shell, connectionConfig, true, true);
verify(shell, times(1)).connect(connectionConfig);
verify(shell, times(1)).connect(connectionConfig, null);
}

@Test
public void promptsForUsernameAndPasswordIfNoneGivenIfInteractive() throws Exception {
doThrow(authException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doNothing().when(shell).connect(connectionConfig, null);

String inputString = "bob\nsecret\n";
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
Expand All @@ -96,7 +96,7 @@ public void promptsForUsernameAndPasswordIfNoneGivenIfInteractive() throws Excep
assertEquals(String.format( "username: bob%npassword: ******%n" ), out);
verify(connectionConfig).setUsername("bob");
verify(connectionConfig).setPassword("secret");
verify(shell, times(2)).connect(connectionConfig);
verify(shell, times(2)).connect(connectionConfig, null);
}

@Test
Expand All @@ -106,7 +106,7 @@ public void promptsSilentlyForUsernameAndPasswordIfNoneGivenIfOutputRedirected()
return;
}

doThrow(authException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doNothing().when(shell).connect(connectionConfig, null);
doReturn("").doReturn("secret").when(connectionConfig).password();

String inputString = "bob\nsecret\n";
Expand All @@ -130,7 +130,7 @@ public void promptsSilentlyForUsernameAndPasswordIfNoneGivenIfOutputRedirected()
assertEquals("", out);
verify(connectionConfig).setUsername("bob");
verify(connectionConfig).setPassword("secret");
verify(shell, times(2)).connect(connectionConfig);
verify(shell, times(2)).connect(connectionConfig, null);
} finally {
System.setIn(stdIn);
System.setOut(stdOut);
Expand All @@ -139,7 +139,7 @@ public void promptsSilentlyForUsernameAndPasswordIfNoneGivenIfOutputRedirected()

@Test
public void doesNotPromptIfInputRedirected() throws Exception {
doThrow(authException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doNothing().when(shell).connect(connectionConfig, null);

String inputString = "bob\nsecret\n";
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
Expand All @@ -153,13 +153,13 @@ public void doesNotPromptIfInputRedirected() throws Exception {
main.connectMaybeInteractively(shell, connectionConfig, false, true);
fail("Expected auth exception");
} catch (AuthenticationException e) {
verify(shell, times(1)).connect(connectionConfig);
verify(shell, times(1)).connect(connectionConfig, null);
}
}

@Test
public void promptsForUserIfPassExistsIfInteractive() throws Exception {
doThrow(authException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doNothing().when(shell).connect(connectionConfig, null);
doReturn("secret").when(connectionConfig).password();

String inputString = "bob\n";
Expand All @@ -175,7 +175,7 @@ public void promptsForUserIfPassExistsIfInteractive() throws Exception {

assertEquals(out, String.format( "username: bob%n" ));
verify(connectionConfig).setUsername("bob");
verify(shell, times(2)).connect(connectionConfig);
verify(shell, times(2)).connect(connectionConfig, null);
}

@Test
Expand All @@ -185,7 +185,7 @@ public void promptsSilentlyForUserIfPassExistsIfOutputRedirected() throws Except
return;
}

doThrow(authException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doNothing().when(shell).connect(connectionConfig, null);
doReturn("secret").when(connectionConfig).password();

String inputString = "bob\n";
Expand All @@ -208,7 +208,7 @@ public void promptsSilentlyForUserIfPassExistsIfOutputRedirected() throws Except

assertEquals(out, "");
verify(connectionConfig).setUsername("bob");
verify(shell, times(2)).connect(connectionConfig);
verify(shell, times(2)).connect(connectionConfig, null);
} finally {
System.setIn(stdIn);
System.setOut(stdOut);
Expand All @@ -232,7 +232,7 @@ public void promptsForPassBeforeConnectIfUserExistsIfInteractive() throws Except

assertEquals(out, String.format("password: ******%n"));
verify(connectionConfig).setPassword("secret");
verify(shell, times(1)).connect(connectionConfig);
verify(shell, times(1)).connect(connectionConfig, null);
}

@Test
Expand Down Expand Up @@ -265,7 +265,7 @@ public void promptsSilentlyForPassIfUserExistsIfOutputRedirected() throws Except

assertEquals(out, "");
verify(connectionConfig).setPassword("secret");
verify(shell, times(1)).connect(connectionConfig);
verify(shell, times(1)).connect(connectionConfig, null);
} finally {
System.setIn(stdIn);
System.setOut(stdOut);
Expand All @@ -276,7 +276,7 @@ public void promptsSilentlyForPassIfUserExistsIfOutputRedirected() throws Except
public void promptsForNewPasswordIfPasswordChangeRequired() throws Exception {
// Use a real ConnectionConfig instead of the mock in this test
ConnectionConfig connectionConfig = new ConnectionConfig("", "", 0, "", "", false, "");
doThrow(authException).doThrow(passwordChangeRequiredException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doThrow(passwordChangeRequiredException).doNothing().when(shell).connect(connectionConfig, null);

String inputString = "bob\nsecret\nnewsecret\n";
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
Expand All @@ -293,15 +293,15 @@ public void promptsForNewPasswordIfPasswordChangeRequired() throws Exception {
assertEquals("bob", connectionConfig.username());
assertEquals("secret", connectionConfig.password());
assertEquals("newsecret", connectionConfig.newPassword());
verify(shell, times(3)).connect(connectionConfig);
verify(shell, times(3)).connect(connectionConfig, null);
verify(shell).changePassword(connectionConfig);
}

@Test
public void promptsForNewPasswordIfPasswordChangeRequiredCannotBeEmpty() throws Exception {
// Use a real ConnectionConfig instead of the mock in this test
ConnectionConfig connectionConfig = new ConnectionConfig("", "", 0, "", "", false, "");
doThrow(authException).doThrow(passwordChangeRequiredException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doThrow(passwordChangeRequiredException).doNothing().when(shell).connect(connectionConfig, null);

String inputString = "bob\nsecret\n\nnewsecret\n";
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
Expand All @@ -318,13 +318,13 @@ public void promptsForNewPasswordIfPasswordChangeRequiredCannotBeEmpty() throws
assertEquals("bob", connectionConfig.username());
assertEquals("secret", connectionConfig.password());
assertEquals("newsecret", connectionConfig.newPassword());
verify(shell, times(3)).connect(connectionConfig);
verify(shell, times(3)).connect(connectionConfig, null);
verify(shell).changePassword(connectionConfig);
}

@Test
public void promptsHandlesBang() throws Exception {
doThrow(authException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doNothing().when(shell).connect(connectionConfig, null);

String inputString = "bo!b\nsec!ret\n";
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
Expand All @@ -340,12 +340,12 @@ public void promptsHandlesBang() throws Exception {
assertEquals(String.format("username: bo!b%npassword: *******%n"), out);
verify(connectionConfig).setUsername("bo!b");
verify(connectionConfig).setPassword("sec!ret");
verify(shell, times(2)).connect(connectionConfig);
verify(shell, times(2)).connect(connectionConfig, null);
}

@Test
public void triesOnlyOnceIfUserPassExists() throws Exception {
doThrow(authException).doThrow(new RuntimeException("second try")).when(shell).connect(connectionConfig);
doThrow(authException).doThrow(new RuntimeException("second try")).when(shell).connect(connectionConfig, null);
doReturn("bob").when(connectionConfig).username();
doReturn("secret").when(connectionConfig).password();

Expand All @@ -361,13 +361,13 @@ public void triesOnlyOnceIfUserPassExists() throws Exception {
fail("Expected an exception");
} catch (Neo4jException e) {
assertEquals(authException.code(), e.code());
verify(shell, times(1)).connect(connectionConfig);
verify(shell, times(1)).connect(connectionConfig, null);
}
}

@Test
public void repromptsIfUserIsNotProvidedIfInteractive() throws Exception {
doThrow(authException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doNothing().when(shell).connect(connectionConfig, null);

String inputString = "\nbob\nsecret\n";
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
Expand All @@ -383,7 +383,7 @@ public void repromptsIfUserIsNotProvidedIfInteractive() throws Exception {
assertEquals(String.format( "username: %nusername cannot be empty%n%nusername: bob%npassword: ******%n"), out );
verify(connectionConfig).setUsername("bob");
verify(connectionConfig).setPassword("secret");
verify(shell, times(2)).connect(connectionConfig);
verify(shell, times(2)).connect(connectionConfig, null);
}

@Test
Expand All @@ -393,7 +393,7 @@ public void doesNotRepromptIfUserIsNotProvidedIfOutputRedirected() throws Except
return;
}

doThrow(authException).doNothing().when(shell).connect(connectionConfig);
doThrow(authException).doNothing().when(shell).connect(connectionConfig, null);

String inputString = "\nsecret\n";
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
Expand All @@ -416,7 +416,7 @@ public void doesNotRepromptIfUserIsNotProvidedIfOutputRedirected() throws Except
assertEquals("", out );
verify(connectionConfig).setUsername("");
verify(connectionConfig).setPassword("secret");
verify(shell, times(2)).connect(connectionConfig);
verify(shell, times(2)).connect(connectionConfig, null);
} finally {
System.setIn(stdIn);
System.setOut(stdOut);
Expand Down

0 comments on commit a18bcd6

Please sign in to comment.