Search in sources :

Example 1 with CommandSucceededEvent

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;
}
Also used : CommandFailedEvent(com.mongodb.event.CommandFailedEvent) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) ArrayList(java.util.ArrayList) CommandEvent(com.mongodb.event.CommandEvent) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 2 with CommandSucceededEvent

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());
        }
    }
}
Also used : CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) CommandEvent(com.mongodb.event.CommandEvent) Test(org.junit.Test)

Example 3 with CommandSucceededEvent

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));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonDouble(org.bson.BsonDouble) BsonValue(org.bson.BsonValue)

Aggregations

CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)3 CommandEvent (com.mongodb.event.CommandEvent)2 CommandStartedEvent (com.mongodb.event.CommandStartedEvent)2 BsonDocument (org.bson.BsonDocument)2 BsonString (org.bson.BsonString)2 BsonValue (org.bson.BsonValue)2 CommandFailedEvent (com.mongodb.event.CommandFailedEvent)1 ArrayList (java.util.ArrayList)1 BsonDouble (org.bson.BsonDouble)1 BsonInt32 (org.bson.BsonInt32)1 BsonInt64 (org.bson.BsonInt64)1 Test (org.junit.Test)1