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 { @@ -207,7 +207,7 @@ public class ConditionalOperators {
public OtherwiseBuilder thenValueOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return createThenBuilder().then(fieldReference);
return createThenBuilder().thenValueOf(fieldReference);
}
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; @@ -27,7 +27,6 @@ import java.util.List;
import org.bson.Document;
import org.junit.Test;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond;
import org.springframework.data.mongodb.core.query.Criteria;
@ -438,14 +437,14 @@ public class AggregationUnitTests { @@ -438,14 +437,14 @@ public class AggregationUnitTests {
assertThat(getAsDocument(project, "color")).containsEntry("$cond", expectedCondition);
}
@Test // DATAMONGO-861
@Test // DATAMONGO-861, DATAMONGO-2242
public void referencingProjectionAliasesShouldRenderProjectionConditionalWithFieldReferenceCorrectly() {
Document agg = Aggregation.newAggregation(//
project().and("color").as("chroma"), project().and("luminosity") //
.applyCondition(ConditionalOperators //
.when("chroma") //
.thenValueOf("bright") //
.then("bright") //
.otherwise("dark"))) //
.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 { @@ -54,7 +54,7 @@ public class CondExpressionUnitTests {
() -> 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() {
Cond operator = ConditionalOperators.when("isYellow").thenValueOf("bright").otherwise("dark");
@ -62,13 +62,13 @@ public class CondExpressionUnitTests { @@ -62,13 +62,13 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() //
.append("if", "$isYellow") //
.append("then", "bright") //
.append("then", "$bright") //
.append("else", "dark");
assertThat(document).containsEntry("$cond", expectedCondition);
}
@Test // DATAMONGO-861, DATAMONGO-1542
@Test // DATAMONGO-861, DATAMONGO-1542, DATAMONGO-2242
public void simpleCriteriaShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when(Criteria.where("luminosity").gte(100)).thenValueOf("bright")
@ -77,13 +77,13 @@ public class CondExpressionUnitTests { @@ -77,13 +77,13 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() //
.append("if", new Document("$gte", Arrays.<Object> asList("$luminosity", 100))) //
.append("then", "bright") //
.append("then", "$bright") //
.append("else", "dark");
assertThat(document).containsEntry("$cond", expectedCondition);
}
@Test // DATAMONGO-861
@Test // DATAMONGO-861, DATAMONGO-2242
public void andCriteriaShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when(Criteria.where("luminosity").gte(100) //
@ -99,13 +99,13 @@ public class CondExpressionUnitTests { @@ -99,13 +99,13 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() //
.append("if", Arrays.<Object> asList(luminosity, new Document("$and", Arrays.asList(hue, saturation)))) //
.append("then", "bright") //
.append("then", "$bright") //
.append("else", "$dark-field");
assertThat(document).containsEntry("$cond", expectedCondition);
}
@Test // DATAMONGO-861, DATAMONGO-1542
@Test // DATAMONGO-861, DATAMONGO-1542, DATAMONGO-2242
public void twoArgsCriteriaShouldRenderCorrectly() {
Criteria criteria = Criteria.where("luminosity").gte(100) //
@ -119,7 +119,7 @@ public class CondExpressionUnitTests { @@ -119,7 +119,7 @@ public class CondExpressionUnitTests {
Document expectedCondition = new Document() //
.append("if", Arrays.asList(gte, is)) //
.append("then", "bright") //
.append("then", "$bright") //
.append("else", "dark");
assertThat(document).containsEntry("$cond", expectedCondition);

Loading…
Cancel
Save