Merge pull request #15637 from Michael Simons

* gh-15637:
  Polish "Add opt-in support for Neo4j-OGM native types"
  Add opt-in support for Neo4j-OGM native types

Closes gh-15637
This commit is contained in:
Andy Wilkinson
2019-02-13 15:05:00 +00:00
7 changed files with 115 additions and 7 deletions

View File

@@ -782,7 +782,7 @@
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId>
<artifactId>neo4j-ogm-bolt-native-types</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@@ -790,6 +790,11 @@
<artifactId>neo4j-ogm-embedded-driver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -33,6 +33,7 @@ import org.springframework.util.ClassUtils;
* @author Michael Hunger
* @author Vince Bickers
* @author Aurélien Leboulanger
* @author Michael Simons
* @since 1.4.0
*/
@ConfigurationProperties(prefix = "spring.data.neo4j")
@@ -72,6 +73,11 @@ public class Neo4jProperties implements ApplicationContextAware {
*/
private Boolean openInView;
/**
* Whether to use Neo4j native types wherever possible.
*/
private boolean useNativeTypes = false;
private final Embedded embedded = new Embedded();
private ClassLoader classLoader = Neo4jProperties.class.getClassLoader();
@@ -116,6 +122,14 @@ public class Neo4jProperties implements ApplicationContextAware {
this.openInView = openInView;
}
public boolean isUseNativeTypes() {
return this.useNativeTypes;
}
public void setUseNativeTypes(boolean useNativeTypes) {
this.useNativeTypes = useNativeTypes;
}
public Embedded getEmbedded() {
return this.embedded;
}
@@ -146,6 +160,9 @@ public class Neo4jProperties implements ApplicationContextAware {
builder.credentials(this.username, this.password);
}
builder.autoIndex(this.getAutoIndex().getName());
if (this.useNativeTypes) {
builder.useNativeTypes();
}
}
private void configureUriWithDefaults(Builder builder) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.data.neo4j;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.junit.Test;
import org.neo4j.ogm.driver.NativeTypesNotAvailableException;
import org.neo4j.ogm.driver.NativeTypesNotSupportedException;
import org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
@@ -51,8 +53,8 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link Neo4jDataAutoConfiguration}. Tests can't use the embedded driver as we
* use Lucene 4 and Neo4j still requires 3.
* Tests for {@link Neo4jDataAutoConfiguration}. Tests should not use the embedded driver
* as it requires the complete Neo4j-Kernel and server to function properly.
*
* @author Stephane Nicoll
* @author Michael Hunger
@@ -116,7 +118,6 @@ public class Neo4jDataAutoConfigurationTests {
assertThat(context)
.hasSingleBean(org.neo4j.ogm.config.Configuration.class);
});
}
@Test
@@ -144,6 +145,48 @@ public class Neo4jDataAutoConfigurationTests {
.doesNotHaveBean(OpenSessionInViewInterceptor.class));
}
@Test
public void shouldBeAbleToUseNativeTypesWithBolt() {
this.contextRunner
.withPropertyValues("spring.data.neo4j.uri=bolt://localhost:7687",
"spring.data.neo4j.use-native-types:true")
.withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class,
TransactionAutoConfiguration.class))
.run((context) -> assertThat(context)
.getBean(org.neo4j.ogm.config.Configuration.class)
.hasFieldOrPropertyWithValue("useNativeTypes", true));
}
@Test
public void shouldFailWhenNativeTypesAreNotAvailable() {
this.contextRunner
.withClassLoader(
new FilteredClassLoader("org.neo4j.ogm.drivers.bolt.types"))
.withPropertyValues("spring.data.neo4j.uri=bolt://localhost:7687",
"spring.data.neo4j.use-native-types:true")
.withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class,
TransactionAutoConfiguration.class))
.run((context) -> {
assertThat(context).hasFailed();
assertThat(context.getStartupFailure()).hasRootCauseInstanceOf(
NativeTypesNotAvailableException.class);
});
}
@Test
public void shouldFailWhenNativeTypesAreNotSupported() {
this.contextRunner
.withPropertyValues("spring.data.neo4j.uri=http://localhost:7474",
"spring.data.neo4j.use-native-types:true")
.withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class,
TransactionAutoConfiguration.class))
.run((context) -> {
assertThat(context).hasFailed();
assertThat(context.getStartupFailure()).hasRootCauseInstanceOf(
NativeTypesNotSupportedException.class);
});
}
@Test
public void eventListenersAreAutoRegistered() {
this.contextRunner.withUserConfiguration(EventListenerConfiguration.class)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link Neo4jProperties}.
*
* @author Stephane Nicoll
* @author Michael Simons
*/
public class Neo4jPropertiesTests {
@@ -147,6 +148,21 @@ public class Neo4jPropertiesTests {
"file:relative/path/to/my.db");
}
@Test
public void nativeTypesAreSetToFalseByDefault() {
Neo4jProperties properties = load(true);
Configuration configuration = properties.createConfiguration();
assertThat(configuration.getUseNativeTypes()).isFalse();
}
@Test
public void nativeTypesCanBeConfigured() {
Neo4jProperties properties = load(true,
"spring.data.neo4j.use-native-types=true");
Configuration configuration = properties.createConfiguration();
assertThat(configuration.getUseNativeTypes()).isTrue();
}
private static void assertDriver(Configuration actual, String driver, String uri) {
assertThat(actual).isNotNull();
assertThat(actual.getDriverClassName()).isEqualTo(driver);

View File

@@ -2527,6 +2527,11 @@
<artifactId>neo4j-ogm-bolt-driver</artifactId>
<version>${neo4j-ogm.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-bolt-native-types</artifactId>
<version>${neo4j-ogm.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-core</artifactId>
@@ -2537,6 +2542,11 @@
<artifactId>neo4j-ogm-embedded-driver</artifactId>
<version>${neo4j-ogm.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-embedded-native-types</artifactId>
<version>${neo4j-ogm.version}</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId>

View File

@@ -695,6 +695,7 @@ content into your application. Rather, pick only the properties that you need.
spring.data.neo4j.password= # Login password of the server.
spring.data.neo4j.repositories.enabled=true # Whether to enable Neo4j repositories.
spring.data.neo4j.uri= # URI used by the driver. Auto-detected by default.
spring.data.neo4j.use-native-types=false # Whether to use Neo4j native types wherever possible.
spring.data.neo4j.username= # Login user of the server.
# DATA REST ({sc-spring-boot-autoconfigure}/data/rest/RepositoryRestProperties.{sc-ext}[RepositoryRestProperties])

View File

@@ -4440,6 +4440,22 @@ in your configuration, e.g. `spring.data.neo4j.uri=file://var/tmp/graph.db`.
[[boot-features-neo4j-ogm-native-types]]
==== Using Native Types
Neo4j-OGM can map some types, like those in `java.time.*`, to `String`-based properties
or to one of the native types that Neo4j provides. For backwards compatibility reasons
the default for Neo4j-OGM is to use a `String`-based representation. To use native types,
add a dependency on either `org.neo4j:neo4j-ogm-bolt-native-types` or
`org.neo4j:neo4j-ogm-embedded-native-types`, and configure the
`spring.data.neo4j.use-native-types` property as shown in the following example:
[source,properties,indent=0]
----
spring.data.neo4j.use-native-types=true
----
[[boot-features-neo4j-ogm-session]]
==== Neo4jSession
By default, if you are running a web application, the session is bound to the thread for