28
28
import org .junit .jupiter .api .AfterEach ;
29
29
import org .junit .jupiter .api .BeforeEach ;
30
30
import org .junit .jupiter .api .Test ;
31
+ import org .junit .jupiter .params .ParameterizedTest ;
32
+ import org .junit .jupiter .params .provider .Arguments ;
33
+ import org .junit .jupiter .params .provider .MethodSource ;
31
34
32
35
import java .util .List ;
33
36
import java .util .Optional ;
34
37
import java .util .concurrent .TimeUnit ;
38
+ import java .util .stream .Stream ;
35
39
36
40
import static com .mongodb .ClusterFixture .isAuthenticated ;
37
41
import static com .mongodb .ClusterFixture .isLoadBalanced ;
38
42
import static com .mongodb .ClusterFixture .sleep ;
43
+ import static java .util .Optional .ofNullable ;
39
44
import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
40
45
import static org .junit .jupiter .api .Assertions .assertFalse ;
41
46
import static org .junit .jupiter .api .Assertions .assertNotNull ;
@@ -62,8 +67,25 @@ public void setUp() {
62
67
InternalStreamConnection .setRecordEverything (true );
63
68
}
64
69
65
- @ Test
66
- void shouldAppendToPreviousMetadataWhenUpdatedAfterInitialization () {
70
+ @ AfterEach
71
+ public void tearDown () {
72
+ InternalStreamConnection .setRecordEverything (false );
73
+ }
74
+
75
+ public static Stream <Arguments > provideDriverInformation () {
76
+ return Stream .of (
77
+ Arguments .of ("1.0" , "Framework" , "Framework Platform" ),
78
+ Arguments .of ("1.0" , "Framework" , null ),
79
+ Arguments .of (null , "Framework" , "Framework Platform" ),
80
+ Arguments .of (null , "Framework" , null )
81
+ );
82
+ }
83
+
84
+ @ ParameterizedTest
85
+ @ MethodSource ("provideDriverInformation" )
86
+ void shouldAppendToPreviousMetadataWhenUpdatedAfterInitialization (@ Nullable final String driverVersion ,
87
+ @ Nullable final String driverName ,
88
+ @ Nullable final String driverPlatform ) {
67
89
//given
68
90
MongoDriverInformation initialWrappingLibraryDriverInformation = MongoDriverInformation .builder ()
69
91
.driverName ("library" )
@@ -85,29 +107,31 @@ void shouldAppendToPreviousMetadataWhenUpdatedAfterInitialization() {
85
107
86
108
//when
87
109
sleep (5 ); // wait for connection to become idle
88
- mongoClient .updateMetadata (MongoDriverInformation .builder ()
89
- .driverVersion ("1.0" )
90
- .driverName ("Framework" )
91
- .driverPlatform ("Framework Platform" )
92
- .build ());
110
+ updateClientMetadata (driverVersion , driverName , driverPlatform , mongoClient );
93
111
94
112
//then
95
113
//TODO change get() to orElseThrow
96
114
BsonDocument updatedClientMetadata = executePingAndCaptureMetadataHandshake (mongoClient ).get ();
97
115
BsonDocument updatedDriverInformation = updatedClientMetadata .getDocument ("driver" );
98
116
99
- assertThat ( updatedDriverInformation . getString ( "name" ). getValue ()). isEqualTo ( generatedDriverName + "|Framework" ) ;
100
- assertThat ( updatedDriverInformation . getString ( "version" ). getValue ()). isEqualTo ( generatedVersionName + "|1.0" ) ;
101
- assertThat ( updatedClientMetadata . getString ( "platform" ). getValue ()). isEqualTo ( generatedPlatformName + "|Framework Platform" ) ;
117
+ String expectedDriverName = driverName == null ? generatedDriverName : generatedDriverName + "|" + driverName ;
118
+ String expectedDriverVersion = driverVersion == null ? generatedVersionName : generatedVersionName + "|" + driverVersion ;
119
+ String expectedDriverPlatform = driverPlatform == null ? generatedPlatformName : generatedPlatformName + "|" + driverPlatform ;
102
120
121
+ assertThat (updatedDriverInformation .getString ("name" ).getValue ()).isEqualTo (expectedDriverName );
122
+ assertThat (updatedDriverInformation .getString ("version" ).getValue ()).endsWith (expectedDriverVersion );
123
+ assertThat (updatedClientMetadata .getString ("platform" ).getValue ()).endsWith (expectedDriverPlatform );
103
124
assertThat (withRemovedKeys (updatedClientMetadata , "driver" , "platform" ))
104
125
.usingRecursiveAssertion ()
105
126
.isEqualTo (withRemovedKeys (initialClientMetadata , "driver" , "platform" ));
106
127
}
107
128
}
108
129
109
- @ Test
110
- void shouldAppendToDefaultClientMetadataWhenUpdatedAfterInitialization () {
130
+ @ ParameterizedTest
131
+ @ MethodSource ("provideDriverInformation" )
132
+ void shouldAppendToDefaultClientMetadataWhenUpdatedAfterInitialization (@ Nullable final String driverVersion ,
133
+ @ Nullable final String driverName ,
134
+ @ Nullable final String driverPlatform ) {
111
135
//given
112
136
try (MongoClient mongoClient = createMongoClient (null , getMongoClientSettingsBuilder ()
113
137
.applyToConnectionPoolSettings (builder ->
@@ -124,21 +148,20 @@ void shouldAppendToDefaultClientMetadataWhenUpdatedAfterInitialization() {
124
148
125
149
//when
126
150
sleep (5 ); // wait for connection to become idle
127
- mongoClient .updateMetadata (MongoDriverInformation .builder ()
128
- .driverVersion ("1.0" )
129
- .driverName ("Framework" )
130
- .driverPlatform ("Framework Platform" )
131
- .build ());
151
+ updateClientMetadata (driverVersion , driverName , driverPlatform , mongoClient );
132
152
133
153
//then
134
154
//TODO change get() to orElseThrow
135
155
BsonDocument updatedClientMetadata = executePingAndCaptureMetadataHandshake (mongoClient ).get ();
136
156
BsonDocument updatedDriverInformation = updatedClientMetadata .getDocument ("driver" );
137
157
138
- assertThat ( updatedDriverInformation . getString ( "name" ). getValue ()). isEqualTo ( generatedDriverName + "|Framework" ) ;
139
- assertThat ( updatedDriverInformation . getString ( "version" ). getValue ()). endsWith ( generatedVersionName + "|1.0" ) ;
140
- assertThat ( updatedClientMetadata . getString ( "platform" ). getValue ()). endsWith ( generatedPlatformName + "|Framework Platform" ) ;
158
+ String expectedDriverName = driverName == null ? generatedDriverName : generatedDriverName + "|" + driverName ;
159
+ String expectedDriverVersion = driverVersion == null ? generatedVersionName : generatedVersionName + "|" + driverVersion ;
160
+ String expectedDriverPlatform = driverPlatform == null ? generatedPlatformName : generatedPlatformName + "|" + driverPlatform ;
141
161
162
+ assertThat (updatedDriverInformation .getString ("name" ).getValue ()).isEqualTo (expectedDriverName );
163
+ assertThat (updatedDriverInformation .getString ("version" ).getValue ()).endsWith (expectedDriverVersion );
164
+ assertThat (updatedClientMetadata .getString ("platform" ).getValue ()).endsWith (expectedDriverPlatform );
142
165
assertThat (withRemovedKeys (updatedClientMetadata , "driver" , "platform" ))
143
166
.usingRecursiveAssertion ()
144
167
.isEqualTo (withRemovedKeys (initialClientMetadata , "driver" , "platform" ));
@@ -172,8 +195,9 @@ void shouldNotCloseExistingConnectionsToUpdateMetadata() {
172
195
}
173
196
}
174
197
198
+ // Not a prose test. Additional test for better coverage.
175
199
@ Test
176
- void shouldAppendAppendProvidedMetadatDuringInitialization () {
200
+ void shouldAppendProvidedMetadatDuringInitialization () {
177
201
//given
178
202
MongoDriverInformation initialWrappingLibraryDriverInformation = MongoDriverInformation .builder ()
179
203
.driverName ("library" )
@@ -218,11 +242,6 @@ protected MongoClientSettings.Builder getMongoClientSettingsBuilder() {
218
242
builder .addConnectionPoolListener (connectionPoolListener ));
219
243
}
220
244
221
- @ AfterEach
222
- public void tearDown () {
223
- InternalStreamConnection .setRecordEverything (false );
224
- }
225
-
226
245
private static BsonDocument withRemovedKeys (final BsonDocument updatedClientMetadata ,
227
246
final String ... keysToFilter ) {
228
247
BsonDocument clone = updatedClientMetadata .clone ();
@@ -231,5 +250,17 @@ private static BsonDocument withRemovedKeys(final BsonDocument updatedClientMeta
231
250
}
232
251
return clone ;
233
252
}
253
+
254
+ private static void updateClientMetadata (@ Nullable final String driverVersion ,
255
+ @ Nullable final String driverName ,
256
+ @ Nullable final String driverPlatform ,
257
+ final MongoClient mongoClient ) {
258
+ MongoDriverInformation .Builder builder ;
259
+ builder = MongoDriverInformation .builder ();
260
+ ofNullable (driverName ).ifPresent (builder ::driverName );
261
+ ofNullable (driverVersion ).ifPresent (builder ::driverVersion );
262
+ ofNullable (driverPlatform ).ifPresent (builder ::driverPlatform );
263
+ mongoClient .updateMetadata (builder .build ());
264
+ }
234
265
}
235
266
0 commit comments