Skip to content

Support authorizedCollections option for listCollections helpers #1270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<ID>LongMethod:KotlinSerializerCodecTest.kt$KotlinSerializerCodecTest$@Test fun testDataClassOptionalBsonValues()</ID>
<ID>MaxLineLength:MapReduceFlow.kt$MapReduceFlow$*</ID>
<ID>MaxLineLength:MapReduceIterable.kt$MapReduceIterable$*</ID>
<ID>MaxLineLength:ListCollectionsFlow.kt$ListCollectionsFlow$*</ID>
<ID>MaxLineLength:ListCollectionsIterable.kt$ListCollectionsIterable$*</ID>
<ID>MaxLineLength:ListCollectionNamesIterable.kt$ListCollectionNamesIterable$*</ID>
<ID>MaxLineLength:ListCollectionNamesFlow.kt$ListCollectionNamesFlow$*</ID>
<ID>SwallowedException:MockitoHelper.kt$MockitoHelper.DeepReflectionEqMatcher$e: Throwable</ID>
<ID>TooManyFunctions:ClientSession.kt$ClientSession : jClientSession</ID>
<ID>TooManyFunctions:FindFlow.kt$FindFlow&lt;T : Any> : Flow</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,11 @@ public AsyncWriteOperation<Void> dropIndex(final Bson keys, final DropIndexOptio
}

public <TResult> AsyncReadOperation<AsyncBatchCursor<TResult>> listCollections(final String databaseName, final Class<TResult> resultClass,
final Bson filter, final boolean collectionNamesOnly,
final Bson filter, final boolean collectionNamesOnly, final boolean authorizedCollections,
final Integer batchSize, final long maxTimeMS,
final BsonValue comment) {
return operations.listCollections(databaseName, resultClass, filter, collectionNamesOnly, batchSize, maxTimeMS, comment);
return operations.listCollections(databaseName, resultClass, filter, collectionNamesOnly, authorizedCollections,
batchSize, maxTimeMS, comment);
}

public <TResult> AsyncReadOperation<AsyncBatchCursor<TResult>> listDatabases(final Class<TResult> resultClass, final Bson filter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.mongodb.MongoCommandException;
import com.mongodb.MongoNamespace;
import com.mongodb.internal.VisibleForTesting;
import com.mongodb.internal.async.AsyncBatchCursor;
import com.mongodb.internal.async.SingleResultCallback;
import com.mongodb.internal.async.function.AsyncCallbackSupplier;
Expand All @@ -37,6 +38,7 @@
import java.util.function.Supplier;

import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback;
import static com.mongodb.internal.operation.AsyncOperationHelper.CommandReadTransformerAsync;
import static com.mongodb.internal.operation.AsyncOperationHelper.createReadCommandAndExecuteAsync;
Expand All @@ -49,6 +51,7 @@
import static com.mongodb.internal.operation.CommandOperationHelper.rethrowIfNotNamespaceError;
import static com.mongodb.internal.operation.CursorHelper.getCursorDocumentFromBatchSize;
import static com.mongodb.internal.operation.DocumentHelper.putIfNotNull;
import static com.mongodb.internal.operation.DocumentHelper.putIfTrue;
import static com.mongodb.internal.operation.OperationHelper.LOGGER;
import static com.mongodb.internal.operation.OperationHelper.canRetryRead;
import static com.mongodb.internal.operation.SingleBatchCursor.createEmptySingleBatchCursor;
Expand All @@ -62,6 +65,8 @@
* An operation that provides a cursor allowing iteration through the metadata of all the collections in a database. This operation
* ensures that the value of the {@code name} field of each returned document is the simple name of the collection rather than the full
* namespace.
* <p>
* See <a href="https://docs.mongodb.com/manual/reference/command/listCollections/">{@code listCollections}</a></p>.
*
* <p>This class is not part of the public API and may be removed or changed at any time</p>
*/
Expand All @@ -73,6 +78,7 @@ public class ListCollectionsOperation<T> implements AsyncReadOperation<AsyncBatc
private int batchSize;
private long maxTimeMS;
private boolean nameOnly;
private boolean authorizedCollections;
private BsonValue comment;

public ListCollectionsOperation(final String databaseName, final Decoder<T> decoder) {
Expand Down Expand Up @@ -137,6 +143,20 @@ public ListCollectionsOperation<T> comment(@Nullable final BsonValue comment) {
return this;
}

public ListCollectionsOperation<T> authorizedCollections(final boolean authorizedCollections) {
this.authorizedCollections = authorizedCollections;
return this;
}

/**
* This method is used by tests via the reflection API. See
* {@code com.mongodb.reactivestreams.client.internal.TestHelper.assertOperationIsTheSameAs}.
*/
@VisibleForTesting(otherwise = PRIVATE)
public boolean isAuthorizedCollections() {
return authorizedCollections;
}

@Override
public BatchCursor<T> execute(final ReadBinding binding) {
RetryState retryState = initialRetryState(retryReads);
Expand Down Expand Up @@ -206,6 +226,7 @@ private BsonDocument getCommand() {
if (nameOnly) {
command.append("nameOnly", BsonBoolean.TRUE);
}
putIfTrue(command, "authorizedCollections", authorizedCollections);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, add a unit test for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

The added ListCollectionsOperationTest tests the public method ListCollectionsOperation.execute instead of testing the (currently private) method getCommand. This way it covers more of ListCollectionsOperation. Since mocking is needed for this, I introduced MongoMockito.mock (see its documentation for the detailed explanation and links to demo tests). I demonstrated it to @vbabanin and @rozza, and my understanding was that they liked this improvement over Mockito.mock.

if (maxTimeMS > 0) {
command.put("maxTimeMS", new BsonInt64(maxTimeMS));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,14 @@ DropIndexOperation dropIndex(final Bson keys, final DropIndexOptions dropIndexOp

<TResult> ListCollectionsOperation<TResult> listCollections(final String databaseName, final Class<TResult> resultClass,
final Bson filter, final boolean collectionNamesOnly,
final boolean authorizedCollections,
@Nullable final Integer batchSize, final long maxTimeMS,
final BsonValue comment) {
return new ListCollectionsOperation<>(databaseName, codecRegistry.get(resultClass))
.retryReads(retryReads)
.filter(toBsonDocument(filter))
.nameOnly(collectionNamesOnly)
.authorizedCollections(authorizedCollections)
.batchSize(batchSize == null ? 0 : batchSize)
.maxTime(maxTimeMS, MILLISECONDS)
.comment(comment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,11 @@ public WriteOperation<Void> dropIndex(final Bson keys, final DropIndexOptions op

public <TResult> ReadOperation<BatchCursor<TResult>> listCollections(final String databaseName, final Class<TResult> resultClass,
final Bson filter, final boolean collectionNamesOnly,
final boolean authorizedCollections,
@Nullable final Integer batchSize, final long maxTimeMS,
final BsonValue comment) {
return operations.listCollections(databaseName, resultClass, filter, collectionNamesOnly, batchSize, maxTimeMS, comment);
return operations.listCollections(databaseName, resultClass, filter, collectionNamesOnly, authorizedCollections,
batchSize, maxTimeMS, comment);
}

public <TResult> ReadOperation<BatchCursor<TResult>> listDatabases(final Class<TResult> resultClass, final Bson filter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,28 @@ class ListCollectionsOperationSpecification extends OperationFunctionalSpecifica
collection.size() == 2
}

@IgnoreIf({ serverVersionLessThan(4, 0) })
def 'should only get collection names when nameOnly and authorizedCollections are requested'() {
given:
def operation = new ListCollectionsOperation(databaseName, new DocumentCodec())
.nameOnly(true)
.authorizedCollections(true)
getCollectionHelper().create('collection6', new CreateCollectionOptions())

when:
def cursor = operation.execute(getBinding())
def collection = cursor.next()[0]

then:
collection.size() == 2
}

@IgnoreIf({ serverVersionLessThan(3, 4) || serverVersionAtLeast(4, 0) })
def 'should only get all field names when nameOnly is requested on server versions that do not support nameOnly'() {
given:
def operation = new ListCollectionsOperation(databaseName, new DocumentCodec())
.nameOnly(true)
getCollectionHelper().create('collection6', new CreateCollectionOptions())
getCollectionHelper().create('collection7', new CreateCollectionOptions())

when:
def cursor = operation.execute(getBinding())
Expand All @@ -205,6 +221,21 @@ class ListCollectionsOperationSpecification extends OperationFunctionalSpecifica
collection.size() > 2
}

@IgnoreIf({ serverVersionLessThan(4, 0) })
def 'should get all fields when authorizedCollections is requested and nameOnly is not requested'() {
given:
def operation = new ListCollectionsOperation(databaseName, new DocumentCodec())
.nameOnly(false)
.authorizedCollections(true)
getCollectionHelper().create('collection8', new CreateCollectionOptions())

when:
def cursor = operation.execute(getBinding())
def collection = cursor.next()[0]

then:
collection.size() > 2
}

def 'should return collection names if a collection exists asynchronously'() {
given:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal.mockito;

import com.mongodb.lang.Nullable;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.function.Consumer;

import static com.mongodb.assertions.Assertions.fail;
import static java.lang.String.format;

/**
* @see MongoMockito#mock(Class, Consumer)
*/
final class InsufficientStubbingDetector implements Answer<Void> {
private boolean enabled;

InsufficientStubbingDetector() {
}

@Nullable
@Override
public Void answer(final InvocationOnMock invocation) throws AssertionError {
if (enabled) {
throw fail(format("Insufficient stubbing. Unexpected invocation %s on the object %s.", invocation, invocation.getMock()));
}
return null;
}

void enable() {
enabled = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal.mockito;

import com.mongodb.internal.binding.ReadBinding;
import com.mongodb.internal.connection.OperationContext;
import com.mongodb.internal.diagnostics.logging.Logger;
import com.mongodb.internal.diagnostics.logging.Loggers;
import com.mongodb.internal.operation.ListCollectionsOperation;
import org.bson.BsonDocument;
import org.bson.codecs.BsonDocumentCodec;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.internal.stubbing.answers.ThrowsException;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

final class InsufficientStubbingDetectorDemoTest {
private static final Logger LOGGER = Loggers.getLogger(InsufficientStubbingDetectorDemoTest.class.getSimpleName());

private ListCollectionsOperation<BsonDocument> operation;

@BeforeEach
void beforeEach() {
operation = new ListCollectionsOperation<>("db", new BsonDocumentCodec());
}

@Test
void mockObjectWithDefaultAnswer() {
ReadBinding binding = Mockito.mock(ReadBinding.class);
LOGGER.info("", assertThrows(NullPointerException.class, () -> operation.execute(binding)));
}

@Test
void mockObjectWithThrowsException() {
ReadBinding binding = Mockito.mock(ReadBinding.class,
new ThrowsException(new AssertionError("Insufficient stubbing for " + ReadBinding.class)));
LOGGER.info("", assertThrows(AssertionError.class, () -> operation.execute(binding)));
}

@Test
void mockObjectWithInsufficientStubbingDetector() {
ReadBinding binding = MongoMockito.mock(ReadBinding.class);
LOGGER.info("", assertThrows(AssertionError.class, () -> operation.execute(binding)));
}

@Test
void stubbingWithThrowsException() {
ReadBinding binding = Mockito.mock(ReadBinding.class,
new ThrowsException(new AssertionError("Unfortunately, you cannot do stubbing")));
assertThrows(AssertionError.class, () -> when(binding.getOperationContext()).thenReturn(new OperationContext()));
}

@Test
void stubbingWithInsufficientStubbingDetector() {
MongoMockito.mock(ReadBinding.class, bindingMock ->
when(bindingMock.getOperationContext()).thenReturn(new OperationContext())
);
}
Comment on lines +69 to +74
Copy link
Member Author

@stIncMale stIncMale Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, unused Mockito stubbing is not reported when we run tests (ideally, stubbingWithInsufficientStubbingDetector must fail with UnnecessaryStubbingException because the test never calls getOperationContext). This has nothing to do with MongoMockito, as I tried the same with Mockito, and nothing was reported there either. Even calling Mockito.validateMockitoUsage didn't help. If someone has any ideas, please comment.

Copy link
Member

@rozza rozza Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the juniper extension and 💥

@ExtendWith(MockitoExtension.class)
final class InsufficientStubbingDetectorDemoTest {

Copy link
Member Author

@stIncMale stIncMale Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, thank you! After looking at MockitoExtension's code, I discovered that the following manually written code also enables validation:

private MockitoSession session;

@BeforeEach
void beforeEach() {
    session = Mockito.mockitoSession().startMocking();
}

@AfterEach
void afterEach() {
    session.finishMocking();
}

finishMocking calls Mockito.validateMockitoUsage, but only after notifying Mockito.framework() that a test has finished via UniversalTestListener.testFinished.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, actually, it's the UniversalTestListener.testFinished that checks unused stubbing, not Mockito.validateMockitoUsage.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal.mockito;

import com.mongodb.lang.Nullable;
import org.mockito.Answers;
import org.mockito.Mockito;
import org.mockito.internal.stubbing.answers.ThrowsException;
import org.mockito.stubbing.OngoingStubbing;

import java.util.function.Consumer;

import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

/**
* Complements {@link Mockito}.
*/
public final class MongoMockito {
/**
* Is equivalent to calling {@link #mock(Class, Consumer)} with a {@code null} {@code tuner}.
*/
public static <T> T mock(final Class<T> classToMock) {
return mock(classToMock, null);
}

/**
* This method is similar to {@link Mockito#mock(Class)} but changes the default behavior of the methods of a mock object
* such that insufficient stubbing is detected and reported. By default, Mockito uses {@link Answers#RETURNS_DEFAULTS}.
* While this answer has potential to save users some stubbing work, the provided convenience may not be worth the cost:
* if the default result (often {@code null} for reference types) is insufficient,
* one likely gets an unhelpful {@link NullPointerException}
* (see {@link InsufficientStubbingDetectorDemoTest#mockObjectWithDefaultAnswer()}),
* or a silent incorrect behavior with no clear indication of the mock object method that caused the problem.
* Furthermore, a working test that uses mock objects may be unwittingly broken when refactoring production code.
* While this particular issue is inherent to tests that use mock objects,
* broken tests not indicating clearly what is wrong make matters worse.
* <p>
* Mockito has {@link ThrowsException},
* and at first glance it may seem like using it may help detecting insufficient stubbing.
* It can point us to a line where the insufficiently stubbed method was called at, but it cannot tell us the name of that method
* (see {@link InsufficientStubbingDetectorDemoTest#mockObjectWithThrowsException()}).
* Moreover, a mock object created with {@link ThrowsException} as its default answer cannot be stubbed:
* stubbing requires calling methods of the mock object, but they all complete abruptly
* (see {@link InsufficientStubbingDetectorDemoTest#stubbingWithThrowsException()}).
* Therefore, {@link ThrowsException} is not suitable for detecting insufficient stubbing.</p>
* <p>
* This method overcomes both of the aforementioned limitations by using {@link InsufficientStubbingDetector} as the default answer
* (see {@link InsufficientStubbingDetectorDemoTest#mockObjectWithInsufficientStubbingDetector()},
* {@link InsufficientStubbingDetectorDemoTest#stubbingWithInsufficientStubbingDetector()}).
* Note also that for convenience, {@link InsufficientStubbingDetector} stubs the {@link Object#toString()} method by using
* {@link OngoingStubbing#thenCallRealMethod()}, unless this stubbing is overwritten by the {@code tuner}.</p>
*/
public static <T> T mock(final Class<T> classToMock, @Nullable final Consumer<T> tuner) {
final InsufficientStubbingDetector insufficientStubbingDetector = new InsufficientStubbingDetector();
final T mock = Mockito.mock(classToMock, withSettings().defaultAnswer(insufficientStubbingDetector));
when(mock.toString()).thenCallRealMethod();
if (tuner != null) {
tuner.accept(mock);
}
insufficientStubbingDetector.enable();
return mock;
}

private MongoMockito() {
}
}
Loading