2626import org .junit .jupiter .api .io .TempDir ;
2727import org .junit .jupiter .params .ParameterizedTest ;
2828import org .junit .jupiter .params .provider .ValueSource ;
29- import org .neo4j .driver .AuthToken ;
3029import org .neo4j .driver .AuthTokens ;
3130import org .neo4j .driver .Config ;
3231import org .neo4j .driver .Config .ConfigBuilder ;
3332import org .neo4j .driver .Driver ;
3433
3534import org .springframework .boot .autoconfigure .AutoConfigurations ;
35+ import org .springframework .boot .autoconfigure .neo4j .Neo4jAutoConfiguration .PropertiesNeo4jConnectionDetails ;
3636import org .springframework .boot .autoconfigure .neo4j .Neo4jProperties .Authentication ;
3737import org .springframework .boot .autoconfigure .neo4j .Neo4jProperties .Security .TrustStrategy ;
3838import org .springframework .boot .context .properties .source .InvalidConfigurationPropertyValueException ;
3939import org .springframework .boot .test .context .FilteredClassLoader ;
4040import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
41- import org .springframework .core .env .Environment ;
42- import org .springframework .mock .env .MockEnvironment ;
41+ import org .springframework .context .annotation .Bean ;
4342
4443import static org .assertj .core .api .Assertions .assertThat ;
4544import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
5049 *
5150 * @author Michael J. Simons
5251 * @author Stephane Nicoll
52+ * @author Moritz Halbritter
53+ * @author Andy Wilkinson
54+ * @author Phillip Webb
5355 */
5456class Neo4jAutoConfigurationTests {
5557
@@ -103,6 +105,22 @@ void uriWithInvalidSchemesAreDetected(String invalidScheme) {
103105 .hasMessageContaining ("'%s' is not a supported scheme." , invalidScheme ));
104106 }
105107
108+ @ Bean
109+ void usesCustomConnectionDetails () {
110+ this .contextRunner .withBean (Neo4jConnectionDetails .class , () -> new Neo4jConnectionDetails () {
111+
112+ @ Override
113+ public URI getUri () {
114+ return URI .create ("bolt+ssc://localhost:12345" );
115+ }
116+
117+ }).run ((context ) -> {
118+ assertThat (context ).hasSingleBean (Driver .class );
119+ Driver driver = context .getBean (Driver .class );
120+ assertThat (driver .isEncrypted ()).isTrue ();
121+ });
122+ }
123+
106124 @ Test
107125 void connectionTimeout () {
108126 Neo4jProperties properties = new Neo4jProperties ();
@@ -118,8 +136,8 @@ void maxTransactionRetryTime() {
118136 }
119137
120138 @ Test
121- void determineServerUriShouldDefaultToLocalhost () {
122- assertThat (determineServerUri ( new Neo4jProperties (), new MockEnvironment () ))
139+ void uriShouldDefaultToLocalhost () {
140+ assertThat (new PropertiesNeo4jConnectionDetails ( new Neo4jProperties ()). getUri ( ))
123141 .isEqualTo (URI .create ("bolt://localhost:7687" ));
124142 }
125143
@@ -128,44 +146,52 @@ void determineServerUriWithCustomUriShouldOverrideDefault() {
128146 URI customUri = URI .create ("bolt://localhost:4242" );
129147 Neo4jProperties properties = new Neo4jProperties ();
130148 properties .setUri (customUri );
131- assertThat (determineServerUri ( properties , new MockEnvironment () )).isEqualTo (customUri );
149+ assertThat (new PropertiesNeo4jConnectionDetails ( properties ). getUri ( )).isEqualTo (customUri );
132150 }
133151
134152 @ Test
135153 void authenticationShouldDefaultToNone () {
136- assertThat (mapAuthToken (new Authentication ())).isEqualTo (AuthTokens .none ());
154+ assertThat (new PropertiesNeo4jConnectionDetails (new Neo4jProperties ()).getAuthToken ())
155+ .isEqualTo (AuthTokens .none ());
137156 }
138157
139158 @ Test
140159 void authenticationWithUsernameShouldEnableBasicAuth () {
141- Authentication authentication = new Authentication ();
142- authentication .setUsername ("Farin" );
143- authentication .setPassword ("Urlaub" );
144- assertThat (mapAuthToken (authentication )).isEqualTo (AuthTokens .basic ("Farin" , "Urlaub" ));
160+ Neo4jProperties properties = new Neo4jProperties ();
161+ properties .getAuthentication ().setUsername ("Farin" );
162+ properties .getAuthentication ().setPassword ("Urlaub" );
163+ assertThat (new PropertiesNeo4jConnectionDetails (properties ).getAuthToken ())
164+ .isEqualTo (AuthTokens .basic ("Farin" , "Urlaub" ));
145165 }
146166
147167 @ Test
148168 void authenticationWithUsernameAndRealmShouldEnableBasicAuth () {
149- Authentication authentication = new Authentication ();
169+ Neo4jProperties properties = new Neo4jProperties ();
170+ Authentication authentication = properties .getAuthentication ();
150171 authentication .setUsername ("Farin" );
151172 authentication .setPassword ("Urlaub" );
152173 authentication .setRealm ("Test Realm" );
153- assertThat (mapAuthToken (authentication )).isEqualTo (AuthTokens .basic ("Farin" , "Urlaub" , "Test Realm" ));
174+ assertThat (new PropertiesNeo4jConnectionDetails (properties ).getAuthToken ())
175+ .isEqualTo (AuthTokens .basic ("Farin" , "Urlaub" , "Test Realm" ));
154176 }
155177
156178 @ Test
157179 void authenticationWithKerberosTicketShouldEnableKerberos () {
158- Authentication authentication = new Authentication ();
180+ Neo4jProperties properties = new Neo4jProperties ();
181+ Authentication authentication = properties .getAuthentication ();
159182 authentication .setKerberosTicket ("AABBCCDDEE" );
160- assertThat (mapAuthToken (authentication )).isEqualTo (AuthTokens .kerberos ("AABBCCDDEE" ));
183+ assertThat (new PropertiesNeo4jConnectionDetails (properties ).getAuthToken ())
184+ .isEqualTo (AuthTokens .kerberos ("AABBCCDDEE" ));
161185 }
162186
163187 @ Test
164188 void authenticationWithBothUsernameAndKerberosShouldNotBeAllowed () {
165- Authentication authentication = new Authentication ();
189+ Neo4jProperties properties = new Neo4jProperties ();
190+ Authentication authentication = properties .getAuthentication ();
166191 authentication .setUsername ("Farin" );
167192 authentication .setKerberosTicket ("AABBCCDDEE" );
168- assertThatIllegalStateException ().isThrownBy (() -> mapAuthToken (authentication ))
193+ assertThatIllegalStateException ()
194+ .isThrownBy (() -> new PropertiesNeo4jConnectionDetails (properties ).getAuthToken ())
169195 .withMessage ("Cannot specify both username ('Farin') and kerberos ticket ('AABBCCDDEE')" );
170196 }
171197
@@ -279,20 +305,9 @@ void driverConfigShouldBeConfiguredToUseUseSpringJclLogging() {
279305 assertThat (mapDriverConfig (new Neo4jProperties ()).logging ()).isInstanceOf (Neo4jSpringJclLogging .class );
280306 }
281307
282- private URI determineServerUri (Neo4jProperties properties , Environment environment ) {
283- return new Neo4jAutoConfiguration ().determineServerUri (properties , environment );
284- }
285-
286- private AuthToken mapAuthToken (Authentication authentication , Environment environment ) {
287- return new Neo4jAutoConfiguration ().mapAuthToken (authentication , environment );
288- }
289-
290- private AuthToken mapAuthToken (Authentication authentication ) {
291- return mapAuthToken (authentication , new MockEnvironment ());
292- }
293-
294308 private Config mapDriverConfig (Neo4jProperties properties , ConfigBuilderCustomizer ... customizers ) {
295- return new Neo4jAutoConfiguration ().mapDriverConfig (properties , Arrays .asList (customizers ));
309+ return new Neo4jAutoConfiguration ().mapDriverConfig (properties ,
310+ new PropertiesNeo4jConnectionDetails (properties ), Arrays .asList (customizers ));
296311 }
297312
298313}
0 commit comments