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();
}
Aggregations