-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multi-database support to HTTP driver #228
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
neo4j-jdbc-http/src/test/java/org/neo4j/jdbc/http/driver/CypherExecutorContainerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package org.neo4j.jdbc.http.driver; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URI; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.Assume; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.neo4j.driver.AuthTokens; | ||
import org.neo4j.driver.Driver; | ||
import org.neo4j.driver.GraphDatabase; | ||
import org.neo4j.driver.Session; | ||
import org.neo4j.driver.SessionConfig; | ||
import org.testcontainers.containers.Neo4jContainer; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.collection.IsEmptyCollection.empty; | ||
import static org.junit.Assert.assertThat; | ||
import static org.neo4j.driver.SessionConfig.forDatabase; | ||
|
||
public class CypherExecutorContainerIT { | ||
|
||
private static final String EXTRA_DATABASE_NAME = "extra"; | ||
|
||
private static final String USERNAME = "neo4j"; | ||
|
||
private static final String PASSWORD = "password"; | ||
|
||
private static Neo4jContainer<?> neo4jContainer; | ||
|
||
@BeforeClass | ||
public static void startDockerImage() { | ||
neo4jContainer = startEnterpriseDockerContainer(imageCoordinates(), USERNAME, PASSWORD); | ||
Assume.assumeTrue("neo4j container should be not null", neo4jContainer != null); | ||
Assume.assumeTrue("neo4j container should be up and running", neo4jContainer.isRunning()); | ||
} | ||
|
||
@BeforeClass | ||
public static void createExtraDatabase() throws Exception { | ||
createDatabase(EXTRA_DATABASE_NAME); | ||
} | ||
|
||
@AfterClass | ||
public static void dropExtraDatabase() throws Exception { | ||
dropDatabase(EXTRA_DATABASE_NAME); | ||
} | ||
|
||
@Test | ||
public void testDefaultDatabaseExecution() throws Exception { | ||
String host = neo4jContainer.getContainerIpAddress(); | ||
Integer port = neo4jContainer.getMappedPort(7474); | ||
CypherExecutor executor = new CypherExecutor(host, port, false, baseProperties()); | ||
|
||
Neo4jResponse neo4jResponse = executor | ||
.executeQuery(new Neo4jStatement("CREATE (:InDefault)", new HashMap<>(0), false)); | ||
|
||
assertThat(neo4jResponse.getErrors(), empty()); | ||
assertThat(neo4jResponse.getCode(), equalTo(200)); | ||
doInSession(forDatabase("neo4j"), (session) -> { | ||
long count = session | ||
.readTransaction(tx -> tx.run("MATCH (n:InDefault) RETURN COUNT(n) AS count").single().get("count") | ||
.asLong()); | ||
assertThat(count, equalTo(1L)); | ||
}); | ||
doInSession(forDatabase(EXTRA_DATABASE_NAME), (session) -> { | ||
long count = session | ||
.readTransaction(tx -> tx.run("MATCH (n:InDefault) RETURN COUNT(n) AS count").single().get("count") | ||
.asLong()); | ||
assertThat(count, equalTo(0L)); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testNonDefaultDatabaseExecution() throws Exception { | ||
String host = neo4jContainer.getContainerIpAddress(); | ||
Integer port = neo4jContainer.getMappedPort(7474); | ||
Properties properties = new Properties(); | ||
properties.putAll(baseProperties()); | ||
properties.setProperty("database", EXTRA_DATABASE_NAME); | ||
CypherExecutor executor = new CypherExecutor(host, port, false, properties); | ||
|
||
Neo4jResponse neo4jResponse = executor | ||
.executeQuery(new Neo4jStatement("CREATE (:InNonDefault)", new HashMap<>(0), false)); | ||
|
||
assertThat(neo4jResponse.getErrors(), empty()); | ||
assertThat(neo4jResponse.getCode(), equalTo(200)); | ||
doInSession(forDatabase("neo4j"), (session) -> { | ||
long count = session | ||
.readTransaction(tx -> tx.run("MATCH (n:InNonDefault) RETURN COUNT(n) AS count").single().get("count") | ||
.asLong()); | ||
assertThat(count, equalTo(0L)); | ||
}); | ||
doInSession(forDatabase(EXTRA_DATABASE_NAME), (session) -> { | ||
long count = session | ||
.readTransaction(tx -> tx.run("MATCH (n:InNonDefault) RETURN COUNT(n) AS count").single().get("count") | ||
.asLong()); | ||
assertThat(count, equalTo(1L)); | ||
}); | ||
} | ||
|
||
private static Neo4jContainer<?> startEnterpriseDockerContainer(String version, String username, String password) { | ||
try { | ||
Neo4jContainer<?> container = new Neo4jContainer<>(version) | ||
.withEnv("NEO4J_AUTH", String.format("%s/%s", username, password)) | ||
.withEnv("NEO4J_ACCEPT_LICENSE_AGREEMENT", "yes"); | ||
container.start(); | ||
return container; | ||
} | ||
catch (Exception exception) { | ||
exception.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
private static String imageCoordinates() { | ||
return String.format("neo4j:%s-enterprise", projectNeo4jVersion()); | ||
} | ||
|
||
private static String projectNeo4jVersion() { | ||
String filteredClasspathResource = "/neo4j.version"; | ||
List<String> lines = readLines(filteredClasspathResource); | ||
int lineCount = lines.size(); | ||
if (lineCount != 1) { | ||
throw new RuntimeException(String | ||
.format("%s should have only 1 (filtered) line, found: %d", filteredClasspathResource, lineCount)); | ||
} | ||
return lines.iterator().next(); | ||
} | ||
|
||
private static List<String> readLines(String classpathResource) { | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(CypherExecutorContainerIT.class | ||
.getResourceAsStream(classpathResource)))) { | ||
|
||
return reader.lines().collect(Collectors.toList()); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static void createDatabase(String databaseName) throws Exception { | ||
doInSession(forDatabase("system"), (Session session) -> session | ||
.writeTransaction((tx) -> tx.run(String.format("CREATE DATABASE %s", databaseName)))); | ||
} | ||
|
||
private static void dropDatabase(String databaseName) throws Exception { | ||
doInSession(forDatabase("system"), (Session session) -> session | ||
.writeTransaction((tx) -> tx.run(String.format("DROP DATABASE %s", databaseName)))); | ||
} | ||
|
||
private static void doInSession(SessionConfig sessionConfig, Consumer<Session> sessionConsumer) throws Exception { | ||
try (Driver driver = GraphDatabase | ||
.driver(new URI(neo4jContainer.getBoltUrl()), AuthTokens.basic(USERNAME, PASSWORD)); | ||
Session session = driver.session(sessionConfig)) { | ||
|
||
sessionConsumer.accept(session); | ||
} | ||
} | ||
|
||
private static Properties baseProperties() { | ||
Properties properties = new Properties(); | ||
properties.setProperty("userAgent", "Integration test"); | ||
properties.setProperty("user", USERNAME); | ||
properties.setProperty("password", PASSWORD); | ||
return properties; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
${neo4j.version} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it appears there is no need for escaping, as database names can only be made of:
"simple ascii characters, numbers, dots and dashes"
and these characters are not URI special characters.