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