1
1
/*
2
- * Copyright 2013-2016 the original author or authors.
2
+ * Copyright 2013-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import static org .hamcrest .core .Is .is ;
20
20
import static org .junit .Assert .*;
21
21
import static org .springframework .data .mongodb .core .aggregation .Aggregation .*;
22
- import static org .springframework .data .mongodb .core .aggregation .VariableOperators .Let .ExpressionVariable .*;
23
22
import static org .springframework .data .mongodb .core .aggregation .AggregationFunctionExpressions .*;
24
23
import static org .springframework .data .mongodb .core .aggregation .Fields .*;
24
+ import static org .springframework .data .mongodb .core .aggregation .VariableOperators .Let .ExpressionVariable .*;
25
25
import static org .springframework .data .mongodb .test .util .IsBsonObject .*;
26
26
import static org .springframework .data .mongodb .util .DBObjectUtils .*;
27
27
32
32
import org .junit .Test ;
33
33
import org .springframework .data .domain .Range ;
34
34
import org .springframework .data .mongodb .core .DBObjectTestUtils ;
35
- import org .springframework .data .mongodb .core .aggregation .VariableOperators .Let .ExpressionVariable ;
36
35
import org .springframework .data .mongodb .core .aggregation .ArrayOperators .Reduce .PropertyExpression ;
37
36
import org .springframework .data .mongodb .core .aggregation .ArrayOperators .Reduce .Variable ;
38
37
import org .springframework .data .mongodb .core .aggregation .ConditionalOperators .Switch .CaseOperator ;
39
38
import org .springframework .data .mongodb .core .aggregation .ProjectionOperation .ProjectionOperationBuilder ;
39
+ import org .springframework .data .mongodb .core .aggregation .VariableOperators .Let .ExpressionVariable ;
40
40
41
41
import com .mongodb .BasicDBObject ;
42
42
import com .mongodb .BasicDBObjectBuilder ;
@@ -60,11 +60,17 @@ public class ProjectionOperationUnitTests {
60
60
static final String DIVIDE = "$divide" ;
61
61
static final String PROJECT = "$project" ;
62
62
63
+ /**
64
+ * @see DATAMONGO-586
65
+ */
63
66
@ Test (expected = IllegalArgumentException .class )
64
67
public void rejectsNullFields () {
65
68
new ProjectionOperation (null );
66
69
}
67
70
71
+ /**
72
+ * @see DATAMONGO-586
73
+ */
68
74
@ Test
69
75
public void declaresBackReferenceCorrectly () {
70
76
@@ -76,6 +82,9 @@ public void declaresBackReferenceCorrectly() {
76
82
assertThat (projectClause .get ("prop" ), is ((Object ) Fields .UNDERSCORE_ID_REF ));
77
83
}
78
84
85
+ /**
86
+ * @see DATAMONGO-586
87
+ */
79
88
@ Test
80
89
public void alwaysUsesExplicitReference () {
81
90
@@ -88,6 +97,9 @@ public void alwaysUsesExplicitReference() {
88
97
assertThat (projectClause .get ("bar" ), is ((Object ) "$foobar" ));
89
98
}
90
99
100
+ /**
101
+ * @see DATAMONGO-586
102
+ */
91
103
@ Test
92
104
public void aliasesSimpleFieldProjection () {
93
105
@@ -99,6 +111,9 @@ public void aliasesSimpleFieldProjection() {
99
111
assertThat (projectClause .get ("bar" ), is ((Object ) "$foo" ));
100
112
}
101
113
114
+ /**
115
+ * @see DATAMONGO-586
116
+ */
102
117
@ Test
103
118
public void aliasesArithmeticProjection () {
104
119
@@ -114,6 +129,9 @@ public void aliasesArithmeticProjection() {
114
129
assertThat (addClause .get (1 ), is ((Object ) 41 ));
115
130
}
116
131
132
+ /**
133
+ * @see DATAMONGO-586
134
+ */
117
135
@ Test
118
136
public void arithmenticProjectionOperationWithoutAlias () {
119
137
@@ -127,6 +145,9 @@ public void arithmenticProjectionOperationWithoutAlias() {
127
145
assertThat (oper .get (ADD ), is ((Object ) Arrays .<Object > asList ("$a" , 1 )));
128
146
}
129
147
148
+ /**
149
+ * @see DATAMONGO-586
150
+ */
130
151
@ Test
131
152
public void arithmenticProjectionOperationPlus () {
132
153
@@ -141,6 +162,9 @@ public void arithmenticProjectionOperationPlus() {
141
162
assertThat (oper .get (ADD ), is ((Object ) Arrays .<Object > asList ("$a" , 1 )));
142
163
}
143
164
165
+ /**
166
+ * @see DATAMONGO-586
167
+ */
144
168
@ Test
145
169
public void arithmenticProjectionOperationMinus () {
146
170
@@ -155,6 +179,9 @@ public void arithmenticProjectionOperationMinus() {
155
179
assertThat (oper .get (SUBTRACT ), is ((Object ) Arrays .<Object > asList ("$a" , 1 )));
156
180
}
157
181
182
+ /**
183
+ * @see DATAMONGO-586
184
+ */
158
185
@ Test
159
186
public void arithmenticProjectionOperationMultiply () {
160
187
@@ -169,6 +196,9 @@ public void arithmenticProjectionOperationMultiply() {
169
196
assertThat (oper .get (MULTIPLY ), is ((Object ) Arrays .<Object > asList ("$a" , 1 )));
170
197
}
171
198
199
+ /**
200
+ * @see DATAMONGO-586
201
+ */
172
202
@ Test
173
203
public void arithmenticProjectionOperationDivide () {
174
204
@@ -183,12 +213,18 @@ public void arithmenticProjectionOperationDivide() {
183
213
assertThat (oper .get (DIVIDE ), is ((Object ) Arrays .<Object > asList ("$a" , 1 )));
184
214
}
185
215
216
+ /**
217
+ * @see DATAMONGO-586
218
+ */
186
219
@ Test (expected = IllegalArgumentException .class )
187
220
public void arithmenticProjectionOperationDivideByZeroException () {
188
221
189
222
new ProjectionOperation ().and ("a" ).divide (0 );
190
223
}
191
224
225
+ /**
226
+ * @see DATAMONGO-586
227
+ */
192
228
@ Test
193
229
public void arithmenticProjectionOperationMod () {
194
230
@@ -1757,7 +1793,8 @@ public void shouldRenderLetExpressionCorrectly() {
1757
1793
.define (
1758
1794
newVariable ("total" )
1759
1795
.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 )))
1761
1798
.andApply (AggregationFunctionExpressions .MULTIPLY .of (Fields .field ("total" ), Fields .field ("discounted" )))) //
1762
1799
.as ("finalTotal" ).toDBObject (Aggregation .DEFAULT_CONTEXT );
1763
1800
@@ -1807,7 +1844,8 @@ public void shouldRenderIndexOfBytesCorrectly() {
1807
1844
DBObject agg = project ().and (StringOperators .valueOf ("item" ).indexOf ("foo" )).as ("byteLocation" )
1808
1845
.toDBObject (Aggregation .DEFAULT_CONTEXT );
1809
1846
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\" ] } } }" )));
1811
1849
}
1812
1850
1813
1851
/**
@@ -1914,8 +1952,8 @@ public void shouldRenderIndexOfArrayCorrectly() {
1914
1952
@ Test
1915
1953
public void shouldRenderRangeCorrectly () {
1916
1954
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 );
1919
1957
1920
1958
assertThat (agg , isBsonObject ().containing ("$project.rest_stops.$range.[0]" , 0L )
1921
1959
.containing ("$project.rest_stops.$range.[1]" , "$distance" ).containing ("$project.rest_stops.$range.[2]" , 25L ));
@@ -1995,7 +2033,8 @@ public void shouldRenderInCorrectly() {
1995
2033
DBObject agg = project ().and (ArrayOperators .arrayOf ("in_stock" ).containsValue ("bananas" )).as ("has_bananas" )
1996
2034
.toDBObject (Aggregation .DEFAULT_CONTEXT );
1997
2035
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\" ] } } }" )));
1999
2038
}
2000
2039
2001
2040
/**
@@ -2061,11 +2100,16 @@ public void shouldRenderSwitchCorrectly() {
2061
2100
" }\n " + //
2062
2101
"}" ;
2063
2102
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 ))
2065
2105
.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 ))
2069
2113
.then ("Needs improvement." );
2070
2114
2071
2115
DBObject agg = project ().and (ConditionalOperators .switchCases (cond1 , cond2 , cond3 ).defaultTo ("No scores found." ))
@@ -2080,8 +2124,7 @@ public void shouldRenderSwitchCorrectly() {
2080
2124
@ Test
2081
2125
public void shouldTypeCorrectly () {
2082
2126
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 );
2085
2128
2086
2129
assertThat (agg , Matchers .is (JSON .parse ("{ $project : { a: { $type: \" $a\" } } }" )));
2087
2130
}
0 commit comments