|
| 1 | +package org.neo4j.shell.commands; |
| 2 | + |
| 3 | +import org.junit.After; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Rule; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.rules.ExpectedException; |
| 8 | + |
| 9 | +import org.neo4j.driver.exceptions.ClientException; |
| 10 | +import org.neo4j.shell.ConnectionConfig; |
| 11 | +import org.neo4j.shell.CypherShell; |
| 12 | +import org.neo4j.shell.StringLinePrinter; |
| 13 | +import org.neo4j.shell.cli.Format; |
| 14 | +import org.neo4j.shell.exception.CommandException; |
| 15 | +import org.neo4j.shell.prettyprint.PrettyConfig; |
| 16 | + |
| 17 | +import static org.hamcrest.CoreMatchers.is; |
| 18 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | +import static org.junit.Assume.assumeTrue; |
| 20 | +import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME; |
| 21 | +import static org.neo4j.shell.Versions.majorVersion; |
| 22 | + |
| 23 | +public class CypherShellMultiDatabaseIntegrationTest |
| 24 | +{ |
| 25 | + private static final String SYSTEM_DB_NAME = "system"; |
| 26 | + private static final String DEFAULT_DB_NAME = "neo4j"; |
| 27 | + |
| 28 | + @Rule |
| 29 | + public final ExpectedException thrown = ExpectedException.none(); |
| 30 | + |
| 31 | + private StringLinePrinter linePrinter = new StringLinePrinter(); |
| 32 | + private Command useCommand; |
| 33 | + private Command beginCommand; |
| 34 | + private Command rollbackCommand; |
| 35 | + private CypherShell shell; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setUp() throws Exception { |
| 39 | + linePrinter.clear(); |
| 40 | + shell = new CypherShell(linePrinter, new PrettyConfig(Format.PLAIN, true, 1000)); |
| 41 | + useCommand = new Use(shell); |
| 42 | + beginCommand = new Begin(shell); |
| 43 | + rollbackCommand = new Rollback(shell); |
| 44 | + |
| 45 | + shell.connect(new ConnectionConfig("bolt://", "localhost", 7687, "neo4j", "neo", true, ABSENT_DB_NAME)); |
| 46 | + |
| 47 | + // Multiple databases are only available from 4.0 |
| 48 | + assumeTrue( majorVersion( shell.getServerVersion() ) >= 4 ); |
| 49 | + } |
| 50 | + |
| 51 | + @After |
| 52 | + public void tearDown() throws Exception { |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void switchingToSystemDatabaseWorks() throws CommandException { |
| 57 | + useCommand.execute(SYSTEM_DB_NAME); |
| 58 | + |
| 59 | + assertThat(linePrinter.output(), is("")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void switchingToSystemDatabaseAndBackToDefaultWorks() throws CommandException { |
| 64 | + useCommand.execute(SYSTEM_DB_NAME); |
| 65 | + useCommand.execute(DEFAULT_DB_NAME); |
| 66 | + |
| 67 | + assertThat(linePrinter.output(), is("")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void switchingDatabaseInOpenTransactionShouldFail() throws CommandException { |
| 72 | + thrown.expect(CommandException.class); |
| 73 | + thrown.expectMessage("There is an open transaction."); |
| 74 | + |
| 75 | + beginCommand.execute(""); |
| 76 | + useCommand.execute("another_database"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void switchingDatabaseAfterRollbackTransactionWorks() throws CommandException { |
| 81 | + beginCommand.execute(""); |
| 82 | + rollbackCommand.execute(""); |
| 83 | + useCommand.execute(SYSTEM_DB_NAME); |
| 84 | + |
| 85 | + assertThat(linePrinter.output(), is("")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void switchingToNonExistingDatabaseShouldGiveErrorResponseFromServer() throws CommandException { |
| 90 | + thrown.expect(ClientException.class); |
| 91 | + thrown.expectMessage("The database requested does not exist."); |
| 92 | + |
| 93 | + useCommand.execute("this_database_name_does_not_exist_in_test_container"); |
| 94 | + } |
| 95 | +} |
0 commit comments