Skip to content
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

Support authorizedCollections option for listCollections helpers #1270

Merged
merged 6 commits into from
Dec 8, 2023

Conversation

stIncMale
Copy link
Member

@stIncMale stIncMale commented Dec 5, 2023

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
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoMoreInteractions

class ListCollectionNamesFlowTest {
Copy link
Member Author

@stIncMale stIncMale Dec 5, 2023

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.

import org.bson.BsonValue
import org.bson.conversions.Bson

data class SyncListCollectionNamesIterable(val wrapped: ListCollectionNamesFlow) :
Copy link
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.

*
* @see [List collections](https://www.mongodb.com/docs/manual/reference/command/listCollections/)
*/
public class ListCollectionNamesFlow(private val wrapped: ListCollectionNamesPublisher) :
Copy link
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.

import org.bson.BsonValue
import org.bson.conversions.Bson

internal class SyncListCollectionNamesIterable(val wrapped: ListCollectionNamesIterable) :
Copy link
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.

*
* @see [List collections](https://www.mongodb.com/docs/manual/reference/command/listCollections/)
*/
public class ListCollectionNamesIterable(private val wrapped: JListCollectionNamesIterable) :
Copy link
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
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoMoreInteractions

class ListCollectionNamesIterableTest {
Copy link
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.


import java.util.concurrent.TimeUnit;

final class SyncListCollectionNamesIterable extends SyncMongoIterable<String> implements ListCollectionNamesIterable {
Copy link
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.

import static com.mongodb.reactivestreams.client.MongoFixture.PUBLISHER_REFERENCE_CLEANUP_TIMEOUT_MILLIS;
import static com.mongodb.reactivestreams.client.MongoFixture.run;

public class ListCollectionNamesPublisherVerification extends PublisherVerification<String> {
Copy link
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.

* @param wrapped the underlying java ListCollectionNamesPublisher
* @since 5.0
*/
case class ListCollectionNamesObservable(wrapped: ListCollectionNamesPublisher) extends Observable[String] {
Copy link
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.

import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration

class ListCollectionNamesObservableSpec extends BaseSpec with MockitoSugar {
Copy link
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
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;

final class ListCollectionNamesPublisherImplTest extends TestHelper {
Copy link
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.

Copy link
Contributor

@jyemin jyemin left a comment

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
Copy link
Contributor

@jyemin jyemin left a comment

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.

@@ -206,6 +226,7 @@ private BsonDocument getCommand() {
if (nameOnly) {
command.append("nameOnly", BsonBoolean.TRUE);
}
putIfTrue(command, "authorizedCollections", authorizedCollections);
Copy link
Contributor

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.

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

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

@rozza rozza left a comment

Choose a reason for hiding this comment

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

lgtm

@stIncMale stIncMale requested a review from jyemin December 8, 2023 17:47
Copy link
Contributor

@jyemin jyemin left a comment

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
56 checks passed
@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
3 participants