Support of Neo4j auto-index configuration

See gh-8843
This commit is contained in:
Aurélien Leboulanger 2017-04-07 11:32:44 +02:00 committed by Stephane Nicoll
parent 1dc256eb23
commit 779733c379
3 changed files with 55 additions and 0 deletions

View File

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.data.neo4j;
import org.neo4j.ogm.config.AutoIndexMode;
import org.neo4j.ogm.config.Configuration;
import org.neo4j.ogm.config.Configuration.Builder;
@ -31,6 +32,7 @@ import org.springframework.util.ClassUtils;
* @author Stephane Nicoll
* @author Michael Hunger
* @author Vince Bickers
* @author Aurélien Leboulanger
* @since 1.4.0
*/
@ConfigurationProperties(prefix = "spring.data.neo4j")
@ -61,6 +63,11 @@ public class Neo4jProperties implements ApplicationContextAware {
private final Embedded embedded = new Embedded();
/**
* Index generation behaviour. {@link AutoIndexMode#NONE} by default.
*/
private AutoIndexMode autoIndex = AutoIndexMode.NONE;
private ClassLoader classLoader = Neo4jProperties.class.getClassLoader();
public String getUri() {
@ -91,6 +98,14 @@ public class Neo4jProperties implements ApplicationContextAware {
return this.embedded;
}
public AutoIndexMode getAutoIndex() {
return this.autoIndex;
}
public void setAutoIndex(AutoIndexMode autoIndex) {
this.autoIndex = autoIndex;
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.classLoader = ctx.getClassLoader();
@ -116,6 +131,8 @@ public class Neo4jProperties implements ApplicationContextAware {
if (this.username != null && this.password != null) {
builder.credentials(this.username, this.password);
}
builder.autoIndex(this.getAutoIndex().name());
}
private void configureUriWithDefaults(Builder builder) {

View File

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.data.neo4j;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Test;
import org.neo4j.ogm.config.AutoIndexMode;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.neo4j.ogm.session.event.Event;
@ -32,6 +33,7 @@ import org.springframework.boot.autoconfigure.data.neo4j.city.City;
import org.springframework.boot.autoconfigure.data.neo4j.country.Country;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -56,6 +58,7 @@ import static org.mockito.Mockito.verify;
* @author Vince Bickers
* @author Andy Wilkinson
* @author Kazuki Shimizu
* @author Aurélien Leboulanger
*/
public class Neo4jDataAutoConfigurationTests {
@ -139,6 +142,25 @@ public class Neo4jDataAutoConfigurationTests {
.onPreSave(any(Event.class));
}
@Test
public void autoIndexConfiguration() {
load(CustomConfigurationFactory.class);
assertThat(this.context.getBean(org.neo4j.ogm.config.Configuration.class)
.getAutoIndex()).isEqualTo(AutoIndexMode.NONE);
load(CustomConfigurationFactory.class, "spring.data.neo4j.auto-index=assert");
assertThat(this.context.getBean(org.neo4j.ogm.config.Configuration.class)
.getAutoIndex()).isEqualTo(AutoIndexMode.ASSERT);
load(CustomConfigurationFactory.class, "spring.data.neo4j.auto-index=dump");
assertThat(this.context.getBean(org.neo4j.ogm.config.Configuration.class)
.getAutoIndex()).isEqualTo(AutoIndexMode.DUMP);
load(CustomConfigurationFactory.class, "spring.data.neo4j.auto-index=validate");
assertThat(this.context.getBean(org.neo4j.ogm.config.Configuration.class)
.getAutoIndex()).isEqualTo(AutoIndexMode.VALIDATE);
}
private void load(Class<?> config, String... environment) {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
EnvironmentTestUtils.addEnvironment(ctx, environment);
@ -174,6 +196,21 @@ public class Neo4jDataAutoConfigurationTests {
}
@Configuration
@EnableConfigurationProperties(Neo4jProperties.class)
static class CustomConfigurationFactory {
@Bean
public org.neo4j.ogm.config.Configuration configuration(Neo4jProperties properties) {
return properties.createConfiguration();
}
@Bean
public SessionFactory customSessionFactory() {
return mock(SessionFactory.class);
}
}
@Configuration
static class CustomConfiguration {

View File

@ -619,6 +619,7 @@ content into your application; rather pick only the properties that you need.
spring.data.neo4j.repositories.enabled=true # Enable Neo4j repositories.
spring.data.neo4j.uri= # URI used by the driver. Auto-detected by default.
spring.data.neo4j.username= # Login user of the server.
spring.data.neo4j.indexes.auto=none # Configure auto indexing. Disable by default.
# DATA REST ({sc-spring-boot-autoconfigure}/data/rest/RepositoryRestProperties.{sc-ext}[RepositoryRestProperties])
spring.data.rest.base-path= # Base path to be used by Spring Data REST to expose repository resources.