Search in sources :

Example 1 with TransientException

use of org.neo4j.driver.exceptions.TransientException in project neo4j by neo4j.

the class MainIntegrationTest method switchingToUnavailableDatabaseIfInteractive.

@Test
public void switchingToUnavailableDatabaseIfInteractive() throws Exception {
    shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell));
    inputBuffer.put(String.format("neo4j%nneo%n").getBytes());
    assertEquals("", connectionConfig.username());
    assertEquals("", connectionConfig.password());
    // when
    main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
    // Multiple databases are only available from 4.0
    assumeTrue(majorVersion(shell.getServerVersion()) >= 4);
    // then
    // should be connected
    assertTrue(shell.isConnected());
    // should have prompted and set the username and password
    String expectedLoginOutput = format("username: neo4j%npassword: ***%n");
    assertEquals(expectedLoginOutput, baos.toString());
    assertEquals("neo4j", connectionConfig.username());
    assertEquals("neo", connectionConfig.password());
    // Stop the default database
    shell.execute(":use " + SYSTEM_DB_NAME);
    shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME);
    try {
        // Should get exception that database is unavailable when trying to connect
        shell.execute(":use " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME);
        fail("No exception thrown");
    } catch (TransientException | ServiceUnavailableException e) {
        expectDatabaseUnavailable(e, "neo4j");
    } finally {
        // Start the default database again
        ensureDefaultDatabaseStarted();
    }
}
Also used : TransientException(org.neo4j.driver.exceptions.TransientException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) CommandHelper(org.neo4j.shell.commands.CommandHelper) Test(org.junit.Test)

Example 2 with TransientException

use of org.neo4j.driver.exceptions.TransientException in project neo4j by neo4j.

the class MainIntegrationTest method doesNotStartWhenDefaultDatabaseUnavailableIfInteractive.

@Test
public void doesNotStartWhenDefaultDatabaseUnavailableIfInteractive() throws Exception {
    shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell));
    inputBuffer.put(String.format("neo4j%nneo%n").getBytes());
    assertEquals("", connectionConfig.username());
    assertEquals("", connectionConfig.password());
    // when
    main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
    // Multiple databases are only available from 4.0
    assumeTrue(majorVersion(shell.getServerVersion()) >= 4);
    // then
    // should be connected
    assertTrue(shell.isConnected());
    // should have prompted and set the username and password
    String expectedLoginOutput = format("username: neo4j%npassword: ***%n");
    assertEquals(expectedLoginOutput, baos.toString());
    assertEquals("neo4j", connectionConfig.username());
    assertEquals("neo", connectionConfig.password());
    // Stop the default database
    shell.execute(":use " + SYSTEM_DB_NAME);
    shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME);
    try {
        shell.disconnect();
        // Should get exception that database is unavailable when trying to connect
        main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
        fail("No exception thrown");
    } catch (TransientException | ServiceUnavailableException e) {
        expectDatabaseUnavailable(e, "neo4j");
    } finally {
        // Start the default database again
        ensureDefaultDatabaseStarted();
    }
}
Also used : TransientException(org.neo4j.driver.exceptions.TransientException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) CommandHelper(org.neo4j.shell.commands.CommandHelper) Test(org.junit.Test)

Example 3 with TransientException

use of org.neo4j.driver.exceptions.TransientException in project neo4j by neo4j.

the class InteractiveShellRunnerTest method testSwitchToUnavailableDatabase1.

@Test
public void testSwitchToUnavailableDatabase1() throws Exception {
    // given
    String input = ":use foo;\n";
    TestInteractiveShellRunner sr = setupInteractiveTestShellRunner(input);
    // when
    when(sr.mockedBoltStateHandler.getActualDatabaseAsReportedByServer()).thenReturn("foo");
    doThrow(new TransientException(DatabaseManager.DATABASE_UNAVAILABLE_ERROR_CODE, "Not available")).when(sr.mockedBoltStateHandler).setActiveDatabase("foo");
    sr.runner.runUntilEnd();
    // then
    assertThat(sr.output.toString(), containsString(format("myusername@foo%s> ", DATABASE_UNAVAILABLE_ERROR_PROMPT_TEXT)));
    assertThat(sr.error.toString(), containsString("Not available"));
}
Also used : TransientException(org.neo4j.driver.exceptions.TransientException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 4 with TransientException

use of org.neo4j.driver.exceptions.TransientException in project neo4j by neo4j.

the class MainIntegrationTest method switchingToUnavailableDefaultDatabaseIfInteractive.

@Test
public void switchingToUnavailableDefaultDatabaseIfInteractive() throws Exception {
    shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell));
    inputBuffer.put(String.format("neo4j%nneo%n").getBytes());
    assertEquals("", connectionConfig.username());
    assertEquals("", connectionConfig.password());
    // when
    main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
    // Multiple databases are only available from 4.0
    assumeTrue(majorVersion(shell.getServerVersion()) >= 4);
    // then
    // should be connected
    assertTrue(shell.isConnected());
    // should have prompted and set the username and password
    String expectedLoginOutput = format("username: neo4j%npassword: ***%n");
    assertEquals(expectedLoginOutput, baos.toString());
    assertEquals("neo4j", connectionConfig.username());
    assertEquals("neo", connectionConfig.password());
    // Stop the default database
    shell.execute(":use " + SYSTEM_DB_NAME);
    shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME);
    try {
        // Should get exception that database is unavailable when trying to connect
        shell.execute(":use");
        fail("No exception thrown");
    } catch (TransientException | ServiceUnavailableException e) {
        expectDatabaseUnavailable(e, "neo4j");
    } finally {
        // Start the default database again
        ensureDefaultDatabaseStarted();
    }
}
Also used : TransientException(org.neo4j.driver.exceptions.TransientException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) CommandHelper(org.neo4j.shell.commands.CommandHelper) Test(org.junit.Test)

Aggregations

CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 Test (org.junit.Test)4 TransientException (org.neo4j.driver.exceptions.TransientException)4 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)3 CommandHelper (org.neo4j.shell.commands.CommandHelper)3