Skip to content

Commit 95c1bb1

Browse files
committed
Add parametrized tests.
JAVA-5870
1 parent e9f8dd6 commit 95c1bb1

File tree

3 files changed

+78
-40
lines changed

3 files changed

+78
-40
lines changed

driver-core/src/main/com/mongodb/internal/connection/ClientMetadataHelper.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.File;
3333
import java.nio.charset.StandardCharsets;
3434
import java.nio.file.Files;
35+
import java.util.ArrayList;
3536
import java.util.List;
3637
import java.util.function.Consumer;
3738

@@ -185,22 +186,25 @@ public static BsonDocument updateClientMedataDocument(final BsonDocument clientM
185186
BsonDocument updatedClientMetadataDocument = clientMetadataDocument.clone();
186187
BsonDocument driverInformation = clientMetadataDocument.getDocument("driver");
187188

188-
MongoDriverInformation.Builder builder = MongoDriverInformation.builder(mongoDriverInformation)
189-
.driverName(driverInformation.getString("name").getValue())
190-
.driverVersion(driverInformation.getString("version").getValue());
189+
List<String> updatedDriverNames = new ArrayList<>();
190+
List<String> updatedDriverVersions = new ArrayList<>();
191+
List<String> updateDriverPlatforms = new ArrayList<>();
191192

192-
if (updatedClientMetadataDocument.containsKey("platform")) {
193-
builder.driverPlatform(updatedClientMetadataDocument.getString("platform").getValue());
194-
}
193+
updatedDriverNames.add(driverInformation.getString("name").getValue());
194+
updatedDriverNames.addAll(mongoDriverInformation.getDriverNames());
195+
196+
updatedDriverVersions.add(driverInformation.getString("version").getValue());
197+
updatedDriverVersions.addAll(mongoDriverInformation.getDriverVersions());
195198

196-
MongoDriverInformation updatedDriverInformation = builder.build();
199+
updateDriverPlatforms.add(clientMetadataDocument.getString("platform").getValue());
200+
updateDriverPlatforms.addAll(mongoDriverInformation.getDriverPlatforms());
197201

198202
tryWithLimit(updatedClientMetadataDocument, d -> {
199-
putAtPath(d, "driver.name", listToString(updatedDriverInformation.getDriverNames()));
200-
putAtPath(d, "driver.version", listToString(updatedDriverInformation.getDriverVersions()));
203+
putAtPath(d, "driver.name", listToString(updatedDriverNames));
204+
putAtPath(d, "driver.version", listToString(updatedDriverVersions));
201205
});
202206
tryWithLimit(updatedClientMetadataDocument, d -> {
203-
putAtPath(d, "platform", listToString(updatedDriverInformation.getDriverPlatforms()));
207+
putAtPath(d, "platform", listToString(updateDriverPlatforms));
204208
});
205209
return updatedClientMetadataDocument;
206210
}

driver-kotlin-coroutine/src/test/kotlin/com/mongodb/kotlin/client/coroutine/MongoClientTest.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ class MongoClientTest {
4444

4545
@Test
4646
fun shouldHaveTheSameMethods() {
47-
val jMongoClientFunctions = JMongoClient::class.declaredFunctions.map { it.name }
48-
// TODO-JAVA-5871 remove .filterNot { it == "updateMetadata" }
49-
.filterNot { it == "updateMetadata" }
50-
.toSet()
47+
val jMongoClientFunctions =
48+
JMongoClient::class
49+
.declaredFunctions
50+
.map { it.name }
51+
// TODO-JAVA-5871 remove .filterNot { it == "updateMetadata" }
52+
.filterNot { it == "updateMetadata" }
53+
.toSet()
5154
val kMongoClientFunctions = MongoClient::class.declaredFunctions.map { it.name }.toSet()
5255

5356
assertEquals(jMongoClientFunctions, kMongoClientFunctions)

driver-sync/src/test/functional/com/mongodb/client/AbstractClientMetadataProseTest.java

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@
2828
import org.junit.jupiter.api.AfterEach;
2929
import org.junit.jupiter.api.BeforeEach;
3030
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;
3134

3235
import java.util.List;
3336
import java.util.Optional;
3437
import java.util.concurrent.TimeUnit;
38+
import java.util.stream.Stream;
3539

3640
import static com.mongodb.ClusterFixture.isAuthenticated;
3741
import static com.mongodb.ClusterFixture.isLoadBalanced;
3842
import static com.mongodb.ClusterFixture.sleep;
43+
import static java.util.Optional.ofNullable;
3944
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
4045
import static org.junit.jupiter.api.Assertions.assertFalse;
4146
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -62,8 +67,25 @@ public void setUp() {
6267
InternalStreamConnection.setRecordEverything(true);
6368
}
6469

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) {
6789
//given
6890
MongoDriverInformation initialWrappingLibraryDriverInformation = MongoDriverInformation.builder()
6991
.driverName("library")
@@ -85,29 +107,31 @@ void shouldAppendToPreviousMetadataWhenUpdatedAfterInitialization() {
85107

86108
//when
87109
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);
93111

94112
//then
95113
//TODO change get() to orElseThrow
96114
BsonDocument updatedClientMetadata = executePingAndCaptureMetadataHandshake(mongoClient).get();
97115
BsonDocument updatedDriverInformation = updatedClientMetadata.getDocument("driver");
98116

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;
102120

121+
assertThat(updatedDriverInformation.getString("name").getValue()).isEqualTo(expectedDriverName);
122+
assertThat(updatedDriverInformation.getString("version").getValue()).endsWith(expectedDriverVersion);
123+
assertThat(updatedClientMetadata.getString("platform").getValue()).endsWith(expectedDriverPlatform);
103124
assertThat(withRemovedKeys(updatedClientMetadata, "driver", "platform"))
104125
.usingRecursiveAssertion()
105126
.isEqualTo(withRemovedKeys(initialClientMetadata, "driver", "platform"));
106127
}
107128
}
108129

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) {
111135
//given
112136
try (MongoClient mongoClient = createMongoClient(null, getMongoClientSettingsBuilder()
113137
.applyToConnectionPoolSettings(builder ->
@@ -124,21 +148,20 @@ void shouldAppendToDefaultClientMetadataWhenUpdatedAfterInitialization() {
124148

125149
//when
126150
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);
132152

133153
//then
134154
//TODO change get() to orElseThrow
135155
BsonDocument updatedClientMetadata = executePingAndCaptureMetadataHandshake(mongoClient).get();
136156
BsonDocument updatedDriverInformation = updatedClientMetadata.getDocument("driver");
137157

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;
141161

162+
assertThat(updatedDriverInformation.getString("name").getValue()).isEqualTo(expectedDriverName);
163+
assertThat(updatedDriverInformation.getString("version").getValue()).endsWith(expectedDriverVersion);
164+
assertThat(updatedClientMetadata.getString("platform").getValue()).endsWith(expectedDriverPlatform);
142165
assertThat(withRemovedKeys(updatedClientMetadata, "driver", "platform"))
143166
.usingRecursiveAssertion()
144167
.isEqualTo(withRemovedKeys(initialClientMetadata, "driver", "platform"));
@@ -172,8 +195,9 @@ void shouldNotCloseExistingConnectionsToUpdateMetadata() {
172195
}
173196
}
174197

198+
// Not a prose test. Additional test for better coverage.
175199
@Test
176-
void shouldAppendAppendProvidedMetadatDuringInitialization() {
200+
void shouldAppendProvidedMetadatDuringInitialization() {
177201
//given
178202
MongoDriverInformation initialWrappingLibraryDriverInformation = MongoDriverInformation.builder()
179203
.driverName("library")
@@ -218,11 +242,6 @@ protected MongoClientSettings.Builder getMongoClientSettingsBuilder() {
218242
builder.addConnectionPoolListener(connectionPoolListener));
219243
}
220244

221-
@AfterEach
222-
public void tearDown() {
223-
InternalStreamConnection.setRecordEverything(false);
224-
}
225-
226245
private static BsonDocument withRemovedKeys(final BsonDocument updatedClientMetadata,
227246
final String... keysToFilter) {
228247
BsonDocument clone = updatedClientMetadata.clone();
@@ -231,5 +250,17 @@ private static BsonDocument withRemovedKeys(final BsonDocument updatedClientMeta
231250
}
232251
return clone;
233252
}
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+
}
234265
}
235266

0 commit comments

Comments
 (0)