26
26
import org .junit .jupiter .api .io .TempDir ;
27
27
import org .junit .jupiter .params .ParameterizedTest ;
28
28
import org .junit .jupiter .params .provider .ValueSource ;
29
- import org .neo4j .driver .AuthToken ;
30
29
import org .neo4j .driver .AuthTokens ;
31
30
import org .neo4j .driver .Config ;
32
31
import org .neo4j .driver .Config .ConfigBuilder ;
33
32
import org .neo4j .driver .Driver ;
34
33
35
34
import org .springframework .boot .autoconfigure .AutoConfigurations ;
35
+ import org .springframework .boot .autoconfigure .neo4j .Neo4jAutoConfiguration .PropertiesNeo4jConnectionDetails ;
36
36
import org .springframework .boot .autoconfigure .neo4j .Neo4jProperties .Authentication ;
37
37
import org .springframework .boot .autoconfigure .neo4j .Neo4jProperties .Security .TrustStrategy ;
38
38
import org .springframework .boot .context .properties .source .InvalidConfigurationPropertyValueException ;
39
39
import org .springframework .boot .test .context .FilteredClassLoader ;
40
40
import 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 ;
43
42
44
43
import static org .assertj .core .api .Assertions .assertThat ;
45
44
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
50
49
*
51
50
* @author Michael J. Simons
52
51
* @author Stephane Nicoll
52
+ * @author Moritz Halbritter
53
+ * @author Andy Wilkinson
54
+ * @author Phillip Webb
53
55
*/
54
56
class Neo4jAutoConfigurationTests {
55
57
@@ -103,6 +105,22 @@ void uriWithInvalidSchemesAreDetected(String invalidScheme) {
103
105
.hasMessageContaining ("'%s' is not a supported scheme." , invalidScheme ));
104
106
}
105
107
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
+
106
124
@ Test
107
125
void connectionTimeout () {
108
126
Neo4jProperties properties = new Neo4jProperties ();
@@ -118,8 +136,8 @@ void maxTransactionRetryTime() {
118
136
}
119
137
120
138
@ Test
121
- void determineServerUriShouldDefaultToLocalhost () {
122
- assertThat (determineServerUri ( new Neo4jProperties (), new MockEnvironment () ))
139
+ void uriShouldDefaultToLocalhost () {
140
+ assertThat (new PropertiesNeo4jConnectionDetails ( new Neo4jProperties ()). getUri ( ))
123
141
.isEqualTo (URI .create ("bolt://localhost:7687" ));
124
142
}
125
143
@@ -128,44 +146,52 @@ void determineServerUriWithCustomUriShouldOverrideDefault() {
128
146
URI customUri = URI .create ("bolt://localhost:4242" );
129
147
Neo4jProperties properties = new Neo4jProperties ();
130
148
properties .setUri (customUri );
131
- assertThat (determineServerUri ( properties , new MockEnvironment () )).isEqualTo (customUri );
149
+ assertThat (new PropertiesNeo4jConnectionDetails ( properties ). getUri ( )).isEqualTo (customUri );
132
150
}
133
151
134
152
@ Test
135
153
void authenticationShouldDefaultToNone () {
136
- assertThat (mapAuthToken (new Authentication ())).isEqualTo (AuthTokens .none ());
154
+ assertThat (new PropertiesNeo4jConnectionDetails (new Neo4jProperties ()).getAuthToken ())
155
+ .isEqualTo (AuthTokens .none ());
137
156
}
138
157
139
158
@ Test
140
159
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" ));
145
165
}
146
166
147
167
@ Test
148
168
void authenticationWithUsernameAndRealmShouldEnableBasicAuth () {
149
- Authentication authentication = new Authentication ();
169
+ Neo4jProperties properties = new Neo4jProperties ();
170
+ Authentication authentication = properties .getAuthentication ();
150
171
authentication .setUsername ("Farin" );
151
172
authentication .setPassword ("Urlaub" );
152
173
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" ));
154
176
}
155
177
156
178
@ Test
157
179
void authenticationWithKerberosTicketShouldEnableKerberos () {
158
- Authentication authentication = new Authentication ();
180
+ Neo4jProperties properties = new Neo4jProperties ();
181
+ Authentication authentication = properties .getAuthentication ();
159
182
authentication .setKerberosTicket ("AABBCCDDEE" );
160
- assertThat (mapAuthToken (authentication )).isEqualTo (AuthTokens .kerberos ("AABBCCDDEE" ));
183
+ assertThat (new PropertiesNeo4jConnectionDetails (properties ).getAuthToken ())
184
+ .isEqualTo (AuthTokens .kerberos ("AABBCCDDEE" ));
161
185
}
162
186
163
187
@ Test
164
188
void authenticationWithBothUsernameAndKerberosShouldNotBeAllowed () {
165
- Authentication authentication = new Authentication ();
189
+ Neo4jProperties properties = new Neo4jProperties ();
190
+ Authentication authentication = properties .getAuthentication ();
166
191
authentication .setUsername ("Farin" );
167
192
authentication .setKerberosTicket ("AABBCCDDEE" );
168
- assertThatIllegalStateException ().isThrownBy (() -> mapAuthToken (authentication ))
193
+ assertThatIllegalStateException ()
194
+ .isThrownBy (() -> new PropertiesNeo4jConnectionDetails (properties ).getAuthToken ())
169
195
.withMessage ("Cannot specify both username ('Farin') and kerberos ticket ('AABBCCDDEE')" );
170
196
}
171
197
@@ -279,20 +305,9 @@ void driverConfigShouldBeConfiguredToUseUseSpringJclLogging() {
279
305
assertThat (mapDriverConfig (new Neo4jProperties ()).logging ()).isInstanceOf (Neo4jSpringJclLogging .class );
280
306
}
281
307
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
-
294
308
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 ));
296
311
}
297
312
298
313
}
0 commit comments