|
14 | 14 |
|
15 | 15 | import org.neo4j.driver.exceptions.ClientException; |
16 | 16 | import org.neo4j.driver.exceptions.ServiceUnavailableException; |
| 17 | +import org.neo4j.driver.exceptions.TransientException; |
17 | 18 | import org.neo4j.shell.cli.CliArgs; |
18 | 19 | import org.neo4j.shell.commands.CommandHelper; |
19 | 20 | import org.neo4j.shell.exception.CommandException; |
|
27 | 28 | import static java.lang.String.format; |
28 | 29 | import static org.hamcrest.CoreMatchers.isA; |
29 | 30 | import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertFalse; |
30 | 32 | import static org.junit.Assert.assertNull; |
31 | 33 | import static org.junit.Assert.assertTrue; |
32 | 34 | import static org.junit.Assert.fail; |
| 35 | +import static org.junit.Assume.assumeTrue; |
33 | 36 | import static org.mockito.Matchers.any; |
34 | 37 | import static org.mockito.Mockito.mock; |
35 | 38 | import static org.mockito.Mockito.verify; |
@@ -102,6 +105,16 @@ private void ensureUser() throws Exception { |
102 | 105 | } |
103 | 106 | } |
104 | 107 |
|
| 108 | + private void ensureDefaultDatabaseStarted() throws Exception { |
| 109 | + CliArgs cliArgs = new CliArgs(); |
| 110 | + cliArgs.setUsername("neo4j", ""); |
| 111 | + cliArgs.setPassword("neo", ""); |
| 112 | + cliArgs.setDatabase("system"); |
| 113 | + ShellAndConnection sac = getShell(cliArgs); |
| 114 | + main.connectMaybeInteractively(sac.shell, sac.connectionConfig, true, false); |
| 115 | + sac.shell.execute("START DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME); |
| 116 | + } |
| 117 | + |
105 | 118 | @Test |
106 | 119 | public void promptsOnWrongAuthenticationIfInteractive() throws Exception { |
107 | 120 | // when |
@@ -239,7 +252,7 @@ public void wrongPortWithNeo4j() throws Exception |
239 | 252 | ConnectionConfig connectionConfig = sac.connectionConfig; |
240 | 253 |
|
241 | 254 | exception.expect( ServiceUnavailableException.class ); |
242 | | - exception.expectMessage( "Unable to connect to database, ensure the database is running and that there is a working network connection to it" ); |
| 255 | + // The error message here may be subject to change and is not stable across versions so let us not assert on it |
243 | 256 | main.connectMaybeInteractively( shell, connectionConfig, true, true ); |
244 | 257 | } |
245 | 258 |
|
@@ -374,6 +387,173 @@ public void shouldFailIfInputFileDoesntExistInteractively() throws Exception { |
374 | 387 | shell.execute( ":source what.cypher" ); |
375 | 388 | } |
376 | 389 |
|
| 390 | + @Test |
| 391 | + public void doesNotStartWhenDefaultDatabaseUnavailableIfInteractive() throws Exception { |
| 392 | + shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell)); |
| 393 | + inputBuffer.put(String.format("neo4j%nneo%n").getBytes()); |
| 394 | + |
| 395 | + assertEquals("", connectionConfig.username()); |
| 396 | + assertEquals("", connectionConfig.password()); |
| 397 | + |
| 398 | + // when |
| 399 | + main.connectMaybeInteractively(shell, connectionConfig, true, true); |
| 400 | + |
| 401 | + // Multiple databases are only available from 4.0 |
| 402 | + assumeTrue( majorVersion( shell.getServerVersion() ) >= 4 ); |
| 403 | + |
| 404 | + // then |
| 405 | + // should be connected |
| 406 | + assertTrue(shell.isConnected()); |
| 407 | + // should have prompted and set the username and password |
| 408 | + String expectedLoginOutput = format( "username: neo4j%npassword: ***%n" ); |
| 409 | + assertEquals(expectedLoginOutput, baos.toString()); |
| 410 | + assertEquals("neo4j", connectionConfig.username()); |
| 411 | + assertEquals("neo", connectionConfig.password()); |
| 412 | + |
| 413 | + // Stop the default database |
| 414 | + shell.execute(":use " + DatabaseManager.SYSTEM_DB_NAME); |
| 415 | + shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME); |
| 416 | + |
| 417 | + try { |
| 418 | + shell.disconnect(); |
| 419 | + |
| 420 | + // Should get exception that database is unavailable when trying to connect |
| 421 | + exception.expect(TransientException.class); |
| 422 | + exception.expectMessage("Database 'neo4j' is unavailable"); |
| 423 | + main.connectMaybeInteractively(shell, connectionConfig, true, true); |
| 424 | + |
| 425 | + // then |
| 426 | + assertFalse(shell.isConnected()); |
| 427 | + } finally { |
| 428 | + // Start the default database again |
| 429 | + ensureDefaultDatabaseStarted(); |
| 430 | + } |
| 431 | + } |
| 432 | + |
| 433 | + @Test |
| 434 | + public void startsAgainstSystemDatabaseWhenDefaultDatabaseUnavailableIfInteractive() throws Exception { |
| 435 | + shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell)); |
| 436 | + |
| 437 | + assertEquals("", connectionConfig.username()); |
| 438 | + assertEquals("", connectionConfig.password()); |
| 439 | + |
| 440 | + // when |
| 441 | + main.connectMaybeInteractively(shell, connectionConfig, true, true); |
| 442 | + |
| 443 | + // Multiple databases are only available from 4.0 |
| 444 | + assumeTrue( majorVersion( shell.getServerVersion() ) >= 4 ); |
| 445 | + |
| 446 | + // then |
| 447 | + // should be connected |
| 448 | + assertTrue(shell.isConnected()); |
| 449 | + // should have prompted and set the username and password |
| 450 | + String expectedLoginOutput = format( "username: neo4j%npassword: ***%n" ); |
| 451 | + assertEquals(expectedLoginOutput, baos.toString()); |
| 452 | + assertEquals("neo4j", connectionConfig.username()); |
| 453 | + assertEquals("neo", connectionConfig.password()); |
| 454 | + |
| 455 | + // Stop the default database |
| 456 | + shell.execute(":use " + DatabaseManager.SYSTEM_DB_NAME); |
| 457 | + shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME); |
| 458 | + |
| 459 | + try { |
| 460 | + shell.disconnect(); |
| 461 | + |
| 462 | + // Connect to system database |
| 463 | + CliArgs cliArgs = new CliArgs(); |
| 464 | + cliArgs.setUsername("neo4j", ""); |
| 465 | + cliArgs.setPassword("neo", ""); |
| 466 | + cliArgs.setDatabase("system"); |
| 467 | + ShellAndConnection sac = getShell(cliArgs); |
| 468 | + // Use the new shell and connection config from here on |
| 469 | + shell = sac.shell; |
| 470 | + connectionConfig = sac.connectionConfig; |
| 471 | + main.connectMaybeInteractively(shell, connectionConfig, true, false); |
| 472 | + |
| 473 | + // then |
| 474 | + assertTrue(shell.isConnected()); |
| 475 | + } finally { |
| 476 | + // Start the default database again |
| 477 | + ensureDefaultDatabaseStarted(); |
| 478 | + } |
| 479 | + } |
| 480 | + |
| 481 | + @Test |
| 482 | + public void switchingToUnavailableDatabaseIfInteractive() throws Exception { |
| 483 | + shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell)); |
| 484 | + inputBuffer.put(String.format("neo4j%nneo%n").getBytes()); |
| 485 | + |
| 486 | + assertEquals("", connectionConfig.username()); |
| 487 | + assertEquals("", connectionConfig.password()); |
| 488 | + |
| 489 | + // when |
| 490 | + main.connectMaybeInteractively(shell, connectionConfig, true, true); |
| 491 | + |
| 492 | + // Multiple databases are only available from 4.0 |
| 493 | + assumeTrue(majorVersion( shell.getServerVersion() ) >= 4); |
| 494 | + |
| 495 | + // then |
| 496 | + // should be connected |
| 497 | + assertTrue(shell.isConnected()); |
| 498 | + // should have prompted and set the username and password |
| 499 | + String expectedLoginOutput = format( "username: neo4j%npassword: ***%n" ); |
| 500 | + assertEquals(expectedLoginOutput, baos.toString()); |
| 501 | + assertEquals("neo4j", connectionConfig.username()); |
| 502 | + assertEquals("neo", connectionConfig.password()); |
| 503 | + |
| 504 | + // Stop the default database |
| 505 | + shell.execute(":use " + DatabaseManager.SYSTEM_DB_NAME); |
| 506 | + shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME); |
| 507 | + |
| 508 | + try { |
| 509 | + // Should get exception that database is unavailable when trying to connect |
| 510 | + exception.expect(TransientException.class); |
| 511 | + exception.expectMessage("Database 'neo4j' is unavailable"); |
| 512 | + shell.execute(":use " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME); |
| 513 | + } finally { |
| 514 | + // Start the default database again |
| 515 | + ensureDefaultDatabaseStarted(); |
| 516 | + } |
| 517 | + } |
| 518 | + |
| 519 | + @Test |
| 520 | + public void switchingToUnavailableDefaultDatabaseIfInteractive() throws Exception { |
| 521 | + shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell)); |
| 522 | + inputBuffer.put(String.format("neo4j%nneo%n").getBytes()); |
| 523 | + |
| 524 | + assertEquals("", connectionConfig.username()); |
| 525 | + assertEquals("", connectionConfig.password()); |
| 526 | + |
| 527 | + // when |
| 528 | + main.connectMaybeInteractively(shell, connectionConfig, true, true); |
| 529 | + |
| 530 | + // Multiple databases are only available from 4.0 |
| 531 | + assumeTrue(majorVersion( shell.getServerVersion() ) >= 4); |
| 532 | + |
| 533 | + // then |
| 534 | + // should be connected |
| 535 | + assertTrue(shell.isConnected()); |
| 536 | + // should have prompted and set the username and password |
| 537 | + String expectedLoginOutput = format( "username: neo4j%npassword: ***%n" ); |
| 538 | + assertEquals(expectedLoginOutput, baos.toString()); |
| 539 | + assertEquals("neo4j", connectionConfig.username()); |
| 540 | + assertEquals("neo", connectionConfig.password()); |
| 541 | + |
| 542 | + // Stop the default database |
| 543 | + shell.execute(":use " + DatabaseManager.SYSTEM_DB_NAME); |
| 544 | + shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME); |
| 545 | + |
| 546 | + try { |
| 547 | + // Should get exception that database is unavailable when trying to connect |
| 548 | + exception.expect(TransientException.class); |
| 549 | + exception.expectMessage("Database 'neo4j' is unavailable"); |
| 550 | + shell.execute(":use"); |
| 551 | + } finally { |
| 552 | + // Start the default database again |
| 553 | + ensureDefaultDatabaseStarted(); |
| 554 | + } |
| 555 | + } |
| 556 | + |
377 | 557 | private String executeFileNonInteractively(String filename) throws Exception { |
378 | 558 | return executeFileNonInteractively(filename, mock(Logger.class)); |
379 | 559 | } |
|
0 commit comments