Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit d4b0199

Browse files
authored
Merge pull request #155 from sherfert/1.2-database-env-var
Use NEO4J_DATABASE environment variable as the default.
2 parents 72fecb9 + 3ffed2d commit d4b0199

File tree

5 files changed

+65
-18
lines changed

5 files changed

+65
-18
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@ ext {
7777
jansiVersion = '1.13'
7878
jlineVersion = '2.14.6'
7979
mockitoVersion = '1.9.5'
80+
systemRulesVersion = '1.19.0'
8081
}

cypher-shell/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ dependencies {
4646
}
4747
testCompile "junit:junit:$junitVersion"
4848
testCompile "org.mockito:mockito-core:$mockitoVersion"
49+
testCompile "com.github.stefanbirkner:system-rules:$systemRulesVersion"
4950
testCompileOnly "com.google.code.findbugs:annotations:$findbugsVersion"
5051
}

cypher-shell/src/main/java/org/neo4j/shell/ConnectionConfig.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.neo4j.shell;
22

3-
import org.neo4j.driver.Config;
4-
53
import javax.annotation.Nonnull;
64

75
public class ConnectionConfig {
6+
public static final String USERNAME_ENV_VAR = "NEO4J_USERNAME";
7+
public static final String PASSWORD_ENV_VAR = "NEO4J_PASSWORD";
8+
public static final String DATABASE_ENV_VAR = "NEO4J_DATABASE";
9+
810
private final String scheme;
911
private final String host;
1012
private final int port;
@@ -13,23 +15,27 @@ public class ConnectionConfig {
1315
private String password;
1416
private String database;
1517

16-
public ConnectionConfig(@Nonnull String scheme, @Nonnull String host, int port,
17-
@Nonnull String username, @Nonnull String password, boolean encryption,
18+
public ConnectionConfig(@Nonnull String scheme,
19+
@Nonnull String host,
20+
int port,
21+
@Nonnull String username,
22+
@Nonnull String password,
23+
boolean encryption,
1824
@Nonnull String database) {
1925
this.host = host;
2026
this.port = port;
21-
this.username = fallbackToEnvVariable(username, "NEO4J_USERNAME");
22-
this.password = fallbackToEnvVariable(password, "NEO4J_PASSWORD");
27+
this.username = fallbackToEnvVariable(username, USERNAME_ENV_VAR);
28+
this.password = fallbackToEnvVariable(password, PASSWORD_ENV_VAR);
2329
this.encryption = encryption;
2430
this.scheme = scheme;
25-
this.database = database;
31+
this.database = fallbackToEnvVariable(database, DATABASE_ENV_VAR);
2632
}
2733

2834
/**
2935
* @return preferredValue if not empty, else the contents of the fallback environment variable
3036
*/
3137
@Nonnull
32-
static String fallbackToEnvVariable(@Nonnull String preferredValue, @Nonnull String fallbackEnvVar) {
38+
private static String fallbackToEnvVariable(@Nonnull String preferredValue, @Nonnull String fallbackEnvVar) {
3339
String result = System.getenv(fallbackEnvVar);
3440
if (result == null || !preferredValue.isEmpty()) {
3541
result = preferredValue;

cypher-shell/src/main/java/org/neo4j/shell/cli/CliArgHelper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import javax.annotation.Nonnull;
1414
import javax.annotation.Nullable;
1515

16+
import org.neo4j.shell.ConnectionConfig;
17+
1618
import static java.lang.String.format;
1719
import static org.neo4j.shell.cli.FailBehavior.FAIL_AT_END;
1820
import static org.neo4j.shell.cli.FailBehavior.FAIL_FAST;
@@ -129,17 +131,17 @@ private static ArgumentParser setupParser()
129131
.setDefault("bolt://localhost:7687");
130132
connGroup.addArgument("-u", "--username")
131133
.setDefault("")
132-
.help("username to connect as. Can also be specified using environment variable NEO4J_USERNAME");
134+
.help("username to connect as. Can also be specified using environment variable " + ConnectionConfig.USERNAME_ENV_VAR);
133135
connGroup.addArgument("-p", "--password")
134136
.setDefault("")
135-
.help("password to connect with. Can also be specified using environment variable NEO4J_PASSWORD");
137+
.help("password to connect with. Can also be specified using environment variable " + ConnectionConfig.PASSWORD_ENV_VAR);
136138
connGroup.addArgument("--encryption")
137139
.help("whether the connection to Neo4j should be encrypted; must be consistent with Neo4j's " +
138140
"configuration")
139141
.type(new BooleanArgumentType())
140142
.setDefault(true);
141143
connGroup.addArgument("-d", "--database")
142-
.help("database to connect to")
144+
.help("database to connect to. Can also be specified using environment variable " + ConnectionConfig.DATABASE_ENV_VAR)
143145
.setDefault("");
144146

145147
MutuallyExclusiveGroup failGroup = parser.addMutuallyExclusiveGroup();

cypher-shell/src/test/java/org/neo4j/shell/ConnectionConfigTest.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.neo4j.shell;
22

3+
import org.junit.Rule;
34
import org.junit.Test;
45
import org.neo4j.shell.log.Logger;
56

@@ -9,38 +10,74 @@
910
import static org.mockito.Mockito.mock;
1011
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
1112

13+
import org.junit.contrib.java.lang.system.EnvironmentVariables;
14+
1215
public class ConnectionConfigTest {
16+
17+
@Rule
18+
public final EnvironmentVariables environmentVariables
19+
= new EnvironmentVariables();
20+
1321
private Logger logger = mock(Logger.class);
1422
private ConnectionConfig config = new ConnectionConfig("bolt://", "localhost", 1, "bob",
15-
"pass", false, ABSENT_DB_NAME);
23+
"pass", false, "db");
24+
1625

1726
@Test
18-
public void scheme() throws Exception {
27+
public void scheme() {
1928
assertEquals("bolt://", config.scheme());
2029
}
2130

2231
@Test
23-
public void host() throws Exception {
32+
public void host() {
2433
assertEquals("localhost", config.host());
2534
}
2635

2736
@Test
28-
public void port() throws Exception {
37+
public void port() {
2938
assertEquals(1, config.port());
3039
}
3140

3241
@Test
33-
public void username() throws Exception {
42+
public void username() {
3443
assertEquals("bob", config.username());
3544
}
3645

3746
@Test
38-
public void password() throws Exception {
47+
public void usernameDefaultsToEnvironmentVar() {
48+
environmentVariables.set(ConnectionConfig.USERNAME_ENV_VAR, "alice");
49+
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt://", "localhost", 1, "",
50+
"", false, ABSENT_DB_NAME);
51+
assertEquals("alice", configWithEmptyParams.username());
52+
}
53+
54+
@Test
55+
public void password() {
3956
assertEquals("pass", config.password());
4057
}
4158

4259
@Test
43-
public void driverUrlDefaultScheme() throws Exception {
60+
public void passwordDefaultsToEnvironmentVar() {
61+
environmentVariables.set(ConnectionConfig.PASSWORD_ENV_VAR, "ssap");
62+
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt://", "localhost", 1, "",
63+
"", false, ABSENT_DB_NAME);
64+
assertEquals("ssap", configWithEmptyParams.password());
65+
}
66+
67+
@Test
68+
public void database() {
69+
assertEquals("db", config.database());
70+
}
71+
72+
@Test
73+
public void databaseDefaultsToEnvironmentVar() {
74+
environmentVariables.set(ConnectionConfig.DATABASE_ENV_VAR, "funnyDB");
75+
ConnectionConfig configWithEmptyParams = new ConnectionConfig("bolt://", "localhost", 1, "",
76+
"", false, ABSENT_DB_NAME);
77+
assertEquals("funnyDB", configWithEmptyParams.database());
78+
}
79+
@Test
80+
public void driverUrlDefaultScheme() {
4481
assertEquals("bolt://localhost:1", config.driverUrl());
4582
}
4683

0 commit comments

Comments
 (0)