Skip to content

Commit cdd2a89

Browse files
committed
Fix notNull checks and add unit tests for listDatabaseNames and listCollectionNames
1 parent 4aff442 commit cdd2a89

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

driver/src/main/com/mongodb/MappingIterable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class MappingIterable<U, V> implements MongoIterable<V> {
3131
this.mapper = mapper;
3232
}
3333

34+
MongoIterable<U> getMapped() {
35+
return iterable;
36+
}
37+
3438
@Override
3539
public MongoCursor<V> iterator() {
3640
return new MongoMappingCursor<U, V>(iterable.iterator(), mapper);

driver/src/main/com/mongodb/MongoClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,11 @@ public MongoIterable<String> listDatabaseNames() {
382382
* @mongodb.driver.manual reference/command/listDatabases List Databases
383383
*/
384384
public MongoIterable<String> listDatabaseNames(final ClientSession clientSession) {
385+
notNull("clientSession", clientSession);
385386
return executeListDatabaseNames(clientSession);
386387
}
387388

388389
private MongoIterable<String> executeListDatabaseNames(final ClientSession clientSession) {
389-
notNull("clientSession", clientSession);
390390
return executeListDatabases(clientSession, BsonDocument.class).map(new Function<BsonDocument, String>() {
391391
@Override
392392
public String apply(final BsonDocument result) {

driver/src/main/com/mongodb/MongoDatabaseImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public void drop() {
170170

171171
@Override
172172
public void drop(final ClientSession clientSession) {
173+
notNull("clientSession", clientSession);
173174
executeDrop(clientSession);
174175
}
175176

@@ -184,11 +185,11 @@ public MongoIterable<String> listCollectionNames() {
184185

185186
@Override
186187
public MongoIterable<String> listCollectionNames(final ClientSession clientSession) {
188+
notNull("clientSession", clientSession);
187189
return executeListCollectionNames(clientSession);
188190
}
189191

190192
private MongoIterable<String> executeListCollectionNames(final ClientSession clientSession) {
191-
notNull("clientSession", clientSession);
192193
return executeListCollections(clientSession, BsonDocument.class)
193194
.map(new Function<BsonDocument, String>() {
194195
@Override
@@ -296,12 +297,12 @@ public void createView(final ClientSession clientSession, final String viewName,
296297
@Override
297298
public void createView(final ClientSession clientSession, final String viewName, final String viewOn,
298299
final List<? extends Bson> pipeline, final CreateViewOptions createViewOptions) {
300+
notNull("clientSession", clientSession);
299301
executeCreateView(clientSession, viewName, viewOn, pipeline, createViewOptions);
300302
}
301303

302304
private void executeCreateView(final ClientSession clientSession, final String viewName, final String viewOn,
303305
final List<? extends Bson> pipeline, final CreateViewOptions createViewOptions) {
304-
notNull("clientSession", clientSession);
305306
notNull("createViewOptions", createViewOptions);
306307
executor.execute(new CreateViewOperation(name, viewName, viewOn, createBsonDocumentList(pipeline), writeConcern)
307308
.collation(createViewOptions.getCollation()),

driver/src/test/unit/com/mongodb/MongoClientSpecification.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ class MongoClientSpecification extends Specification {
4444
given:
4545
def executor = new TestOperationExecutor([null, null])
4646
def client = Spy(MongoClient) {
47-
2 * createOperationExecutor() >> {
47+
3 * createOperationExecutor() >> {
4848
executor
4949
}
5050
}
5151
def listDatabasesMethod = client.&listDatabases
52+
def listDatabasesNamesMethod = client.&listDatabaseNames
5253

5354
when:
5455
def listDatabasesIterable = execute(listDatabasesMethod, session)
@@ -64,6 +65,14 @@ class MongoClientSpecification extends Specification {
6465
expect listDatabasesIterable, isTheSameAs(new ListDatabasesIterableImpl<BsonDocument>(session, BsonDocument,
6566
getDefaultCodecRegistry(), primary(), executor))
6667

68+
when:
69+
def listDatabaseNamesIterable = execute(listDatabasesNamesMethod, session)
70+
71+
then:
72+
// listDatabaseNamesIterable is an instance of a MappingIterable, so have to get the mapped iterable inside it
73+
expect listDatabaseNamesIterable.getMapped(), isTheSameAs(new ListDatabasesIterableImpl<BsonDocument>(session, BsonDocument,
74+
getDefaultCodecRegistry(), primary(), executor))
75+
6776
cleanup:
6877
client?.close()
6978

driver/src/test/unit/com/mongodb/MongoDatabaseSpecification.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class MongoDatabaseSpecification extends Specification {
208208
def executor = new TestOperationExecutor([null, null, null])
209209
def database = new MongoDatabaseImpl(name, codecRegistry, readPreference, writeConcern, readConcern, executor)
210210
def listCollectionsMethod = database.&listCollections
211+
def listCollectionNamesMethod = database.&listCollectionNames
211212

212213
when:
213214
def listCollectionIterable = execute(listCollectionsMethod, session)
@@ -223,6 +224,14 @@ class MongoDatabaseSpecification extends Specification {
223224
expect listCollectionIterable, isTheSameAs(new ListCollectionsIterableImpl<BsonDocument>(session, name, BsonDocument, codecRegistry,
224225
primary(), executor))
225226

227+
when:
228+
def listCollectionNamesIterable = execute(listCollectionNamesMethod, session)
229+
230+
then:
231+
// listCollectionNamesIterable is an instance of a MappingIterable, so have to get the mapped iterable inside it
232+
expect listCollectionNamesIterable.getMapped(), isTheSameAs(new ListCollectionsIterableImpl<BsonDocument>(session, name,
233+
BsonDocument, codecRegistry, primary(), executor))
234+
226235
where:
227236
session << [null, Stub(ClientSession)]
228237
}

0 commit comments

Comments
 (0)