Search in sources :

Example 11 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class MongoTemplateUnitTests method convertsUpdateConstraintsUsingConverters.

// DATAMONGO-374
@Test
void convertsUpdateConstraintsUsingConverters() {
    CustomConversions conversions = new MongoCustomConversions(Collections.singletonList(MyConverter.INSTANCE));
    this.converter.setCustomConversions(conversions);
    this.converter.afterPropertiesSet();
    Query query = new Query();
    Update update = new Update().set("foo", new AutogenerateableId());
    template.updateFirst(query, update, Wrapper.class);
    QueryMapper queryMapper = new QueryMapper(converter);
    Document reference = queryMapper.getMappedObject(update.getUpdateObject(), Optional.empty());
    verify(collection, times(1)).updateOne(any(org.bson.Document.class), eq(reference), any(UpdateOptions.class));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) CustomConversions(org.springframework.data.convert.CustomConversions) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) Update(org.springframework.data.mongodb.core.query.Update) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Test(org.junit.jupiter.api.Test)

Example 12 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class AddFieldsOperationUnitTests method contextFor.

private static AggregationOperationContext contextFor(@Nullable Class<?> type) {
    if (type == null) {
        return Aggregation.DEFAULT_CONTEXT;
    }
    MappingMongoConverter mongoConverter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, new MongoMappingContext());
    mongoConverter.afterPropertiesSet();
    return new TypeBasedAggregationOperationContext(type, mongoConverter.getMappingContext(), new QueryMapper(mongoConverter)).continueOnMissingFieldReference();
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper)

Example 13 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class CountQueryUnitTests method setUp.

@BeforeEach
void setUp() {
    this.context = new MongoMappingContext();
    this.converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, context);
    this.converter.afterPropertiesSet();
    this.mapper = new QueryMapper(converter);
}
Also used : MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsUnitTests method noAfterSaveEventOnFailure.

// DATAMONGO-2290
@Test
void noAfterSaveEventOnFailure() {
    ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
    when(collection.bulkWrite(anyList(), any(BulkWriteOptions.class))).thenThrow(new MongoWriteException(new WriteError(89, "NetworkTimeout", new BsonDocument("hi", new BsonString("there!"))), null));
    ops = new DefaultBulkOperations(template, "collection-1", new BulkOperationContext(BulkMode.ORDERED, Optional.of(mappingContext.getPersistentEntity(Person.class)), new QueryMapper(converter), new UpdateMapper(converter), eventPublisher, null));
    ops.insert(new SomeDomainType());
    verify(eventPublisher).publishEvent(any(BeforeConvertEvent.class));
    try {
        ops.execute();
        fail("Missing MongoWriteException");
    } catch (MongoWriteException expected) {
    }
    verify(eventPublisher).publishEvent(any(BeforeSaveEvent.class));
}
Also used : BeforeSaveEvent(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent) MongoWriteException(com.mongodb.MongoWriteException) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) WriteError(com.mongodb.WriteError) BulkWriteError(com.mongodb.bulk.BulkWriteError) BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) Test(org.junit.jupiter.api.Test)

Example 15 with QueryMapper

use of org.springframework.data.mongodb.core.convert.QueryMapper in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsUnitTests method setUp.

@BeforeEach
void setUp() {
    when(factory.getMongoDatabase()).thenReturn(database);
    when(factory.getExceptionTranslator()).thenReturn(new NullExceptionTranslator());
    when(database.getCollection(anyString(), eq(Document.class))).thenReturn(collection);
    mappingContext = new MongoMappingContext();
    mappingContext.afterPropertiesSet();
    converter = new MappingMongoConverter(dbRefResolver, mappingContext);
    template = new MongoTemplate(factory, converter);
    ops = new DefaultBulkOperations(template, "collection-1", new BulkOperationContext(BulkMode.ORDERED, Optional.of(mappingContext.getPersistentEntity(SomeDomainType.class)), new QueryMapper(converter), new UpdateMapper(converter), null, null));
}
Also used : BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)29 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)19 MongoMappingContext (org.springframework.data.mongodb.core.mapping.MongoMappingContext)19 Test (org.junit.jupiter.api.Test)14 Document (org.bson.Document)11 BulkOperationContext (org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext)7 UpdateMapper (org.springframework.data.mongodb.core.convert.UpdateMapper)7 BsonDocument (org.bson.BsonDocument)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)3 Index (org.springframework.data.mongodb.core.index.Index)3 IndexDefinition (org.springframework.data.mongodb.core.index.IndexDefinition)3 IndexInfo (org.springframework.data.mongodb.core.index.IndexInfo)3 BeforeConvertEvent (org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent)3 BeforeSaveEvent (org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent)3 Test (org.junit.Test)2 MongoConverter (org.springframework.data.mongodb.core.convert.MongoConverter)2 AfterSaveEvent (org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent)2 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)2 MongoWriteException (com.mongodb.MongoWriteException)1