Simplify usage of user provided aggregation operations.
Introduce Aggregation.stage which allows to use a plain JSON String or any valid Bson representation to be used within an aggregation pipeline stage, without having to implement AggregationOperation directly. The change allows to make use of driver native builder API for aggregates. Original pull request: #4059. Closes #4038
This commit is contained in:
committed by
Mark Paluch
parent
1184d6ee2d
commit
ee076ec02f
@@ -21,6 +21,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.mongodb.core.aggregation.AddFieldsOperation.AddFieldsOperationBuilder;
|
||||
@@ -238,6 +239,40 @@ public class Aggregation {
|
||||
return AddFieldsOperation.builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link AggregationOperation} taking the given {@link Bson bson value} as is. <br />
|
||||
*
|
||||
* <pre class="code">
|
||||
* Aggregation.stage(Aggregates.search(exists(fieldPath("..."))));
|
||||
* </pre>
|
||||
*
|
||||
* Field mapping against a potential domain type or previous aggregation stages will not happen.
|
||||
*
|
||||
* @param aggregationOperation the must not be {@literal null}.
|
||||
* @return new instance of {@link AggregationOperation}.
|
||||
* @since 4.0
|
||||
*/
|
||||
public static AggregationOperation stage(Bson aggregationOperation) {
|
||||
return new BasicAggregationOperation(aggregationOperation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link AggregationOperation} taking the given {@link String json value} as is. <br />
|
||||
*
|
||||
* <pre class="code">
|
||||
* Aggregation.stage("{ $search : { near : { path : 'released' , origin : ... } } }");
|
||||
* </pre>
|
||||
*
|
||||
* Field mapping against a potential domain type or previous aggregation stages will not happen.
|
||||
*
|
||||
* @param json the JSON representation of the pipeline stage. Must not be {@literal null}.
|
||||
* @return new instance of {@link AggregationOperation}.
|
||||
* @since 4.0
|
||||
*/
|
||||
public static AggregationOperation stage(String json) {
|
||||
return new BasicAggregationOperation(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link ProjectionOperation} including the given fields.
|
||||
*
|
||||
|
||||
@@ -20,12 +20,16 @@ import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.data.mongodb.CodecRegistryProvider;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import com.mongodb.MongoClientSettings;
|
||||
|
||||
/**
|
||||
* The context for an {@link AggregationOperation}.
|
||||
*
|
||||
@@ -33,7 +37,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
* @author Christoph Strobl
|
||||
* @since 1.3
|
||||
*/
|
||||
public interface AggregationOperationContext {
|
||||
public interface AggregationOperationContext extends CodecRegistryProvider {
|
||||
|
||||
/**
|
||||
* Returns the mapped {@link Document}, potentially converting the source considering mapping metadata etc.
|
||||
@@ -114,4 +118,9 @@ public interface AggregationOperationContext {
|
||||
default AggregationOperationContext continueOnMissingFieldReference() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
default CodecRegistry getCodecRegistry() {
|
||||
return MongoClientSettings.getDefaultCodecRegistry();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.aggregation;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.springframework.data.mongodb.util.BsonUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* {@link AggregationOperation} implementation that c
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @since 4.0
|
||||
*/
|
||||
class BasicAggregationOperation implements AggregationOperation {
|
||||
|
||||
private final Object value;
|
||||
|
||||
BasicAggregationOperation(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Document toDocument(AggregationOperationContext context) {
|
||||
|
||||
if (value instanceof Document document) {
|
||||
return document;
|
||||
}
|
||||
|
||||
if (value instanceof Bson bson) {
|
||||
return BsonUtils.asDocument(bson, context.getCodecRegistry());
|
||||
}
|
||||
|
||||
if (value instanceof Map map) {
|
||||
return new Document(map);
|
||||
}
|
||||
|
||||
if (value instanceof String json && BsonUtils.isJsonDocument(json)) {
|
||||
return BsonUtils.parse(json, context);
|
||||
}
|
||||
|
||||
throw new IllegalStateException(
|
||||
String.format("%s cannot be converted to org.bson.Document.", ObjectUtils.nullSafeClassName(value)));
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.mongodb.core.aggregation;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
|
||||
@@ -141,4 +142,9 @@ class ExposedFieldsAggregationOperationContext implements AggregationOperationCo
|
||||
AggregationOperationContext getRootContext() {
|
||||
return rootContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodecRegistry getCodecRegistry() {
|
||||
return getRootContext().getCodecRegistry();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.util.Collection;
|
||||
|
||||
import org.bson.Document;
|
||||
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExpressionFieldReference;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -92,4 +93,9 @@ class NestedDelegatingExpressionAggregationOperationContext implements Aggregati
|
||||
public Fields getFields(Class<?> type) {
|
||||
return delegate.getFields(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodecRegistry getCodecRegistry() {
|
||||
return delegate.getCodecRegistry();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -80,6 +81,11 @@ public class PrefixingDelegatingAggregationOperationContext implements Aggregati
|
||||
return delegate.getFields(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodecRegistry getCodecRegistry() {
|
||||
return delegate.getCodecRegistry();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Document doPrefix(Document source) {
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
|
||||
import org.bson.Document;
|
||||
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.springframework.data.mapping.PersistentPropertyPath;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference;
|
||||
@@ -147,4 +148,9 @@ public class TypeBasedAggregationOperationContext implements AggregationOperatio
|
||||
public Class<?> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodecRegistry getCodecRegistry() {
|
||||
return this.mapper.getConverter().getCodecRegistry();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.bson.Document;
|
||||
import org.bson.codecs.Codec;
|
||||
import org.bson.codecs.DecoderContext;
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.bson.json.JsonReader;
|
||||
import org.bson.types.ObjectId;
|
||||
@@ -1793,6 +1794,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
return conversions.getCustomWriteTarget(source).orElse(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodecRegistry getCodecRegistry() {
|
||||
return codecRegistryProvider != null ? codecRegistryProvider.getCodecRegistry() : super.getCodecRegistry();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MappingMongoConverter} using the given {@link MongoDatabaseFactory} when loading {@link DBRef}.
|
||||
*
|
||||
|
||||
@@ -15,16 +15,18 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.convert;
|
||||
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import org.bson.BsonValue;
|
||||
import org.bson.Document;
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import org.springframework.core.convert.ConversionException;
|
||||
import org.springframework.data.convert.CustomConversions;
|
||||
import org.springframework.data.convert.EntityConverter;
|
||||
import org.springframework.data.convert.EntityReader;
|
||||
import org.springframework.data.convert.TypeMapper;
|
||||
import org.springframework.data.mongodb.CodecRegistryProvider;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
|
||||
import org.springframework.data.mongodb.util.BsonUtils;
|
||||
@@ -48,7 +50,7 @@ import com.mongodb.DBRef;
|
||||
*/
|
||||
public interface MongoConverter
|
||||
extends EntityConverter<MongoPersistentEntity<?>, MongoPersistentProperty, Object, Bson>, MongoWriter<Object>,
|
||||
EntityReader<Object, Bson> {
|
||||
EntityReader<Object, Bson>, CodecRegistryProvider {
|
||||
|
||||
/**
|
||||
* Returns the {@link TypeMapper} being used to write type information into {@link Document}s created with that
|
||||
@@ -188,4 +190,9 @@ public interface MongoConverter
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
default CodecRegistry getCodecRegistry() {
|
||||
return MongoClientSettings.getDefaultCodecRegistry();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1486,4 +1486,8 @@ public class QueryMapper {
|
||||
public MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> getMappingContext() {
|
||||
return mappingContext;
|
||||
}
|
||||
|
||||
public MongoConverter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.bson.BsonString;
|
||||
import org.bson.BsonValue;
|
||||
import org.bson.Document;
|
||||
import org.bson.codecs.DocumentCodec;
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.bson.json.JsonParseException;
|
||||
import org.bson.types.ObjectId;
|
||||
@@ -81,18 +82,36 @@ public class BsonUtils {
|
||||
* @return
|
||||
*/
|
||||
public static Map<String, Object> asMap(Bson bson) {
|
||||
return asMap(bson, MongoClientSettings.getDefaultCodecRegistry());
|
||||
}
|
||||
|
||||
if (bson instanceof Document) {
|
||||
return (Document) bson;
|
||||
}
|
||||
if (bson instanceof BasicDBObject) {
|
||||
return ((BasicDBObject) bson);
|
||||
}
|
||||
if (bson instanceof DBObject) {
|
||||
return ((DBObject) bson).toMap();
|
||||
/**
|
||||
* Return the {@link Bson} object as {@link Map}. Depending on the input type, the return value can be either a casted
|
||||
* version of {@code bson} or a converted (detached from the original value) using the given {@link CodecRegistry} to
|
||||
* obtain {@link org.bson.codecs.Codec codecs} that might be required for conversion.
|
||||
*
|
||||
* @param bson can be {@literal null}.
|
||||
* @param codecRegistry must not be {@literal null}.
|
||||
* @return never {@literal null}. Returns an empty {@link Map} if input {@link Bson} is {@literal null}.
|
||||
* @since 4.0
|
||||
*/
|
||||
public static Map<String, Object> asMap(@Nullable Bson bson, CodecRegistry codecRegistry) {
|
||||
|
||||
if (bson == null) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
return (Map) bson.toBsonDocument(Document.class, MongoClientSettings.getDefaultCodecRegistry());
|
||||
if (bson instanceof Document document) {
|
||||
return document;
|
||||
}
|
||||
if (bson instanceof BasicDBObject dbo) {
|
||||
return dbo;
|
||||
}
|
||||
if (bson instanceof DBObject dbo) {
|
||||
return dbo.toMap();
|
||||
}
|
||||
|
||||
return new Document((Map) bson.toBsonDocument(Document.class, codecRegistry));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,12 +123,26 @@ public class BsonUtils {
|
||||
* @since 3.2.5
|
||||
*/
|
||||
public static Document asDocument(Bson bson) {
|
||||
return asDocument(bson, MongoClientSettings.getDefaultCodecRegistry());
|
||||
}
|
||||
|
||||
if (bson instanceof Document) {
|
||||
return (Document) bson;
|
||||
/**
|
||||
* Return the {@link Bson} object as {@link Document}. Depending on the input type, the return value can be either a
|
||||
* casted version of {@code bson} or a converted (detached from the original value) using the given
|
||||
* {@link CodecRegistry} to obtain {@link org.bson.codecs.Codec codecs} that might be required for conversion.
|
||||
*
|
||||
* @param bson
|
||||
* @param codecRegistry must not be {@literal null}.
|
||||
* @return never {@literal null}.
|
||||
* @since 4.0
|
||||
*/
|
||||
public static Document asDocument(Bson bson, CodecRegistry codecRegistry) {
|
||||
|
||||
if (bson instanceof Document document) {
|
||||
return document;
|
||||
}
|
||||
|
||||
Map<String, Object> map = asMap(bson);
|
||||
Map<String, Object> map = asMap(bson, codecRegistry);
|
||||
|
||||
if (map instanceof Document) {
|
||||
return (Document) map;
|
||||
@@ -413,7 +446,7 @@ public class BsonUtils {
|
||||
*/
|
||||
public static boolean isJsonDocument(@Nullable String value) {
|
||||
|
||||
if(!StringUtils.hasText(value)) {
|
||||
if (!StringUtils.hasText(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.mongodb.util.aggregation;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.codecs.configuration.CodecRegistry;
|
||||
import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
||||
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
|
||||
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
|
||||
@@ -72,4 +73,9 @@ public class TestAggregationContext implements AggregationOperationContext {
|
||||
public FieldReference getReference(String name) {
|
||||
return delegate.getReference(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodecRegistry getCodecRegistry() {
|
||||
return delegate.getCodecRegistry();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -37,6 +38,9 @@ import org.springframework.data.mongodb.core.convert.QueryMapper;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
|
||||
import com.mongodb.client.model.Aggregates;
|
||||
import com.mongodb.client.model.Projections;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Aggregation}.
|
||||
*
|
||||
@@ -599,31 +603,51 @@ public class AggregationUnitTests {
|
||||
assertThat(extractPipelineElement(target, 1, "$project")).isEqualTo(Document.parse(" { \"_id\" : \"$_id\" }"));
|
||||
}
|
||||
|
||||
|
||||
@Test // GH-3898
|
||||
void shouldNotConvertIncludeExcludeValuesForProjectOperation() {
|
||||
|
||||
MongoMappingContext mappingContext = new MongoMappingContext();
|
||||
RelaxedTypeBasedAggregationOperationContext context = new RelaxedTypeBasedAggregationOperationContext(WithRetypedIdField.class, mappingContext,
|
||||
RelaxedTypeBasedAggregationOperationContext context = new RelaxedTypeBasedAggregationOperationContext(
|
||||
WithRetypedIdField.class, mappingContext,
|
||||
new QueryMapper(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext)));
|
||||
Document document = project(WithRetypedIdField.class).toDocument(context);
|
||||
assertThat(document).isEqualTo(new Document("$project", new Document("_id", 1).append("renamed-field", 1)));
|
||||
}
|
||||
|
||||
@Test // GH-4038
|
||||
void createsBasicAggregationOperationFromJsonString() {
|
||||
|
||||
AggregationOperation stage = stage("{ $project : { name : 1} }");
|
||||
Document target = newAggregation(stage).toDocument("col-1", DEFAULT_CONTEXT);
|
||||
assertThat(extractPipelineElement(target, 0, "$project")).containsEntry("name", 1);
|
||||
}
|
||||
|
||||
@Test // GH-4038
|
||||
void createsBasicAggregationOperationFromBson() {
|
||||
|
||||
AggregationOperation stage = stage(Aggregates.project(Projections.fields(Projections.include("name"))));
|
||||
Document target = newAggregation(stage).toDocument("col-1", DEFAULT_CONTEXT);
|
||||
assertThat(extractPipelineElement(target, 0, "$project")).containsKey("name");
|
||||
}
|
||||
|
||||
private Document extractPipelineElement(Document agg, int index, String operation) {
|
||||
|
||||
List<Document> pipeline = (List<Document>) agg.get("pipeline");
|
||||
return (Document) pipeline.get(index).get(operation);
|
||||
Object value = pipeline.get(index).get(operation);
|
||||
if (value instanceof Document document) {
|
||||
return document;
|
||||
}
|
||||
if (value instanceof Map map) {
|
||||
return new Document(map);
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public class WithRetypedIdField {
|
||||
|
||||
@Id
|
||||
@org.springframework.data.mongodb.core.mapping.Field
|
||||
private String id;
|
||||
@Id @org.springframework.data.mongodb.core.mapping.Field private String id;
|
||||
|
||||
@org.springframework.data.mongodb.core.mapping.Field("renamed-field")
|
||||
private String foo;
|
||||
@org.springframework.data.mongodb.core.mapping.Field("renamed-field") private String foo;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.aggregation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverter;
|
||||
import org.springframework.data.mongodb.core.convert.QueryMapper;
|
||||
import org.springframework.data.mongodb.core.mapping.Field;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
|
||||
import com.mongodb.MongoClientSettings;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class BasicAggregationOperationUnitTests {
|
||||
|
||||
@Mock QueryMapper queryMapper;
|
||||
@Mock MongoConverter converter;
|
||||
|
||||
TypeBasedAggregationOperationContext ctx;
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
|
||||
// no field mapping though having a type based context
|
||||
ctx = new TypeBasedAggregationOperationContext(Person.class, new MongoMappingContext(), queryMapper);
|
||||
when(queryMapper.getConverter()).thenReturn(converter);
|
||||
when(converter.getCodecRegistry()).thenReturn(MongoClientSettings.getDefaultCodecRegistry());
|
||||
}
|
||||
|
||||
@Test // GH-4038
|
||||
void usesGivenDocumentAsIs() {
|
||||
|
||||
Document source = new Document("value", 1);
|
||||
assertThat(new BasicAggregationOperation(source).toDocument(ctx)).isSameAs(source);
|
||||
}
|
||||
|
||||
@Test // GH-4038
|
||||
void parsesJson() {
|
||||
|
||||
Document source = new Document("value", 1);
|
||||
assertThat(new BasicAggregationOperation(source.toJson()).toDocument(ctx)).isEqualTo(source);
|
||||
}
|
||||
|
||||
@Test // GH-4038
|
||||
void errorsOnInvalidValue() {
|
||||
|
||||
BasicAggregationOperation agg = new BasicAggregationOperation(new Object());
|
||||
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> agg.toDocument(ctx));
|
||||
}
|
||||
|
||||
@Test // GH-4038
|
||||
void errorsOnNonJsonSting() {
|
||||
|
||||
BasicAggregationOperation agg = new BasicAggregationOperation("#005BBB #FFD500");
|
||||
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> agg.toDocument(ctx));
|
||||
}
|
||||
|
||||
private static class Person {
|
||||
|
||||
@Field("v-a-l-u-e") Object value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user