Browse Source

DATAMONGO-2442 - Fix thenValueOf in $cond builder.

We now create a field reference when calling the builder instead of using the value as is.

Original pull request: #818.
pull/819/head
Christoph Strobl 6 years ago committed by Mark Paluch
parent
commit
4a45928aee
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ConditionalOperators.java
  2. 5
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java
  3. 16
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/CondExpressionUnitTests.java

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

@ -207,7 +207,7 @@ public class ConditionalOperators {
public OtherwiseBuilder thenValueOf(String fieldReference) { public OtherwiseBuilder thenValueOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!"); Assert.notNull(fieldReference, "FieldReference must not be null!");
return createThenBuilder().then(fieldReference); return createThenBuilder().thenValueOf(fieldReference);
} }
private ThenBuilder createThenBuilder() { private ThenBuilder createThenBuilder() {

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

@ -27,7 +27,6 @@ import java.util.List;
import org.bson.Document; import org.bson.Document;
import org.junit.Test; import org.junit.Test;
import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond; import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
@ -438,14 +437,14 @@ public class AggregationUnitTests {
assertThat(getAsDocument(project, "color")).containsEntry("$cond", expectedCondition); assertThat(getAsDocument(project, "color")).containsEntry("$cond", expectedCondition);
} }
@Test // DATAMONGO-861 @Test // DATAMONGO-861, DATAMONGO-2242
public void referencingProjectionAliasesShouldRenderProjectionConditionalWithFieldReferenceCorrectly() { public void referencingProjectionAliasesShouldRenderProjectionConditionalWithFieldReferenceCorrectly() {
Document agg = Aggregation.newAggregation(// Document agg = Aggregation.newAggregation(//
project().and("color").as("chroma"), project().and("luminosity") // project().and("color").as("chroma"), project().and("luminosity") //
.applyCondition(ConditionalOperators // .applyCondition(ConditionalOperators //
.when("chroma") // .when("chroma") //
.thenValueOf("bright") // .then("bright") //
.otherwise("dark"))) // .otherwise("dark"))) //
.toDocument("foo", Aggregation.DEFAULT_CONTEXT); .toDocument("foo", Aggregation.DEFAULT_CONTEXT);

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

@ -54,7 +54,7 @@ public class CondExpressionUnitTests {
() -> newBuilder().when("isYellow").then(newBuilder().when("field").then("then-value")).otherwise("otherwise")); () -> newBuilder().when("isYellow").then(newBuilder().when("field").then("then-value")).otherwise("otherwise"));
} }
@Test // DATAMONGO-861, DATAMONGO-1542 @Test // DATAMONGO-861, DATAMONGO-1542, DATAMONGO-2242
public void simpleBuilderShouldRenderCorrectly() { public void simpleBuilderShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when("isYellow").thenValueOf("bright").otherwise("dark"); Cond operator = ConditionalOperators.when("isYellow").thenValueOf("bright").otherwise("dark");
@ -62,13 +62,13 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() // Document expectedCondition = new Document() //
.append("if", "$isYellow") // .append("if", "$isYellow") //
.append("then", "bright") // .append("then", "$bright") //
.append("else", "dark"); .append("else", "dark");
assertThat(document).containsEntry("$cond", expectedCondition); assertThat(document).containsEntry("$cond", expectedCondition);
} }
@Test // DATAMONGO-861, DATAMONGO-1542 @Test // DATAMONGO-861, DATAMONGO-1542, DATAMONGO-2242
public void simpleCriteriaShouldRenderCorrectly() { public void simpleCriteriaShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when(Criteria.where("luminosity").gte(100)).thenValueOf("bright") Cond operator = ConditionalOperators.when(Criteria.where("luminosity").gte(100)).thenValueOf("bright")
@ -77,13 +77,13 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() // Document expectedCondition = new Document() //
.append("if", new Document("$gte", Arrays.<Object> asList("$luminosity", 100))) // .append("if", new Document("$gte", Arrays.<Object> asList("$luminosity", 100))) //
.append("then", "bright") // .append("then", "$bright") //
.append("else", "dark"); .append("else", "dark");
assertThat(document).containsEntry("$cond", expectedCondition); assertThat(document).containsEntry("$cond", expectedCondition);
} }
@Test // DATAMONGO-861 @Test // DATAMONGO-861, DATAMONGO-2242
public void andCriteriaShouldRenderCorrectly() { public void andCriteriaShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when(Criteria.where("luminosity").gte(100) // Cond operator = ConditionalOperators.when(Criteria.where("luminosity").gte(100) //
@ -99,13 +99,13 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() // Document expectedCondition = new Document() //
.append("if", Arrays.<Object> asList(luminosity, new Document("$and", Arrays.asList(hue, saturation)))) // .append("if", Arrays.<Object> asList(luminosity, new Document("$and", Arrays.asList(hue, saturation)))) //
.append("then", "bright") // .append("then", "$bright") //
.append("else", "$dark-field"); .append("else", "$dark-field");
assertThat(document).containsEntry("$cond", expectedCondition); assertThat(document).containsEntry("$cond", expectedCondition);
} }
@Test // DATAMONGO-861, DATAMONGO-1542 @Test // DATAMONGO-861, DATAMONGO-1542, DATAMONGO-2242
public void twoArgsCriteriaShouldRenderCorrectly() { public void twoArgsCriteriaShouldRenderCorrectly() {
Criteria criteria = Criteria.where("luminosity").gte(100) // Criteria criteria = Criteria.where("luminosity").gte(100) //
@ -119,7 +119,7 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() // Document expectedCondition = new Document() //
.append("if", Arrays.asList(gte, is)) // .append("if", Arrays.asList(gte, is)) //
.append("then", "bright") // .append("then", "$bright") //
.append("else", "dark"); .append("else", "dark");
assertThat(document).containsEntry("$cond", expectedCondition); assertThat(document).containsEntry("$cond", expectedCondition);

Loading…
Cancel
Save