Polishing.

Add license headers. Update Javadoc, author, and since tags. Add tests. Add toCriteriaDefinition method.

See #3790
This commit is contained in:
Mark Paluch
2021-09-06 15:07:02 +02:00
parent e71ec874ab
commit 34d66a276a
8 changed files with 156 additions and 73 deletions

View File

@@ -500,13 +500,14 @@ public class Aggregation {
} }
/** /**
* Creates a new {@link MatchOperation} * Creates a new {@link MatchOperation} using the given {@link AggregationExpression}.
* *
* @param expression must not be {@literal null}.
* @return new instance of {@link MatchOperation}. * @return new instance of {@link MatchOperation}.
* @since 1.10 * @since 3.3
*/ */
public static MatchOperation match() { public static MatchOperation match(AggregationExpression expression) {
return new MatchOperation(); return new MatchOperation(expression);
} }
/** /**

View File

@@ -1,7 +1,31 @@
/*
* Copyright 2021 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; package org.springframework.data.mongodb.core.aggregation;
import org.bson.Document;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/**
* Gateway to {@literal evaluation operators} such as {@literal $expr}.
*
* @author Divya Srivastava
* @since 3.3
*/
public class EvaluationOperators { public class EvaluationOperators {
/** /**
@@ -41,7 +65,6 @@ public class EvaluationOperators {
this.expression = null; this.expression = null;
} }
/** /**
* Creates new {@link EvaluationOperatorFactory} for given {@link AggregationExpression}. * Creates new {@link EvaluationOperatorFactory} for given {@link AggregationExpression}.
* *
@@ -63,7 +86,9 @@ public class EvaluationOperators {
return usesFieldRef() ? Expr.valueOf(fieldReference) : Expr.valueOf(expression); return usesFieldRef() ? Expr.valueOf(fieldReference) : Expr.valueOf(expression);
} }
/**
* Allows the use of aggregation expressions within the query language.
*/
public static class Expr extends AbstractAggregationExpression { public static class Expr extends AbstractAggregationExpression {
private Expr(Object value) { private Expr(Object value) {
@@ -99,6 +124,27 @@ public class EvaluationOperators {
return new Expr(expression); return new Expr(expression);
} }
/**
* Creates {@code $expr} as {@link CriteriaDefinition}.
*
* @return the {@link CriteriaDefinition} from this expression.
*/
public CriteriaDefinition toCriteriaDefinition(AggregationOperationContext context) {
Document criteriaObject = toDocument(context);
return new CriteriaDefinition() {
@Override
public Document getCriteriaObject() {
return criteriaObject;
}
@Override
public String getKey() {
return getMongoMethod();
}
};
}
} }
private boolean usesFieldRef() { private boolean usesFieldRef() {

View File

@@ -16,7 +16,7 @@
package org.springframework.data.mongodb.core.aggregation; package org.springframework.data.mongodb.core.aggregation;
import org.bson.Document; import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.EvaluationOperators.EvaluationOperatorFactory.Expr;
import org.springframework.data.mongodb.core.query.CriteriaDefinition; import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -30,6 +30,7 @@ import org.springframework.util.Assert;
* @author Sebastian Herold * @author Sebastian Herold
* @author Thomas Darimont * @author Thomas Darimont
* @author Oliver Gierke * @author Oliver Gierke
* @author Divya Srivastava
* @since 1.3 * @since 1.3
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/match/">MongoDB Aggregation Framework: * @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/match/">MongoDB Aggregation Framework:
* $match</a> * $match</a>
@@ -39,14 +40,6 @@ public class MatchOperation implements AggregationOperation {
private final CriteriaDefinition criteriaDefinition; private final CriteriaDefinition criteriaDefinition;
private final AggregationExpression expression; private final AggregationExpression expression;
/**
* Creates a new {@link MatchOperation}
*/
public MatchOperation() {
this.criteriaDefinition = null;
this.expression = null;
}
/** /**
* Creates a new {@link MatchOperation} for the given {@link CriteriaDefinition}. * Creates a new {@link MatchOperation} for the given {@link CriteriaDefinition}.
* *
@@ -55,29 +48,23 @@ public class MatchOperation implements AggregationOperation {
public MatchOperation(CriteriaDefinition criteriaDefinition) { public MatchOperation(CriteriaDefinition criteriaDefinition) {
Assert.notNull(criteriaDefinition, "Criteria must not be null!"); Assert.notNull(criteriaDefinition, "Criteria must not be null!");
this.criteriaDefinition = criteriaDefinition; this.criteriaDefinition = criteriaDefinition;
this.expression = null; this.expression = null;
} }
/**
* Creates a new {@link MatchOperation} for the given {@link Expression}.
*
* @param criteriaDefinition must not be {@literal null}.
*/
private MatchOperation(Expr expression) {
Assert.notNull(expression, "Expression must not be null!");
this.criteriaDefinition = null;
this.expression = expression;
}
/** /**
* Creates a new {@link MatchOperation} for the given {@link AggregationExpression}. * Creates a new {@link MatchOperation} for the given {@link AggregationExpression}.
* *
* @param expression must not be {@literal null}. * @param expression must not be {@literal null}.
* @since 3.3
*/ */
public MatchOperation withValueOf(AggregationExpression expression) { public MatchOperation(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null!"); Assert.notNull(expression, "Expression must not be null!");
return new MatchOperation(EvaluationOperators.valueOf(expression).expr());
this.criteriaDefinition = null;
this.expression = expression;
} }
/* /*
@@ -86,10 +73,9 @@ public class MatchOperation implements AggregationOperation {
*/ */
@Override @Override
public Document toDocument(AggregationOperationContext context) { public Document toDocument(AggregationOperationContext context) {
if(expression != null) {
return new Document(getOperator(), expression.toDocument()); return new Document(getOperator(),
} context.getMappedObject(expression != null ? expression.toDocument() : criteriaDefinition.getCriteriaObject()));
return new Document(getOperator(), context.getMappedObject(criteriaDefinition.getCriteriaObject()));
} }
/* /*

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2021 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.springframework.data.mongodb.test.util.Assertions.*;
import org.junit.jupiter.api.Test;
/**
* Unit tests for {@link EvaluationOperators}.
*
* @author Mark Paluch
*/
class EvaluationOperatorsUnitTests {
@Test // GH-3790
void shouldRenderExprCorrectly() {
assertThat(EvaluationOperators.valueOf("foo").expr().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $expr: \"$foo\" }");
}
}

View File

@@ -1,26 +1,23 @@
package org.springframework.data.mongodb.core.aggregation; package org.springframework.data.mongodb.core.aggregation;
import static org.assertj.core.api.Assertions.*;
import org.bson.Document; import static org.springframework.data.mongodb.test.util.Assertions.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/**
* Unit tests for {@link MatchOperation}.
*
* @author Divya Srivastava
*/
class MatchOperationUnitTests { class MatchOperationUnitTests {
@Test // DATAMONGO - 3729 @Test // GH-3790
public void shouldRenderStdDevPopCorrectly() { void matchShouldRenderCorrectly() {
MatchOperation operation = Aggregation.match().withValueOf(ArithmeticOperators.valueOf("quiz").stdDevPop());
MatchOperation operation = Aggregation.match(ArithmeticOperators.valueOf("quiz").stdDevPop());
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT)). assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT)).
isEqualTo(Document.parse("{ $match: { \"$expr\" : { \"$stdDevPop\" : \"$quiz\" } } } ")); isEqualTo("{ $match: { \"$stdDevPop\" : \"$quiz\" } } ");
}
@Test // DATAMONGO - 3729
public void shouldRenderStdDevSampCorrectly() {
MatchOperation operation = Aggregation.match().withValueOf(ArithmeticOperators.valueOf("quiz").stdDevSamp());
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT)).
isEqualTo(Document.parse("{ $match: { \"$expr\" : { \"$stdDevSamp\" : \"$quiz\" } } } "));
} }
} }

View File

@@ -27,20 +27,20 @@ import org.springframework.data.mongodb.core.aggregation.ReplaceRootOperation.Re
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
public class ReplaceRootOperationUnitTests { class ReplaceRootOperationUnitTests {
@Test // DATAMONGO-1550 @Test // DATAMONGO-1550
public void rejectsNullField() { void rejectsNullField() {
assertThatIllegalArgumentException().isThrownBy(() -> new ReplaceRootOperation((Field) null)); assertThatIllegalArgumentException().isThrownBy(() -> new ReplaceRootOperation((Field) null));
} }
@Test // DATAMONGO-1550 @Test // DATAMONGO-1550
public void rejectsNullExpression() { void rejectsNullExpression() {
assertThatIllegalArgumentException().isThrownBy(() -> new ReplaceRootOperation((AggregationExpression) null)); assertThatIllegalArgumentException().isThrownBy(() -> new ReplaceRootOperation((AggregationExpression) null));
} }
@Test // DATAMONGO-1550 @Test // DATAMONGO-1550
public void shouldRenderCorrectly() { void shouldRenderCorrectly() {
ReplaceRootOperation operation = ReplaceRootDocumentOperation.builder() ReplaceRootOperation operation = ReplaceRootDocumentOperation.builder()
.withDocument(new Document("hello", "world")); .withDocument(new Document("hello", "world"));
@@ -50,7 +50,7 @@ public class ReplaceRootOperationUnitTests {
} }
@Test // DATAMONGO-1550 @Test // DATAMONGO-1550
public void shouldRenderExpressionCorrectly() { void shouldRenderExpressionCorrectly() {
ReplaceRootOperation operation = new ReplaceRootOperation(VariableOperators // ReplaceRootOperation operation = new ReplaceRootOperation(VariableOperators //
.mapItemsOf("array") // .mapItemsOf("array") //
@@ -64,7 +64,7 @@ public class ReplaceRootOperationUnitTests {
} }
@Test // DATAMONGO-1550 @Test // DATAMONGO-1550
public void shouldComposeDocument() { void shouldComposeDocument() {
ReplaceRootOperation operation = ReplaceRootDocumentOperation.builder().withDocument() // ReplaceRootOperation operation = ReplaceRootDocumentOperation.builder().withDocument() //
.andValue("value").as("key") // .andValue("value").as("key") //
@@ -77,7 +77,7 @@ public class ReplaceRootOperationUnitTests {
} }
@Test // DATAMONGO-1550 @Test // DATAMONGO-1550
public void shouldComposeSubDocument() { void shouldComposeSubDocument() {
Document partialReplacement = new Document("key", "override").append("key2", "value2"); Document partialReplacement = new Document("key", "override").append("key2", "value2");
@@ -92,7 +92,7 @@ public class ReplaceRootOperationUnitTests {
} }
@Test // DATAMONGO-1550 @Test // DATAMONGO-1550
public void shouldNotExposeFields() { void shouldNotExposeFields() {
ReplaceRootOperation operation = new ReplaceRootOperation(Fields.field("field")); ReplaceRootOperation operation = new ReplaceRootOperation(Fields.field("field"));

View File

@@ -25,15 +25,15 @@ import org.junit.jupiter.api.Test;
* *
* @author Christoph Strobl * @author Christoph Strobl
*/ */
public class ReplaceWithOperationUnitTests { class ReplaceWithOperationUnitTests {
@Test // DATAMONGO-2331 @Test // DATAMONGO-2331
public void rejectsNullField() { void rejectsNullField() {
assertThatIllegalArgumentException().isThrownBy(() -> new ReplaceWithOperation(null)); assertThatIllegalArgumentException().isThrownBy(() -> new ReplaceWithOperation(null));
} }
@Test // DATAMONGO-2331 @Test // DATAMONGO-2331
public void shouldRenderValueCorrectly() { void shouldRenderValueCorrectly() {
ReplaceWithOperation operation = ReplaceWithOperation.replaceWithValue(new Document("hello", "world")); ReplaceWithOperation operation = ReplaceWithOperation.replaceWithValue(new Document("hello", "world"));
Document dbObject = operation.toDocument(Aggregation.DEFAULT_CONTEXT); Document dbObject = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
@@ -42,7 +42,7 @@ public class ReplaceWithOperationUnitTests {
} }
@Test // DATAMONGO-2331 @Test // DATAMONGO-2331
public void shouldRenderExpressionCorrectly() { void shouldRenderExpressionCorrectly() {
ReplaceWithOperation operation = ReplaceWithOperation.replaceWithValueOf(VariableOperators // ReplaceWithOperation operation = ReplaceWithOperation.replaceWithValueOf(VariableOperators //
.mapItemsOf("array") // .mapItemsOf("array") //

View File

@@ -43,6 +43,9 @@ import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.geo.Point; import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.DocumentTestUtils; import org.springframework.data.mongodb.core.DocumentTestUtils;
import org.springframework.data.mongodb.core.Person; import org.springframework.data.mongodb.core.Person;
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators;
import org.springframework.data.mongodb.core.aggregation.EvaluationOperators;
import org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint; import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon; import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core.mapping.DBRef;
@@ -1330,6 +1333,21 @@ public class QueryMapperUnitTests {
assertThat(mapper.getMappedSort(query.getQueryObject(), context.getPersistentEntity(Customer.class))).isEqualTo(new org.bson.Document("address.street", "1007 Mountain Drive")); assertThat(mapper.getMappedSort(query.getQueryObject(), context.getPersistentEntity(Customer.class))).isEqualTo(new org.bson.Document("address.street", "1007 Mountain Drive"));
} }
@Test // GH-3790
void shouldAcceptExprAsCriteriaDefinition() {
EvaluationOperators.EvaluationOperatorFactory.Expr expr = EvaluationOperators
.valueOf(ConditionalOperators.ifNull("customizedField").then(true)).expr();
Query query = query(
expr.toCriteriaDefinition(new TypeBasedAggregationOperationContext(EmbeddedClass.class, context, mapper)));
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(),
context.getRequiredPersistentEntity(EmbeddedClass.class));
assertThat(mappedQuery).isEqualTo("{ $expr : { $ifNull : [\"$fancy_custom_name\", true] } }");
}
@Test // GH-3668 @Test // GH-3668
void mapStringIdFieldProjection() { void mapStringIdFieldProjection() {