Search in sources :

Example 86 with BsonDocument

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

the class MessageHelper method encodeJson.

private static ByteBuf encodeJson(final String json) {
    OutputBuffer outputBuffer = new BasicOutputBuffer();
    JsonReader jsonReader = new JsonReader(json);
    BsonDocumentCodec codec = new BsonDocumentCodec();
    BsonDocument document = codec.decode(jsonReader, DecoderContext.builder().build());
    BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
    codec.encode(writer, document, EncoderContext.builder().build());
    ByteBuffer documentByteBuffer = ByteBuffer.allocate(outputBuffer.size());
    documentByteBuffer.put(outputBuffer.toByteArray());
    return new ByteBufNIO(documentByteBuffer);
}
Also used : BsonDocument(org.bson.BsonDocument) JsonReader(org.bson.json.JsonReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) ByteBuffer(java.nio.ByteBuffer) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 87 with BsonDocument

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

the class ServerDiscoveryAndMonitoringMonitoringTest method assertEvents.

private void assertEvents(final BsonArray events) {
    Iterator<ClusterDescriptionChangedEvent> clusterDescriptionChangedEventIterator = clusterListener.getClusterDescriptionChangedEvents().iterator();
    for (BsonValue eventValue : events) {
        BsonDocument eventDocument = eventValue.asDocument();
        if (eventDocument.containsKey("topology_opening_event")) {
            ClusterOpeningEvent event = clusterListener.getClusterOpeningEvent();
            assertNotNull("event", event);
            assertEquals("clusterId", getCluster().getClusterId(), event.getClusterId());
        } else if (eventDocument.containsKey("topology_description_changed_event")) {
            ClusterDescriptionChangedEvent event = clusterDescriptionChangedEventIterator.next();
            assertNotNull("event", event);
            assertEquals(getCluster().getClusterId(), event.getClusterId());
            BsonDocument topologyDescriptionChangedEventDocument = eventDocument.getDocument("topology_description_changed_event");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(topologyDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            BsonDocument newDescription = topologyDescriptionChangedEventDocument.getDocument("newDescription");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(newDescription), event.getNewDescription());
            if (newDescription.getString("topologyType").getValue().equals("Single")) {
                assertEquals(SingleServerCluster.class, getCluster().getClass());
            } else {
                assertEquals(MultiServerCluster.class, getCluster().getClass());
            }
        } else if (eventDocument.containsKey("server_opening_event")) {
            BsonDocument serverOpeningEventDocument = eventDocument.getDocument("server_opening_event");
            ServerAddress serverAddress = new ServerAddress(serverOpeningEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerOpeningEvent event = serverListener.getServerOpeningEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_closed_event")) {
            BsonDocument serverClosedEventDocument = eventDocument.getDocument("server_closed_event");
            ServerAddress serverAddress = new ServerAddress(serverClosedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerClosedEvent event = serverListener.getServerClosedEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_description_changed_event")) {
            BsonDocument serverDescriptionChangedEventDocument = eventDocument.getDocument("server_description_changed_event");
            ServerAddress serverAddress = new ServerAddress(serverDescriptionChangedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            assertEquals("serverDescriptionChangedEvents size", 1, serverListener.getServerDescriptionChangedEvents().size());
            ServerDescriptionChangedEvent event = serverListener.getServerDescriptionChangedEvents().get(0);
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("newDescription")), event.getNewDescription());
        } else {
            throw new IllegalArgumentException("Unsupported event type: " + eventDocument.keySet().iterator().next());
        }
    }
    assertFalse(clusterDescriptionChangedEventIterator.hasNext());
}
Also used : ServerAddress(com.mongodb.ServerAddress) ServerClosedEvent(com.mongodb.event.ServerClosedEvent) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) BsonDocument(org.bson.BsonDocument) ServerOpeningEvent(com.mongodb.event.ServerOpeningEvent) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent) BsonValue(org.bson.BsonValue)

Example 88 with BsonDocument

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

the class ServerDiscoveryAndMonitoringMonitoringTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    for (BsonValue phase : getDefinition().getArray("phases")) {
        for (BsonValue response : phase.asDocument().getArray("responses")) {
            applyResponse(response.asArray());
        }
        BsonDocument outcome = phase.asDocument().getDocument("outcome");
        assertEvents(outcome.getArray("events"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 89 with BsonDocument

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

the class UpdateUserOperation method getCommand.

private BsonDocument getCommand(final ConnectionDescription description) {
    BsonDocument commandDocument = asCommandDocument(credential, readOnly, "updateUser");
    appendWriteConcernToCommand(writeConcern, commandDocument, description);
    return commandDocument;
}
Also used : BsonDocument(org.bson.BsonDocument)

Example 90 with BsonDocument

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

the class UserOperationHelper method asCommandDocument.

static BsonDocument asCommandDocument(final MongoCredential credential, final boolean readOnly, final String commandName) {
    BsonDocument document = new BsonDocument();
    document.put(commandName, new BsonString(credential.getUserName()));
    document.put("pwd", new BsonString(createAuthenticationHash(credential.getUserName(), credential.getPassword())));
    document.put("digestPassword", BsonBoolean.FALSE);
    document.put("roles", new BsonArray(Arrays.<BsonValue>asList(new BsonString(getRoleName(credential, readOnly)))));
    return document;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonValue(org.bson.BsonValue)

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