use of com.mongodb.event.CommandSucceededEvent 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.CommandSucceededEvent 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.CommandSucceededEvent in project mongo-java-driver by mongodb.
the class CommandMonitoringTest method massageActualCommandSucceededEvent.
private CommandSucceededEvent massageActualCommandSucceededEvent(final CommandSucceededEvent actual) {
BsonDocument response = getWritableCloneOfCommand(actual.getResponse());
// massage numbers that are the wrong BSON type
response.put("ok", new BsonDouble(response.getNumber("ok").doubleValue()));
if (response.containsKey("n")) {
response.put("n", new BsonInt32(response.getNumber("n").intValue()));
}
if (actual.getCommandName().equals("find") || actual.getCommandName().equals("getMore")) {
if (response.containsKey("cursor")) {
if (response.getDocument("cursor").containsKey("id") && !response.getDocument("cursor").getInt64("id").equals(new BsonInt64(0))) {
response.getDocument("cursor").put("id", new BsonInt64(42));
}
}
} else if (actual.getCommandName().equals("killCursors")) {
response.getArray("cursorsUnknown").set(0, new BsonInt64(42));
} else if (isWriteCommand(actual.getCommandName())) {
if (response.containsKey("writeErrors")) {
for (Iterator<BsonValue> iter = response.getArray("writeErrors").iterator(); iter.hasNext(); ) {
BsonDocument cur = iter.next().asDocument();
cur.put("code", new BsonInt32(42));
cur.put("errmsg", new BsonString(""));
}
}
if (actual.getCommandName().equals("update")) {
response.remove("nModified");
}
}
return new CommandSucceededEvent(actual.getRequestId(), actual.getConnectionDescription(), actual.getCommandName(), response, actual.getElapsedTime(TimeUnit.NANOSECONDS));
}
Aggregations