Search in sources :

Example 1 with BsonValueCodec

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

the class ReactiveSessionBoundMongoTemplateUnitTests method setUp.

@Before
public void setUp() {
    when(client.getDatabase(anyString())).thenReturn(database);
    when(codecRegistry.get(any(Class.class))).thenReturn(new BsonValueCodec());
    when(database.getCodecRegistry()).thenReturn(codecRegistry);
    when(database.getCollection(anyString())).thenReturn(collection);
    when(database.getCollection(anyString(), any())).thenReturn(collection);
    when(database.listCollectionNames(any(ClientSession.class))).thenReturn(findPublisher);
    when(database.createCollection(any(ClientSession.class), any(), any())).thenReturn(resultPublisher);
    when(database.runCommand(any(ClientSession.class), any(), any(Class.class))).thenReturn(resultPublisher);
    when(collection.find(any(ClientSession.class))).thenReturn(findPublisher);
    when(collection.find(any(ClientSession.class), any(Document.class))).thenReturn(findPublisher);
    when(collection.find(any(ClientSession.class), any(Class.class))).thenReturn(findPublisher);
    when(collection.find(any(ClientSession.class), any(), any())).thenReturn(findPublisher);
    when(collection.deleteMany(any(ClientSession.class), any(), any())).thenReturn(resultPublisher);
    when(collection.insertOne(any(ClientSession.class), any(Document.class))).thenReturn(resultPublisher);
    when(collection.aggregate(any(ClientSession.class), anyList(), any(Class.class))).thenReturn(aggregatePublisher);
    when(collection.countDocuments(any(ClientSession.class), any(), any(CountOptions.class))).thenReturn(resultPublisher);
    when(collection.drop(any(ClientSession.class))).thenReturn(resultPublisher);
    when(collection.findOneAndUpdate(any(ClientSession.class), any(), any(Bson.class), any())).thenReturn(resultPublisher);
    when(collection.distinct(any(ClientSession.class), any(), any(Bson.class), any())).thenReturn(distinctPublisher);
    when(collection.updateOne(any(ClientSession.class), any(), any(Bson.class), any(UpdateOptions.class))).thenReturn(resultPublisher);
    when(collection.updateMany(any(ClientSession.class), any(), any(Bson.class), any(UpdateOptions.class))).thenReturn(resultPublisher);
    when(collection.dropIndex(any(ClientSession.class), anyString())).thenReturn(resultPublisher);
    when(collection.mapReduce(any(ClientSession.class), any(), any(), any())).thenReturn(mapReducePublisher);
    when(findPublisher.projection(any())).thenReturn(findPublisher);
    when(findPublisher.limit(anyInt())).thenReturn(findPublisher);
    when(findPublisher.collation(any())).thenReturn(findPublisher);
    when(findPublisher.first()).thenReturn(resultPublisher);
    when(aggregatePublisher.allowDiskUse(anyBoolean())).thenReturn(aggregatePublisher);
    factory = new SimpleReactiveMongoDatabaseFactory(client, "foo");
    this.mappingContext = new MongoMappingContext();
    this.converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
    this.template = new ReactiveSessionBoundMongoTemplate(clientSession, new ReactiveMongoTemplate(factory, converter));
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) ClientSession(com.mongodb.reactivestreams.client.ClientSession) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) CountOptions(com.mongodb.client.model.CountOptions) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Document(org.bson.Document) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) UpdateOptions(com.mongodb.client.model.UpdateOptions) ReactiveSessionBoundMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate.ReactiveSessionBoundMongoTemplate) Bson(org.bson.conversions.Bson) Before(org.junit.Before)

Example 2 with BsonValueCodec

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

the class DBCollection method distinct.

/**
 * Find the distinct values for a specified field across a collection and returns the results in an array.
 *
 * @param fieldName  Specifies the field for which to return the distinct values
 * @param options    the options to apply for this operation
 * @return A {@code List} of the distinct values
 * @mongodb.driver.manual reference/command/distinct Distinct Command
 * @since 3.4
 */
@SuppressWarnings("unchecked")
public List distinct(final String fieldName, final DBCollectionDistinctOptions options) {
    notNull("fieldName", fieldName);
    return new MongoIterableImpl<BsonValue>(null, executor, options.getReadConcern() != null ? options.getReadConcern() : getReadConcern(), options.getReadPreference() != null ? options.getReadPreference() : getReadPreference(), retryReads) {

        @Override
        public ReadOperation<BatchCursor<BsonValue>> asReadOperation() {
            return new DistinctOperation<BsonValue>(getNamespace(), fieldName, new BsonValueCodec()).filter(wrapAllowNull(options.getFilter())).collation(options.getCollation()).retryReads(retryReads);
        }
    }.map(new Function<BsonValue, Object>() {

        @Override
        public Object apply(final BsonValue bsonValue) {
            if (bsonValue == null) {
                return null;
            }
            BsonDocument document = new BsonDocument("value", bsonValue);
            DBObject obj = getDefaultDBObjectCodec().decode(new BsonDocumentReader(document), DecoderContext.builder().build());
            return obj.get("value");
        }
    }).into(new ArrayList<Object>());
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) BatchCursor(com.mongodb.internal.operation.BatchCursor) MapReduceBatchCursor(com.mongodb.internal.operation.MapReduceBatchCursor) BsonDocument(org.bson.BsonDocument) BsonDocumentReader(org.bson.BsonDocumentReader) MongoIterableImpl(com.mongodb.client.internal.MongoIterableImpl) BsonValue(org.bson.BsonValue)

Example 3 with BsonValueCodec

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

the class SessionBoundMongoTemplateUnitTests method setUp.

@Before
public void setUp() {
    when(client.getDatabase(anyString())).thenReturn(database);
    when(codecRegistry.get(any(Class.class))).thenReturn(new BsonValueCodec());
    when(database.getCodecRegistry()).thenReturn(codecRegistry);
    when(database.getCollection(anyString(), any())).thenReturn(collection);
    when(database.listCollectionNames(any(ClientSession.class))).thenReturn(mongoIterable);
    when(collection.find(any(ClientSession.class), any(), any())).thenReturn(findIterable);
    when(collection.aggregate(any(ClientSession.class), anyList(), any())).thenReturn(aggregateIterable);
    when(collection.distinct(any(ClientSession.class), any(), any(), any())).thenReturn(distinctIterable);
    when(collection.mapReduce(any(ClientSession.class), any(), any(), any())).thenReturn(mapReduceIterable);
    when(findIterable.iterator()).thenReturn(cursor);
    when(aggregateIterable.collation(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.allowDiskUse(anyBoolean())).thenReturn(aggregateIterable);
    when(aggregateIterable.batchSize(anyInt())).thenReturn(aggregateIterable);
    when(aggregateIterable.map(any())).thenReturn(aggregateIterable);
    when(aggregateIterable.into(any())).thenReturn(Collections.emptyList());
    when(mongoIterable.iterator()).thenReturn(cursor);
    when(distinctIterable.map(any())).thenReturn(distinctIterable);
    when(distinctIterable.into(any())).thenReturn(Collections.emptyList());
    when(mapReduceIterable.sort(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.filter(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.map(any())).thenReturn(mapReduceIterable);
    when(mapReduceIterable.iterator()).thenReturn(cursor);
    when(cursor.hasNext()).thenReturn(false);
    when(findIterable.projection(any())).thenReturn(findIterable);
    factory = new SimpleMongoClientDatabaseFactory(client, "foo");
    this.mappingContext = new MongoMappingContext();
    this.converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext);
    this.template = new SessionBoundMongoTemplate(clientSession, new MongoTemplate(factory, converter));
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) ClientSession(com.mongodb.client.ClientSession) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Before(org.junit.Before)

Aggregations

BsonValueCodec (org.bson.codecs.BsonValueCodec)3 Before (org.junit.Before)2 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)2 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)2 ClientSession (com.mongodb.client.ClientSession)1 MongoIterableImpl (com.mongodb.client.internal.MongoIterableImpl)1 CountOptions (com.mongodb.client.model.CountOptions)1 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)1 UpdateOptions (com.mongodb.client.model.UpdateOptions)1 BatchCursor (com.mongodb.internal.operation.BatchCursor)1 MapReduceBatchCursor (com.mongodb.internal.operation.MapReduceBatchCursor)1 ClientSession (com.mongodb.reactivestreams.client.ClientSession)1 BsonDocument (org.bson.BsonDocument)1 BsonDocumentReader (org.bson.BsonDocumentReader)1 BsonValue (org.bson.BsonValue)1 Document (org.bson.Document)1 Bson (org.bson.conversions.Bson)1 SessionBoundMongoTemplate (org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate)1 ReactiveSessionBoundMongoTemplate (org.springframework.data.mongodb.core.ReactiveMongoTemplate.ReactiveSessionBoundMongoTemplate)1 DefaultDbRefResolver (org.springframework.data.mongodb.core.convert.DefaultDbRefResolver)1