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