@ -26,20 +26,19 @@ 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.Neo4jAutoConfiguration.PropertiesNeo4jConnectionDetails ;
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 org.springframework.core.env.Environment ;
import org.springframework.mock.env.MockEnvironment ;
import org.springframework.context.annotation.Bean ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
@ -50,6 +49,9 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
*
* @author Michael J . Simons
* @author Stephane Nicoll
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* /
class Neo4jAutoConfigurationTests {
@ -103,6 +105,22 @@ class Neo4jAutoConfigurationTests {
. hasMessageContaining ( "'%s' is not a supported scheme." , invalidScheme ) ) ;
}
@Bean
void usesCustomConnectionDetails ( ) {
this . contextRunner . withBean ( Neo4jConnectionDetails . class , ( ) - > new Neo4jConnectionDetails ( ) {
@Override
public URI getUri ( ) {
return URI . create ( "bolt+ssc://localhost:12345" ) ;
}
} ) . run ( ( context ) - > {
assertThat ( context ) . hasSingleBean ( Driver . class ) ;
Driver driver = context . getBean ( Driver . class ) ;
assertThat ( driver . isEncrypted ( ) ) . isTrue ( ) ;
} ) ;
}
@Test
void connectionTimeout ( ) {
Neo4jProperties properties = new Neo4jProperties ( ) ;
@ -118,8 +136,8 @@ class Neo4jAutoConfigurationTests {
}
@Test
void determineServerU riShouldDefaultToLocalhost( ) {
assertThat ( determineServerUri ( new Neo4jProperties ( ) , new MockEnvironment ( ) ) )
void u riShouldDefaultToLocalhost( ) {
assertThat ( new PropertiesNeo4jConnectionDetails ( new Neo4jProperties ( ) ) . getUri ( ) )
. isEqualTo ( URI . create ( "bolt://localhost:7687" ) ) ;
}
@ -128,44 +146,52 @@ class Neo4jAutoConfigurationTests {
URI customUri = URI . create ( "bolt://localhost:4242" ) ;
Neo4jProperties properties = new Neo4jProperties ( ) ;
properties . setUri ( customUri ) ;
assertThat ( determineServerUri ( properties , new MockEnvironment ( ) ) ) . isEqualTo ( customUri ) ;
assertThat ( new PropertiesNeo4jConnectionDetails ( properties ) . getUri ( ) ) . isEqualTo ( customUri ) ;
}
@Test
void authenticationShouldDefaultToNone ( ) {
assertThat ( mapAuthToken ( new Authentication ( ) ) ) . isEqualTo ( AuthTokens . none ( ) ) ;
assertThat ( new PropertiesNeo4jConnectionDetails ( new Neo4jProperties ( ) ) . getAuthToken ( ) )
. isEqualTo ( AuthTokens . none ( ) ) ;
}
@Test
void authenticationWithUsernameShouldEnableBasicAuth ( ) {
Authentication authentication = new Authentication ( ) ;
authentication . setUsername ( "Farin" ) ;
authentication . setPassword ( "Urlaub" ) ;
assertThat ( mapAuthToken ( authentication ) ) . isEqualTo ( AuthTokens . basic ( "Farin" , "Urlaub" ) ) ;
Neo4jProperties properties = new Neo4jProperties ( ) ;
properties . getAuthentication ( ) . setUsername ( "Farin" ) ;
properties . getAuthentication ( ) . setPassword ( "Urlaub" ) ;
assertThat ( new PropertiesNeo4jConnectionDetails ( properties ) . getAuthToken ( ) )
. isEqualTo ( AuthTokens . basic ( "Farin" , "Urlaub" ) ) ;
}
@Test
void authenticationWithUsernameAndRealmShouldEnableBasicAuth ( ) {
Authentication authentication = new Authentication ( ) ;
Neo4jProperties properties = new Neo4jProperties ( ) ;
Authentication authentication = properties . getAuthentication ( ) ;
authentication . setUsername ( "Farin" ) ;
authentication . setPassword ( "Urlaub" ) ;
authentication . setRealm ( "Test Realm" ) ;
assertThat ( mapAuthToken ( authentication ) ) . isEqualTo ( AuthTokens . basic ( "Farin" , "Urlaub" , "Test Realm" ) ) ;
assertThat ( new PropertiesNeo4jConnectionDetails ( properties ) . getAuthToken ( ) )
. isEqualTo ( AuthTokens . basic ( "Farin" , "Urlaub" , "Test Realm" ) ) ;
}
@Test
void authenticationWithKerberosTicketShouldEnableKerberos ( ) {
Authentication authentication = new Authentication ( ) ;
Neo4jProperties properties = new Neo4jProperties ( ) ;
Authentication authentication = properties . getAuthentication ( ) ;
authentication . setKerberosTicket ( "AABBCCDDEE" ) ;
assertThat ( mapAuthToken ( authentication ) ) . isEqualTo ( AuthTokens . kerberos ( "AABBCCDDEE" ) ) ;
assertThat ( new PropertiesNeo4jConnectionDetails ( properties ) . getAuthToken ( ) )
. isEqualTo ( AuthTokens . kerberos ( "AABBCCDDEE" ) ) ;
}
@Test
void authenticationWithBothUsernameAndKerberosShouldNotBeAllowed ( ) {
Authentication authentication = new Authentication ( ) ;
Neo4jProperties properties = new Neo4jProperties ( ) ;
Authentication authentication = properties . getAuthentication ( ) ;
authentication . setUsername ( "Farin" ) ;
authentication . setKerberosTicket ( "AABBCCDDEE" ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - > mapAuthToken ( authentication ) )
assertThatIllegalStateException ( )
. isThrownBy ( ( ) - > new PropertiesNeo4jConnectionDetails ( properties ) . getAuthToken ( ) )
. withMessage ( "Cannot specify both username ('Farin') and kerberos ticket ('AABBCCDDEE')" ) ;
}
@ -279,20 +305,9 @@ class Neo4jAutoConfigurationTests {
assertThat ( mapDriverConfig ( new Neo4jProperties ( ) ) . logging ( ) ) . isInstanceOf ( Neo4jSpringJclLogging . class ) ;
}
private URI determineServerUri ( Neo4jProperties properties , Environment environment ) {
return new Neo4jAutoConfiguration ( ) . determineServerUri ( properties , environment ) ;
}
private AuthToken mapAuthToken ( Authentication authentication , Environment environment ) {
return new Neo4jAutoConfiguration ( ) . mapAuthToken ( authentication , environment ) ;
}
private AuthToken mapAuthToken ( Authentication authentication ) {
return mapAuthToken ( authentication , new MockEnvironment ( ) ) ;
}
private Config mapDriverConfig ( Neo4jProperties properties , ConfigBuilderCustomizer . . . customizers ) {
return new Neo4jAutoConfiguration ( ) . mapDriverConfig ( properties , Arrays . asList ( customizers ) ) ;
return new Neo4jAutoConfiguration ( ) . mapDriverConfig ( properties ,
new PropertiesNeo4jConnectionDetails ( properties ) , Arrays . asList ( customizers ) ) ;
}
}