summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/aggregation_request_test.cpp
blob: 66bac44423ca799984546a0ded46a9d651cd17bb (plain)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include "mongo/db/pipeline/aggregation_request_helper.h"

#include "mongo/bson/bsonobj.h"
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/bson/json.h"
#include "mongo/db/catalog/document_validation.h"
#include "mongo/db/exec/document_value/document.h"
#include "mongo/db/exec/document_value/document_value_test_util.h"
#include "mongo/db/exec/document_value/value.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/query/query_request_helper.h"
#include "mongo/db/repl/read_concern_args.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/assert_util.h"

namespace mongo {
namespace {

static constexpr auto kBatchSizeFieldName = "batchSize"_sd;

const Document kDefaultCursorOptionDocument{
    {kBatchSizeFieldName, aggregation_request_helper::kDefaultBatchSize}};

//
// Parsing
//

TEST(AggregationRequestTest, ShouldParseAllKnownOptions) {
    // Using oplog namespace so that validation of $_requestReshardingResumeToken succeeds.
    NamespaceString nss("local.oplog.rs");
    BSONObj inputBson = fromjson(
        "{aggregate: 'oplog.rs', pipeline: [{$match: {a: 'abc'}}], explain: false, allowDiskUse: "
        "true, fromMongos: true, "
        "needsMerge: true, bypassDocumentValidation: true, $_requestReshardingResumeToken: true, "
        "collation: {locale: 'en_US'}, cursor: {batchSize: 10}, hint: {a: 1}, maxTimeMS: 100, "
        "readConcern: {level: 'linearizable'}, $queryOptions: {$readPreference: 'nearest'}, "
        "exchange: {policy: 'roundrobin', consumers:NumberInt(2)}, isMapReduceCommand: true, $db: "
        "'local'}");
    auto uuid = UUID::gen();
    BSONObjBuilder uuidBob;
    uuid.appendToBuilder(&uuidBob, AggregateCommandRequest::kCollectionUUIDFieldName);
    inputBson = inputBson.addField(uuidBob.obj().firstElement());

    auto request =
        unittest::assertGet(aggregation_request_helper::parseFromBSONForTests(nss, inputBson));
    ASSERT_FALSE(request.getExplain());
    ASSERT_TRUE(request.getAllowDiskUse());
    ASSERT_TRUE(request.getFromMongos());
    ASSERT_TRUE(request.getNeedsMerge());
    ASSERT_TRUE(request.getBypassDocumentValidation().value_or(false));
    ASSERT_TRUE(request.getRequestReshardingResumeToken());
    ASSERT_EQ(
        request.getCursor().getBatchSize().value_or(aggregation_request_helper::kDefaultBatchSize),
        10);
    ASSERT_BSONOBJ_EQ(request.getHint().value_or(BSONObj()), BSON("a" << 1));
    ASSERT_BSONOBJ_EQ(request.getCollation().value_or(BSONObj()),
                      BSON("locale"
                           << "en_US"));
    ASSERT_EQ(*request.getMaxTimeMS(), 100u);
    ASSERT_BSONOBJ_EQ(*request.getReadConcern(),
                      BSON("level"
                           << "linearizable"));
    ASSERT_BSONOBJ_EQ(request.getUnwrappedReadPref().value_or(BSONObj()),
                      BSON("$readPreference"
                           << "nearest"));
    ASSERT_TRUE(request.getExchange().is_initialized());
    ASSERT_TRUE(request.getIsMapReduceCommand());
    ASSERT_EQ(*request.getCollectionUUID(), uuid);
}

TEST(AggregationRequestTest, ShouldParseExplicitRequestReshardingResumeTokenFalseForNonOplog) {
    NamespaceString nss("a.collection");
    const BSONObj inputBson = fromjson(
        "{aggregate: 'collection', pipeline: [], $_requestReshardingResumeToken: false, cursor: "
        "{}, $db: 'a'}");
    auto request =
        unittest::assertGet(aggregation_request_helper::parseFromBSONForTests(nss, inputBson));
    ASSERT_FALSE(request.getRequestReshardingResumeToken());
}

TEST(AggregationRequestTest, ShouldParseExplicitExplainTrue) {
    NamespaceString nss("a.collection");
    const BSONObj inputBson =
        fromjson("{aggregate: 'collection', pipeline: [], explain: true, cursor: {}, $db: 'a'}");
    auto request =
        unittest::assertGet(aggregation_request_helper::parseFromBSONForTests(nss, inputBson));
    ASSERT_TRUE(request.getExplain());
    ASSERT(*request.getExplain() == ExplainOptions::Verbosity::kQueryPlanner);
}

TEST(AggregationRequestTest, ShouldParseExplicitExplainFalseWithCursorOption) {
    NamespaceString nss("a.collection");
    const BSONObj inputBson = fromjson(
        "{aggregate: 'collection', pipeline: [], explain: false, cursor: {batchSize: 10}, $db: "
        "'a'}");
    auto request =
        unittest::assertGet(aggregation_request_helper::parseFromBSONForTests(nss, inputBson));
    ASSERT_FALSE(request.getExplain());
    ASSERT_EQ(
        request.getCursor().getBatchSize().value_or(aggregation_request_helper::kDefaultBatchSize),
        10);
}

TEST(AggregationRequestTest, ShouldParseWithSeparateQueryPlannerExplainModeArg) {
    NamespaceString nss("a.collection");
    const BSONObj inputBson =
        fromjson("{aggregate: 'collection', pipeline: [], cursor: {}, $db: 'a'}");
    auto request = unittest::assertGet(aggregation_request_helper::parseFromBSONForTests(
        nss, inputBson, ExplainOptions::Verbosity::kQueryPlanner));
    ASSERT_TRUE(request.getExplain());
    ASSERT(*request.getExplain() == ExplainOptions::Verbosity::kQueryPlanner);
}

TEST(AggregationRequestTest, ShouldParseWithSeparateQueryPlannerExplainModeArgAndCursorOption) {
    NamespaceString nss("a.collection");
    const BSONObj inputBson =
        fromjson("{aggregate: 'collection', pipeline: [], cursor: {batchSize: 10}, $db: 'a'}");
    auto request = unittest::assertGet(aggregation_request_helper::parseFromBSONForTests(
        nss, inputBson, ExplainOptions::Verbosity::kExecStats));
    ASSERT_TRUE(request.getExplain());
    ASSERT(*request.getExplain() == ExplainOptions::Verbosity::kExecStats);
    ASSERT_EQ(
        request.getCursor().getBatchSize().value_or(aggregation_request_helper::kDefaultBatchSize),
        10);
}

TEST(AggregationRequestTest, ShouldParseExplainFlagWithReadConcern) {
    NamespaceString nss("a.collection");
    // Non-local readConcern should not be allowed with the explain flag, but this is checked
    // elsewhere to avoid having to parse the readConcern in AggregationCommand.
    const BSONObj inputBson = fromjson(
        "{aggregate: 'collection', pipeline: [], explain: true, readConcern: {level: 'majority'}, "
        "$db: 'a'}");
    auto request =
        unittest::assertGet(aggregation_request_helper::parseFromBSONForTests(nss, inputBson));
    ASSERT_TRUE(request.getExplain());
    ASSERT_BSONOBJ_EQ(*request.getReadConcern(),
                      BSON("level"
                           << "majority"));
}

//
// Serialization
//

TEST(AggregationRequestTest, ShouldOnlySerializeRequiredFieldsIfNoOptionalFieldsAreSpecified) {
    NamespaceString nss("a.collection");
    AggregateCommandRequest request(nss, {});

    auto expectedSerialization =
        Document{{AggregateCommandRequest::kCommandName, nss.coll()},
                 {AggregateCommandRequest::kPipelineFieldName, std::vector<Value>{}},
                 {AggregateCommandRequest::kCursorFieldName, Value(kDefaultCursorOptionDocument)}};
    ASSERT_DOCUMENT_EQ(aggregation_request_helper::serializeToCommandDoc(request),
                       expectedSerialization);
}

TEST(AggregationRequestTest, ShouldSerializeOptionalValuesIfSet) {
    NamespaceString nss("a.collection");
    AggregateCommandRequest request(nss, {});
    request.setAllowDiskUse(true);
    request.setFromMongos(true);
    request.setNeedsMerge(true);
    request.setBypassDocumentValidation(true);
    request.setRequestReshardingResumeToken(true);
    SimpleCursorOptions cursor;
    cursor.setBatchSize(10);
    request.setCursor(cursor);
    request.setMaxTimeMS(10u);
    const auto hintObj = BSON("a" << 1);
    request.setHint(hintObj);
    const auto collationObj = BSON("locale"
                                   << "en_US");
    request.setCollation(collationObj);
    const auto readPrefObj = BSON("$readPreference"
                                  << "nearest");
    request.setUnwrappedReadPref(readPrefObj);
    const auto readConcernObj = BSON("level"
                                     << "linearizable");
    request.setReadConcern(readConcernObj);
    request.setIsMapReduceCommand(true);
    const auto letParamsObj = BSON("foo"
                                   << "bar");
    request.setLet(letParamsObj);
    auto uuid = UUID::gen();
    request.setCollectionUUID(uuid);

    auto expectedSerialization = Document{
        {AggregateCommandRequest::kCommandName, nss.coll()},
        {AggregateCommandRequest::kPipelineFieldName, std::vector<Value>{}},
        {AggregateCommandRequest::kAllowDiskUseFieldName, true},
        {AggregateCommandRequest::kCursorFieldName, Value(Document({{kBatchSizeFieldName, 10}}))},
        {query_request_helper::cmdOptionMaxTimeMS, 10},
        {AggregateCommandRequest::kBypassDocumentValidationFieldName, true},
        {repl::ReadConcernArgs::kReadConcernFieldName, readConcernObj},
        {AggregateCommandRequest::kCollationFieldName, collationObj},
        {AggregateCommandRequest::kHintFieldName, hintObj},
        {AggregateCommandRequest::kLetFieldName, letParamsObj},
        {AggregateCommandRequest::kNeedsMergeFieldName, true},
        {AggregateCommandRequest::kFromMongosFieldName, true},
        {query_request_helper::kUnwrappedReadPrefField, readPrefObj},
        {AggregateCommandRequest::kRequestReshardingResumeTokenFieldName, true},
        {AggregateCommandRequest::kIsMapReduceCommandFieldName, true},
        {AggregateCommandRequest::kCollectionUUIDFieldName, uuid}};
    ASSERT_DOCUMENT_EQ(aggregation_request_helper::serializeToCommandDoc(request),
                       expectedSerialization);
}

TEST(AggregationRequestTest, ShouldSerializeBatchSizeIfSetAndExplainFalse) {
    NamespaceString nss("a.collection");
    AggregateCommandRequest request(nss, {});
    SimpleCursorOptions cursor;
    cursor.setBatchSize(10);
    request.setCursor(cursor);

    auto expectedSerialization = Document{
        {AggregateCommandRequest::kCommandName, nss.coll()},
        {AggregateCommandRequest::kPipelineFieldName, std::vector<Value>{}},
        {AggregateCommandRequest::kCursorFieldName, Value(Document({{kBatchSizeFieldName, 10}}))}};
    ASSERT_DOCUMENT_EQ(aggregation_request_helper::serializeToCommandDoc(request),
                       expectedSerialization);
}

TEST(AggregationRequestTest, ShouldSerialiseAggregateFieldToOneIfCollectionIsAggregateOneNSS) {
    NamespaceString nss = NamespaceString::makeCollectionlessAggregateNSS("a");
    AggregateCommandRequest request(nss, {});

    auto expectedSerialization =
        Document{{AggregateCommandRequest::kCommandName, 1},
                 {AggregateCommandRequest::kPipelineFieldName, std::vector<Value>{}},
                 {AggregateCommandRequest::kCursorFieldName,
                  Value(Document({{aggregation_request_helper::kBatchSizeField,
                                   aggregation_request_helper::kDefaultBatchSize}}))}};

    ASSERT_DOCUMENT_EQ(aggregation_request_helper::serializeToCommandDoc(request),
                       expectedSerialization);
}

TEST(AggregationRequestTest, ShouldSetBatchSizeToDefaultOnEmptyCursorObject) {
    NamespaceString nss("a.collection");
    const BSONObj inputBson = fromjson(
        "{aggregate: 'collection', pipeline: [{$match: {a: 'abc'}}], cursor: {}, $db: 'a'}");
    auto request = aggregation_request_helper::parseFromBSONForTests(nss, inputBson);
    ASSERT_OK(request.getStatus());
    ASSERT_EQ(request.getValue().getCursor().getBatchSize().value_or(
                  aggregation_request_helper::kDefaultBatchSize),
              aggregation_request_helper::kDefaultBatchSize);
}

TEST(AggregationRequestTest, ShouldAcceptHintAsString) {
    NamespaceString nss("a.collection");
    const BSONObj inputBson = fromjson(
        "{aggregate: 'collection', pipeline: [{$match: {a: 'abc'}}], hint: 'a_1', cursor: {}, $db: "
        "'a'}");
    auto request = aggregation_request_helper::parseFromBSONForTests(nss, inputBson);
    ASSERT_OK(request.getStatus());
    ASSERT_BSONOBJ_EQ(request.getValue().getHint().value_or(BSONObj()),
                      BSON("$hint"
                           << "a_1"));
}

TEST(AggregationRequestTest, ShouldNotSerializeBatchSizeWhenExplainSet) {
    NamespaceString nss("a.collection");
    AggregateCommandRequest request(nss, {});
    SimpleCursorOptions cursor;
    cursor.setBatchSize(10);
    request.setCursor(cursor);
    request.setExplain(ExplainOptions::Verbosity::kQueryPlanner);

    auto expectedSerialization =
        Document{{AggregateCommandRequest::kCommandName, nss.coll()},
                 {AggregateCommandRequest::kPipelineFieldName, std::vector<Value>{}},
                 {AggregateCommandRequest::kCursorFieldName, Value(Document())}};
    ASSERT_DOCUMENT_EQ(aggregation_request_helper::serializeToCommandDoc(request),
                       expectedSerialization);
}

//
// Error cases.
//

/**
 * Combines 'validRequest' and 'invalidFields' into a single BSONObj. Note that if the two share
 * a common field, the field from 'invalidFields' will be kept.
 */
BSONObj constructInvalidRequest(const BSONObj& validRequest, const BSONObj& invalidFields) {
    BSONObjBuilder invalidRequestBuilder;

    // An aggregate command expects the first field in the request to be 'aggregate'. As such, we
    // pull out the aggregate field from whichever BSONObj supplied it and append it before any
    // other fields.
    auto validAggregateField = validRequest.getField(AggregateCommandRequest::kCommandName);
    auto invalidAggregateField = invalidFields.getField(AggregateCommandRequest::kCommandName);
    if (!invalidAggregateField.eoo()) {
        invalidRequestBuilder.append(invalidAggregateField);
    } else {
        invariant(!validAggregateField.eoo());
        invalidRequestBuilder.append(validAggregateField);
    }

    // Construct a command object containing a union of the two objects.
    for (auto&& elem : invalidFields) {
        auto fieldName = elem.fieldName();
        if (!invalidRequestBuilder.hasField(fieldName)) {
            invalidRequestBuilder.append(elem);
        }
    }


    for (auto&& elem : validRequest) {
        auto fieldName = elem.fieldName();
        if (!invalidRequestBuilder.hasField(fieldName)) {
            invalidRequestBuilder.append(elem);
        }
    }
    return invalidRequestBuilder.obj();
}

/**
 * Helper which verifies that 'validRequest' parses correctly, but fails to parse once
 * 'invalidFields' are added to it.
 */
void aggregationRequestParseFailureHelper(
    const NamespaceString& nss,
    const BSONObj& validRequest,
    const BSONObj& invalidFields,
    ErrorCodes::Error expectedCode,
    boost::optional<ExplainOptions::Verbosity> explainVerbosity = boost::none) {
    // Verify that 'validRequest' parses correctly.
    ASSERT_OK(aggregation_request_helper::parseFromBSONForTests(nss, validRequest, explainVerbosity)
                  .getStatus());

    auto invalidRequest = constructInvalidRequest(validRequest, invalidFields);

    // Verify that the constructed invalid request fails to parse.
    auto status =
        aggregation_request_helper::parseFromBSONForTests(nss, invalidRequest, explainVerbosity)
            .getStatus();
    ASSERT_NOT_OK(status);
    ASSERT_EQ(status.code(), expectedCode);
}

/**
 * Verifies that 'validRequest' parses correctly, but throws 'expectedCode' once 'invalidFields'
 * are added to it.
 */
void parseNSHelper(const std::string& dbName,
                   const BSONObj& validRequest,
                   const BSONObj& invalidFields,
                   ErrorCodes::Error expectedCode) {
    // Verify that 'validRequest' parses correctly.
    auto shouldNotThrow = aggregation_request_helper::parseNs(dbName, validRequest);

    auto invalidRequest = constructInvalidRequest(validRequest, invalidFields);

    // Verify that the constructed invalid request fails to parse with 'expectedCode'.
    ASSERT_THROWS_CODE(
        aggregation_request_helper::parseNs("a", invalidRequest), AssertionException, expectedCode);
}

TEST(AggregationRequestTest, ShouldRejectNonArrayPipeline) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest =
        fromjson("{aggregate: 'collection', pipeline: [], cursor: {}, $db: 'a'}");
    const BSONObj nonArrayPipeline = fromjson("{pipeline: {}}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonArrayPipeline, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectPipelineArrayIfAnElementIsNotAnObject) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection', pipeline: [{$match: {a: 'abc'}}], cursor: {}, $db: 'a'}");
    BSONObj nonObjectPipelineElem = fromjson("{pipeline: [4]}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonObjectPipelineElem, ErrorCodes::TypeMismatch);

    nonObjectPipelineElem = fromjson("{pipeline: [{$match: {a: 'abc'}}, 4]}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonObjectPipelineElem, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectNonObjectCollation) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection', "
        "pipeline: [{$match: {a: 'abc'}}],"
        "cursor: {}, "
        "collation: {locale: 'simple'}, "
        "$db: 'a'}");
    const BSONObj nonObjectCollation = fromjson("{collation: 1}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonObjectCollation, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectNonStringNonObjectHint) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection', "
        "pipeline: [{$match: {a: 'abc'}}],"
        "cursor: {},"
        "hint: {_id: 1},"
        "$db: 'a'}");
    const BSONObj nonObjectHint = fromjson("{hint: 1}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonObjectHint, ErrorCodes::FailedToParse);
}

TEST(AggregationRequestTest, ShouldRejectExplainIfNumber) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [{$match: {a: 'abc'}}],"
        "cursor: {},"
        "explain: true, "
        "$db: 'a'}");
    const BSONObj numericExplain = fromjson("{explain: 1}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, numericExplain, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectExplainIfObject) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [{$match: {a: 'abc'}}],"
        "cursor: {},"
        "explain: true, "
        "$db: 'a'}");
    const BSONObj objectExplain = fromjson("{explain: {}}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, objectExplain, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectNonBoolFromMongos) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [{$match: {a: 'abc'}}],"
        "cursor: {}, "
        "fromMongos: true, "
        "$db: 'a'}");
    const BSONObj nonBoolFromMongos = fromjson("{fromMongos: 1}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonBoolFromMongos, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectNonBoolNeedsMerge) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [{$match: {a: 'abc'}}], "
        "cursor: {},"
        "needsMerge: true, "
        "fromMongos: true,"
        "$db: 'a'}");
    const BSONObj nonBoolNeedsMerge = fromjson("{needsMerge: 1}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonBoolNeedsMerge, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectNeedsMergeIfFromMongosNotPresent) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [{$match: {a: 'abc'}}],"
        "cursor: {}, "
        "$db: 'a'}");
    const BSONObj needsMergeNoFromMongos = fromjson("{needsMerge: true}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, needsMergeNoFromMongos, ErrorCodes::FailedToParse);
}

TEST(AggregationRequestTest, ShouldRejectNonBoolAllowDiskUse) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [{$match: {a: 'abc'}}],"
        "cursor: {},"
        "allowDiskUse: true, "
        "$db: 'a'}");
    const BSONObj nonBoolAllowDiskUse = fromjson("{allowDiskUse: 1}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonBoolAllowDiskUse, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectNonBoolIsMapReduceCommand) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [{$match: {a: 'abc'}}],"
        "cursor: {}, "
        "isMapReduceCommand: true,"
        "$db: 'a'}");
    const BSONObj nonBoolIsMapReduceCommand = fromjson("{isMapReduceCommand: 1}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonBoolIsMapReduceCommand, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectNoCursorNoExplain) {
    NamespaceString nss("a.collection");

    // An aggregate with neither cursor nor explain should fail to parse.
    const BSONObj invalidRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [{$match: {a: 'abc'}}],"
        "$db: 'a'}");
    auto status =
        aggregation_request_helper::parseFromBSONForTests(nss, invalidRequest).getStatus();
    ASSERT_NOT_OK(status);
    ASSERT_EQ(status.code(), ErrorCodes::FailedToParse);

    // Adding explain should cause the aggregate to parse successfully.
    BSONObjBuilder explainRequest(invalidRequest);
    explainRequest.append("explain", true);
    ASSERT_OK(
        aggregation_request_helper::parseFromBSONForTests(nss, explainRequest.done()).getStatus());

    // Adding cursor should cause the aggregate to parse successfully.
    BSONObjBuilder cursorRequest(invalidRequest);
    cursorRequest.append("cursor", BSONObj());
    ASSERT_OK(
        aggregation_request_helper::parseFromBSONForTests(nss, cursorRequest.done()).getStatus());
}

TEST(AggregationRequestTest, ShouldRejectExplainTrueWithSeparateExplainArg) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [],"
        "cursor: {},"
        "$db: 'a'}");
    const BSONObj explainTrue = fromjson("{explain: true}");
    aggregationRequestParseFailureHelper(nss,
                                         validRequest,
                                         explainTrue,
                                         ErrorCodes::FailedToParse,
                                         ExplainOptions::Verbosity::kExecStats);
}

TEST(AggregationRequestTest, ShouldRejectExplainFalseWithSeparateExplainArg) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [],"
        "cursor: {},"
        "$db: 'a'}");
    const BSONObj explainFalse = fromjson("{explain: false}");
    aggregationRequestParseFailureHelper(nss,
                                         validRequest,
                                         explainFalse,
                                         ErrorCodes::FailedToParse,
                                         ExplainOptions::Verbosity::kExecStats);
}

TEST(AggregationRequestTest, ShouldRejectExplainWithWriteConcernMajority) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest =
        fromjson("{aggregate: 'collection', pipeline: [], explain: true, $db: 'a'}");
    const BSONObj wcMajority = fromjson("{writeConcern: {w: 'majority'}}");
    aggregationRequestParseFailureHelper(nss, validRequest, wcMajority, ErrorCodes::FailedToParse);
}

TEST(AggregationRequestTest, ShouldRejectExplainExecStatsVerbosityWithWriteConcernMajority) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest =
        fromjson("{aggregate: 'collection', pipeline: [], cursor: {}, $db: 'a'}");
    const BSONObj wcMajority = fromjson("{writeConcern: {w: 'majority'}}");
    aggregationRequestParseFailureHelper(nss,
                                         validRequest,
                                         wcMajority,
                                         ErrorCodes::FailedToParse,
                                         ExplainOptions::Verbosity::kExecStats);
}

TEST(AggregationRequestTest, ShouldRejectRequestReshardingResumeTokenIfNonBooleanType) {
    NamespaceString oplogNss("local.oplog.rs");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [],"
        "$_requestReshardingResumeToken: true,"
        "$db: 'a', "
        "cursor: {}}");
    const BSONObj nonBoolReshardingResumeToken =
        fromjson("{$_requestReshardingResumeToken: 'yes'}");
    aggregationRequestParseFailureHelper(
        oplogNss, validRequest, nonBoolReshardingResumeToken, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectRequestReshardingResumeTokenIfNonOplogNss) {
    NamespaceString oplogNss("local.oplog.rs");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [],"
        "$_requestReshardingResumeToken: true,"
        "$db: 'a', "
        "cursor: {}}");
    ASSERT_OK(
        aggregation_request_helper::parseFromBSONForTests(oplogNss, validRequest).getStatus());

    NamespaceString nonOplogNss("a.collection");
    auto status =
        aggregation_request_helper::parseFromBSONForTests(nonOplogNss, validRequest).getStatus();
    ASSERT_NOT_OK(status);
    ASSERT_EQ(status, ErrorCodes::FailedToParse);
}

TEST(AggregationRequestTest, ParseNSShouldReturnAggregateOneNSIfAggregateFieldIsOne) {
    const std::vector<std::string> ones{
        "1", "1.0", "NumberInt(1)", "NumberLong(1)", "NumberDecimal('1')"};

    for (auto& one : ones) {
        const BSONObj inputBSON =
            fromjson(str::stream() << "{aggregate: " << one << ", pipeline: [], $db: 'a'}");
        ASSERT(aggregation_request_helper::parseNs("a", inputBSON).isCollectionlessAggregateNS());
    }
}

TEST(AggregationRequestTest, ParseNSShouldRejectNumericNSIfAggregateFieldIsNotOne) {
    const BSONObj validRequest = fromjson("{aggregate: 1, pipeline: [], $db: 'a'}");
    BSONObj nonOneAggregate = fromjson("{aggregate: 2}");
    parseNSHelper("a", validRequest, nonOneAggregate, ErrorCodes::FailedToParse);
}

TEST(AggregationRequestTest, ParseNSShouldRejectNonStringNonNumericNS) {
    const BSONObj validRequest = fromjson("{aggregate: 1, pipeline: [], $db: 'a'}");
    BSONObj nonStringNonNumericNS = fromjson("{aggregate: {}}");
    parseNSHelper("a", validRequest, nonStringNonNumericNS, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ParseNSShouldRejectAggregateOneStringAsCollectionName) {
    const BSONObj validRequest = fromjson("{aggregate: 1, pipeline: [], $db: 'a'}");
    BSONObj oneStringAsCollectionName = fromjson("{aggregate: '$cmd.aggregate'}");
    parseNSHelper("a", validRequest, oneStringAsCollectionName, ErrorCodes::InvalidNamespace);
}

TEST(AggregationRequestTest, ParseNSShouldRejectInvalidCollectionName) {
    const BSONObj validRequest = fromjson("{aggregate: 1, pipeline: [], $db: 'a'}");
    BSONObj invalidCollectionName = fromjson("{aggregate: ''}");
    parseNSHelper("a", validRequest, invalidCollectionName, ErrorCodes::InvalidNamespace);
}

TEST(AggregationRequestTest, ParseFromBSONOverloadsShouldProduceIdenticalRequests) {
    const BSONObj inputBSON = fromjson(
        "{aggregate: 'collection', pipeline: [{$match: {}}, {$project: {}}], cursor: {}, $db: "
        "'a'}");
    NamespaceString nss("a.collection");

    auto aggReqDBName =
        unittest::assertGet(aggregation_request_helper::parseFromBSONForTests("a", inputBSON));
    auto aggReqNSS =
        unittest::assertGet(aggregation_request_helper::parseFromBSONForTests(nss, inputBSON));

    ASSERT_DOCUMENT_EQ(aggregation_request_helper::serializeToCommandDoc(aggReqDBName),
                       aggregation_request_helper::serializeToCommandDoc(aggReqNSS));
}

TEST(AggregationRequestTest, ShouldRejectExchangeNotObject) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection', "
        "pipeline: [],"
        "cursor: {},"
        "exchange: {policy: 'roundrobin', consumers: 2},"
        "$db: 'a'}");
    const BSONObj nonObjectExchange = fromjson("{exchange: '42'}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonObjectExchange, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectExchangeInvalidSpec) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [],"
        "cursor: {},"
        "exchange: {policy: 'roundrobin', consumers: 2}, "
        "$db: 'a'}");
    const BSONObj invalidExchangeSpec = fromjson("{exchange: {}}");
    auto missingFieldErrorCode = ErrorCodes::duplicateCodeForTest(40414);
    aggregationRequestParseFailureHelper(
        nss, validRequest, invalidExchangeSpec, missingFieldErrorCode);
}

TEST(AggregationRequestTest, ShouldRejectInvalidWriteConcern) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection',"
        "pipeline: [{$match: {a: 'abc'}}],"
        "cursor: {},"
        "writeConcern: {w: 'majority'}, "
        "$db: 'a'}");
    const BSONObj invalidWC = fromjson("{writeConcern: 'invalid'}");
    aggregationRequestParseFailureHelper(nss, validRequest, invalidWC, ErrorCodes::TypeMismatch);
}

TEST(AggregationRequestTest, ShouldRejectInvalidCollectionUUID) {
    NamespaceString nss("a.collection");
    auto uuid = UUID::gen();
    BSONObjBuilder validRequestBuilder(
        fromjson("{aggregate: 'collection', cursor: {}, pipeline: [{$match: {}}], $db: 'a'}"));
    uuid.appendToBuilder(&validRequestBuilder, AggregateCommandRequest::kCollectionUUIDFieldName);
    const BSONObj validRequest = validRequestBuilder.done();
    const BSONObj invalidCollectionUUID = fromjson("{collectionUUID: 2}");
    aggregationRequestParseFailureHelper(
        nss, validRequest, invalidCollectionUUID, ErrorCodes::TypeMismatch);
}

//
// Ignore fields parsed elsewhere.
//

TEST(AggregationRequestTest, ShouldRejectUnknownField) {
    NamespaceString nss("a.collection");
    const BSONObj validRequest = fromjson(
        "{aggregate: 'collection', "
        "pipeline: [],"
        "cursor: {},"
        "$db: 'a'}");
    const BSONObj nonObjectExchange =
        fromjson("{thisIsNotARealField: 'this is not a real option'}");
    auto unknownFieldErrorCode = ErrorCodes::duplicateCodeForTest(40415);
    aggregationRequestParseFailureHelper(
        nss, validRequest, nonObjectExchange, unknownFieldErrorCode);
}

TEST(AggregationRequestTest, ShouldIgnoreQueryOptions) {
    NamespaceString nss("a.collection");
    const BSONObj inputBson = fromjson(
        "{aggregate: 'collection', pipeline: [{$match: {a: 'abc'}}], cursor: {}, $queryOptions: "
        "{}, $db: 'a'}");
    ASSERT_OK(aggregation_request_helper::parseFromBSONForTests(nss, inputBson).getStatus());
}

}  // namespace
}  // namespace mongo