Search in sources :

Example 56 with BsonDocument

use of org.bson.BsonDocument 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 57 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class CommandMonitoringTest method setUp.

@Before
public void setUp() {
    ServerVersion serverVersion = ClusterFixture.getServerVersion();
    if (definition.containsKey("ignore_if_server_version_less_than")) {
        assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_less_than")) < 0);
    }
    if (definition.containsKey("ignore_if_server_version_greater_than")) {
        assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_greater_than")) > 0);
    }
    if (definition.containsKey("ignore_if_topology_type")) {
        BsonArray topologyTypes = definition.getArray("ignore_if_topology_type");
        for (BsonValue type : topologyTypes) {
            String typeString = type.asString().getValue();
            if (typeString.equals("sharded")) {
                assumeFalse(isSharded());
            } else if (typeString.equals("replica_set")) {
                assumeFalse(isDiscoverableReplicaSet());
            } else if (typeString.equals("standalone")) {
                assumeFalse(isSharded());
            }
        }
    }
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    for (BsonValue document : data) {
        documents.add(document.asDocument());
    }
    CollectionHelper<Document> collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
    collectionHelper.drop();
    collectionHelper.insertDocuments(documents);
    commandListener.reset();
    collection = mongoClient.getDatabase(databaseName).getCollection(collectionName, BsonDocument.class);
    if (definition.getDocument("operation").containsKey("read_preference")) {
        collection = collection.withReadPreference(ReadPreference.valueOf(definition.getDocument("operation").getDocument("read_preference").getString("mode").getValue()));
    }
    helper = new JsonPoweredCrudTestHelper(description, collection);
}
Also used : ServerVersion(com.mongodb.connection.ServerVersion) ArrayList(java.util.ArrayList) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) CollectionHelper(com.mongodb.client.test.CollectionHelper) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 58 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class DBObjectCodecTest method shouldEncodeNull.

@Test
public void shouldEncodeNull() {
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject doc = new BasicDBObject("null", null);
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
    assertEquals(new BsonDocument("null", BsonNull.VALUE), writer.getDocument());
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 59 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class DBObjectCodecTest method shouldRespectEncodeIdFirstPropertyInEncoderContext.

@Test
public void shouldRespectEncodeIdFirstPropertyInEncoderContext() {
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    // given
    DBObject doc = new BasicDBObject("x", 2).append("_id", 2);
    // when
    BsonDocument encodedDocument = new BsonDocument();
    dbObjectCodec.encode(new BsonDocumentWriter(encodedDocument), doc, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
    // then
    assertEquals(new ArrayList<String>(encodedDocument.keySet()), asList("_id", "x"));
    // when
    encodedDocument.clear();
    dbObjectCodec.encode(new BsonDocumentWriter(encodedDocument), doc, EncoderContext.builder().isEncodingCollectibleDocument(false).build());
    // then
    assertEquals(new ArrayList<String>(encodedDocument.keySet()), asList("x", "_id"));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 60 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class DBObjectCodecTest method shouldEncodeIterableMapAsMap.

@Test
public void shouldEncodeIterableMapAsMap() {
    IterableMap iterableMap = new IterableMap();
    iterableMap.put("first", 1);
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject doc = new BasicDBObject("map", iterableMap);
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
    assertEquals(new BsonDocument("map", new BsonDocument("first", new BsonInt32(1))), writer.getDocument());
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonInt32(org.bson.BsonInt32) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Aggregations

BsonDocument (org.bson.BsonDocument)169 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)36 BsonArray (org.bson.BsonArray)29 BsonInt32 (org.bson.BsonInt32)28 ArrayList (java.util.ArrayList)24 BsonDocumentReader (org.bson.BsonDocumentReader)17 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)14 BsonDocumentWriter (org.bson.BsonDocumentWriter)14 BsonInt64 (org.bson.BsonInt64)14 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 BsonDouble (org.bson.BsonDouble)8 Document (org.bson.Document)7 MongoNamespace (com.mongodb.MongoNamespace)6 Before (org.junit.Before)6 MongoGridFSException (com.mongodb.MongoGridFSException)5 BsonObjectId (org.bson.BsonObjectId)5 BsonWriter (org.bson.BsonWriter)5 ObjectId (org.bson.types.ObjectId)5