Search in sources :

Example 16 with CommandStartedEvent

use of com.mongodb.event.CommandStartedEvent 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

CommandStartedEvent (com.mongodb.event.CommandStartedEvent)16 CommandEvent (com.mongodb.event.CommandEvent)13 BsonDocument (org.bson.BsonDocument)10 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)6 BsonString (org.bson.BsonString)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 BsonValue (org.bson.BsonValue)4 MongoNamespace (com.mongodb.MongoNamespace)3 ClusterFixture.isDataLakeTest (com.mongodb.ClusterFixture.isDataLakeTest)2 MongoClientSettings (com.mongodb.MongoClientSettings)2 WriteConcern (com.mongodb.WriteConcern)2 Fixture.getMongoClient (com.mongodb.client.Fixture.getMongoClient)2 CollectionHelper (com.mongodb.client.test.CollectionHelper)2 TestCommandListener (com.mongodb.internal.connection.TestCommandListener)2 TestConnectionPoolListener (com.mongodb.internal.connection.TestConnectionPoolListener)2 Nullable (com.mongodb.lang.Nullable)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 BsonArray (org.bson.BsonArray)2