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 old mode 100644 new mode 100755 index 0ae99b89d..7613171d8 --- 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 @@ -15,12 +15,10 @@ */ package org.springframework.data.mongodb.core.aggregation; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; import static org.springframework.data.mongodb.core.DocumentTestUtils.*; import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; import static org.springframework.data.mongodb.core.query.Criteria.*; -import static org.springframework.data.mongodb.test.util.IsBsonObject.*; +import static org.springframework.data.mongodb.test.util.Assertions.*; import java.util.ArrayList; import java.util.Arrays; @@ -118,10 +116,9 @@ public class AggregationUnitTests { @SuppressWarnings("unchecked") Document unwind = ((List) agg.get("pipeline")).get(0); - assertThat((Document) unwind.get("$unwind"), - isBsonObject(). // - containing("includeArrayIndex", "x").// - containing("preserveNullAndEmptyArrays", true)); + assertThat(unwind.get("$unwind", Document.class)). // + containsEntry("includeArrayIndex", "x"). // + containsEntry("preserveNullAndEmptyArrays", true); } @Test // DATAMONGO-1391 @@ -132,8 +129,9 @@ public class AggregationUnitTests { @SuppressWarnings("unchecked") Document unwind = ((List) agg.get("pipeline")).get(0); - assertThat(unwind, - isBsonObject().notContaining("includeArrayIndex").containing("preserveNullAndEmptyArrays", true)); + assertThat(unwind) // + .doesNotContainKey("$unwind.includeArrayIndex") // + .containsEntry("$unwind.preserveNullAndEmptyArrays", true); } @Test // DATAMONGO-1550 @@ -145,7 +143,7 @@ public class AggregationUnitTests { @SuppressWarnings("unchecked") Document unwind = ((List) agg.get("pipeline")).get(0); - assertThat(unwind, isBsonObject().containing("$replaceRoot.newRoot", new Document("field", "value"))); + assertThat(unwind).containsEntry("$replaceRoot.newRoot", new Document("field", "value")); } @Test // DATAMONGO-753 @@ -170,8 +168,8 @@ public class AggregationUnitTests { @SuppressWarnings("unchecked") Document secondProjection = ((List) agg.get("pipeline")).get(2); Document fields = getAsDocument(secondProjection, "$project"); - assertThat(fields.get("aCnt"), is((Object) 1)); - assertThat(fields.get("a"), is((Object) "$_id.a")); + assertThat(fields.get("aCnt")).isEqualTo(1); + assertThat(fields.get("a")).isEqualTo("$_id.a"); } @Test // DATAMONGO-791 @@ -187,14 +185,14 @@ public class AggregationUnitTests { @SuppressWarnings("unchecked") Document secondProjection = ((List) agg.get("pipeline")).get(2); Document fields = getAsDocument(secondProjection, "$project"); - assertThat(fields.get("aCnt"), is((Object) 1)); - assertThat(fields.get("a"), is((Object) "$_id.a")); + assertThat(fields.get("aCnt")).isEqualTo(1); + assertThat(fields.get("a")).isEqualTo("$_id.a"); } @Test // DATAMONGO-791 public void allowTypedAggregationOperationsToBePassedAsIterable() { - List ops = new ArrayList(); + List ops = new ArrayList<>(); ops.add(project("a")); ops.add(group("a").count().as("aCnt")); ops.add(project("aCnt", "a")); @@ -204,8 +202,8 @@ public class AggregationUnitTests { @SuppressWarnings("unchecked") Document secondProjection = ((List) agg.get("pipeline")).get(2); Document fields = getAsDocument(secondProjection, "$project"); - assertThat(fields.get("aCnt"), is((Object) 1)); - assertThat(fields.get("a"), is((Object) "$_id.a")); + assertThat(fields.get("aCnt")).isEqualTo((Object) 1); + assertThat(fields.get("a")).isEqualTo((Object) "$_id.a"); } @Test // DATAMONGO-838 @@ -219,7 +217,7 @@ public class AggregationUnitTests { @SuppressWarnings("unchecked") Document secondProjection = ((List) agg.get("pipeline")).get(1); Document fields = getAsDocument(secondProjection, "$group"); - assertThat(fields.get("foosum"), is((Object) new Document("$sum", "$foo"))); + assertThat(fields.get("foosum")).isEqualTo(new Document("$sum", "$foo")); } @Test // DATAMONGO-908 @@ -231,13 +229,13 @@ public class AggregationUnitTests { group("cmsParameterId", "rules.ruleType").count().as("totol") // ).toDocument("foo", Aggregation.DEFAULT_CONTEXT); - assertThat(agg, is(notNullValue())); + assertThat(agg).isNotNull(); Document group = ((List) agg.get("pipeline")).get(2); Document fields = getAsDocument(group, "$group"); Document id = getAsDocument(fields, "_id"); - assertThat(id.get("ruleType"), is((Object) "$rules.ruleType")); + assertThat(id.get("ruleType")).isEqualTo("$rules.ruleType"); } @Test // DATAMONGO-1585 @@ -248,11 +246,12 @@ public class AggregationUnitTests { sort(Direction.ASC, "cmsParameterId", "titles") // ).toDocument("foo", Aggregation.DEFAULT_CONTEXT); - assertThat(agg, is(notNullValue())); + assertThat(agg).isNotNull(); Document sort = ((List) agg.get("pipeline")).get(1); - assertThat(getAsDocument(sort, "$sort"), is(Document.parse("{ \"_id.cmsParameterId\" : 1 , \"titles\" : 1}"))); + assertThat(getAsDocument(sort, "$sort")) + .isEqualTo(Document.parse("{ \"_id.cmsParameterId\" : 1 , \"titles\" : 1}")); } @Test // DATAMONGO-1585 @@ -265,14 +264,13 @@ public class AggregationUnitTests { sort(Direction.ASC, "cmsParameterId", "titles", "alias") // ).toDocument("foo", Aggregation.DEFAULT_CONTEXT); - assertThat(agg, is(notNullValue())); + assertThat(agg).isNotNull(); Document sort = ((List) agg.get("pipeline")).get(1); - assertThat(getAsDocument(sort, "$sort"), - isBsonObject().containing("cmsParameterId", 1) // - .containing("titles", 1) // - .containing("alias", 1)); + assertThat(getAsDocument(sort, "$sort")).containsEntry("cmsParameterId", 1) // + .containsEntry("titles", 1) // + .containsEntry("alias", 1); } @Test // DATAMONGO-924 @@ -284,10 +282,10 @@ public class AggregationUnitTests { ).toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document projection0 = extractPipelineElement(agg, 0, "$project"); - assertThat(projection0, is((Document) new Document("ba", "$foo.bar"))); + assertThat(projection0).isEqualTo(new Document("ba", "$foo.bar")); Document projection1 = extractPipelineElement(agg, 1, "$project"); - assertThat(projection1, is((Document) new Document("b", "$ba"))); + assertThat(projection1).isEqualTo(new Document("b", "$ba")); } @Test // DATAMONGO-960 @@ -297,8 +295,8 @@ public class AggregationUnitTests { project().and("a").as("aa") // ).toDocument("foo", Aggregation.DEFAULT_CONTEXT); - assertThat(agg, - is(Document.parse("{ \"aggregate\" : \"foo\" , \"pipeline\" : [ { \"$project\" : { \"aa\" : \"$a\"}}]}"))); + assertThat(agg).isEqualTo( + Document.parse("{ \"aggregate\" : \"foo\" , \"pipeline\" : [ { \"$project\" : { \"aa\" : \"$a\"}}]}")); } @Test // DATAMONGO-960 @@ -313,13 +311,12 @@ public class AggregationUnitTests { .withOptions(aggregationOptions) // .toDocument("foo", Aggregation.DEFAULT_CONTEXT); - assertThat(agg, - is(Document.parse("{ \"aggregate\" : \"foo\" , " // - + "\"pipeline\" : [ { \"$project\" : { \"aa\" : \"$a\"}}] , " // - + "\"allowDiskUse\" : true , " // - + "\"explain\" : true , " // - + "\"cursor\" : { \"foo\" : 1}}") // - )); + assertThat(agg).isEqualTo(Document.parse("{ \"aggregate\" : \"foo\" , " // + + "\"pipeline\" : [ { \"$project\" : { \"aa\" : \"$a\"}}] , " // + + "\"allowDiskUse\" : true , " // + + "\"explain\" : true , " // + + "\"cursor\" : { \"foo\" : 1}}") // + ); } @Test // DATAMONGO-954, DATAMONGO-1585 @@ -334,13 +331,13 @@ public class AggregationUnitTests { ).toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document projection0 = extractPipelineElement(agg, 0, "$project"); - assertThat(projection0, is((Document) new Document("someKey", 1).append("a1", "$a").append("a2", "$$CURRENT.a"))); + assertThat(projection0).isEqualTo(new Document("someKey", 1).append("a1", "$a").append("a2", "$$CURRENT.a")); Document sort = extractPipelineElement(agg, 1, "$sort"); - assertThat(sort, is((Document) new Document("a1", -1))); + assertThat(sort).isEqualTo(new Document("a1", -1)); Document group = extractPipelineElement(agg, 2, "$group"); - assertThat(group, is((Document) new Document("_id", "$someKey").append("doc", new Document("$first", "$$ROOT")))); + assertThat(group).isEqualTo(new Document("_id", "$someKey").append("doc", new Document("$first", "$$ROOT"))); } @Test // DATAMONGO-1254 @@ -354,7 +351,7 @@ public class AggregationUnitTests { ).toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document group = extractPipelineElement(agg, 1, "$group"); - assertThat(getAsDocument(group, "count"), is(new Document().append("$sum", "$tags_count"))); + assertThat(getAsDocument(group, "count")).isEqualTo(new Document().append("$sum", "$tags_count")); } @Test // DATAMONGO-1254 @@ -368,7 +365,7 @@ public class AggregationUnitTests { ).toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document group = extractPipelineElement(agg, 1, "$group"); - assertThat(getAsDocument(group, "count"), is(new Document().append("$sum", "$tags_count"))); + assertThat(getAsDocument(group, "count")).isEqualTo(new Document().append("$sum", "$tags_count")); } @Test // DATAMONGO-861 @@ -384,9 +381,9 @@ public class AggregationUnitTests { @SuppressWarnings("unchecked") Document secondProjection = ((List) agg.get("pipeline")).get(1); Document fields = getAsDocument(secondProjection, "$group"); - assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first")); - assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first.$cond.then", "$answer")); - assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first.$cond.else", "no-answer")); + assertThat(getAsDocument(fields, "foosum")).containsKey("$first"); + assertThat(getAsDocument(fields, "foosum")).containsEntry("$first.$cond.then", "$answer"); + assertThat(getAsDocument(fields, "foosum")).containsEntry("$first.$cond.else", "no-answer"); } @Test // DATAMONGO-861 @@ -405,18 +402,17 @@ public class AggregationUnitTests { .append("then", "bright") // .append("else", "dark"); - assertThat(getAsDocument(project, "color"), isBsonObject().containing("$cond", expectedCondition)); + assertThat(getAsDocument(project, "color")).containsEntry("$cond", expectedCondition); } @Test // DATAMONGO-861 public void shouldRenderProjectionConditionalCorrectly() { Document agg = Aggregation.newAggregation(// - project().and("color") - .applyCondition(ConditionalOperators.Cond.newBuilder() // - .when("isYellow") // - .then("bright") // - .otherwise("dark"))) + project().and("color").applyCondition(ConditionalOperators.Cond.newBuilder() // + .when("isYellow") // + .then("bright") // + .otherwise("dark"))) .toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document project = extractPipelineElement(agg, 0, "$project"); @@ -425,17 +421,16 @@ public class AggregationUnitTests { .append("then", "bright") // .append("else", "dark"); - assertThat(getAsDocument(project, "color"), isBsonObject().containing("$cond", expectedCondition)); + assertThat(getAsDocument(project, "color")).containsEntry("$cond", expectedCondition); } @Test // DATAMONGO-861 public void shouldRenderProjectionConditionalWithCriteriaCorrectly() { - Document agg = Aggregation - .newAggregation(project()// - .and("color")// - .applyCondition(ConditionalOperators.Cond.newBuilder().when(Criteria.where("key").gt(5)) // - .then("bright").otherwise("dark"))) // + Document agg = Aggregation.newAggregation(project()// + .and("color")// + .applyCondition(ConditionalOperators.Cond.newBuilder().when(Criteria.where("key").gt(5)) // + .then("bright").otherwise("dark"))) // .toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document project = extractPipelineElement(agg, 0, "$project"); @@ -444,20 +439,18 @@ public class AggregationUnitTests { .append("then", "bright") // .append("else", "dark"); - assertThat(getAsDocument(project, "color"), isBsonObject().containing("$cond", expectedCondition)); + assertThat(getAsDocument(project, "color")).containsEntry("$cond", expectedCondition); } @Test // DATAMONGO-861 public void referencingProjectionAliasesShouldRenderProjectionConditionalWithFieldReferenceCorrectly() { - Document agg = Aggregation - .newAggregation(// - project().and("color").as("chroma"), - project().and("luminosity") // - .applyCondition(ConditionalOperators // - .when("chroma") // - .thenValueOf("bright") // - .otherwise("dark"))) // + Document agg = Aggregation.newAggregation(// + project().and("color").as("chroma"), project().and("luminosity") // + .applyCondition(ConditionalOperators // + .when("chroma") // + .thenValueOf("bright") // + .otherwise("dark"))) // .toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document project = extractPipelineElement(agg, 1, "$project"); @@ -466,20 +459,17 @@ public class AggregationUnitTests { .append("then", "bright") // .append("else", "dark"); - assertThat(getAsDocument(project, "luminosity"), isBsonObject().containing("$cond", expectedCondition)); + assertThat(getAsDocument(project, "luminosity")).containsEntry("$cond", expectedCondition); } @Test // DATAMONGO-861 public void referencingProjectionAliasesShouldRenderProjectionConditionalWithCriteriaReferenceCorrectly() { - Document agg = Aggregation - .newAggregation(// - project().and("color").as("chroma"), - project().and("luminosity") // - .applyCondition(ConditionalOperators.Cond.newBuilder() - .when(Criteria.where("chroma") // - .is(100)) // - .then("bright").otherwise("dark"))) // + Document agg = Aggregation.newAggregation(// + project().and("color").as("chroma"), project().and("luminosity") // + .applyCondition(ConditionalOperators.Cond.newBuilder().when(Criteria.where("chroma") // + .is(100)) // + .then("bright").otherwise("dark"))) // .toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document project = extractPipelineElement(agg, 1, "$project"); @@ -488,56 +478,50 @@ public class AggregationUnitTests { .append("then", "bright") // .append("else", "dark"); - assertThat(getAsDocument(project, "luminosity"), isBsonObject().containing("$cond", expectedCondition)); + assertThat(getAsDocument(project, "luminosity")).containsEntry("$cond", expectedCondition); } @Test // DATAMONGO-861 public void shouldRenderProjectionIfNullWithFieldReferenceCorrectly() { - Document agg = Aggregation - .newAggregation(// - project().and("color"), // - project().and("luminosity") // - .applyCondition(ConditionalOperators // - .ifNull("chroma") // - .then("unknown"))) // + Document agg = Aggregation.newAggregation(// + project().and("color"), // + project().and("luminosity") // + .applyCondition(ConditionalOperators // + .ifNull("chroma") // + .then("unknown"))) // .toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document project = extractPipelineElement(agg, 1, "$project"); - assertThat(getAsDocument(project, "luminosity"), - isBsonObject().containing("$ifNull", Arrays. asList("$chroma", "unknown"))); + assertThat(getAsDocument(project, "luminosity")).containsEntry("$ifNull", Arrays.asList("$chroma", "unknown")); } @Test // DATAMONGO-861 public void shouldRenderProjectionIfNullWithFallbackFieldReferenceCorrectly() { - Document agg = Aggregation - .newAggregation(// - project("fallback").and("color").as("chroma"), - project().and("luminosity") // - .applyCondition(ConditionalOperators.ifNull("chroma") // - .thenValueOf("fallback"))) // + Document agg = Aggregation.newAggregation(// + project("fallback").and("color").as("chroma"), project().and("luminosity") // + .applyCondition(ConditionalOperators.ifNull("chroma") // + .thenValueOf("fallback"))) // .toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document project = extractPipelineElement(agg, 1, "$project"); - assertThat(getAsDocument(project, "luminosity"), - isBsonObject().containing("$ifNull", Arrays.asList("$chroma", "$fallback"))); + assertThat(getAsDocument(project, "luminosity")).containsEntry("$ifNull", Arrays.asList("$chroma", "$fallback")); } @Test // DATAMONGO-1552 public void shouldHonorDefaultCountField() { - Document agg = Aggregation - .newAggregation(// - bucket("year"), // - project("count")) // + Document agg = Aggregation.newAggregation(// + bucket("year"), // + project("count")) // .toDocument("foo", Aggregation.DEFAULT_CONTEXT); Document project = extractPipelineElement(agg, 1, "$project"); - assertThat(project, isBsonObject().containing("count", 1)); + assertThat(project).containsEntry("count", 1); } @Test // DATAMONGO-1533 @@ -551,11 +535,11 @@ public class AggregationUnitTests { @SuppressWarnings("unchecked") Document secondProjection = ((List) agg.get("pipeline")).get(1); Document fields = getAsDocument(secondProjection, "$group"); - assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first")); - assertThat(getAsDocument(fields, "foosum"), - isBsonObject().containing("$first.$cond.if", new Document("$gte", new Document("$a", 42)))); - assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first.$cond.then", "answer")); - assertThat(getAsDocument(fields, "foosum"), isBsonObject().containing("$first.$cond.else", "no-answer")); + assertThat(getAsDocument(fields, "foosum")).containsKey("$first"); + assertThat(getAsDocument(fields, "foosum")).containsEntry("$first.$cond.if", + new Document("$gte", Arrays.asList("$a", 42))); + assertThat(getAsDocument(fields, "foosum")).containsEntry("$first.$cond.then", "answer"); + assertThat(getAsDocument(fields, "foosum")).containsEntry("$first.$cond.else", "no-answer"); } @Test // DATAMONGO-1756 @@ -564,8 +548,8 @@ public class AggregationUnitTests { Document agg = newAggregation(project().and("value1.value").plus("value2.value").as("val")).toDocument("collection", Aggregation.DEFAULT_CONTEXT); - assertThat(extractPipelineElement(agg, 0, "$project"), - is(equalTo(new Document("val", new Document("$add", Arrays.asList("$value1.value", "$value2.value")))))); + Document expected = new Document("val", new Document("$add", Arrays.asList("$value1.value", "$value2.value"))); + assertThat(extractPipelineElement(agg, 0, "$project")).isEqualTo(expected); } @Test // DATAMONGO-1871 @@ -582,7 +566,7 @@ public class AggregationUnitTests { Document $project = extractPipelineElement(agg.toDocument("collection-1", Aggregation.DEFAULT_CONTEXT), 0, "$project"); - assertThat($project.containsKey("plts.ests"), is(true)); + assertThat($project.containsKey("plts.ests")).isTrue(); } private Document extractPipelineElement(Document agg, int index, String operation) {