1
1
/*
2
- * Copyright 2016-2017 the original author or authors.
2
+ * Copyright 2016-2018 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.
15
15
*/
16
16
package org .springframework .data .mongodb .core .aggregation ;
17
17
18
- import static org .hamcrest .MatcherAssert .* ;
18
+ import static org .hamcrest .MatcherAssert .assertThat ;
19
19
import static org .hamcrest .Matchers .*;
20
20
import static org .springframework .data .mongodb .core .aggregation .Aggregation .*;
21
+ import static org .springframework .data .mongodb .test .util .Assertions .assertThat ;
21
22
23
+ import org .bson .Document ;
22
24
import org .junit .Test ;
23
25
import org .springframework .data .mongodb .core .query .Criteria ;
24
26
25
- import org .bson .Document ;
26
-
27
27
/**
28
28
* Unit tests for {@link FacetOperation}.
29
29
*
34
34
public class FacetOperationUnitTests {
35
35
36
36
@ Test // DATAMONGO-1552
37
- public void shouldRenderCorrectly () throws Exception {
38
-
39
- FacetOperation facetOperation = new FacetOperation ()
40
- .and (match (Criteria .where ("price" ).exists (true )), //
41
- bucket ("price" ) //
42
- .withBoundaries (0 , 150 , 200 , 300 , 400 ) //
43
- .withDefaultBucket ("Other" ) //
44
- .andOutputCount ().as ("count" ) //
45
- .andOutput ("title" ).push ().as ("titles" )) //
37
+ public void shouldRenderCorrectly () {
38
+
39
+ FacetOperation facetOperation = new FacetOperation ().and (match (Criteria .where ("price" ).exists (true )), //
40
+ bucket ("price" ) //
41
+ .withBoundaries (0 , 150 , 200 , 300 , 400 ) //
42
+ .withDefaultBucket ("Other" ) //
43
+ .andOutputCount ().as ("count" ) //
44
+ .andOutput ("title" ).push ().as ("titles" )) //
46
45
.as ("categorizedByPrice" ) //
47
46
.and (bucketAuto ("year" , 5 )).as ("categorizedByYears" );
48
47
49
48
Document agg = facetOperation .toDocument (Aggregation .DEFAULT_CONTEXT );
50
49
51
- assertThat (agg ,
52
- is (Document .parse ("{ $facet: { categorizedByPrice: [" + "{ $match: { price: { $exists: true } } }, "
50
+ assertThat (agg )
51
+ . isEqualTo (Document .parse ("{ $facet: { categorizedByPrice: [" + "{ $match: { price: { $exists: true } } }, "
53
52
+ "{ $bucket: { boundaries: [ 0, 150, 200, 300, 400 ], groupBy: \" $price\" , default: \" Other\" , "
54
53
+ "output: { count: { $sum: 1 }, titles: { $push: \" $title\" } } } } ],"
55
- + "categorizedByYears: [ { $bucketAuto: { buckets: 5, groupBy: \" $year\" } } ] } }" ))) ;
54
+ + "categorizedByYears: [ { $bucketAuto: { buckets: 5, groupBy: \" $year\" } } ] } }" ));
56
55
}
57
56
58
57
@ Test // DATAMONGO-1552
59
- public void shouldRenderEmpty () throws Exception {
58
+ public void shouldRenderEmpty () {
60
59
61
60
FacetOperation facetOperation = facet ();
62
61
@@ -66,7 +65,7 @@ public void shouldRenderEmpty() throws Exception {
66
65
}
67
66
68
67
@ Test (expected = IllegalArgumentException .class ) // DATAMONGO-1552
69
- public void shouldRejectNonExistingFields () throws Exception {
68
+ public void shouldRejectNonExistingFields () {
70
69
71
70
FacetOperation facetOperation = new FacetOperation ()
72
71
.and (project ("price" ), //
@@ -79,11 +78,11 @@ public void shouldRejectNonExistingFields() throws Exception {
79
78
80
79
Document agg = facetOperation .toDocument (Aggregation .DEFAULT_CONTEXT );
81
80
82
- assertThat (agg ,
83
- is (Document .parse ("{ $facet: { categorizedByPrice: [" + "{ $match: { price: { $exists: true } } }, "
81
+ assertThat (agg )
82
+ . isEqualTo (Document .parse ("{ $facet: { categorizedByPrice: [" + "{ $match: { price: { $exists: true } } }, "
84
83
+ "{ $bucket: {boundaries: [ 0, 150, 200, 300, 400 ], groupBy: \" $price\" , default: \" Other\" , "
85
84
+ "output: { count: { $sum: 1 }, titles: { $push: \" $title\" } } } } ],"
86
- + "categorizedByYears: [ { $bucketAuto: { buckets: 5, groupBy: \" $year\" } } ] } }" ))) ;
85
+ + "categorizedByYears: [ { $bucketAuto: { buckets: 5, groupBy: \" $year\" } } ] } }" ));
87
86
}
88
87
89
88
@ Test // DATAMONGO-1552
@@ -97,22 +96,21 @@ public void shouldHonorProjectedFields() {
97
96
98
97
Document agg = facetOperation .toDocument (Aggregation .DEFAULT_CONTEXT );
99
98
100
- assertThat (agg ,
101
- is ( Document . parse ( "{ $facet: { categorizedByPrice: [" + "{ $project: { price: 1, name: \" $title\" } }, "
99
+ assertThat (agg ). isEqualTo ( Document . parse ( "{ $facet: { categorizedByPrice: ["
100
+ + "{ $project: { price: 1, name: \" $title\" } }, "
102
101
+ "{ $bucketAuto: { buckets: 5, groupBy: \" $price\" , "
103
- + "output: { titles: { $push: \" $name\" } } } } ] } }" ))) ;
102
+ + "output: { titles: { $push: \" $name\" } } } } ] } }" ));
104
103
}
105
104
106
105
@ Test // DATAMONGO-1553
107
106
public void shouldRenderSortByCountCorrectly () {
108
107
109
- FacetOperation facetOperation = new FacetOperation ()
110
- .and (sortByCount ("country" ))
108
+ FacetOperation facetOperation = new FacetOperation () //
109
+ .and (sortByCount ("country" )) //
111
110
.as ("categorizedByCountry" );
112
111
113
112
Document agg = facetOperation .toDocument (Aggregation .DEFAULT_CONTEXT );
114
113
115
- assertThat (agg ,
116
- is (Document .parse ("{ $facet: { categorizedByCountry: [{ $sortByCount: \" $country\" } ] } }" )));
114
+ assertThat (agg ).containsEntry ("$facet.categorizedByCountry.[0].$sortByCount" , "$country" );
117
115
}
118
116
}
0 commit comments