|
1 | 1 | package org.neo4j.shell; |
2 | 2 |
|
| 3 | +import org.junit.Ignore; |
| 4 | +import org.junit.Rule; |
3 | 5 | import org.junit.Test; |
4 | | -import org.neo4j.shell.cli.CliArgs; |
5 | | -import org.neo4j.shell.log.AnsiLogger; |
6 | | -import org.neo4j.shell.log.Logger; |
7 | | -import org.neo4j.shell.prettyprint.PrettyConfig; |
| 6 | +import org.junit.rules.ExpectedException; |
8 | 7 |
|
9 | 8 | import java.io.ByteArrayInputStream; |
10 | 9 | import java.io.ByteArrayOutputStream; |
11 | 10 | import java.io.InputStream; |
12 | 11 | import java.io.PrintStream; |
13 | 12 |
|
| 13 | +import org.neo4j.driver.exceptions.ServiceUnavailableException; |
| 14 | +import org.neo4j.shell.cli.CliArgs; |
| 15 | +import org.neo4j.shell.log.AnsiLogger; |
| 16 | +import org.neo4j.shell.log.Logger; |
| 17 | +import org.neo4j.shell.prettyprint.PrettyConfig; |
| 18 | + |
14 | 19 | import static org.junit.Assert.assertEquals; |
15 | 20 | import static org.junit.Assert.assertTrue; |
16 | 21 |
|
17 | | -public class MainIntegrationTest { |
| 22 | +public class MainIntegrationTest |
| 23 | +{ |
| 24 | + |
| 25 | + private static class ShellAndConnection |
| 26 | + { |
| 27 | + CypherShell shell; |
| 28 | + ConnectionConfig connectionConfig; |
| 29 | + |
| 30 | + ShellAndConnection( CypherShell shell, ConnectionConfig connectionConfig ) |
| 31 | + { |
| 32 | + this.shell = shell; |
| 33 | + this.connectionConfig = connectionConfig; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Rule |
| 38 | + public final ExpectedException exception = ExpectedException.none(); |
18 | 39 |
|
19 | 40 | @Test |
20 | | - public void connectInteractivelyPromptOnWrongAuthentication() throws Exception { |
| 41 | + public void connectInteractivelyPromptOnWrongAuthentication() throws Exception |
| 42 | + { |
21 | 43 | // given |
22 | | - // what the user inputs when prompted |
23 | | - String inputString = String.format( "neo4j%nneo%n" ); |
24 | | - InputStream inputStream = new ByteArrayInputStream(inputString.getBytes()); |
25 | | - |
26 | 44 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
27 | | - PrintStream ps = new PrintStream(baos); |
28 | | - |
29 | | - Main main = new Main(inputStream, ps); |
| 45 | + Main main = getMain( baos ); |
30 | 46 |
|
31 | 47 | CliArgs cliArgs = new CliArgs(); |
32 | | - cliArgs.setUsername("", ""); |
| 48 | + cliArgs.setUsername( "", "" ); |
33 | 49 | cliArgs.setPassword( "", "" ); |
34 | 50 |
|
35 | | - Logger logger = new AnsiLogger(cliArgs.getDebugMode()); |
36 | | - PrettyConfig prettyConfig = new PrettyConfig(cliArgs); |
37 | | - ConnectionConfig connectionConfig = new ConnectionConfig( |
38 | | - cliArgs.getScheme(), |
39 | | - cliArgs.getHost(), |
40 | | - cliArgs.getPort(), |
41 | | - cliArgs.getUsername(), |
42 | | - cliArgs.getPassword(), |
43 | | - cliArgs.getEncryption(), |
44 | | - cliArgs.getDatabase()); |
45 | | - |
46 | | - CypherShell shell = new CypherShell(logger, prettyConfig, true); |
| 51 | + ShellAndConnection sac = getShell( cliArgs ); |
| 52 | + CypherShell shell = sac.shell; |
| 53 | + ConnectionConfig connectionConfig = sac.connectionConfig; |
47 | 54 |
|
48 | 55 | // when |
49 | | - assertEquals("", connectionConfig.username()); |
50 | | - assertEquals("", connectionConfig.password()); |
| 56 | + assertEquals( "", connectionConfig.username() ); |
| 57 | + assertEquals( "", connectionConfig.password() ); |
51 | 58 |
|
52 | | - main.connectMaybeInteractively(shell, connectionConfig, true); |
| 59 | + main.connectMaybeInteractively( shell, connectionConfig, true ); |
53 | 60 |
|
54 | 61 | // then |
55 | 62 | // should be connected |
56 | | - assertTrue(shell.isConnected()); |
| 63 | + assertTrue( shell.isConnected() ); |
57 | 64 | // should have prompted and set the username and password |
58 | | - assertEquals("neo4j", connectionConfig.username()); |
59 | | - assertEquals("neo", connectionConfig.password()); |
| 65 | + assertEquals( "neo4j", connectionConfig.username() ); |
| 66 | + assertEquals( "neo", connectionConfig.password() ); |
60 | 67 |
|
61 | 68 | String out = baos.toString(); |
62 | 69 | assertEquals( String.format( "username: neo4j%npassword: ***%n" ), out ); |
63 | 70 | } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void wrongPortWithBolt() throws Exception |
| 74 | + { |
| 75 | + // given |
| 76 | + Main main = getMain( new ByteArrayOutputStream() ); |
| 77 | + |
| 78 | + CliArgs cliArgs = new CliArgs(); |
| 79 | + cliArgs.setScheme( "bolt://", "" ); |
| 80 | + cliArgs.setPort( 1234 ); |
| 81 | + |
| 82 | + ShellAndConnection sac = getShell( cliArgs ); |
| 83 | + CypherShell shell = sac.shell; |
| 84 | + ConnectionConfig connectionConfig = sac.connectionConfig; |
| 85 | + |
| 86 | + exception.expect( ServiceUnavailableException.class ); |
| 87 | + exception.expectMessage( "Unable to connect to localhost:1234, ensure the database is running and that there is a working network connection to it" ); |
| 88 | + main.connectMaybeInteractively( shell, connectionConfig, true ); |
| 89 | + } |
| 90 | + |
| 91 | + @Ignore |
| 92 | + public void wrongPortWithNeo4j() throws Exception |
| 93 | + { |
| 94 | + // given |
| 95 | + Main main = getMain( new ByteArrayOutputStream() ); |
| 96 | + |
| 97 | + CliArgs cliArgs = new CliArgs(); |
| 98 | + cliArgs.setScheme( "neo4j://", "" ); |
| 99 | + cliArgs.setPort( 1234 ); |
| 100 | + |
| 101 | + ShellAndConnection sac = getShell( cliArgs ); |
| 102 | + CypherShell shell = sac.shell; |
| 103 | + ConnectionConfig connectionConfig = sac.connectionConfig; |
| 104 | + |
| 105 | + exception.expect( ServiceUnavailableException.class ); |
| 106 | + exception.expectMessage( "Unable to connect to localhost:1234, ensure the database is running and that there is a working network connection to it" ); |
| 107 | + main.connectMaybeInteractively( shell, connectionConfig, true ); |
| 108 | + } |
| 109 | + |
| 110 | + private Main getMain( ByteArrayOutputStream baos ) |
| 111 | + { |
| 112 | + // what the user inputs when prompted |
| 113 | + String inputString = String.format( "neo4j%nneo%n" ); |
| 114 | + InputStream inputStream = new ByteArrayInputStream( inputString.getBytes() ); |
| 115 | + |
| 116 | + PrintStream ps = new PrintStream( baos ); |
| 117 | + |
| 118 | + return new Main( inputStream, ps ); |
| 119 | + } |
| 120 | + |
| 121 | + private ShellAndConnection getShell( CliArgs cliArgs ) |
| 122 | + { |
| 123 | + Logger logger = new AnsiLogger( cliArgs.getDebugMode() ); |
| 124 | + PrettyConfig prettyConfig = new PrettyConfig( cliArgs ); |
| 125 | + ConnectionConfig connectionConfig = new ConnectionConfig( |
| 126 | + cliArgs.getScheme(), |
| 127 | + cliArgs.getHost(), |
| 128 | + cliArgs.getPort(), |
| 129 | + cliArgs.getUsername(), |
| 130 | + cliArgs.getPassword(), |
| 131 | + cliArgs.getEncryption(), |
| 132 | + cliArgs.getDatabase() ); |
| 133 | + |
| 134 | + return new ShellAndConnection( new CypherShell( logger, prettyConfig, true ), connectionConfig ); |
| 135 | + } |
64 | 136 | } |
0 commit comments