Skip to content

Commit 0e32b73

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-2312 - Polishing.
Fix spelling and add missing operator to Sum aggregation operation. Original pull request: #770.
1 parent c2436bc commit 0e32b73

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AccumulatorOperators.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ public Sum and(AggregationExpression expression) {
213213
return new Sum(append(expression));
214214
}
215215

216+
/**
217+
* Creates new {@link Sum} with all previously added arguments appending the given one. <br />
218+
* <strong>NOTE:</strong> Only possible in {@code $project} stage.
219+
*
220+
* @param value the value to add.
221+
* @return new instance of {@link Sum}.
222+
* @since 2.2
223+
*/
224+
public Sum and(Number value) {
225+
226+
Assert.notNull(value, "Value must not be null!");
227+
return new Sum(append(value));
228+
}
229+
216230
/* (non-Javadoc)
217231
* @see org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.AbstractAggregationExpression#toDocument(java.lang.Object, org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
218232
*/

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void arithmeticProjectionOperationWithoutAlias() {
127127
ProjectionOperationBuilder operation = new ProjectionOperation().and(fieldName).plus(1);
128128
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
129129
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);
130-
Document oper = exctractOperation(fieldName, projectClause);
130+
Document oper = extractOperation(fieldName, projectClause);
131131

132132
assertThat(oper.containsKey(ADD)).isTrue();
133133
assertThat(oper.get(ADD)).isEqualTo(Arrays.<Object> asList("$a", 1));
@@ -142,7 +142,7 @@ public void arithmeticProjectionOperationPlus() {
142142
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
143143
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);
144144

145-
Document oper = exctractOperation(fieldAlias, projectClause);
145+
Document oper = extractOperation(fieldAlias, projectClause);
146146
assertThat(oper.containsKey(ADD)).isTrue();
147147
assertThat(oper.get(ADD)).isEqualTo(Arrays.<Object> asList("$a", 1));
148148
}
@@ -155,7 +155,7 @@ public void arithmeticProjectionOperationMinus() {
155155
ProjectionOperation operation = new ProjectionOperation().and(fieldName).minus(1).as(fieldAlias);
156156
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
157157
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);
158-
Document oper = exctractOperation(fieldAlias, projectClause);
158+
Document oper = extractOperation(fieldAlias, projectClause);
159159

160160
assertThat(oper.containsKey(SUBTRACT)).isTrue();
161161
assertThat(oper.get(SUBTRACT)).isEqualTo(Arrays.<Object> asList("$a", 1));
@@ -169,7 +169,7 @@ public void arithmeticProjectionOperationMultiply() {
169169
ProjectionOperation operation = new ProjectionOperation().and(fieldName).multiply(1).as(fieldAlias);
170170
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
171171
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);
172-
Document oper = exctractOperation(fieldAlias, projectClause);
172+
Document oper = extractOperation(fieldAlias, projectClause);
173173

174174
assertThat(oper.containsKey(MULTIPLY)).isTrue();
175175
assertThat(oper.get(MULTIPLY)).isEqualTo(Arrays.<Object> asList("$a", 1));
@@ -183,7 +183,7 @@ public void arithmeticProjectionOperationDivide() {
183183
ProjectionOperation operation = new ProjectionOperation().and(fieldName).divide(1).as(fieldAlias);
184184
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
185185
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);
186-
Document oper = exctractOperation(fieldAlias, projectClause);
186+
Document oper = extractOperation(fieldAlias, projectClause);
187187

188188
assertThat(oper.containsKey(DIVIDE)).isTrue();
189189
assertThat(oper.get(DIVIDE)).isEqualTo(Arrays.<Object> asList("$a", 1));
@@ -202,7 +202,7 @@ public void arithmeticProjectionOperationMod() {
202202
ProjectionOperation operation = new ProjectionOperation().and(fieldName).mod(3).as(fieldAlias);
203203
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
204204
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);
205-
Document oper = exctractOperation(fieldAlias, projectClause);
205+
Document oper = extractOperation(fieldAlias, projectClause);
206206

207207
assertThat(oper.containsKey(MOD)).isTrue();
208208
assertThat(oper.get(MOD)).isEqualTo(Arrays.<Object> asList("$a", 3));
@@ -328,7 +328,7 @@ public void shouldRenderDateTimeFragmentExtractionsForSimpleFieldProjectionsCorr
328328
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
329329
assertThat(document).isNotNull();
330330

331-
Document projected = exctractOperation("$project", document);
331+
Document projected = extractOperation("$project", document);
332332

333333
assertThat(projected.get("hour")).isEqualTo(new Document("$hour", Arrays.asList("$date")));
334334
assertThat(projected.get("min")).isEqualTo(new Document("$minute", Arrays.asList("$date")));
@@ -354,7 +354,7 @@ public void shouldRenderDateTimeFragmentExtractionsForExpressionProjectionsCorre
354354
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
355355
assertThat(document).isNotNull();
356356

357-
Document projected = exctractOperation("$project", document);
357+
Document projected = extractOperation("$project", document);
358358
assertThat(projected.get("dayOfYearPlus1Day")).isEqualTo(
359359
new Document("$dayOfYear", Arrays.asList(new Document("$add", Arrays.<Object> asList("$date", 86400000)))));
360360
}
@@ -370,7 +370,7 @@ public void shouldRenderSizeExpressionInProjection() {
370370

371371
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
372372

373-
Document projected = exctractOperation("$project", document);
373+
Document projected = extractOperation("$project", document);
374374
assertThat(projected.get("tags_count")).isEqualTo(new Document("$size", Arrays.asList("$tags")));
375375
}
376376

@@ -384,7 +384,7 @@ public void shouldRenderGenericSizeExpressionInProjection() {
384384

385385
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
386386

387-
Document projected = exctractOperation("$project", document);
387+
Document projected = extractOperation("$project", document);
388388
assertThat(projected.get("tags_count")).isEqualTo(new Document("$size", Arrays.asList("$tags")));
389389
}
390390

@@ -394,7 +394,7 @@ public void shouldRenderSliceCorrectly() throws Exception {
394394
ProjectionOperation operation = Aggregation.project().and("field").slice(10).as("renamed");
395395

396396
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
397-
Document projected = exctractOperation("$project", document);
397+
Document projected = extractOperation("$project", document);
398398

399399
assertThat(projected.get("renamed")).isEqualTo(new Document("$slice", Arrays.<Object> asList("$field", 10)));
400400
}
@@ -405,7 +405,7 @@ public void shouldRenderSliceWithPositionCorrectly() throws Exception {
405405
ProjectionOperation operation = Aggregation.project().and("field").slice(10, 5).as("renamed");
406406

407407
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
408-
Document projected = exctractOperation("$project", document);
408+
Document projected = extractOperation("$project", document);
409409

410410
assertThat(projected.get("renamed")).isEqualTo(new Document("$slice", Arrays.<Object> asList("$field", 5, 10)));
411411
}
@@ -2219,7 +2219,7 @@ public void nestedMappedFieldReferenceInArrayField() {
22192219
"{ $project: { \"author\" : 1, \"myArray\" : [ \"$ti_t_le\", \"plain - string\", { \"$sum\" : [\"$ti_t_le\", 10] } ] } } ] }"));
22202220
}
22212221

2222-
private static Document exctractOperation(String field, Document fromProjectClause) {
2222+
private static Document extractOperation(String field, Document fromProjectClause) {
22232223
return (Document) fromProjectClause.get(field);
22242224
}
22252225

0 commit comments

Comments
 (0)