Search in sources :

Example 86 with BsonString

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

the class GridFsTemplateIntegrationTests method storeSavesGridFsUploadWithGivenIdCorrectly.

// DATAMONGO-625
@Test
public void storeSavesGridFsUploadWithGivenIdCorrectly() throws IOException {
    String id = "id-1";
    GridFsUpload<String> upload = // 
    GridFsUpload.fromStream(resource.getInputStream()).id(// 
    id).filename(// 
    "gridFsUpload.xml").contentType(// 
    "xml").build();
    assertThat(operations.store(upload)).isEqualTo(id);
    GridFsResource fsFile = operations.getResource(operations.findOne(query(where("_id").is(id))));
    byte[] content = StreamUtils.copyToByteArray(fsFile.getInputStream());
    assertThat(content).isEqualTo(StreamUtils.copyToByteArray(resource.getInputStream()));
    assertThat(fsFile.getFilename()).isEqualTo("gridFsUpload.xml");
    assertThat(fsFile.getId()).isEqualTo(new BsonString(id));
    assertThat(fsFile.getFileId()).isEqualTo(id);
    assertThat(fsFile.getContentType()).isEqualTo("xml");
}
Also used : BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) Test(org.junit.Test)

Example 87 with BsonString

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

the class ReactiveGridFsTemplateTests method storeSavesGridFsUploadWithGivenIdCorrectly.

// DATAMONGO-625
@Test
public void storeSavesGridFsUploadWithGivenIdCorrectly() throws IOException {
    String id = "id-1";
    byte[] content = StreamUtils.copyToByteArray(resource.getInputStream());
    Flux<DataBuffer> data = DataBufferUtils.read(resource, new DefaultDataBufferFactory(), 256);
    ReactiveGridFsUpload<String> upload = // 
    ReactiveGridFsUpload.fromPublisher(data).id(// 
    id).filename(// 
    "gridFsUpload.xml").contentType(// 
    "xml").build();
    operations.store(upload).as(StepVerifier::create).expectNext(id).verifyComplete();
    operations.findOne(query(where("_id").is(id))).flatMap(operations::getResource).flatMapMany(// 
    ReactiveGridFsResource::getDownloadStream).transform(// 
    DataBufferUtils::join).as(// 
    StepVerifier::create).consumeNextWith(dataBuffer -> {
        byte[] actual = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(actual);
        assertThat(actual).isEqualTo(content);
    }).verifyComplete();
    operations.findOne(query(where("_id").is(id))).as(StepVerifier::create).consumeNextWith(it -> {
        assertThat(it.getFilename()).isEqualTo("gridFsUpload.xml");
        assertThat(it.getId()).isEqualTo(new BsonString(id));
        assertThat(it.getMetadata()).containsValue("xml");
    }).verifyComplete();
}
Also used : Document(org.bson.Document) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) RunWith(org.junit.runner.RunWith) ClassPathResource(org.springframework.core.io.ClassPathResource) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) Autowired(org.springframework.beans.factory.annotation.Autowired) BsonString(org.bson.BsonString) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) ByteBuffer(java.nio.ByteBuffer) Assertions(org.assertj.core.api.Assertions) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Sort(org.springframework.data.domain.Sort) SpringRunner(org.springframework.test.context.junit4.SpringRunner) Before(org.junit.Before) Resource(org.springframework.core.io.Resource) StreamUtils(org.springframework.util.StreamUtils) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) BsonObjectId(org.bson.BsonObjectId) PageRequest(org.springframework.data.domain.PageRequest) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) Test(org.junit.Test) GridFsCriteria(org.springframework.data.mongodb.gridfs.GridFsCriteria) DataBuffer(org.springframework.core.io.buffer.DataBuffer) InputStreamReader(java.io.InputStreamReader) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Flux(reactor.core.publisher.Flux) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ObjectId(org.bson.types.ObjectId) ReactiveMongoDatabaseFactory(org.springframework.data.mongodb.ReactiveMongoDatabaseFactory) HexUtils(com.mongodb.internal.HexUtils) FileCopyUtils(org.springframework.util.FileCopyUtils) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) StepVerifier(reactor.test.StepVerifier) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 88 with BsonString

use of org.bson.BsonString in project brave by openzipkin.

the class TraceMongoCommandListenerTest method getCollectionName_allowListedCommandAndCollectionField.

@Test
public void getCollectionName_allowListedCommandAndCollectionField() {
    BsonDocument command = new BsonDocument(Arrays.asList(new BsonElement("collection", new BsonString("coll")), new BsonElement("find", new BsonString("bar"))));
    // command wins
    assertThat(listener.getCollectionName(command, "find")).isEqualTo("bar");
}
Also used : BsonElement(org.bson.BsonElement) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) TraceMongoCommandListener.getNonEmptyBsonString(brave.mongodb.TraceMongoCommandListener.getNonEmptyBsonString) Test(org.junit.Test)

Example 89 with BsonString

use of org.bson.BsonString 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)

Example 90 with BsonString

use of org.bson.BsonString in project morphia by mongodb.

the class IndexHelperTest method calculateKeys.

@Test
public void calculateKeys() {
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    BsonDocument keys = indexHelper.calculateKeys(mappedClass, new IndexBuilder().fields(new FieldBuilder().value("text").type(IndexType.TEXT).weight(1), new FieldBuilder().value("nest").type(IndexType.DESC)));
    assertEquals(new BsonDocument().append("text", new BsonString("text")).append("nest", new BsonInt32(-1)), keys);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.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