Skip to content

Commit c4222c6

Browse files
committed
JAVA-2656: Deprecate MongoClientSettings methods that take or return a list of credentials, and add otherwise equivalent methods that take or return a single credential
1 parent aa8a99c commit c4222c6

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

driver-async/src/main/com/mongodb/async/client/MongoClientSettings.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
import java.util.Collections;
3838
import java.util.List;
3939

40+
import static com.mongodb.assertions.Assertions.isTrue;
4041
import static com.mongodb.assertions.Assertions.isTrueArgument;
4142
import static com.mongodb.assertions.Assertions.notNull;
43+
import static java.util.Collections.singletonList;
4244

4345

4446
/**
@@ -272,12 +274,27 @@ public Builder readConcern(final ReadConcern readConcern) {
272274
* @param credentialList the credential list
273275
* @return {@code this}
274276
* @see MongoClientSettings#getCredentialList()
277+
* @deprecated Prefer {@link #credential(MongoCredential)}
275278
*/
279+
@Deprecated
276280
public Builder credentialList(final List<MongoCredential> credentialList) {
277281
this.credentialList = Collections.unmodifiableList(notNull("credentialList", credentialList));
278282
return this;
279283
}
280284

285+
/**
286+
* Sets the credential.
287+
*
288+
* @param credential the credential
289+
* @return {@code this}
290+
* @see MongoClientSettings#getCredentialList()
291+
* @since 3.6
292+
*/
293+
public Builder credential(final MongoCredential credential) {
294+
this.credentialList = singletonList(notNull("credential", credential));
295+
return this;
296+
}
297+
281298
/**
282299
* Sets the codec registry
283300
*
@@ -379,11 +396,24 @@ public ReadPreference getReadPreference() {
379396
* Gets the credential list.
380397
*
381398
* @return the credential list
399+
* @deprecated Prefer {@link #getCredential()}
382400
*/
401+
@Deprecated
383402
public List<MongoCredential> getCredentialList() {
384403
return credentialList;
385404
}
386405

406+
/**
407+
* Gets the credential list.
408+
*
409+
* @return the credential list
410+
* @since 3.6
411+
*/
412+
public MongoCredential getCredential() {
413+
isTrue("Single or no credential", credentialList.size() <= 1);
414+
return credentialList.isEmpty() ? null : credentialList.get(0);
415+
}
416+
387417
/**
388418
* The write concern to use.
389419
*

driver-async/src/test/unit/com/mongodb/async/client/MongoClientSettingsSpecification.groovy

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class MongoClientSettingsSpecification extends Specification {
5454
options.serverSettings == ServerSettings.builder().build()
5555
options.streamFactoryFactory == null
5656
options.compressorList == []
57+
options.credentialList == []
58+
options.credential == null
5759
}
5860

5961
@SuppressWarnings('UnnecessaryObjectReferences')
@@ -101,6 +103,11 @@ class MongoClientSettingsSpecification extends Specification {
101103
then:
102104
thrown(IllegalArgumentException)
103105

106+
when:
107+
builder.credential(null)
108+
then:
109+
thrown(IllegalArgumentException)
110+
104111
when:
105112
builder.credentialList(null)
106113
then:
@@ -140,6 +147,7 @@ class MongoClientSettingsSpecification extends Specification {
140147
def commandListener = Stub(CommandListener)
141148
def clusterSettings = ClusterSettings.builder().hosts([new ServerAddress('localhost')]).requiredReplicaSetName('test').build()
142149

150+
when:
143151
def options = MongoClientSettings.builder()
144152
.readPreference(ReadPreference.secondary())
145153
.writeConcern(WriteConcern.JOURNALED)
@@ -159,7 +167,7 @@ class MongoClientSettingsSpecification extends Specification {
159167
.compressorList([MongoCompressor.createZlibCompressor()])
160168
.build()
161169

162-
expect:
170+
then:
163171
options.getReadPreference() == ReadPreference.secondary()
164172
options.getWriteConcern() == WriteConcern.JOURNALED
165173
options.getRetryWrites()
@@ -172,10 +180,20 @@ class MongoClientSettingsSpecification extends Specification {
172180
options.serverSettings == serverSettings
173181
options.codecRegistry == codecRegistry
174182
options.credentialList == credentialList
183+
options.credential == credentialList.get(0)
175184
options.connectionPoolSettings == connectionPoolSettings
176185
options.clusterSettings == clusterSettings
177186
options.streamFactoryFactory == streamFactoryFactory
178187
options.compressorList == [MongoCompressor.createZlibCompressor()]
188+
189+
when:
190+
options = MongoClientSettings.builder()
191+
.credential(credentialList.get(0))
192+
.build()
193+
194+
then:
195+
options.credentialList == credentialList
196+
options.credential == credentialList.get(0)
179197
}
180198

181199
def 'should be easy to create new options from existing'() {

0 commit comments

Comments
 (0)