Merge pull request #22301 from meistermeier
* pr/22301: Polish "Add auto-configuration for Neo4j driver" Add auto-configuration for Neo4j driver Closes gh-22301
This commit is contained in:
@@ -80,8 +80,8 @@ public class DocumentConfigurationProperties extends DefaultTask {
|
||||
.addSection("security").withKeyPrefixes("spring.security", "spring.ldap", "spring.session")
|
||||
.addSection("data-migration").withKeyPrefixes("spring.flyway", "spring.liquibase").addSection("data")
|
||||
.withKeyPrefixes("spring.couchbase", "spring.elasticsearch", "spring.h2", "spring.influx",
|
||||
"spring.mongodb", "spring.redis", "spring.dao", "spring.data", "spring.datasource",
|
||||
"spring.jooq", "spring.jdbc", "spring.jpa", "spring.r2dbc")
|
||||
"spring.mongodb", "spring.neo4j", "spring.redis", "spring.dao", "spring.data",
|
||||
"spring.datasource", "spring.jooq", "spring.jdbc", "spring.jpa", "spring.r2dbc")
|
||||
.addOverride("spring.datasource.dbcp2",
|
||||
"Commons DBCP2 specific settings bound to an instance of DBCP2's BasicDataSource")
|
||||
.addOverride("spring.datasource.tomcat",
|
||||
|
||||
@@ -180,6 +180,7 @@ dependencies {
|
||||
testImplementation("org.testcontainers:couchbase")
|
||||
testImplementation("org.testcontainers:elasticsearch")
|
||||
testImplementation("org.testcontainers:junit-jupiter")
|
||||
testImplementation("org.testcontainers:neo4j")
|
||||
testImplementation("org.testcontainers:testcontainers")
|
||||
testImplementation("org.yaml:snakeyaml")
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.neo4j;
|
||||
|
||||
import org.neo4j.driver.Config;
|
||||
import org.neo4j.driver.Config.ConfigBuilder;
|
||||
|
||||
/**
|
||||
* Callback interface that can be implemented by beans wishing to customize the
|
||||
* {@link Config} via a {@link ConfigBuilder} whilst retaining default auto-configuration.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.4.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ConfigBuilderCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the {@link ConfigBuilder}.
|
||||
* @param configBuilder the {@link ConfigBuilder} to customize
|
||||
*/
|
||||
void customize(ConfigBuilder configBuilder);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.neo4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.neo4j.driver.AuthToken;
|
||||
import org.neo4j.driver.AuthTokens;
|
||||
import org.neo4j.driver.Config;
|
||||
import org.neo4j.driver.Config.TrustStrategy;
|
||||
import org.neo4j.driver.Driver;
|
||||
import org.neo4j.driver.GraphDatabase;
|
||||
import org.neo4j.driver.internal.Scheme;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties.Pool;
|
||||
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties.Security;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Neo4j.
|
||||
*
|
||||
* @author Michael J. Simons
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.4.0
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(Driver.class)
|
||||
@EnableConfigurationProperties(Neo4jProperties.class)
|
||||
public class Neo4jAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public Driver neo4jDriver(Neo4jProperties properties,
|
||||
ObjectProvider<ConfigBuilderCustomizer> configBuilderCustomizers) {
|
||||
AuthToken authToken = mapAuthToken(properties.getAuthentication());
|
||||
Config config = mapDriverConfig(properties,
|
||||
configBuilderCustomizers.orderedStream().collect(Collectors.toList()));
|
||||
return GraphDatabase.driver(properties.getUri(), authToken, config);
|
||||
}
|
||||
|
||||
AuthToken mapAuthToken(Neo4jProperties.Authentication authentication) {
|
||||
String username = authentication.getUsername();
|
||||
String password = authentication.getPassword();
|
||||
String kerberosTicket = authentication.getKerberosTicket();
|
||||
String realm = authentication.getRealm();
|
||||
|
||||
boolean hasUsername = StringUtils.hasText(username);
|
||||
boolean hasPassword = StringUtils.hasText(password);
|
||||
boolean hasKerberosTicket = StringUtils.hasText(kerberosTicket);
|
||||
|
||||
if (hasUsername && hasKerberosTicket) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"Cannot specify both username ('%s') and kerberos ticket ('%s')", username, kerberosTicket));
|
||||
}
|
||||
if (hasUsername && hasPassword) {
|
||||
return AuthTokens.basic(username, password, realm);
|
||||
}
|
||||
if (hasKerberosTicket) {
|
||||
return AuthTokens.kerberos(kerberosTicket);
|
||||
}
|
||||
return AuthTokens.none();
|
||||
}
|
||||
|
||||
Config mapDriverConfig(Neo4jProperties properties, List<ConfigBuilderCustomizer> customizers) {
|
||||
Config.ConfigBuilder builder = Config.builder();
|
||||
configurePoolSettings(builder, properties.getPool());
|
||||
URI uri = properties.getUri();
|
||||
String scheme = (uri != null) ? uri.getScheme() : "bolt";
|
||||
configureDriverSettings(builder, properties, isSimpleScheme(scheme));
|
||||
builder.withLogging(new Neo4jSpringJclLogging());
|
||||
customizers.forEach((customizer) -> customizer.customize(builder));
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private boolean isSimpleScheme(String scheme) {
|
||||
String lowerCaseScheme = scheme.toLowerCase(Locale.ENGLISH);
|
||||
try {
|
||||
Scheme.validateScheme(lowerCaseScheme);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
throw new IllegalArgumentException(String.format("'%s' is not a supported scheme.", scheme));
|
||||
}
|
||||
return lowerCaseScheme.equals("bolt") || lowerCaseScheme.equals("neo4j");
|
||||
}
|
||||
|
||||
private void configurePoolSettings(Config.ConfigBuilder builder, Pool pool) {
|
||||
if (pool.isLogLeakedSessions()) {
|
||||
builder.withLeakedSessionsLogging();
|
||||
}
|
||||
builder.withMaxConnectionPoolSize(pool.getMaxConnectionPoolSize());
|
||||
Duration idleTimeBeforeConnectionTest = pool.getIdleTimeBeforeConnectionTest();
|
||||
if (idleTimeBeforeConnectionTest != null) {
|
||||
builder.withConnectionLivenessCheckTimeout(idleTimeBeforeConnectionTest.toMillis(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
builder.withMaxConnectionLifetime(pool.getMaxConnectionLifetime().toMillis(), TimeUnit.MILLISECONDS);
|
||||
builder.withConnectionAcquisitionTimeout(pool.getConnectionAcquisitionTimeout().toMillis(),
|
||||
TimeUnit.MILLISECONDS);
|
||||
if (pool.isMetricsEnabled()) {
|
||||
builder.withDriverMetrics();
|
||||
}
|
||||
else {
|
||||
builder.withoutDriverMetrics();
|
||||
}
|
||||
}
|
||||
|
||||
private void configureDriverSettings(Config.ConfigBuilder builder, Neo4jProperties properties,
|
||||
boolean withEncryptionAndTrustSettings) {
|
||||
if (withEncryptionAndTrustSettings) {
|
||||
applyEncryptionAndTrustSettings(builder, properties.getSecurity());
|
||||
}
|
||||
builder.withConnectionTimeout(properties.getConnectionTimeout().toMillis(), TimeUnit.MILLISECONDS);
|
||||
builder.withMaxTransactionRetryTime(properties.getMaxTransactionRetryTime().toMillis(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private void applyEncryptionAndTrustSettings(Config.ConfigBuilder builder,
|
||||
Neo4jProperties.Security securityProperties) {
|
||||
if (securityProperties.isEncrypted()) {
|
||||
builder.withEncryption();
|
||||
}
|
||||
else {
|
||||
builder.withoutEncryption();
|
||||
}
|
||||
builder.withTrustStrategy(mapTrustStrategy(securityProperties));
|
||||
}
|
||||
|
||||
private Config.TrustStrategy mapTrustStrategy(Neo4jProperties.Security securityProperties) {
|
||||
String propertyName = "spring.neo4j.security.trust-strategy";
|
||||
Security.TrustStrategy strategy = securityProperties.getTrustStrategy();
|
||||
TrustStrategy trustStrategy = createTrustStrategy(securityProperties, propertyName, strategy);
|
||||
if (securityProperties.isHostnameVerificationEnabled()) {
|
||||
trustStrategy.withHostnameVerification();
|
||||
}
|
||||
else {
|
||||
trustStrategy.withoutHostnameVerification();
|
||||
}
|
||||
return trustStrategy;
|
||||
}
|
||||
|
||||
private TrustStrategy createTrustStrategy(Neo4jProperties.Security securityProperties, String propertyName,
|
||||
Security.TrustStrategy strategy) {
|
||||
switch (strategy) {
|
||||
case TRUST_ALL_CERTIFICATES:
|
||||
return TrustStrategy.trustAllCertificates();
|
||||
case TRUST_SYSTEM_CA_SIGNED_CERTIFICATES:
|
||||
return TrustStrategy.trustSystemCertificates();
|
||||
case TRUST_CUSTOM_CA_SIGNED_CERTIFICATES:
|
||||
File certFile = securityProperties.getCertFile();
|
||||
if (certFile == null || !certFile.isFile()) {
|
||||
throw new InvalidConfigurationPropertyValueException(propertyName, strategy.name(),
|
||||
"Configured trust strategy requires a certificate file.");
|
||||
}
|
||||
return TrustStrategy.trustCustomCertificateSignedBy(certFile);
|
||||
default:
|
||||
throw new InvalidConfigurationPropertyValueException(propertyName, strategy.name(), "Unknown strategy.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.neo4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Configuration properties for Neo4j.
|
||||
*
|
||||
* @author Michael J. Simons
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.4.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.neo4j")
|
||||
public class Neo4jProperties {
|
||||
|
||||
/**
|
||||
* URI used by the driver.
|
||||
*/
|
||||
private URI uri = URI.create("bolt://localhost:7687");
|
||||
|
||||
/**
|
||||
* Timeout for borrowing connections from the pool.
|
||||
*/
|
||||
private Duration connectionTimeout = Duration.ofSeconds(30);
|
||||
|
||||
/**
|
||||
* Maximum time transactions are allowed to retry.
|
||||
*/
|
||||
private Duration maxTransactionRetryTime = Duration.ofSeconds(30);
|
||||
|
||||
private final Authentication authentication = new Authentication();
|
||||
|
||||
private final Pool pool = new Pool();
|
||||
|
||||
private final Security security = new Security();
|
||||
|
||||
public URI getUri() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
public void setUri(URI uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public Duration getConnectionTimeout() {
|
||||
return this.connectionTimeout;
|
||||
}
|
||||
|
||||
public void setConnectionTimeout(Duration connectionTimeout) {
|
||||
this.connectionTimeout = connectionTimeout;
|
||||
}
|
||||
|
||||
public Duration getMaxTransactionRetryTime() {
|
||||
return this.maxTransactionRetryTime;
|
||||
}
|
||||
|
||||
public void setMaxTransactionRetryTime(Duration maxTransactionRetryTime) {
|
||||
this.maxTransactionRetryTime = maxTransactionRetryTime;
|
||||
}
|
||||
|
||||
public Authentication getAuthentication() {
|
||||
return this.authentication;
|
||||
}
|
||||
|
||||
public Pool getPool() {
|
||||
return this.pool;
|
||||
}
|
||||
|
||||
public Security getSecurity() {
|
||||
return this.security;
|
||||
}
|
||||
|
||||
public static class Authentication {
|
||||
|
||||
/**
|
||||
* Login user of the server.
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* Login password of the server.
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Realm to connect to.
|
||||
*/
|
||||
private String realm;
|
||||
|
||||
/**
|
||||
* Kerberos ticket for connecting to the database. Mutual exclusive with a given
|
||||
* username.
|
||||
*/
|
||||
private String kerberosTicket;
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getRealm() {
|
||||
return this.realm;
|
||||
}
|
||||
|
||||
public void setRealm(String realm) {
|
||||
this.realm = realm;
|
||||
}
|
||||
|
||||
public String getKerberosTicket() {
|
||||
return this.kerberosTicket;
|
||||
}
|
||||
|
||||
public void setKerberosTicket(String kerberosTicket) {
|
||||
this.kerberosTicket = kerberosTicket;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Pool {
|
||||
|
||||
/**
|
||||
* Whether to enable metrics.
|
||||
*/
|
||||
private boolean metricsEnabled = false;
|
||||
|
||||
/**
|
||||
* Whether to log leaked sessions.
|
||||
*/
|
||||
private boolean logLeakedSessions = false;
|
||||
|
||||
/**
|
||||
* Maximum amount of connections in the connection pool towards a single database.
|
||||
*/
|
||||
private int maxConnectionPoolSize = 100;
|
||||
|
||||
/**
|
||||
* Pooled connections that have been idle in the pool for longer than this
|
||||
* threshold will be tested before they are used again.
|
||||
*/
|
||||
private Duration idleTimeBeforeConnectionTest;
|
||||
|
||||
/**
|
||||
* Pooled connections older than this threshold will be closed and removed from
|
||||
* the pool.
|
||||
*/
|
||||
private Duration maxConnectionLifetime = Duration.ofHours(1);
|
||||
|
||||
/**
|
||||
* Acquisition of new connections will be attempted for at most configured
|
||||
* timeout.
|
||||
*/
|
||||
private Duration connectionAcquisitionTimeout = Duration.ofSeconds(60);
|
||||
|
||||
public boolean isLogLeakedSessions() {
|
||||
return this.logLeakedSessions;
|
||||
}
|
||||
|
||||
public void setLogLeakedSessions(boolean logLeakedSessions) {
|
||||
this.logLeakedSessions = logLeakedSessions;
|
||||
}
|
||||
|
||||
public int getMaxConnectionPoolSize() {
|
||||
return this.maxConnectionPoolSize;
|
||||
}
|
||||
|
||||
public void setMaxConnectionPoolSize(int maxConnectionPoolSize) {
|
||||
this.maxConnectionPoolSize = maxConnectionPoolSize;
|
||||
}
|
||||
|
||||
public Duration getIdleTimeBeforeConnectionTest() {
|
||||
return this.idleTimeBeforeConnectionTest;
|
||||
}
|
||||
|
||||
public void setIdleTimeBeforeConnectionTest(Duration idleTimeBeforeConnectionTest) {
|
||||
this.idleTimeBeforeConnectionTest = idleTimeBeforeConnectionTest;
|
||||
}
|
||||
|
||||
public Duration getMaxConnectionLifetime() {
|
||||
return this.maxConnectionLifetime;
|
||||
}
|
||||
|
||||
public void setMaxConnectionLifetime(Duration maxConnectionLifetime) {
|
||||
this.maxConnectionLifetime = maxConnectionLifetime;
|
||||
}
|
||||
|
||||
public Duration getConnectionAcquisitionTimeout() {
|
||||
return this.connectionAcquisitionTimeout;
|
||||
}
|
||||
|
||||
public void setConnectionAcquisitionTimeout(Duration connectionAcquisitionTimeout) {
|
||||
this.connectionAcquisitionTimeout = connectionAcquisitionTimeout;
|
||||
}
|
||||
|
||||
public boolean isMetricsEnabled() {
|
||||
return this.metricsEnabled;
|
||||
}
|
||||
|
||||
public void setMetricsEnabled(boolean metricsEnabled) {
|
||||
this.metricsEnabled = metricsEnabled;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Security {
|
||||
|
||||
/**
|
||||
* Whether the driver should use encrypted traffic.
|
||||
*/
|
||||
private boolean encrypted = false;
|
||||
|
||||
/**
|
||||
* Trust strategy to use.
|
||||
*/
|
||||
private TrustStrategy trustStrategy = TrustStrategy.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES;
|
||||
|
||||
/**
|
||||
* Path to the file that holds the trusted certificates.
|
||||
*/
|
||||
private File certFile;
|
||||
|
||||
/**
|
||||
* Whether hostname verification is required.
|
||||
*/
|
||||
private boolean hostnameVerificationEnabled = true;
|
||||
|
||||
public boolean isEncrypted() {
|
||||
return this.encrypted;
|
||||
}
|
||||
|
||||
public void setEncrypted(boolean encrypted) {
|
||||
this.encrypted = encrypted;
|
||||
}
|
||||
|
||||
public TrustStrategy getTrustStrategy() {
|
||||
return this.trustStrategy;
|
||||
}
|
||||
|
||||
public void setTrustStrategy(TrustStrategy trustStrategy) {
|
||||
this.trustStrategy = trustStrategy;
|
||||
}
|
||||
|
||||
public File getCertFile() {
|
||||
return this.certFile;
|
||||
}
|
||||
|
||||
public void setCertFile(File certFile) {
|
||||
this.certFile = certFile;
|
||||
}
|
||||
|
||||
public boolean isHostnameVerificationEnabled() {
|
||||
return this.hostnameVerificationEnabled;
|
||||
}
|
||||
|
||||
public void setHostnameVerificationEnabled(boolean hostnameVerificationEnabled) {
|
||||
this.hostnameVerificationEnabled = hostnameVerificationEnabled;
|
||||
}
|
||||
|
||||
public enum TrustStrategy {
|
||||
|
||||
/**
|
||||
* Trust all certificates.
|
||||
*/
|
||||
TRUST_ALL_CERTIFICATES,
|
||||
|
||||
/**
|
||||
* Trust certificates that are signed by a trusted certificate.
|
||||
*/
|
||||
TRUST_CUSTOM_CA_SIGNED_CERTIFICATES,
|
||||
|
||||
/**
|
||||
* Trust certificates that can be verified through the local system store.
|
||||
*/
|
||||
TRUST_SYSTEM_CA_SIGNED_CERTIFICATES
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.neo4j;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.neo4j.driver.Logger;
|
||||
import org.neo4j.driver.Logging;
|
||||
|
||||
/**
|
||||
* Shim to use Spring JCL implementation, delegating all the hard work of deciding the
|
||||
* underlying system to Spring and Spring Boot.
|
||||
*
|
||||
* @author Michael J. Simons
|
||||
*/
|
||||
class Neo4jSpringJclLogging implements Logging {
|
||||
|
||||
/**
|
||||
* This prefix gets added to the log names the driver requests to add some namespace
|
||||
* around it in a bigger application scenario.
|
||||
*/
|
||||
private static final String AUTOMATIC_PREFIX = "org.neo4j.driver.";
|
||||
|
||||
@Override
|
||||
public Logger getLog(String name) {
|
||||
String requestedLog = name;
|
||||
if (!requestedLog.startsWith(AUTOMATIC_PREFIX)) {
|
||||
requestedLog = AUTOMATIC_PREFIX + name;
|
||||
}
|
||||
Log springJclLog = LogFactory.getLog(requestedLog);
|
||||
return new SpringJclLogger(springJclLog);
|
||||
}
|
||||
|
||||
private static final class SpringJclLogger implements Logger {
|
||||
|
||||
private final Log delegate;
|
||||
|
||||
SpringJclLogger(Log delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String message, Throwable cause) {
|
||||
this.delegate.error(message, cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String format, Object... params) {
|
||||
this.delegate.info(String.format(format, params));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String format, Object... params) {
|
||||
this.delegate.warn(String.format(format, params));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String message, Throwable cause) {
|
||||
this.delegate.warn(message, cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(String format, Object... params) {
|
||||
if (isDebugEnabled()) {
|
||||
this.delegate.debug(String.format(format, params));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trace(String format, Object... params) {
|
||||
if (isTraceEnabled()) {
|
||||
this.delegate.trace(String.format(format, params));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTraceEnabled() {
|
||||
return this.delegate.isTraceEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDebugEnabled() {
|
||||
return this.delegate.isDebugEnabled();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Auto-configuration for Neo4j.
|
||||
*/
|
||||
package org.springframework.boot.autoconfigure.neo4j;
|
||||
@@ -100,6 +100,7 @@ org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfigura
|
||||
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.neo4j;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.neo4j.driver.Driver;
|
||||
import org.neo4j.driver.Result;
|
||||
import org.neo4j.driver.Session;
|
||||
import org.neo4j.driver.Transaction;
|
||||
import org.testcontainers.containers.Neo4jContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link Neo4jAutoConfiguration}.
|
||||
*
|
||||
* @author Michael J. Simons
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@SpringBootTest
|
||||
@Testcontainers(disabledWithoutDocker = true)
|
||||
class Neo4jAutoConfigurationIntegrationTests {
|
||||
|
||||
@Container
|
||||
private static final Neo4jContainer<?> neo4jServer = new Neo4jContainer<>("neo4j:4.0");
|
||||
|
||||
@DynamicPropertySource
|
||||
static void neo4jProperties(DynamicPropertyRegistry registry) {
|
||||
registry.add("spring.neo4j.uri", neo4jServer::getBoltUrl);
|
||||
registry.add("spring.neo4j.authentication.username", () -> "neo4j");
|
||||
registry.add("spring.neo4j.authentication.password", neo4jServer::getAdminPassword);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Driver driver;
|
||||
|
||||
@Test
|
||||
void driverCanHandleRequest() {
|
||||
try (Session session = this.driver.session(); Transaction tx = session.beginTransaction()) {
|
||||
Result statementResult = tx.run("MATCH (n:Thing) RETURN n LIMIT 1");
|
||||
assertThat(statementResult.hasNext()).isFalse();
|
||||
tx.commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ImportAutoConfiguration(Neo4jAutoConfiguration.class)
|
||||
static class TestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.neo4j;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.neo4j.driver.AuthToken;
|
||||
import org.neo4j.driver.AuthTokens;
|
||||
import org.neo4j.driver.Config;
|
||||
import org.neo4j.driver.Config.ConfigBuilder;
|
||||
import org.neo4j.driver.Driver;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties.Authentication;
|
||||
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties.Security.TrustStrategy;
|
||||
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link Neo4jAutoConfiguration}.
|
||||
*
|
||||
* @author Michael J. Simons
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class Neo4jAutoConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(Neo4jAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
void driverNotConfiguredWithoutDriverApi() {
|
||||
this.contextRunner.withPropertyValues("spring.neo4j.uri=bolt://localhost:4711")
|
||||
.withClassLoader(new FilteredClassLoader(Driver.class))
|
||||
.run((ctx) -> assertThat(ctx).doesNotHaveBean(Driver.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void driverShouldNotRequireUri() {
|
||||
this.contextRunner.run((ctx) -> assertThat(ctx).hasSingleBean(Driver.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void driverShouldInvokeConfigBuilderCustomizers() {
|
||||
this.contextRunner.withPropertyValues("spring.neo4j.uri=bolt://localhost:4711")
|
||||
.withBean(ConfigBuilderCustomizer.class, () -> ConfigBuilder::withEncryption)
|
||||
.run((ctx) -> assertThat(ctx.getBean(Driver.class).isEncrypted()).isTrue());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "bolt", "neo4j" })
|
||||
void uriWithSimpleSchemeAreDetected(String scheme) {
|
||||
this.contextRunner.withPropertyValues("spring.neo4j.uri=" + scheme + "://localhost:4711").run((ctx) -> {
|
||||
assertThat(ctx).hasSingleBean(Driver.class);
|
||||
assertThat(ctx.getBean(Driver.class).isEncrypted()).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "bolt+s", "bolt+ssc", "neo4j+s", "neo4j+ssc" })
|
||||
void uriWithAdvancedSchemesAreDetected(String scheme) {
|
||||
this.contextRunner.withPropertyValues("spring.neo4j.uri=" + scheme + "://localhost:4711").run((ctx) -> {
|
||||
assertThat(ctx).hasSingleBean(Driver.class);
|
||||
Driver driver = ctx.getBean(Driver.class);
|
||||
assertThat(driver.isEncrypted()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "bolt+routing", "bolt+x", "neo4j+wth" })
|
||||
void uriWithInvalidSchemesAreDetected(String invalidScheme) {
|
||||
this.contextRunner.withPropertyValues("spring.neo4j.uri=" + invalidScheme + "://localhost:4711")
|
||||
.run((ctx) -> assertThat(ctx).hasFailed().getFailure()
|
||||
.hasMessageContaining("'%s' is not a supported scheme.", invalidScheme));
|
||||
}
|
||||
|
||||
@Test
|
||||
void connectionTimeout() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.setConnectionTimeout(Duration.ofMillis(500));
|
||||
assertThat(mapDriverConfig(properties).connectionTimeoutMillis()).isEqualTo(500);
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxTransactionRetryTime() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.setMaxTransactionRetryTime(Duration.ofSeconds(2));
|
||||
assertThat(mapDriverConfig(properties)).extracting("retrySettings")
|
||||
.hasFieldOrPropertyWithValue("maxRetryTimeMs", 2000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticationShouldDefaultToNone() {
|
||||
assertThat(mapAuthToken(new Authentication())).isEqualTo(AuthTokens.none());
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticationWithUsernameShouldEnableBasicAuth() {
|
||||
Authentication authentication = new Authentication();
|
||||
authentication.setUsername("Farin");
|
||||
authentication.setPassword("Urlaub");
|
||||
assertThat(mapAuthToken(authentication)).isEqualTo(AuthTokens.basic("Farin", "Urlaub"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticationWithUsernameAndRealmShouldEnableBasicAuth() {
|
||||
Authentication authentication = new Authentication();
|
||||
authentication.setUsername("Farin");
|
||||
authentication.setPassword("Urlaub");
|
||||
authentication.setRealm("Test Realm");
|
||||
assertThat(mapAuthToken(authentication)).isEqualTo(AuthTokens.basic("Farin", "Urlaub", "Test Realm"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticationWithKerberosTicketShouldEnableKerberos() {
|
||||
Authentication authentication = new Authentication();
|
||||
authentication.setKerberosTicket("AABBCCDDEE");
|
||||
assertThat(mapAuthToken(authentication)).isEqualTo(AuthTokens.kerberos("AABBCCDDEE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticationWithBothUsernameAndKerberosShouldNotBeAllowed() {
|
||||
Authentication authentication = new Authentication();
|
||||
authentication.setUsername("Farin");
|
||||
authentication.setKerberosTicket("AABBCCDDEE");
|
||||
assertThatIllegalStateException().isThrownBy(() -> mapAuthToken(authentication))
|
||||
.withMessage("Cannot specify both username ('Farin') and kerberos ticket ('AABBCCDDEE')");
|
||||
}
|
||||
|
||||
@Test
|
||||
void poolWithMetricsEnabled() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getPool().setMetricsEnabled(true);
|
||||
assertThat(mapDriverConfig(properties).isMetricsEnabled()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void poolWithLogLeakedSessions() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getPool().setLogLeakedSessions(true);
|
||||
assertThat(mapDriverConfig(properties).logLeakedSessions()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void poolWithMaxConnectionPoolSize() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getPool().setMaxConnectionPoolSize(4711);
|
||||
assertThat(mapDriverConfig(properties).maxConnectionPoolSize()).isEqualTo(4711);
|
||||
}
|
||||
|
||||
@Test
|
||||
void poolWithIdleTimeBeforeConnectionTest() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getPool().setIdleTimeBeforeConnectionTest(Duration.ofSeconds(23));
|
||||
assertThat(mapDriverConfig(properties).idleTimeBeforeConnectionTest()).isEqualTo(23000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void poolWithMaxConnectionLifetime() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getPool().setMaxConnectionLifetime(Duration.ofSeconds(30));
|
||||
assertThat(mapDriverConfig(properties).maxConnectionLifetimeMillis()).isEqualTo(30000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void poolWithConnectionAcquisitionTimeout() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getPool().setConnectionAcquisitionTimeout(Duration.ofSeconds(5));
|
||||
assertThat(mapDriverConfig(properties).connectionAcquisitionTimeoutMillis()).isEqualTo(5000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void securityWithEncrypted() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getSecurity().setEncrypted(true);
|
||||
assertThat(mapDriverConfig(properties).encrypted()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void securityWithTrustSignedCertificates() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getSecurity().setTrustStrategy(TrustStrategy.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES);
|
||||
assertThat(mapDriverConfig(properties).trustStrategy().strategy())
|
||||
.isEqualTo(Config.TrustStrategy.Strategy.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES);
|
||||
}
|
||||
|
||||
@Test
|
||||
void securityWithTrustAllCertificates() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getSecurity().setTrustStrategy(TrustStrategy.TRUST_ALL_CERTIFICATES);
|
||||
assertThat(mapDriverConfig(properties).trustStrategy().strategy())
|
||||
.isEqualTo(Config.TrustStrategy.Strategy.TRUST_ALL_CERTIFICATES);
|
||||
}
|
||||
|
||||
@Test
|
||||
void securityWitHostnameVerificationEnabled() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getSecurity().setTrustStrategy(TrustStrategy.TRUST_ALL_CERTIFICATES);
|
||||
properties.getSecurity().setHostnameVerificationEnabled(true);
|
||||
assertThat(mapDriverConfig(properties).trustStrategy().isHostnameVerificationEnabled()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void securityWithCustomCertificates(@TempDir File directory) throws IOException {
|
||||
File certFile = new File(directory, "neo4j-driver.cert");
|
||||
assertThat(certFile.createNewFile());
|
||||
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getSecurity().setTrustStrategy(TrustStrategy.TRUST_CUSTOM_CA_SIGNED_CERTIFICATES);
|
||||
properties.getSecurity().setCertFile(certFile);
|
||||
Config.TrustStrategy trustStrategy = mapDriverConfig(properties).trustStrategy();
|
||||
assertThat(trustStrategy.strategy())
|
||||
.isEqualTo(Config.TrustStrategy.Strategy.TRUST_CUSTOM_CA_SIGNED_CERTIFICATES);
|
||||
assertThat(trustStrategy.certFile()).isEqualTo(certFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
void securityWithCustomCertificatesShouldFailWithoutCertificate() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getSecurity().setTrustStrategy(TrustStrategy.TRUST_CUSTOM_CA_SIGNED_CERTIFICATES);
|
||||
assertThatExceptionOfType(InvalidConfigurationPropertyValueException.class)
|
||||
.isThrownBy(() -> mapDriverConfig(properties)).withMessage(
|
||||
"Property spring.neo4j.security.trust-strategy with value 'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES' is invalid: Configured trust strategy requires a certificate file.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void securityWithTrustSystemCertificates() {
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
properties.getSecurity().setTrustStrategy(TrustStrategy.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES);
|
||||
assertThat(mapDriverConfig(properties).trustStrategy().strategy())
|
||||
.isEqualTo(Config.TrustStrategy.Strategy.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES);
|
||||
}
|
||||
|
||||
@Test
|
||||
void driverConfigShouldBeConfiguredToUseUseSpringJclLogging() {
|
||||
assertThat(mapDriverConfig(new Neo4jProperties()).logging()).isNotNull()
|
||||
.isInstanceOf(Neo4jSpringJclLogging.class);
|
||||
}
|
||||
|
||||
private AuthToken mapAuthToken(Authentication authentication) {
|
||||
return new Neo4jAutoConfiguration().mapAuthToken(authentication);
|
||||
}
|
||||
|
||||
private Config mapDriverConfig(Neo4jProperties properties, ConfigBuilderCustomizer... customizers) {
|
||||
return new Neo4jAutoConfiguration().mapDriverConfig(properties, Arrays.asList(customizers));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.neo4j;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.neo4j.driver.Config;
|
||||
import org.neo4j.driver.internal.retry.RetrySettings;
|
||||
|
||||
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties.Pool;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link Neo4jProperties}.
|
||||
*
|
||||
* @author Michael J. Simons
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class Neo4jPropertiesTests {
|
||||
|
||||
@Test
|
||||
void poolSettingsHaveConsistentDefaults() {
|
||||
Config defaultConfig = Config.defaultConfig();
|
||||
Pool pool = new Neo4jProperties().getPool();
|
||||
assertThat(pool.isMetricsEnabled()).isEqualTo(defaultConfig.isMetricsEnabled());
|
||||
assertThat(pool.isLogLeakedSessions()).isEqualTo(defaultConfig.logLeakedSessions());
|
||||
assertThat(pool.getMaxConnectionPoolSize()).isEqualTo(defaultConfig.maxConnectionPoolSize());
|
||||
assertDuration(pool.getIdleTimeBeforeConnectionTest(), defaultConfig.idleTimeBeforeConnectionTest());
|
||||
assertDuration(pool.getMaxConnectionLifetime(), defaultConfig.maxConnectionLifetimeMillis());
|
||||
assertDuration(pool.getConnectionAcquisitionTimeout(), defaultConfig.connectionAcquisitionTimeoutMillis());
|
||||
}
|
||||
|
||||
@Test
|
||||
void securitySettingsHaveConsistentDefaults() {
|
||||
Config defaultConfig = Config.defaultConfig();
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
assertThat(properties.getSecurity().isEncrypted()).isEqualTo(defaultConfig.encrypted());
|
||||
assertThat(properties.getSecurity().getTrustStrategy().name())
|
||||
.isEqualTo(defaultConfig.trustStrategy().strategy().name());
|
||||
assertThat(properties.getSecurity().isHostnameVerificationEnabled())
|
||||
.isEqualTo(defaultConfig.trustStrategy().isHostnameVerificationEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void driverSettingsHaveConsistentDefaults() {
|
||||
Config defaultConfig = Config.defaultConfig();
|
||||
Neo4jProperties properties = new Neo4jProperties();
|
||||
assertDuration(properties.getConnectionTimeout(), defaultConfig.connectionTimeoutMillis());
|
||||
assertDuration(properties.getMaxTransactionRetryTime(), RetrySettings.DEFAULT.maxRetryTimeMs());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAssumeDefaultValuesForUrl() {
|
||||
Neo4jProperties driverProperties = new Neo4jProperties();
|
||||
assertThat(driverProperties.getUri()).isEqualTo(URI.create("bolt://localhost:7687"));
|
||||
}
|
||||
|
||||
private static void assertDuration(Duration duration, long expectedValueInMillis) {
|
||||
if (expectedValueInMillis == org.neo4j.driver.internal.async.pool.PoolSettings.NOT_CONFIGURED) {
|
||||
assertThat(duration).isNull();
|
||||
}
|
||||
else {
|
||||
assertThat(duration.toMillis()).isEqualTo(expectedValueInMillis);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1170,6 +1170,13 @@ bom {
|
||||
]
|
||||
}
|
||||
}
|
||||
library("Neo4j", "4.1.1") {
|
||||
group("org.neo4j.driver") {
|
||||
modules = [
|
||||
"neo4j-java-driver"
|
||||
]
|
||||
}
|
||||
}
|
||||
library("Neo4j OGM", "3.2.12") {
|
||||
group("org.neo4j") {
|
||||
modules = [
|
||||
|
||||
@@ -4495,20 +4495,20 @@ Spring Boot offers several conveniences for working with Neo4j, including the `s
|
||||
|
||||
[[boot-features-connecting-to-neo4j]]
|
||||
==== Connecting to a Neo4j Database
|
||||
To access a Neo4j server, you can inject an auto-configured `org.neo4j.ogm.session.Session`.
|
||||
To access a Neo4j server, you can inject an auto-configured `org.neo4j.driver.Driver`.
|
||||
By default, the instance tries to connect to a Neo4j server at `localhost:7687` using the Bolt protocol.
|
||||
The following example shows how to inject a Neo4j `Session`:
|
||||
The following example shows how to inject a Neo4j `Driver` that gives you access, amongst other things, to a `Session`:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@Component
|
||||
public class MyBean {
|
||||
|
||||
private final Session session;
|
||||
private final Driver driver;
|
||||
|
||||
@Autowired
|
||||
public MyBean(Session session) {
|
||||
this.session = session;
|
||||
public MyBean(Driver driver) {
|
||||
this.driver = driver;
|
||||
}
|
||||
|
||||
// ...
|
||||
@@ -4516,16 +4516,19 @@ The following example shows how to inject a Neo4j `Session`:
|
||||
}
|
||||
----
|
||||
|
||||
You can configure the uri and credentials to use by setting the `spring.data.neo4j.*` properties, as shown in the following example:
|
||||
You can configure various aspects of the driver using `spring.neo4j.*` properties.
|
||||
The following example shows how to configure the uri and credentials to use:
|
||||
|
||||
[source,properties,indent=0,configprops]
|
||||
----
|
||||
spring.data.neo4j.uri=bolt://my-server:7687
|
||||
spring.data.neo4j.username=neo4j
|
||||
spring.data.neo4j.password=secret
|
||||
spring.neo4j.uri=bolt://my-server:7687
|
||||
spring.neo4j.authentication.username=neo4j
|
||||
spring.neo4j.authentication.password=secret
|
||||
----
|
||||
|
||||
You can take full control over the session creation by adding either an `org.neo4j.ogm.config.Configuration` bean or an `org.neo4j.ogm.session.SessionFactory` bean.
|
||||
The auto-configured `Driver` is created using `ConfigBuilder`.
|
||||
To fine-tune its configuration, declare one or more `ConfigBuilderCustomizer` beans.
|
||||
Each will be called in order with the `ConfigBuilder` that is used to build the `Driver`.
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user