1
1
/*
2
- * Copyright 2013-2020 the original author or authors.
2
+ * Copyright 2013-2021 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.
20
20
import static org .springframework .data .mongodb .core .aggregation .Fields .*;
21
21
22
22
import java .util .Arrays ;
23
+ import java .util .Collections ;
23
24
24
25
import org .bson .Document ;
25
26
import org .junit .jupiter .api .Test ;
33
34
* @author Thomas Darimont
34
35
* @author Gustavo de Geus
35
36
*/
36
- public class GroupOperationUnitTests {
37
+ class GroupOperationUnitTests {
37
38
38
39
@ Test
39
- public void rejectsNullFields () {
40
+ void rejectsNullFields () {
40
41
assertThatIllegalArgumentException ().isThrownBy (() -> new GroupOperation ((Fields ) null ));
41
42
}
42
43
43
44
@ Test // DATAMONGO-759
44
- public void groupOperationWithNoGroupIdFieldsShouldGenerateNullAsGroupId () {
45
+ void groupOperationWithNoGroupIdFieldsShouldGenerateNullAsGroupId () {
45
46
46
47
GroupOperation operation = new GroupOperation (Fields .from ());
47
48
ExposedFields fields = operation .getFields ();
@@ -53,7 +54,7 @@ public void groupOperationWithNoGroupIdFieldsShouldGenerateNullAsGroupId() {
53
54
}
54
55
55
56
@ Test // DATAMONGO-759
56
- public void groupOperationWithNoGroupIdFieldsButAdditionalFieldsShouldGenerateNullAsGroupId () {
57
+ void groupOperationWithNoGroupIdFieldsButAdditionalFieldsShouldGenerateNullAsGroupId () {
57
58
58
59
GroupOperation operation = new GroupOperation (Fields .from ()).count ().as ("cnt" ).last ("foo" ).as ("foo" );
59
60
ExposedFields fields = operation .getFields ();
@@ -67,121 +68,121 @@ public void groupOperationWithNoGroupIdFieldsButAdditionalFieldsShouldGenerateNu
67
68
}
68
69
69
70
@ Test
70
- public void createsGroupOperationWithSingleField () {
71
+ void createsGroupOperationWithSingleField () {
71
72
72
73
GroupOperation operation = new GroupOperation (fields ("a" ));
73
74
74
75
Document groupClause = extractDocumentFromGroupOperation (operation );
75
76
76
- assertThat (groupClause . get ( UNDERSCORE_ID )). isEqualTo (( Object ) "$a" );
77
+ assertThat (groupClause ). containsEntry ( UNDERSCORE_ID , "$a" );
77
78
}
78
79
79
80
@ Test
80
- public void createsGroupOperationWithMultipleFields () {
81
+ void createsGroupOperationWithMultipleFields () {
81
82
82
83
GroupOperation operation = new GroupOperation (fields ("a" ).and ("b" , "c" ));
83
84
84
85
Document groupClause = extractDocumentFromGroupOperation (operation );
85
86
Document idClause = DocumentTestUtils .getAsDocument (groupClause , UNDERSCORE_ID );
86
87
87
- assertThat (idClause . get ("a" )). isEqualTo (( Object ) "$a" );
88
- assertThat ( idClause . get ("b" )). isEqualTo (( Object ) "$c" );
88
+ assertThat (idClause ). containsEntry ("a" , "$a" )
89
+ . containsEntry ("b" , "$c" );
89
90
}
90
91
91
92
@ Test
92
- public void groupFactoryMethodWithMultipleFieldsAndSumOperation () {
93
+ void groupFactoryMethodWithMultipleFieldsAndSumOperation () {
93
94
94
95
GroupOperation groupOperation = Aggregation .group (fields ("a" , "b" ).and ("c" )) //
95
96
.sum ("e" ).as ("e" );
96
97
97
98
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
98
99
Document eOp = DocumentTestUtils .getAsDocument (groupClause , "e" );
99
- assertThat (eOp ).isEqualTo (( Document ) new Document ("$sum" , "$e" ));
100
+ assertThat (eOp ).isEqualTo (new Document ("$sum" , "$e" ));
100
101
}
101
102
102
103
@ Test
103
- public void groupFactoryMethodWithMultipleFieldsAndSumOperationWithAlias () {
104
+ void groupFactoryMethodWithMultipleFieldsAndSumOperationWithAlias () {
104
105
105
106
GroupOperation groupOperation = Aggregation .group (fields ("a" , "b" ).and ("c" )) //
106
107
.sum ("e" ).as ("ee" );
107
108
108
109
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
109
110
Document eOp = DocumentTestUtils .getAsDocument (groupClause , "ee" );
110
- assertThat (eOp ).isEqualTo (( Document ) new Document ("$sum" , "$e" ));
111
+ assertThat (eOp ).isEqualTo (new Document ("$sum" , "$e" ));
111
112
}
112
113
113
114
@ Test
114
- public void groupFactoryMethodWithMultipleFieldsAndCountOperationWithout () {
115
+ void groupFactoryMethodWithMultipleFieldsAndCountOperationWithout () {
115
116
116
117
GroupOperation groupOperation = Aggregation .group (fields ("a" , "b" ).and ("c" )) //
117
118
.count ().as ("count" );
118
119
119
120
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
120
121
Document eOp = DocumentTestUtils .getAsDocument (groupClause , "count" );
121
- assertThat (eOp ).isEqualTo (( Document ) new Document ("$sum" , 1 ));
122
+ assertThat (eOp ).isEqualTo (new Document ("$sum" , 1 ));
122
123
}
123
124
124
125
@ Test
125
- public void groupFactoryMethodWithMultipleFieldsAndMultipleAggregateOperationsWithAlias () {
126
+ void groupFactoryMethodWithMultipleFieldsAndMultipleAggregateOperationsWithAlias () {
126
127
127
128
GroupOperation groupOperation = Aggregation .group (fields ("a" , "b" ).and ("c" )) //
128
129
.sum ("e" ).as ("sum" ) //
129
130
.min ("e" ).as ("min" ); //
130
131
131
132
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
132
133
Document sum = DocumentTestUtils .getAsDocument (groupClause , "sum" );
133
- assertThat (sum ).isEqualTo (( Document ) new Document ("$sum" , "$e" ));
134
+ assertThat (sum ).isEqualTo (new Document ("$sum" , "$e" ));
134
135
135
136
Document min = DocumentTestUtils .getAsDocument (groupClause , "min" );
136
- assertThat (min ).isEqualTo (( Document ) new Document ("$min" , "$e" ));
137
+ assertThat (min ).isEqualTo (new Document ("$min" , "$e" ));
137
138
}
138
139
139
140
@ Test
140
- public void groupOperationPushWithValue () {
141
+ void groupOperationPushWithValue () {
141
142
142
143
GroupOperation groupOperation = Aggregation .group ("a" , "b" ).push (1 ).as ("x" );
143
144
144
145
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
145
146
Document push = DocumentTestUtils .getAsDocument (groupClause , "x" );
146
147
147
- assertThat (push ).isEqualTo (( Document ) new Document ("$push" , 1 ));
148
+ assertThat (push ).isEqualTo (new Document ("$push" , 1 ));
148
149
}
149
150
150
151
@ Test
151
- public void groupOperationPushWithReference () {
152
+ void groupOperationPushWithReference () {
152
153
153
154
GroupOperation groupOperation = Aggregation .group ("a" , "b" ).push ("ref" ).as ("x" );
154
155
155
156
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
156
157
Document push = DocumentTestUtils .getAsDocument (groupClause , "x" );
157
158
158
- assertThat (push ).isEqualTo (( Document ) new Document ("$push" , "$ref" ));
159
+ assertThat (push ).isEqualTo (new Document ("$push" , "$ref" ));
159
160
}
160
161
161
162
@ Test
162
- public void groupOperationAddToSetWithReference () {
163
+ void groupOperationAddToSetWithReference () {
163
164
164
165
GroupOperation groupOperation = Aggregation .group ("a" , "b" ).addToSet ("ref" ).as ("x" );
165
166
166
167
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
167
168
Document push = DocumentTestUtils .getAsDocument (groupClause , "x" );
168
169
169
- assertThat (push ).isEqualTo (( Document ) new Document ("$addToSet" , "$ref" ));
170
+ assertThat (push ).isEqualTo (new Document ("$addToSet" , "$ref" ));
170
171
}
171
172
172
173
@ Test
173
- public void groupOperationAddToSetWithValue () {
174
+ void groupOperationAddToSetWithValue () {
174
175
175
176
GroupOperation groupOperation = Aggregation .group ("a" , "b" ).addToSet (42 ).as ("x" );
176
177
177
178
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
178
179
Document push = DocumentTestUtils .getAsDocument (groupClause , "x" );
179
180
180
- assertThat (push ).isEqualTo (( Document ) new Document ("$addToSet" , 42 ));
181
+ assertThat (push ).isEqualTo (new Document ("$addToSet" , 42 ));
181
182
}
182
183
183
184
@ Test // DATAMONGO-979
184
- public void shouldRenderSizeExpressionInGroup () {
185
+ void shouldRenderSizeExpressionInGroup () {
185
186
186
187
GroupOperation groupOperation = Aggregation //
187
188
.group ("username" ) //
@@ -191,11 +192,13 @@ public void shouldRenderSizeExpressionInGroup() {
191
192
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
192
193
Document tagsCount = DocumentTestUtils .getAsDocument (groupClause , "tags_count" );
193
194
194
- assertThat (tagsCount .get ("$first" )).isEqualTo ((Object ) new Document ("$size" , Arrays .asList ("$tags" )));
195
+ assertThat (tagsCount )
196
+ .containsEntry ("$first" , new Document ("$size" , Collections
197
+ .singletonList ("$tags" )));
195
198
}
196
199
197
200
@ Test // DATAMONGO-1327
198
- public void groupOperationStdDevSampWithValue () {
201
+ void groupOperationStdDevSampWithValue () {
199
202
200
203
GroupOperation groupOperation = Aggregation .group ("a" , "b" ).stdDevSamp ("field" ).as ("fieldStdDevSamp" );
201
204
@@ -206,7 +209,7 @@ public void groupOperationStdDevSampWithValue() {
206
209
}
207
210
208
211
@ Test // DATAMONGO-1327
209
- public void groupOperationStdDevPopWithValue () {
212
+ void groupOperationStdDevPopWithValue () {
210
213
211
214
GroupOperation groupOperation = Aggregation .group ("a" , "b" ).stdDevPop ("field" ).as ("fieldStdDevPop" );
212
215
@@ -217,7 +220,7 @@ public void groupOperationStdDevPopWithValue() {
217
220
}
218
221
219
222
@ Test // DATAMONGO-1784
220
- public void shouldRenderSumWithExpressionInGroup () {
223
+ void shouldRenderSumWithExpressionInGroup () {
221
224
222
225
GroupOperation groupOperation = Aggregation //
223
226
.group ("username" ) //
@@ -230,12 +233,12 @@ public void shouldRenderSumWithExpressionInGroup() {
230
233
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
231
234
Document foobar = DocumentTestUtils .getAsDocument (groupClause , "foobar" );
232
235
233
- assertThat (foobar . get ("$sum" )). isEqualTo ( new Document ("$cond" ,
236
+ assertThat (foobar ). containsEntry ("$sum" , new Document ("$cond" ,
234
237
new Document ("if" , new Document ("$eq" , Arrays .asList ("$foo" , "bar" ))).append ("then" , 1 ).append ("else" , -1 )));
235
238
}
236
239
237
240
@ Test // DATAMONGO-1784
238
- public void sumWithNullExpressionShouldThrowException () {
241
+ void sumWithNullExpressionShouldThrowException () {
239
242
assertThatIllegalArgumentException ()
240
243
.isThrownBy (() -> Aggregation .group ("username" ).sum ((AggregationExpression ) null ));
241
244
}
@@ -251,7 +254,7 @@ void accumulatorShouldBeAllowedOnGroupOperation() {
251
254
Document groupClause = extractDocumentFromGroupOperation (groupOperation );
252
255
Document accumulatedValue = DocumentTestUtils .getAsDocument (groupClause , "accumulated-value" );
253
256
254
- assertThat (accumulatedValue . get ("$accumulator" )). isNotNull ( );
257
+ assertThat (accumulatedValue ). containsKey ("$accumulator" );
255
258
}
256
259
257
260
private Document extractDocumentFromGroupOperation (GroupOperation groupOperation ) {
0 commit comments