Search in sources :

Example 16 with CommandEvent

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

the class CommandMonitoringTestHelper method getExpectedEvents.

public static List<CommandEvent> getExpectedEvents(final BsonArray expectedEventDocuments, final String databaseName, final BsonDocument operation) {
    List<CommandEvent> expectedEvents = new ArrayList<CommandEvent>(expectedEventDocuments.size());
    for (BsonValue expectedEventDocument : expectedEventDocuments) {
        BsonDocument curExpectedEventDocument = expectedEventDocument.asDocument();
        String eventType = curExpectedEventDocument.keySet().iterator().next();
        BsonDocument eventDescriptionDocument = curExpectedEventDocument.getDocument(eventType);
        CommandEvent commandEvent;
        String commandName = eventDescriptionDocument.getString("command_name", new BsonString("")).getValue();
        if (eventType.equals("command_started_event")) {
            BsonDocument commandDocument = eventDescriptionDocument.getDocument("command");
            String actualDatabaseName = eventDescriptionDocument.containsKey("database_name") ? eventDescriptionDocument.getString("database_name").getValue() : databaseName;
            // If the spec test supplies a $db field in the command, then use that database.
            if (commandDocument.containsKey("$db")) {
                actualDatabaseName = commandDocument.getString("$db").getValue();
            } else if (commandName.equals("")) {
                commandName = commandDocument.keySet().iterator().next();
            }
            if (isAdminCommand(commandName)) {
                actualDatabaseName = "admin";
            }
            // Not clear whether these global fields should be included, but also not clear how to efficiently exclude them
            if (serverVersionAtLeast(3, 6)) {
                commandDocument.put("$db", new BsonString(actualDatabaseName));
                if (operation != null && operation.containsKey("read_preference")) {
                    commandDocument.put("$readPreference", operation.getDocument("read_preference"));
                }
            }
            commandEvent = new CommandStartedEvent(1, null, actualDatabaseName, commandName, commandDocument);
        } else if (eventType.equals("command_succeeded_event")) {
            BsonDocument replyDocument = eventDescriptionDocument.get("reply").asDocument();
            commandEvent = new CommandSucceededEvent(1, null, commandName, replyDocument, 1);
        } else if (eventType.equals("command_failed_event")) {
            commandEvent = new CommandFailedEvent(1, null, commandName, 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) BsonString(org.bson.BsonString) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 17 with CommandEvent

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

the class AbstractUnifiedTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    try {
        executeOperations(definition.getArray("operations"), false);
    } finally {
        closeAllSessions();
        shutdownAllExecutors();
    }
    if (definition.containsKey("expectations")) {
        List<CommandEvent> expectedEvents = getExpectedEvents(definition.getArray("expectations"), databaseName, null);
        List<CommandEvent> events = commandListener.getCommandStartedEvents();
        assertTrue("Actual number of events is less than expected number of events", events.size() >= expectedEvents.size());
        assertEventsEquality(expectedEvents, events.subList(0, expectedEvents.size()), lsidMap);
    }
    BsonDocument expectedOutcome = definition.getDocument("outcome", new BsonDocument());
    if (expectedOutcome.containsKey("collection")) {
        BsonDocument collectionDocument = expectedOutcome.getDocument("collection");
        List<BsonDocument> collectionData;
        if (collectionDocument.containsKey("name")) {
            collectionData = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionDocument.getString("name").getValue())).find(new BsonDocumentCodec());
        } else {
            collectionData = collectionHelper.find(new BsonDocumentCodec());
        }
        assertEquals(expectedOutcome.getDocument("collection").getArray("data").getValues(), collectionData);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) CommandEvent(com.mongodb.event.CommandEvent) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) CollectionHelper(com.mongodb.client.test.CollectionHelper) MongoNamespace(com.mongodb.MongoNamespace) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test) ClusterFixture.isDataLakeTest(com.mongodb.ClusterFixture.isDataLakeTest)

Example 18 with CommandEvent

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

the class EventMatcher method assertCommandEventsEquality.

public void assertCommandEventsEquality(final String client, final BsonArray expectedEventDocuments, final List<CommandEvent> events) {
    context.push(ContextElement.ofCommandEvents(client, expectedEventDocuments, events));
    assertEquals(context.getMessage("Number of events must be the same"), expectedEventDocuments.size(), events.size());
    for (int i = 0; i < events.size(); i++) {
        CommandEvent actual = events.get(i);
        BsonDocument expectedEventDocument = expectedEventDocuments.get(i).asDocument();
        String eventType = expectedEventDocument.getFirstKey();
        context.push(ContextElement.ofCommandEvent(expectedEventDocument, actual, i));
        BsonDocument expected = expectedEventDocument.getDocument(eventType);
        if (expected.containsKey("commandName")) {
            assertEquals(context.getMessage("Command names must be equal"), expected.getString("commandName").getValue(), actual.getCommandName());
        }
        if (expected.containsKey("hasServiceId")) {
            boolean hasServiceId = expected.getBoolean("hasServiceId").getValue();
            ObjectId serviceId = actual.getConnectionDescription().getServiceId();
            if (hasServiceId) {
                assertNotNull(context.getMessage("Expected serviceId"), serviceId);
            } else {
                assertNull(context.getMessage("Expected no serviceId"), serviceId);
            }
        }
        if (expected.containsKey("hasServerConnectionId")) {
            boolean hasServerConnectionId = expected.getBoolean("hasServerConnectionId").getValue();
            Integer serverConnectionId = actual.getConnectionDescription().getConnectionId().getServerValue();
            if (hasServerConnectionId) {
                assertNotNull(context.getMessage("Expected serverConnectionId"), serverConnectionId);
            } else {
                assertNull(context.getMessage("Expected no serverConnectionId"), serverConnectionId);
            }
        }
        if (actual.getClass().equals(CommandStartedEvent.class)) {
            assertEquals(context.getMessage("Expected CommandStartedEvent"), eventType, "commandStartedEvent");
            CommandStartedEvent actualCommandStartedEvent = (CommandStartedEvent) actual;
            if (expected.containsKey("databaseName")) {
                assertEquals(context.getMessage("Expected database names to match"), expected.getString("databaseName").getValue(), actualCommandStartedEvent.getDatabaseName());
            }
            if (expected.containsKey("command")) {
                valueMatcher.assertValuesMatch(expected.getDocument("command"), actualCommandStartedEvent.getCommand());
            }
        } else if (actual.getClass().equals(CommandSucceededEvent.class)) {
            assertEquals(context.getMessage("Expected CommandSucceededEvent"), eventType, "commandSucceededEvent");
            CommandSucceededEvent actualCommandSucceededEvent = (CommandSucceededEvent) actual;
            if (expected.containsKey("reply")) {
                valueMatcher.assertValuesMatch(expected.getDocument("reply"), actualCommandSucceededEvent.getResponse());
            }
        } else if (actual.getClass().equals(CommandFailedEvent.class)) {
            assertEquals(context.getMessage("Expected CommandFailedEvent"), eventType, "commandFailedEvent");
        } else {
            throw new UnsupportedOperationException("Unsupported event type: " + actual.getClass());
        }
        context.pop();
    }
    context.pop();
}
Also used : CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) ObjectId(org.bson.types.ObjectId) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent)

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