From 3ff0975ab91b19b5816c1032841cdf9218893950 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 9 Jul 2018 19:38:13 +0200 Subject: [PATCH] DATAMONGO-2023 - Polishing. Add tests verifying the behavior when using both typed and untyped aggregation. Original Pull Request: #585 --- .../core/aggregation/AggregationUnitTests.java | 18 +++++++++++++++++- ...edAggregationOperationContextUnitTests.java | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java index d56bb9c20..0f563b05b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2017 the original author or authors. + * Copyright 2013-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. @@ -591,6 +591,22 @@ public class AggregationUnitTests { assertThat($project.containsField("plts.ests"), is(true)); } + @Test // DATAMONGO-2023 + public void mapsNativeKeywordsCorrectly() { + + Aggregation agg = Aggregation.newAggregation(new AggregationOperation() { + @Override + public DBObject toDBObject(AggregationOperationContext context) { + return new BasicDBObject("$sample", new BasicDBObject("size", "my-custom-value")); + } + }); + + DBObject $sample = extractPipelineElement(agg.toDbObject("collection-1", Aggregation.DEFAULT_CONTEXT), 0, + "$sample"); + + assertThat($sample, is(equalTo(BasicDBObjectBuilder.start("size", "my-custom-value").get()))); + } + private DBObject extractPipelineElement(DBObject agg, int index, String operation) { List pipeline = (List) agg.get("pipeline"); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java index c01f8052f..358f11a17 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java @@ -52,6 +52,7 @@ import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.test.util.BasicDbListBuilder; import com.mongodb.BasicDBObject; +import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DBObject; import com.mongodb.util.JSON; @@ -367,6 +368,23 @@ public class TypeBasedAggregationOperationContextUnitTests { new BasicDbListBuilder().add("$nested1.value1").add("$field2.nestedValue2").get()))))); } + @Test // DATAMONGO-2023 + public void mapsNativeKeywordsForTypedAggregationCorrectly() { + + AggregationOperationContext context = getContext(FooPerson.class); + + DBObject agg = Aggregation.newAggregation(new AggregationOperation() { + + @Override + public DBObject toDBObject(AggregationOperationContext context) { + return new BasicDBObject("$sample", new BasicDBObject("name", "foo")); + } + }).toDbObject("collection", context); + + BasicDBObject $sample = (BasicDBObject) getPipelineElementFromAggregationAt(agg, 0).get("$sample"); + assertThat($sample, is(equalTo(BasicDBObjectBuilder.start("name", "foo").get()))); + } + @Document(collection = "person") @AllArgsConstructor public static class FooPerson {