Search in sources :

Example 81 with BsonString

use of org.bson.BsonString in project graylog2-server by Graylog2.

the class MongoDBVersionCheck method getVersion.

public static Version getVersion(MongoClient mongoClient) {
    final MongoDatabase adminDb = mongoClient.getDatabase("admin");
    try {
        final Document buildInfoResult = adminDb.runCommand(new BsonDocument("buildinfo", new BsonString("")));
        if (!buildInfoResult.isEmpty()) {
            Object result = buildInfoResult.get("versionArray");
            if (!(result instanceof ArrayList)) {
                LOG.warn("Couldn't retrieve MongoDB buildInfo");
                return null;
            }
            @SuppressWarnings("rawtypes") final ArrayList versionArray = buildInfoResult.get("versionArray", ArrayList.class);
            if (versionArray == null || versionArray.size() < 3) {
                LOG.debug("Couldn't retrieve MongoDB version");
                return null;
            }
            final int majorVersion = (int) versionArray.get(0);
            final int minorVersion = (int) versionArray.get(1);
            final int patchVersion = (int) versionArray.get(2);
            return Version.forIntegers(majorVersion, minorVersion, patchVersion);
        } else {
            LOG.warn("Couldn't retrieve MongoDB buildInfo");
            return null;
        }
    } catch (MongoException e) {
        LOG.warn("Couldn't retrieve MongoDB buildInfo", e);
        return null;
    }
}
Also used : MongoException(com.mongodb.MongoException) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ArrayList(java.util.ArrayList) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 82 with BsonString

use of org.bson.BsonString in project realm-java by realm.

the class ChangeEvent method toBsonDocument.

/**
 * Serializes this change event into a {@link BsonDocument}.
 *
 * @return the serialized document.
 */
@Override
public BsonDocument toBsonDocument() {
    final BsonDocument asDoc = new BsonDocument();
    asDoc.put(Fields.ID_FIELD, id);
    asDoc.put(Fields.OPERATION_TYPE_FIELD, new BsonString(toRemote(getOperationType())));
    final BsonDocument nsDoc = new BsonDocument();
    nsDoc.put(Fields.NS_DB_FIELD, new BsonString(ns.getDatabaseName()));
    nsDoc.put(Fields.NS_COLL_FIELD, new BsonString(getNamespace().getCollectionName()));
    asDoc.put(Fields.NS_FIELD, nsDoc);
    asDoc.put(Fields.DOCUMENT_KEY_FIELD, getDocumentKey());
    DocumentT fullDocument = getFullDocument();
    if ((fullDocument instanceof BsonValue) && ((BsonValue) fullDocument).isDocument()) {
        asDoc.put(Fields.FULL_DOCUMENT_FIELD, (BsonValue) fullDocument);
    }
    UpdateDescription updateDescription = getUpdateDescription();
    if (updateDescription != null) {
        asDoc.put(Fields.UPDATE_DESCRIPTION_FIELD, updateDescription.toBsonDocument());
    }
    asDoc.put(Fields.WRITE_PENDING_FIELD, new BsonBoolean(hasUncommittedWrites()));
    return asDoc;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) UpdateDescription(io.realm.mongodb.mongo.events.UpdateDescription) BsonBoolean(org.bson.BsonBoolean) BsonValue(org.bson.BsonValue)

Example 83 with BsonString

use of org.bson.BsonString in project spring-data-mongodb by spring-projects.

the class ChangeStreamTaskUnitTests method shouldNotBreakLovelaceBehavior.

// DATAMONGO-2258
@Test
void shouldNotBreakLovelaceBehavior() {
    BsonDocument resumeToken = new BsonDocument("token", new BsonString(UUID.randomUUID().toString()));
    when(changeStreamIterable.resumeAfter(any())).thenReturn(changeStreamIterable);
    ChangeStreamRequest request = // 
    ChangeStreamRequest.builder().collection(// 
    "start-wars").resumeToken(// 
    resumeToken).publishTo(// 
    message -> {
    }).build();
    initTask(request, Document.class);
    verify(changeStreamIterable).resumeAfter(eq(resumeToken));
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCollection(com.mongodb.client.MongoCollection) Mock(org.mockito.Mock) UUID(java.util.UUID) MongoDatabase(com.mongodb.client.MongoDatabase) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonString(org.bson.BsonString) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) ChangeStreamIterable(com.mongodb.client.ChangeStreamIterable) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) NoOpDbRefResolver(org.springframework.data.mongodb.core.convert.NoOpDbRefResolver) Mockito(org.mockito.Mockito) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoCursor(com.mongodb.client.MongoCursor) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Test(org.junit.jupiter.api.Test)

Example 84 with BsonString

use of org.bson.BsonString in project spring-data-mongodb by spring-projects.

the class ChangeStreamTaskUnitTests method shouldApplyStartAfterToChangeStream.

// DATAMONGO-2258
@Test
void shouldApplyStartAfterToChangeStream() {
    when(changeStreamIterable.startAfter(any())).thenReturn(changeStreamIterable);
    BsonDocument resumeToken = new BsonDocument("token", new BsonString(UUID.randomUUID().toString()));
    ChangeStreamRequest request = // 
    ChangeStreamRequest.builder().collection(// 
    "start-wars").startAfter(// 
    resumeToken).publishTo(// 
    message -> {
    }).build();
    initTask(request, Document.class);
    verify(changeStreamIterable).startAfter(eq(resumeToken));
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCollection(com.mongodb.client.MongoCollection) Mock(org.mockito.Mock) UUID(java.util.UUID) MongoDatabase(com.mongodb.client.MongoDatabase) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonString(org.bson.BsonString) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) ChangeStreamIterable(com.mongodb.client.ChangeStreamIterable) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) NoOpDbRefResolver(org.springframework.data.mongodb.core.convert.NoOpDbRefResolver) Mockito(org.mockito.Mockito) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoCursor(com.mongodb.client.MongoCursor) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Test(org.junit.jupiter.api.Test)

Example 85 with BsonString

use of org.bson.BsonString in project spring-data-mongodb by spring-projects.

the class ChangeStreamTaskUnitTests method shouldApplyResumeAfterToChangeStream.

// DATAMONGO-2258
@Test
void shouldApplyResumeAfterToChangeStream() {
    when(changeStreamIterable.resumeAfter(any())).thenReturn(changeStreamIterable);
    BsonDocument resumeToken = new BsonDocument("token", new BsonString(UUID.randomUUID().toString()));
    ChangeStreamRequest request = // 
    ChangeStreamRequest.builder().collection(// 
    "start-wars").resumeAfter(// 
    resumeToken).publishTo(// 
    message -> {
    }).build();
    initTask(request, Document.class);
    verify(changeStreamIterable).resumeAfter(eq(resumeToken));
}
Also used : Document(org.bson.Document) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoCollection(com.mongodb.client.MongoCollection) Mock(org.mockito.Mock) UUID(java.util.UUID) MongoDatabase(com.mongodb.client.MongoDatabase) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonString(org.bson.BsonString) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) ChangeStreamIterable(com.mongodb.client.ChangeStreamIterable) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) NoOpDbRefResolver(org.springframework.data.mongodb.core.convert.NoOpDbRefResolver) Mockito(org.mockito.Mockito) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MongoCursor(com.mongodb.client.MongoCursor) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Test(org.junit.jupiter.api.Test)

Aggregations

BsonString (org.bson.BsonString)178 BsonDocument (org.bson.BsonDocument)153 BsonInt32 (org.bson.BsonInt32)55 BsonArray (org.bson.BsonArray)48 Test (org.junit.Test)36 Document (org.bson.Document)32 BsonValue (org.bson.BsonValue)31 BsonInt64 (org.bson.BsonInt64)28 ArrayList (java.util.ArrayList)23 Test (org.junit.jupiter.api.Test)18 Map (java.util.Map)14 BsonDouble (org.bson.BsonDouble)14 MongoClientSettings (com.mongodb.MongoClientSettings)13 BsonBinary (org.bson.BsonBinary)13 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)12 HashMap (java.util.HashMap)12 MongoNamespace (com.mongodb.MongoNamespace)11 List (java.util.List)10 Before (org.junit.Before)10 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)9