DATAMONGO-1784 - Polishing.
Update JavaDoc, enforce nullability constraints and add tests. Original Pull Request: #501
This commit is contained in:
@@ -160,10 +160,14 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
|
||||
* Generates an {@link GroupOperationBuilder} for an {@code $sum}-expression for the given
|
||||
* {@link AggregationExpression}.
|
||||
*
|
||||
* @param expr
|
||||
* @return
|
||||
* @param expr must not be {@literal null}.
|
||||
* @return new instance of {@link GroupOperationBuilder}. Never {@literal null}.
|
||||
* @throws IllegalArgumentException when {@code expr} is {@literal null}.
|
||||
* @since 1.10.8
|
||||
*/
|
||||
public GroupOperationBuilder sum(AggregationExpression expr) {
|
||||
|
||||
Assert.notNull(expr, "Expr must not be null!");
|
||||
return newBuilder(GroupOps.SUM, null, expr);
|
||||
}
|
||||
|
||||
|
||||
@@ -835,11 +835,11 @@ public class AggregationTests {
|
||||
assertThat(result.getMappedResults(), hasSize(2));
|
||||
|
||||
Document meh = result.getMappedResults().get(0);
|
||||
assertThat((String) meh.get("_id"), is(equalTo("meh")));
|
||||
assertThat(meh.get("_id"), is(equalTo("meh")));
|
||||
assertThat(((Number) meh.get("score")).longValue(), is(equalTo(2L)));
|
||||
|
||||
Document good = result.getMappedResults().get(1);
|
||||
assertThat((String) good.get("_id"), is(equalTo("good")));
|
||||
assertThat(good.get("_id"), is(equalTo("good")));
|
||||
assertThat(((Number) good.get("score")).longValue(), is(equalTo(18000L)));
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Arrays;
|
||||
import org.bson.Document;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mongodb.core.DocumentTestUtils;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GroupOperation}.
|
||||
@@ -216,6 +217,29 @@ public class GroupOperationUnitTests {
|
||||
assertThat(push, is(new Document("$stdDevPop", "$field")));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1784
|
||||
public void shouldRenderSumWithExpressionInGroup() {
|
||||
|
||||
GroupOperation groupOperation = Aggregation //
|
||||
.group("username") //
|
||||
.sum(ConditionalOperators //
|
||||
.when(Criteria.where("foo").is("bar")) //
|
||||
.then(1) //
|
||||
.otherwise(-1)) //
|
||||
.as("foobar");
|
||||
|
||||
Document groupClause = extractDocumentFromGroupOperation(groupOperation);
|
||||
Document foobar = DocumentTestUtils.getAsDocument(groupClause, "foobar");
|
||||
|
||||
assertThat(foobar.get("$sum"), is(new Document("$cond",
|
||||
new Document("if", new Document("$eq", Arrays.asList("$foo", "bar"))).append("then", 1).append("else", -1))));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1784
|
||||
public void sumWithNullExpressionShouldThrowException() {
|
||||
Aggregation.group("username").sum((AggregationExpression) null);
|
||||
}
|
||||
|
||||
private Document extractDocumentFromGroupOperation(GroupOperation groupOperation) {
|
||||
Document document = groupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
|
||||
Document groupClause = DocumentTestUtils.getAsDocument(document, "$group");
|
||||
|
||||
Reference in New Issue
Block a user