Search in sources :

Example 1 with CommandEvent

use of com.mongodb.event.CommandEvent in project mongo-java-driver by mongodb.

the class CommandMonitoringTest method getExpectedEvents.

private List<CommandEvent> getExpectedEvents(final BsonArray expectedEventDocuments) {
    List<CommandEvent> expectedEvents = new ArrayList<CommandEvent>(expectedEventDocuments.size());
    for (Iterator<BsonValue> iterator = expectedEventDocuments.iterator(); iterator.hasNext(); ) {
        BsonDocument curExpectedEventDocument = iterator.next().asDocument();
        String eventType = curExpectedEventDocument.keySet().iterator().next();
        BsonDocument eventDescriptionDocument = curExpectedEventDocument.getDocument(eventType);
        CommandEvent commandEvent;
        if (eventType.equals("command_started_event")) {
            commandEvent = new CommandStartedEvent(1, null, databaseName, eventDescriptionDocument.getString("command_name").getValue(), eventDescriptionDocument.getDocument("command"));
        } else if (eventType.equals("command_succeeded_event")) {
            BsonDocument replyDocument = eventDescriptionDocument.get("reply").asDocument();
            commandEvent = new CommandSucceededEvent(1, null, eventDescriptionDocument.getString("command_name").getValue(), replyDocument, 1);
        } else if (eventType.equals("command_failed_event")) {
            commandEvent = new CommandFailedEvent(1, null, eventDescriptionDocument.getString("command_name").getValue(), 1, null);
        } else {
            throw new UnsupportedOperationException("Unsupported command event type: " + eventType);
        }
        expectedEvents.add(commandEvent);
    }
    return expectedEvents;
}
Also used : CommandFailedEvent(com.mongodb.event.CommandFailedEvent) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) ArrayList(java.util.ArrayList) CommandEvent(com.mongodb.event.CommandEvent) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 2 with CommandEvent

use of com.mongodb.event.CommandEvent in project mongo-java-driver by mongodb.

the class CommandMonitoringTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    // On server <= 2.4, insertMany generates an insert command for every document, so the test fails
    assumeFalse(filename.startsWith("insertMany") && !serverVersionAtLeast(2, 6));
    executeOperation();
    List<CommandEvent> expectedEvents = getExpectedEvents(definition.getArray("expectations"));
    List<CommandEvent> events = commandListener.getEvents();
    assertEquals(expectedEvents.size(), events.size());
    for (int i = 0; i < events.size(); i++) {
        CommandEvent actual = events.get(i);
        CommandEvent expected = expectedEvents.get(i);
        assertEquals(expected.getClass(), actual.getClass());
        assertEquals(expected.getCommandName(), actual.getCommandName());
        if (actual.getClass().equals(CommandStartedEvent.class)) {
            CommandStartedEvent actualCommandStartedEvent = massageActualCommandStartedEvent((CommandStartedEvent) actual);
            CommandStartedEvent expectedCommandStartedEvent = (CommandStartedEvent) expected;
            assertEquals(expectedCommandStartedEvent.getDatabaseName(), actualCommandStartedEvent.getDatabaseName());
            assertEquals(expectedCommandStartedEvent.getCommand(), actualCommandStartedEvent.getCommand());
        } else if (actual.getClass().equals(CommandSucceededEvent.class)) {
            CommandSucceededEvent actualCommandSucceededEvent = massageActualCommandSucceededEvent((CommandSucceededEvent) actual);
            CommandSucceededEvent expectedCommandSucceededEvent = massageExpectedCommandSucceededEvent((CommandSucceededEvent) expected);
            assertEquals(expectedCommandSucceededEvent.getCommandName(), actualCommandSucceededEvent.getCommandName());
            assertTrue(actualCommandSucceededEvent.getElapsedTime(TimeUnit.NANOSECONDS) > 0);
            if (expectedCommandSucceededEvent.getResponse() == null) {
                assertNull(actualCommandSucceededEvent.getResponse());
            } else {
                assertTrue(String.format("\nExpected: %s\nActual:   %s", expectedCommandSucceededEvent.getResponse(), actualCommandSucceededEvent.getResponse()), actualCommandSucceededEvent.getResponse().entrySet().containsAll(expectedCommandSucceededEvent.getResponse().entrySet()));
            }
        } else if (actual.getClass().equals(CommandFailedEvent.class)) {
        // nothing else to assert here
        } else {
            throw new UnsupportedOperationException("Unsupported event type: " + actual.getClass());
        }
    }
}
Also used : CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent) Test(org.junit.Test)

Example 3 with CommandEvent

use of com.mongodb.event.CommandEvent in project mongo-java-driver by mongodb.

the class ReadConcernTest method shouldIncludeReadConcernInCommand.

@Test
public void shouldIncludeReadConcernInCommand() {
    mongoClient.getDatabase(getDefaultDatabaseName()).getCollection("test").withReadConcern(ReadConcern.LOCAL).find().into(new ArrayList<>());
    List<CommandEvent> events = commandListener.getCommandStartedEvents();
    BsonDocument commandDocument = new BsonDocument("find", new BsonString("test")).append("readConcern", ReadConcern.LOCAL.asDocument()).append("filter", new BsonDocument());
    assertEventsEquality(Arrays.<CommandEvent>asList(new CommandStartedEvent(1, null, getDefaultDatabaseName(), "find", commandDocument)), events);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent) Test(org.junit.Test)

Example 4 with CommandEvent

use of com.mongodb.event.CommandEvent in project mongo-java-driver by mongodb.

the class CommandMonitoringTestHelper method assertEventsEquality.

public static void assertEventsEquality(final List<CommandEvent> expectedEvents, final List<CommandEvent> events, @Nullable final Map<String, BsonDocument> lsidMap) {
    assertEquals(expectedEvents.size(), events.size());
    for (int i = 0; i < events.size(); i++) {
        CommandEvent actual = events.get(i);
        CommandEvent expected = expectedEvents.get(i);
        assertEquals(expected.getClass(), actual.getClass());
        assertEquals(expected.getCommandName().toLowerCase(), actual.getCommandName().toLowerCase());
        if (actual.getClass().equals(CommandStartedEvent.class)) {
            CommandStartedEvent expectedCommandStartedEvent = massageExpectedCommandStartedEvent((CommandStartedEvent) expected, (CommandStartedEvent) actual, lsidMap);
            CommandStartedEvent actualCommandStartedEvent = massageActualCommandStartedEvent((CommandStartedEvent) actual, lsidMap, expectedCommandStartedEvent);
            assertEquals(expectedCommandStartedEvent.getDatabaseName(), actualCommandStartedEvent.getDatabaseName());
            assertEquals(expectedCommandStartedEvent.getCommand(), actualCommandStartedEvent.getCommand());
            if (((CommandStartedEvent) expected).getCommand().containsKey("recoveryToken")) {
                if (((CommandStartedEvent) expected).getCommand().get("recoveryToken").isNull()) {
                    assertFalse(((CommandStartedEvent) actual).getCommand().containsKey("recoveryToken"));
                } else {
                    assertTrue(((CommandStartedEvent) actual).getCommand().containsKey("recoveryToken"));
                }
            }
        } else if (actual.getClass().equals(CommandSucceededEvent.class)) {
            CommandSucceededEvent actualCommandSucceededEvent = massageActualCommandSucceededEvent((CommandSucceededEvent) actual);
            CommandSucceededEvent expectedCommandSucceededEvent = massageExpectedCommandSucceededEvent((CommandSucceededEvent) expected);
            assertEquals(expectedCommandSucceededEvent.getCommandName(), actualCommandSucceededEvent.getCommandName());
            assertTrue(actualCommandSucceededEvent.getElapsedTime(TimeUnit.NANOSECONDS) > 0);
            if (expectedCommandSucceededEvent.getResponse() == null) {
                assertNull(actualCommandSucceededEvent.getResponse());
            } else {
                assertTrue(String.format("\nExpected: %s\nActual:   %s", expectedCommandSucceededEvent.getResponse(), actualCommandSucceededEvent.getResponse()), actualCommandSucceededEvent.getResponse().entrySet().containsAll(expectedCommandSucceededEvent.getResponse().entrySet()));
            }
        } else if (actual.getClass().equals(CommandFailedEvent.class)) {
        // nothing else to assert here
        } else {
            throw new UnsupportedOperationException("Unsupported event type: " + actual.getClass());
        }
    }
}
Also used : CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent)

Example 5 with CommandEvent

use of com.mongodb.event.CommandEvent in project mongo-java-driver by mongodb.

the class UnifiedTest method executeAssertLsidOnLastTwoCommands.

private OperationResult executeAssertLsidOnLastTwoCommands(final BsonDocument operation, final boolean same) {
    TestCommandListener listener = entities.getClientCommandListener(operation.getDocument("arguments").getString("client").getValue());
    List<CommandEvent> events = lastTwoCommandEvents(listener);
    String eventsJson = listener.getCommandStartedEvents().stream().map(e -> ((CommandStartedEvent) e).getCommand().toJson()).collect(Collectors.joining(", "));
    BsonDocument expected = ((CommandStartedEvent) events.get(0)).getCommand().getDocument("lsid");
    BsonDocument actual = ((CommandStartedEvent) events.get(1)).getCommand().getDocument("lsid");
    if (same) {
        assertEquals(eventsJson, expected, actual);
    } else {
        assertNotEquals(eventsJson, expected, actual);
    }
    return OperationResult.NONE;
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) JsonPoweredTestHelper.getTestFiles(util.JsonPoweredTestHelper.getTestFiles) URISyntaxException(java.net.URISyntaxException) MongoDatabase(com.mongodb.client.MongoDatabase) BsonValue(org.bson.BsonValue) BsonArray(org.bson.BsonArray) After(org.junit.After) Parameterized(org.junit.runners.Parameterized) CommandEvent(com.mongodb.event.CommandEvent) CollectionHelper(com.mongodb.client.test.CollectionHelper) Fixture.getMongoClientSettings(com.mongodb.client.Fixture.getMongoClientSettings) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) ClusterFixture.getServerVersion(com.mongodb.ClusterFixture.getServerVersion) Assert.assertFalse(org.junit.Assert.assertFalse) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) NotNull(org.jetbrains.annotations.NotNull) JsonPoweredTestHelper.getTestDocument(util.JsonPoweredTestHelper.getTestDocument) MongoClient(com.mongodb.client.MongoClient) BsonBoolean(org.bson.BsonBoolean) RunWith(org.junit.runner.RunWith) BsonString(org.bson.BsonString) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) Fixture.getMongoClient(com.mongodb.client.Fixture.getMongoClient) Before(org.junit.Before) AssumptionViolatedException(org.junit.AssumptionViolatedException) MongoNamespace(com.mongodb.MongoNamespace) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonDouble(org.bson.BsonDouble) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Collectors.toList(java.util.stream.Collectors.toList) Assert.assertNull(org.junit.Assert.assertNull) RunOnRequirementsMatcher.runOnRequirementsMet(com.mongodb.client.unified.RunOnRequirementsMatcher.runOnRequirementsMet) ClientSession(com.mongodb.client.ClientSession) Nullable(com.mongodb.lang.Nullable) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) Assert.assertEquals(org.junit.Assert.assertEquals) BsonDocument(org.bson.BsonDocument) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) CommandEvent(com.mongodb.event.CommandEvent) BsonString(org.bson.BsonString)

Aggregations

CommandEvent (com.mongodb.event.CommandEvent)18 CommandStartedEvent (com.mongodb.event.CommandStartedEvent)13 BsonDocument (org.bson.BsonDocument)12 Test (org.junit.Test)9 BsonString (org.bson.BsonString)8 BsonValue (org.bson.BsonValue)6 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)5 ArrayList (java.util.ArrayList)5 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)4 JsonTestServerVersionChecker.skipTest (com.mongodb.JsonTestServerVersionChecker.skipTest)3 MongoNamespace (com.mongodb.MongoNamespace)3 CollectionHelper (com.mongodb.client.test.CollectionHelper)3 MongoClientSettings (com.mongodb.MongoClientSettings)2 MongoCommandException (com.mongodb.MongoCommandException)2 MongoWriteConcernException (com.mongodb.MongoWriteConcernException)2 WriteConcern (com.mongodb.WriteConcern)2 Fixture.getMongoClient (com.mongodb.client.Fixture.getMongoClient)2 TestCommandListener (com.mongodb.internal.connection.TestCommandListener)2 TestConnectionPoolListener (com.mongodb.internal.connection.TestConnectionPoolListener)2 Nullable (com.mongodb.lang.Nullable)2