Skip to content

Commit f3397e9

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1986 - Always provide a typed AggregationOperationContext for TypedAggregation.
We now initialize a TypeBasedAggregationOperationContext for TypedAggregations if no context is provided. This makes sure that potential Criteria objects are run trough the QueryMapper. In case the default context is used we now also make sure to at least run the aggregation pipeline through the QueryMapper to avoid passing on non MongoDB simple types to the driver. Original pull request: #564.
1 parent 5807c5d commit f3397e9

File tree

4 files changed

+145
-18
lines changed

4 files changed

+145
-18
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class AggregationOperationRenderer {
4242
* {@link DBObject} representation.
4343
*
4444
* @param operations must not be {@literal null}.
45-
* @param context must not be {@literal null}.
45+
* @param rootContext must not be {@literal null}.
4646
* @return the {@link List} of {@link DBObject}.
4747
*/
4848
static List<DBObject> toDBObject(List<AggregationOperation> operations, AggregationOperationContext rootContext) {
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
/**
22+
* A simple collection of grouped test entities used throughout the test suite.
23+
*
24+
* @author Christoph Strobl
25+
*/
26+
public class TestEntities {
27+
28+
private static final GeoEntities GEO = new GeoEntities();
29+
30+
public static GeoEntities geolocation() {
31+
return GEO;
32+
}
33+
34+
public static class GeoEntities {
35+
36+
/**
37+
* <pre>
38+
* X: -73.99408
39+
* Y: 40.75057
40+
* </pre>
41+
*
42+
* @return new {@link Venue}
43+
*/
44+
public Venue pennStation() {
45+
return new Venue("Penn Station", -73.99408, 40.75057);
46+
}
47+
48+
/**
49+
* <pre>
50+
* X: -73.99171
51+
* Y: 40.738868
52+
* </pre>
53+
*
54+
* @return new {@link Venue}
55+
*/
56+
57+
public Venue tenGenOffice() {
58+
return new Venue("10gen Office", -73.99171, 40.738868);
59+
}
60+
61+
/**
62+
* <pre>
63+
* X: -73.988135
64+
* Y: 40.741404
65+
* </pre>
66+
*
67+
* @return new {@link Venue}
68+
*/
69+
public Venue flatironBuilding() {
70+
return new Venue("Flatiron Building", -73.988135, 40.741404);
71+
}
72+
73+
/**
74+
* <pre>
75+
* X: -74.2713
76+
* Y: 40.73137
77+
* </pre>
78+
*
79+
* @return new {@link Venue}
80+
*/
81+
public Venue maplewoodNJ() {
82+
return new Venue("Maplewood, NJ", -74.2713, 40.73137);
83+
}
84+
85+
public List<Venue> newYork() {
86+
87+
List<Venue> venues = new ArrayList<>();
88+
89+
venues.add(pennStation());
90+
venues.add(tenGenOffice());
91+
venues.add(flatironBuilding());
92+
venues.add(new Venue("Players Club", -73.997812, 40.739128));
93+
venues.add(new Venue("City Bakery ", -73.992491, 40.738673));
94+
venues.add(new Venue("Splash Bar", -73.992491, 40.738673));
95+
venues.add(new Venue("Momofuku Milk Bar", -73.985839, 40.731698));
96+
venues.add(new Venue("Shake Shack", -73.98820, 40.74164));
97+
venues.add(new Venue("Penn Station", -73.99408, 40.75057));
98+
venues.add(new Venue("Empire State Building", -73.98602, 40.74894));
99+
venues.add(new Venue("Ulaanbaatar, Mongolia", 106.9154, 47.9245));
100+
venues.add(maplewoodNJ());
101+
102+
return venues;
103+
}
104+
}
105+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@
5252
import org.springframework.dao.DataAccessException;
5353
import org.springframework.data.annotation.Id;
5454
import org.springframework.data.domain.Sort.Direction;
55+
import org.springframework.data.geo.Box;
5556
import org.springframework.data.geo.Metrics;
57+
import org.springframework.data.geo.Point;
5658
import org.springframework.data.mapping.model.MappingException;
5759
import org.springframework.data.mongodb.core.CollectionCallback;
5860
import org.springframework.data.mongodb.core.MongoTemplate;
61+
import org.springframework.data.mongodb.core.TestEntities;
5962
import org.springframework.data.mongodb.core.Venue;
6063
import org.springframework.data.mongodb.core.aggregation.AggregationTests.CarDescriptor.Entry;
6164
import org.springframework.data.mongodb.core.aggregation.BucketAutoOperation.Granularities;
@@ -147,6 +150,8 @@ private void cleanDb() {
147150
mongoTemplate.dropCollection(Sales2.class);
148151
mongoTemplate.dropCollection(Employee.class);
149152
mongoTemplate.dropCollection(Art.class);
153+
mongoTemplate.dropCollection("personQueryTemp");
154+
mongoTemplate.dropCollection(Venue.class);
150155
}
151156

152157
/**
@@ -1383,9 +1388,8 @@ public void shouldRetrieveDateTimeFragementsCorrectly() throws Exception {
13831388
@Test // DATAMONGO-1127
13841389
public void shouldSupportGeoNearQueriesForAggregationWithDistanceField() {
13851390

1386-
mongoTemplate.insert(new Venue("Penn Station", -73.99408, 40.75057));
1387-
mongoTemplate.insert(new Venue("10gen Office", -73.99171, 40.738868));
1388-
mongoTemplate.insert(new Venue("Flatiron Building", -73.988135, 40.741404));
1391+
mongoTemplate.insertAll(Arrays.asList(TestEntities.geolocation().pennStation(),
1392+
TestEntities.geolocation().tenGenOffice(), TestEntities.geolocation().flatironBuilding()));
13891393

13901394
mongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location"));
13911395

@@ -1727,6 +1731,36 @@ public void facetShouldCreateFacets() {
17271731
assertThat(categorizeByYear, hasSize(3));
17281732
}
17291733

1734+
@Test // DATAMONGO-1986
1735+
public void runMatchOperationCriteriaThroughQueryMapperForTypedAggregation() {
1736+
1737+
mongoTemplate.insertAll(TestEntities.geolocation().newYork());
1738+
1739+
Aggregation aggregation = newAggregation(Venue.class,
1740+
match(Criteria.where("location")
1741+
.within(new Box(new Point(-73.99756, 40.73083), new Point(-73.988135, 40.741404)))),
1742+
project("id", "location", "name"));
1743+
1744+
AggregationResults<Document> groupResults = mongoTemplate.aggregate(aggregation, "newyork", Document.class);
1745+
1746+
assertThat(groupResults.getMappedResults().size(), is(4));
1747+
}
1748+
1749+
@Test // DATAMONGO-1986
1750+
public void runMatchOperationCriteriaThroughQueryMapperForUntypedAggregation() {
1751+
1752+
mongoTemplate.insertAll(TestEntities.geolocation().newYork());
1753+
1754+
Aggregation aggregation = newAggregation(
1755+
match(Criteria.where("location")
1756+
.within(new Box(new Point(-73.99756, 40.73083), new Point(-73.988135, 40.741404)))),
1757+
project("id", "location", "name"));
1758+
1759+
AggregationResults<Document> groupResults = mongoTemplate.aggregate(aggregation, "newyork", Document.class);
1760+
1761+
assertThat(groupResults.getMappedResults().size(), is(4));
1762+
}
1763+
17301764
private void createUsersWithReferencedPersons() {
17311765

17321766
mongoTemplate.dropCollection(User.class);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
import org.springframework.data.geo.Polygon;
3939
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
4040
import org.springframework.data.mongodb.core.MongoTemplate;
41+
import org.springframework.data.mongodb.core.TestEntities;
4142
import org.springframework.data.mongodb.core.Venue;
4243
import org.springframework.data.mongodb.core.query.NearQuery;
4344
import org.springframework.data.mongodb.core.query.Query;
4445
import org.springframework.test.context.ContextConfiguration;
4546
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4647

47-
import com.mongodb.Mongo;
4848
import com.mongodb.MongoClient;
4949
import com.mongodb.WriteConcern;
5050

@@ -103,19 +103,7 @@ protected void removeVenues() {
103103
}
104104

105105
protected void addVenues() {
106-
107-
template.insert(new Venue("Penn Station", -73.99408, 40.75057));
108-
template.insert(new Venue("10gen Office", -73.99171, 40.738868));
109-
template.insert(new Venue("Flatiron Building", -73.988135, 40.741404));
110-
template.insert(new Venue("Players Club", -73.997812, 40.739128));
111-
template.insert(new Venue("City Bakery ", -73.992491, 40.738673));
112-
template.insert(new Venue("Splash Bar", -73.992491, 40.738673));
113-
template.insert(new Venue("Momofuku Milk Bar", -73.985839, 40.731698));
114-
template.insert(new Venue("Shake Shack", -73.98820, 40.74164));
115-
template.insert(new Venue("Penn Station", -73.99408, 40.75057));
116-
template.insert(new Venue("Empire State Building", -73.98602, 40.74894));
117-
template.insert(new Venue("Ulaanbaatar, Mongolia", 106.9154, 47.9245));
118-
template.insert(new Venue("Maplewood, NJ", -74.2713, 40.73137));
106+
template.insertAll(TestEntities.geolocation().newYork());
119107
}
120108

121109
@Test

0 commit comments

Comments
 (0)