Skip to content

Support authorizedCollections option for listCollections helpers - #1270

Merged
stIncMale merged 6 commits into
mongodb:masterfrom
stIncMale:JAVA-4353
Dec 8, 2023
Merged

stIncMale merged 6 commits into
mongodb:masterfrom
stIncMale:JAVA-4353

Conversation

@stIncMale

@stIncMale stIncMale commented Dec 5, 2023 •

Copy link
Copy Markdown
Member

This is the 3rd incarnation of the PR. The previous are (from younger to older): #1263, #855 (the original one).

API changes

MongoDatabase.listCollectionNames

Changed the return type of a method. The change preserves source-code compatibility (existing source code that uses the changed methods does not have to be changed), breaks binary compatibility (existing source code that uses the changed methods has to be recompiled against the updated driver dependency).

package class/interface method(s) old return type new return type
com.mongodb.client MongoDatabase listCollectionNames MongoIterable ListCollectionNamesIterable
com.mongodb.reactivestreams.client MongoDatabase listCollectionNames Publisher ListCollectionNamesPublisher
org.mongodb.scala MongoDatabase listCollectionNames Observable ListCollectionNamesObservable
com.mongodb.kotlin.client MongoDatabase listCollectionNames MongoIterable ListCollectionNamesIterable
com.mongodb.kotlin.client.coroutine MongoDatabase listCollectionNames Flow ListCollectionNamesFlow

authorizedCollections

Added classes/interfaces with a new method authorizedCollections.

package existing class/interface (was not modified) new class/interface
com.mongodb.client ListCollectionsIterable ListCollectionNamesIterable
com.mongodb.reactivestreams.client ListCollectionsPublisher ListCollectionNamesPublisher
org.mongodb.scala ListCollectionsObservable ListCollectionNamesObservable
com.mongodb.kotlin.client ListCollectionsIterable ListCollectionNamesIterable
com.mongodb.kotlin.client.coroutine ListCollectionsFlow ListCollectionNamesFlow

JAVA-4353

@stIncMale stIncMale self-assigned this Dec 5, 2023

@stIncMale stIncMale Dec 5, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on com.mongodb.kotlin.client.coroutine.ListCollectionsFlowTest.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on com.mongodb.kotlin.client.coroutine.syncadapter.SyncListCollectionsIterable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on com.mongodb.kotlin.client.coroutine.ListCollectionsFlow.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on com.mongodb.kotlin.client.syncadapter.SyncListCollectionsIterable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on com.mongodb.kotlin.client.ListCollectionsIterable.

@stIncMale
stIncMale marked this pull request as draft December 5, 2023 20:48

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on com.mongodb.kotlin.client.ListCollectionsIterableTest.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on com.mongodb.reactivestreams.client.syncadapter.SyncListCollectionsIterable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on com.mongodb.reactivestreams.client.ListCollectionsPublisherVerification.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on org.mongodb.scala.ListCollectionsObservable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on org.mongodb.scala.ListCollectionsObservableSpec.

@stIncMale
stIncMale force-pushed the JAVA-4353 branch 2 times, most recently from 72a7033 to 619a974 Compare December 6, 2023 00:13

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is based on com.mongodb.reactivestreams.client.internal.ListCollectionsPublisherImplTest.

@jyemin jyemin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Started with a review of the API, will proceed with the implementation after initial comments are resolved.

@stIncMale
stIncMale requested a review from jyemin December 6, 2023 19:50

@jyemin jyemin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Waiting on the unit test we discussed. Otherwise LGTM.

if (nameOnly) {
command.append("nameOnly", BsonBoolean.TRUE);
}
putIfTrue(command, "authorizedCollections", authorizedCollections);

Copy link
Copy Markdown
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
Copy Markdown
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.

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

@stIncMale stIncMale Dec 7, 2023 •

Copy link
Copy Markdown
Member Author

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.

@rozza rozza Dec 8, 2023 •

Copy link
Copy Markdown
Member

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 {

@stIncMale stIncMale Dec 8, 2023 •

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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.

@stIncMale
stIncMale requested a review from jyemin December 8, 2023 00:04

@rozza rozza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

JAVA-4353
JAVA-4353
@stIncMale
stIncMale requested a review from jyemin December 8, 2023 17:47

@jyemin jyemin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM.

@stIncMale
stIncMale merged commit a03649c into mongodb:master Dec 8, 2023
@stIncMale
stIncMale deleted the JAVA-4353 branch December 8, 2023 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants