forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggregationTest.java
More file actions
312 lines (263 loc) · 12.1 KB
/
AggregationTest.java
File metadata and controls
312 lines (263 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import com.mongodb.AggregationOptions.OutputMode;
import com.mongodb.util.TestCase;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
public class AggregationTest extends TestCase {
private DBCollection collection;
private DB database;
@Before
public void before() {
database = getDatabase();
collection = database.getCollection(getClass().getSimpleName() + System.nanoTime());
collection.drop();
}
@Test
public void testAggregation() {
validate(buildPipeline());
}
@Test
public void testOldAggregationWithOut() {
checkServerVersion(2.6);
List<DBObject> pipeline = new ArrayList<DBObject>(buildPipeline());
pipeline.add(new BasicDBObject("$out", "aggCollection"));
final AggregationOutput out = collection.aggregate(pipeline);
assertFalse(out.results().iterator().hasNext());
assertEquals(database.getCollection("aggCollection")
.count(), 2);
}
@Test
public void testExplain() {
checkServerVersion(2.6);
List<DBObject> pipeline = new ArrayList<DBObject>(buildPipeline());
pipeline.add(new BasicDBObject("$out", "aggCollection"));
final CommandResult out = collection.explainAggregate(pipeline, AggregationOptions.builder()
.allowDiskUse(true)
.outputMode(AggregationOptions.OutputMode.CURSOR)
.build());
assertTrue(out.keySet().iterator().hasNext());
}
@Test(expected = IllegalArgumentException.class)
public void testNullOptions() {
collection.aggregate(new ArrayList<DBObject>(), (AggregationOptions) null);
}
private void validate(List<DBObject> pipeline) {
final AggregationOutput out = collection.aggregate(pipeline);
final Map<String, DBObject> results = new HashMap<String, DBObject>();
for (DBObject result : out.results()) {
results.put((String) result.get("_id"), result);
}
final DBObject fooResult = results.get("foo");
assertNotNull(fooResult);
assertEquals(fooResult.get("docsPerName"), 2);
assertEquals(fooResult.get("countPerName"), 12);
final DBObject barResult = results.get("bar");
assertNotNull(barResult);
assertEquals(barResult.get("docsPerName"), 1);
assertEquals(barResult.get("countPerName"), 2);
final DBObject aggregationCommand = out.getCommand();
assertNotNull(aggregationCommand);
assertEquals(aggregationCommand.get("aggregate"), collection.getName());
assertNotNull(aggregationCommand.get("pipeline"));
}
private List<DBObject> buildPipeline() {
final DBObject foo = new BasicDBObject("name", "foo").append("count", 5);
final DBObject bar = new BasicDBObject("name", "bar").append("count", 2);
final DBObject baz = new BasicDBObject("name", "foo").append("count", 7);
collection.insert(foo, bar, baz);
final DBObject projection = new BasicDBObject("name", 1).append("count", 1);
final DBObject group = new BasicDBObject().append("_id", "$name")
.append("docsPerName", new BasicDBObject("$sum", 1))
.append("countPerName", new BasicDBObject("$sum", "$count"));
return Arrays.<DBObject>asList(new BasicDBObject("$project", projection), new BasicDBObject("$group", group));
}
@Test
public void testAggregationCursor() {
checkServerVersion(2.6);
final List<DBObject> pipeline = prepareData();
verify(pipeline, AggregationOptions.builder()
.batchSize(1)
.outputMode(AggregationOptions.OutputMode.CURSOR)
.allowDiskUse(true)
.build());
verify(pipeline, AggregationOptions.builder()
.batchSize(1)
.outputMode(AggregationOptions.OutputMode.INLINE)
.allowDiskUse(true)
.build());
verify(pipeline, AggregationOptions.builder()
.batchSize(1)
.outputMode(AggregationOptions.OutputMode.CURSOR)
.build());
}
@Test
public void testInlineAndDollarOut() {
checkServerVersion(2.6);
String aggCollection = "aggCollection";
database.getCollection(aggCollection)
.drop();
assertEquals(0, database.getCollection(aggCollection)
.count());
final List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
pipeline.add(new BasicDBObject("$out", aggCollection));
final AggregationOutput out = collection.aggregate(pipeline);
assertFalse(out.results()
.iterator()
.hasNext());
assertEquals(database.getCollection(aggCollection)
.count(), 2);
}
@Test
public void testDollarOut() {
checkServerVersion(2.6);
String aggCollection = "aggCollection";
database.getCollection(aggCollection)
.drop();
assertEquals(database.getCollection(aggCollection).count(), 0);
final List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
pipeline.add(new BasicDBObject("$out", aggCollection));
verify(pipeline, AggregationOptions.builder()
.outputMode(AggregationOptions.OutputMode.CURSOR)
.build());
assertEquals(2, database.getCollection(aggCollection)
.count());
}
@Test
public void testDollarOutOnSecondary() throws UnknownHostException {
checkServerVersion(2.6);
assumeTrue(isReplicaSet(cleanupMongo));
ServerAddress primary = new ServerAddress("localhost");
MongoClient rsClient = new MongoClient(asList(primary, new ServerAddress("localhost", 27018)));
DB rsDatabase = rsClient.getDB(database.getName());
DBCollection aggCollection = rsDatabase.getCollection(collection.getName());
aggCollection.drop();
final List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
pipeline.add(new BasicDBObject("$out", "aggCollection"));
AggregationOptions options = AggregationOptions.builder()
.outputMode(OutputMode.CURSOR)
.build();
Cursor cursor = verify(pipeline, options, ReadPreference.secondary(), aggCollection);
assertEquals(2, rsDatabase.getCollection("aggCollection").count());
assertEquals(primary, cursor.getServerAddress());
}
@Test
@Ignore
public void testAggregateOnSecondary() throws UnknownHostException {
checkServerVersion(2.6);
assumeTrue(isReplicaSet(cleanupMongo));
ServerAddress primary = new ServerAddress("localhost");
ServerAddress secondary = new ServerAddress("localhost", 27018);
MongoClient rsClient = new MongoClient(asList(primary, secondary));
DB rsDatabase = rsClient.getDB(database.getName());
rsDatabase.dropDatabase();
DBCollection aggCollection = rsDatabase.getCollection(collection.getName());
aggCollection.drop();
final List<DBObject> pipeline = new ArrayList<DBObject>(prepareData());
AggregationOptions options = AggregationOptions.builder()
.outputMode(OutputMode.INLINE)
.build();
Cursor cursor = verify(pipeline, options, ReadPreference.secondary(), aggCollection);
assertNotEquals(primary, cursor.getServerAddress());
}
@Test
public void testMaxTime() {
assumeFalse(isSharded(getMongoClient()));
checkServerVersion(2.6);
enableMaxTimeFailPoint();
DBCollection collection = database.getCollection("testMaxTime");
try {
collection.aggregate(prepareData(), AggregationOptions.builder().maxTime(1, SECONDS).build());
fail("Show have thrown");
} catch (MongoExecutionTimeoutException e) {
assertEquals(50, e.getCode());
} finally {
disableMaxTimeFailPoint();
}
}
@Test
public void testInvalidPipelineThrowsError() {
checkServerVersion(2.6);
DBCollection collection = database.getCollection("testInvalidPipeline");
List<DBObject> invalidPipeline = asList((DBObject) new BasicDBObject("name", "foo"));
try {
collection.aggregate(invalidPipeline);
fail("Show have thrown");
} catch (CommandFailureException e) {
// continue
}
try {
collection.aggregate(invalidPipeline, AggregationOptions.builder().build());
fail("Show have thrown");
} catch (CommandFailureException e) {
// continue
}
}
public List<DBObject> prepareData() {
collection.remove(new BasicDBObject());
final DBObject foo = new BasicDBObject("name", "foo").append("count", 5);
final DBObject bar = new BasicDBObject("name", "bar").append("count", 2);
final DBObject baz = new BasicDBObject("name", "foo").append("count", 7);
collection.insert(foo, bar, baz);
final DBObject projection = new BasicDBObject("name", 1).append("count", 1);
final DBObject group = new BasicDBObject().append("_id", "$name")
.append("docsPerName", new BasicDBObject("$sum", 1))
.append("countPerName", new BasicDBObject("$sum", "$count"));
return Arrays.<DBObject>asList(new BasicDBObject("$project", projection), new BasicDBObject("$group", group));
}
private void verify(final List<DBObject> pipeline, final AggregationOptions options) {
verify(pipeline, options, ReadPreference.primary());
}
private void verify(final List<DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference) {
verify(pipeline, options, readPreference, collection);
}
private Cursor verify(final List<DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference,
final DBCollection collection) {
final Cursor cursor = collection.aggregate(pipeline, options, readPreference);
final Map<String, DBObject> results = new HashMap<String, DBObject>();
while (cursor.hasNext()) {
DBObject next = cursor.next();
results.put((String) next.get("_id"), next);
}
final DBObject fooResult = results.get("foo");
assertNotNull(fooResult);
assertEquals(fooResult.get("docsPerName"), 2);
assertEquals(fooResult.get("countPerName"), 12);
final DBObject barResult = results.get("bar");
assertNotNull(barResult);
assertEquals(barResult.get("docsPerName"), 1);
assertEquals(barResult.get("countPerName"), 2);
return cursor;
}
}