From f3397e95bc72ea353fe6c9d89d148c1c1228e96a Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 24 May 2018 09:40:50 +0200 Subject: [PATCH] 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. --- .../AggregationOperationRenderer.java | 2 +- .../data/mongodb/core/TestEntities.java | 105 ++++++++++++++++++ .../core/aggregation/AggregationTests.java | 40 ++++++- .../core/geo/AbstractGeoSpatialTests.java | 16 +-- 4 files changed, 145 insertions(+), 18 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/TestEntities.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationRenderer.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationRenderer.java index 1a6f6d668..811925734 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationRenderer.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationRenderer.java @@ -42,7 +42,7 @@ class AggregationOperationRenderer { * {@link DBObject} representation. * * @param operations must not be {@literal null}. - * @param context must not be {@literal null}. + * @param rootContext must not be {@literal null}. * @return the {@link List} of {@link DBObject}. */ static List toDBObject(List operations, AggregationOperationContext rootContext) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/TestEntities.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/TestEntities.java new file mode 100644 index 000000000..77933cae3 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/TestEntities.java @@ -0,0 +1,105 @@ +/* + * Copyright 2018 the original author or authors. + * + * 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 org.springframework.data.mongodb.core; + +import java.util.ArrayList; +import java.util.List; + +/** + * A simple collection of grouped test entities used throughout the test suite. + * + * @author Christoph Strobl + */ +public class TestEntities { + + private static final GeoEntities GEO = new GeoEntities(); + + public static GeoEntities geolocation() { + return GEO; + } + + public static class GeoEntities { + + /** + *
+		 * X: -73.99408
+		 * Y: 40.75057
+		 * 
+ * + * @return new {@link Venue} + */ + public Venue pennStation() { + return new Venue("Penn Station", -73.99408, 40.75057); + } + + /** + *
+		 * X: -73.99171
+		 * Y: 40.738868
+		 * 
+ * + * @return new {@link Venue} + */ + + public Venue tenGenOffice() { + return new Venue("10gen Office", -73.99171, 40.738868); + } + + /** + *
+		 * X: -73.988135
+		 * Y: 40.741404
+		 * 
+ * + * @return new {@link Venue} + */ + public Venue flatironBuilding() { + return new Venue("Flatiron Building", -73.988135, 40.741404); + } + + /** + *
+		 * X: -74.2713
+		 * Y: 40.73137
+		 * 
+ * + * @return new {@link Venue} + */ + public Venue maplewoodNJ() { + return new Venue("Maplewood, NJ", -74.2713, 40.73137); + } + + public List newYork() { + + List venues = new ArrayList<>(); + + venues.add(pennStation()); + venues.add(tenGenOffice()); + venues.add(flatironBuilding()); + venues.add(new Venue("Players Club", -73.997812, 40.739128)); + venues.add(new Venue("City Bakery ", -73.992491, 40.738673)); + venues.add(new Venue("Splash Bar", -73.992491, 40.738673)); + venues.add(new Venue("Momofuku Milk Bar", -73.985839, 40.731698)); + venues.add(new Venue("Shake Shack", -73.98820, 40.74164)); + venues.add(new Venue("Penn Station", -73.99408, 40.75057)); + venues.add(new Venue("Empire State Building", -73.98602, 40.74894)); + venues.add(new Venue("Ulaanbaatar, Mongolia", 106.9154, 47.9245)); + venues.add(maplewoodNJ()); + + return venues; + } + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java index 6190fbdb0..e2c937be7 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java @@ -52,10 +52,13 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.dao.DataAccessException; import org.springframework.data.annotation.Id; import org.springframework.data.domain.Sort.Direction; +import org.springframework.data.geo.Box; import org.springframework.data.geo.Metrics; +import org.springframework.data.geo.Point; import org.springframework.data.mapping.model.MappingException; import org.springframework.data.mongodb.core.CollectionCallback; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.TestEntities; import org.springframework.data.mongodb.core.Venue; import org.springframework.data.mongodb.core.aggregation.AggregationTests.CarDescriptor.Entry; import org.springframework.data.mongodb.core.aggregation.BucketAutoOperation.Granularities; @@ -147,6 +150,8 @@ public class AggregationTests { mongoTemplate.dropCollection(Sales2.class); mongoTemplate.dropCollection(Employee.class); mongoTemplate.dropCollection(Art.class); + mongoTemplate.dropCollection("personQueryTemp"); + mongoTemplate.dropCollection(Venue.class); } /** @@ -1383,9 +1388,8 @@ public class AggregationTests { @Test // DATAMONGO-1127 public void shouldSupportGeoNearQueriesForAggregationWithDistanceField() { - mongoTemplate.insert(new Venue("Penn Station", -73.99408, 40.75057)); - mongoTemplate.insert(new Venue("10gen Office", -73.99171, 40.738868)); - mongoTemplate.insert(new Venue("Flatiron Building", -73.988135, 40.741404)); + mongoTemplate.insertAll(Arrays.asList(TestEntities.geolocation().pennStation(), + TestEntities.geolocation().tenGenOffice(), TestEntities.geolocation().flatironBuilding())); mongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location")); @@ -1727,6 +1731,36 @@ public class AggregationTests { assertThat(categorizeByYear, hasSize(3)); } + @Test // DATAMONGO-1986 + public void runMatchOperationCriteriaThroughQueryMapperForTypedAggregation() { + + mongoTemplate.insertAll(TestEntities.geolocation().newYork()); + + Aggregation aggregation = newAggregation(Venue.class, + match(Criteria.where("location") + .within(new Box(new Point(-73.99756, 40.73083), new Point(-73.988135, 40.741404)))), + project("id", "location", "name")); + + AggregationResults groupResults = mongoTemplate.aggregate(aggregation, "newyork", Document.class); + + assertThat(groupResults.getMappedResults().size(), is(4)); + } + + @Test // DATAMONGO-1986 + public void runMatchOperationCriteriaThroughQueryMapperForUntypedAggregation() { + + mongoTemplate.insertAll(TestEntities.geolocation().newYork()); + + Aggregation aggregation = newAggregation( + match(Criteria.where("location") + .within(new Box(new Point(-73.99756, 40.73083), new Point(-73.988135, 40.741404)))), + project("id", "location", "name")); + + AggregationResults groupResults = mongoTemplate.aggregate(aggregation, "newyork", Document.class); + + assertThat(groupResults.getMappedResults().size(), is(4)); + } + private void createUsersWithReferencedPersons() { mongoTemplate.dropCollection(User.class); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java index 59ef1c485..20f44d2dd 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java @@ -38,13 +38,13 @@ import org.springframework.data.geo.Point; import org.springframework.data.geo.Polygon; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.TestEntities; import org.springframework.data.mongodb.core.Venue; import org.springframework.data.mongodb.core.query.NearQuery; import org.springframework.data.mongodb.core.query.Query; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.mongodb.Mongo; import com.mongodb.MongoClient; import com.mongodb.WriteConcern; @@ -103,19 +103,7 @@ public abstract class AbstractGeoSpatialTests { } protected void addVenues() { - - template.insert(new Venue("Penn Station", -73.99408, 40.75057)); - template.insert(new Venue("10gen Office", -73.99171, 40.738868)); - template.insert(new Venue("Flatiron Building", -73.988135, 40.741404)); - template.insert(new Venue("Players Club", -73.997812, 40.739128)); - template.insert(new Venue("City Bakery ", -73.992491, 40.738673)); - template.insert(new Venue("Splash Bar", -73.992491, 40.738673)); - template.insert(new Venue("Momofuku Milk Bar", -73.985839, 40.731698)); - template.insert(new Venue("Shake Shack", -73.98820, 40.74164)); - template.insert(new Venue("Penn Station", -73.99408, 40.75057)); - template.insert(new Venue("Empire State Building", -73.98602, 40.74894)); - template.insert(new Venue("Ulaanbaatar, Mongolia", 106.9154, 47.9245)); - template.insert(new Venue("Maplewood, NJ", -74.2713, 40.73137)); + template.insertAll(TestEntities.geolocation().newYork()); } @Test