Skip to content

Commit 3c6db34

Browse files
committed
DATAMONGO-1578 - Polishing.
Add ticket references to test methods. Extend license years in copyright header. Original pull request: #398.
1 parent 9f33199 commit 3c6db34

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

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

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,9 +19,9 @@
1919
import static org.hamcrest.core.Is.is;
2020
import static org.junit.Assert.*;
2121
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
22-
import static org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable.*;
2322
import static org.springframework.data.mongodb.core.aggregation.AggregationFunctionExpressions.*;
2423
import static org.springframework.data.mongodb.core.aggregation.Fields.*;
24+
import static org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable.*;
2525
import static org.springframework.data.mongodb.test.util.IsBsonObject.*;
2626
import static org.springframework.data.mongodb.util.DBObjectUtils.*;
2727

@@ -32,11 +32,11 @@
3232
import org.junit.Test;
3333
import org.springframework.data.domain.Range;
3434
import org.springframework.data.mongodb.core.DBObjectTestUtils;
35-
import org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable;
3635
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
3736
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.Variable;
3837
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Switch.CaseOperator;
3938
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation.ProjectionOperationBuilder;
39+
import org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable;
4040

4141
import com.mongodb.BasicDBObject;
4242
import com.mongodb.BasicDBObjectBuilder;
@@ -60,11 +60,17 @@ public class ProjectionOperationUnitTests {
6060
static final String DIVIDE = "$divide";
6161
static final String PROJECT = "$project";
6262

63+
/**
64+
* @see DATAMONGO-586
65+
*/
6366
@Test(expected = IllegalArgumentException.class)
6467
public void rejectsNullFields() {
6568
new ProjectionOperation(null);
6669
}
6770

71+
/**
72+
* @see DATAMONGO-586
73+
*/
6874
@Test
6975
public void declaresBackReferenceCorrectly() {
7076

@@ -76,6 +82,9 @@ public void declaresBackReferenceCorrectly() {
7682
assertThat(projectClause.get("prop"), is((Object) Fields.UNDERSCORE_ID_REF));
7783
}
7884

85+
/**
86+
* @see DATAMONGO-586
87+
*/
7988
@Test
8089
public void alwaysUsesExplicitReference() {
8190

@@ -88,6 +97,9 @@ public void alwaysUsesExplicitReference() {
8897
assertThat(projectClause.get("bar"), is((Object) "$foobar"));
8998
}
9099

100+
/**
101+
* @see DATAMONGO-586
102+
*/
91103
@Test
92104
public void aliasesSimpleFieldProjection() {
93105

@@ -99,6 +111,9 @@ public void aliasesSimpleFieldProjection() {
99111
assertThat(projectClause.get("bar"), is((Object) "$foo"));
100112
}
101113

114+
/**
115+
* @see DATAMONGO-586
116+
*/
102117
@Test
103118
public void aliasesArithmeticProjection() {
104119

@@ -114,6 +129,9 @@ public void aliasesArithmeticProjection() {
114129
assertThat(addClause.get(1), is((Object) 41));
115130
}
116131

132+
/**
133+
* @see DATAMONGO-586
134+
*/
117135
@Test
118136
public void arithmenticProjectionOperationWithoutAlias() {
119137

@@ -127,6 +145,9 @@ public void arithmenticProjectionOperationWithoutAlias() {
127145
assertThat(oper.get(ADD), is((Object) Arrays.<Object> asList("$a", 1)));
128146
}
129147

148+
/**
149+
* @see DATAMONGO-586
150+
*/
130151
@Test
131152
public void arithmenticProjectionOperationPlus() {
132153

@@ -141,6 +162,9 @@ public void arithmenticProjectionOperationPlus() {
141162
assertThat(oper.get(ADD), is((Object) Arrays.<Object> asList("$a", 1)));
142163
}
143164

165+
/**
166+
* @see DATAMONGO-586
167+
*/
144168
@Test
145169
public void arithmenticProjectionOperationMinus() {
146170

@@ -155,6 +179,9 @@ public void arithmenticProjectionOperationMinus() {
155179
assertThat(oper.get(SUBTRACT), is((Object) Arrays.<Object> asList("$a", 1)));
156180
}
157181

182+
/**
183+
* @see DATAMONGO-586
184+
*/
158185
@Test
159186
public void arithmenticProjectionOperationMultiply() {
160187

@@ -169,6 +196,9 @@ public void arithmenticProjectionOperationMultiply() {
169196
assertThat(oper.get(MULTIPLY), is((Object) Arrays.<Object> asList("$a", 1)));
170197
}
171198

199+
/**
200+
* @see DATAMONGO-586
201+
*/
172202
@Test
173203
public void arithmenticProjectionOperationDivide() {
174204

@@ -183,12 +213,18 @@ public void arithmenticProjectionOperationDivide() {
183213
assertThat(oper.get(DIVIDE), is((Object) Arrays.<Object> asList("$a", 1)));
184214
}
185215

216+
/**
217+
* @see DATAMONGO-586
218+
*/
186219
@Test(expected = IllegalArgumentException.class)
187220
public void arithmenticProjectionOperationDivideByZeroException() {
188221

189222
new ProjectionOperation().and("a").divide(0);
190223
}
191224

225+
/**
226+
* @see DATAMONGO-586
227+
*/
192228
@Test
193229
public void arithmenticProjectionOperationMod() {
194230

@@ -1757,7 +1793,8 @@ public void shouldRenderLetExpressionCorrectly() {
17571793
.define(
17581794
newVariable("total")
17591795
.forExpression(AggregationFunctionExpressions.ADD.of(Fields.field("price"), Fields.field("tax"))),
1760-
newVariable("discounted").forExpression(ConditionalOperators.Cond.when("applyDiscount").then(0.9D).otherwise(1.0D)))
1796+
newVariable("discounted")
1797+
.forExpression(ConditionalOperators.Cond.when("applyDiscount").then(0.9D).otherwise(1.0D)))
17611798
.andApply(AggregationFunctionExpressions.MULTIPLY.of(Fields.field("total"), Fields.field("discounted")))) //
17621799
.as("finalTotal").toDBObject(Aggregation.DEFAULT_CONTEXT);
17631800

@@ -1807,7 +1844,8 @@ public void shouldRenderIndexOfBytesCorrectly() {
18071844
DBObject agg = project().and(StringOperators.valueOf("item").indexOf("foo")).as("byteLocation")
18081845
.toDBObject(Aggregation.DEFAULT_CONTEXT);
18091846

1810-
assertThat(agg, Matchers.is(JSON.parse("{ $project: { byteLocation: { $indexOfBytes: [ \"$item\", \"foo\" ] } } }")));
1847+
assertThat(agg,
1848+
Matchers.is(JSON.parse("{ $project: { byteLocation: { $indexOfBytes: [ \"$item\", \"foo\" ] } } }")));
18111849
}
18121850

18131851
/**
@@ -1914,8 +1952,8 @@ public void shouldRenderIndexOfArrayCorrectly() {
19141952
@Test
19151953
public void shouldRenderRangeCorrectly() {
19161954

1917-
DBObject agg = project().and(ArrayOperators.RangeOperator.rangeStartingAt(0L).to("distance").withStepSize(25L)).as("rest_stops")
1918-
.toDBObject(Aggregation.DEFAULT_CONTEXT);
1955+
DBObject agg = project().and(ArrayOperators.RangeOperator.rangeStartingAt(0L).to("distance").withStepSize(25L))
1956+
.as("rest_stops").toDBObject(Aggregation.DEFAULT_CONTEXT);
19191957

19201958
assertThat(agg, isBsonObject().containing("$project.rest_stops.$range.[0]", 0L)
19211959
.containing("$project.rest_stops.$range.[1]", "$distance").containing("$project.rest_stops.$range.[2]", 25L));
@@ -1995,7 +2033,8 @@ public void shouldRenderInCorrectly() {
19952033
DBObject agg = project().and(ArrayOperators.arrayOf("in_stock").containsValue("bananas")).as("has_bananas")
19962034
.toDBObject(Aggregation.DEFAULT_CONTEXT);
19972035

1998-
assertThat(agg, Matchers.is(JSON.parse("{ $project : { has_bananas : { $in : [\"bananas\", \"$in_stock\" ] } } }")));
2036+
assertThat(agg,
2037+
Matchers.is(JSON.parse("{ $project : { has_bananas : { $in : [\"bananas\", \"$in_stock\" ] } } }")));
19992038
}
20002039

20012040
/**
@@ -2061,11 +2100,16 @@ public void shouldRenderSwitchCorrectly() {
20612100
" }\n" + //
20622101
"}";
20632102

2064-
CaseOperator cond1 = CaseOperator.when(ComparisonOperators.Gte.valueOf(AccumulatorOperators.Avg.avgOf("scores")).greaterThanEqualToValue(90))
2103+
CaseOperator cond1 = CaseOperator
2104+
.when(ComparisonOperators.Gte.valueOf(AccumulatorOperators.Avg.avgOf("scores")).greaterThanEqualToValue(90))
20652105
.then("Doing great!");
2066-
CaseOperator cond2 = CaseOperator.when(BooleanOperators.And.and(ComparisonOperators.Gte.valueOf(AccumulatorOperators.Avg.avgOf("scores")).greaterThanEqualToValue(80),
2067-
ComparisonOperators.Lt.valueOf(AccumulatorOperators.Avg.avgOf("scores")).lessThanValue(90))).then("Doing pretty well.");
2068-
CaseOperator cond3 = CaseOperator.when(ComparisonOperators.Lt.valueOf(AccumulatorOperators.Avg.avgOf("scores")).lessThanValue(80))
2106+
CaseOperator cond2 = CaseOperator
2107+
.when(BooleanOperators.And.and(
2108+
ComparisonOperators.Gte.valueOf(AccumulatorOperators.Avg.avgOf("scores")).greaterThanEqualToValue(80),
2109+
ComparisonOperators.Lt.valueOf(AccumulatorOperators.Avg.avgOf("scores")).lessThanValue(90)))
2110+
.then("Doing pretty well.");
2111+
CaseOperator cond3 = CaseOperator
2112+
.when(ComparisonOperators.Lt.valueOf(AccumulatorOperators.Avg.avgOf("scores")).lessThanValue(80))
20692113
.then("Needs improvement.");
20702114

20712115
DBObject agg = project().and(ConditionalOperators.switchCases(cond1, cond2, cond3).defaultTo("No scores found."))
@@ -2080,8 +2124,7 @@ public void shouldRenderSwitchCorrectly() {
20802124
@Test
20812125
public void shouldTypeCorrectly() {
20822126

2083-
DBObject agg = project().and(DataTypeOperators.Type.typeOf("a")).as("a")
2084-
.toDBObject(Aggregation.DEFAULT_CONTEXT);
2127+
DBObject agg = project().and(DataTypeOperators.Type.typeOf("a")).as("a").toDBObject(Aggregation.DEFAULT_CONTEXT);
20852128

20862129
assertThat(agg, Matchers.is(JSON.parse("{ $project : { a: { $type: \"$a\" } } }")));
20872130
}

0 commit comments

Comments
 (0)