use of org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond in project spring-data-mongodb by spring-projects.
the class CondExpressionUnitTests method nestedCriteriaShouldRenderCorrectly.
// DATAMONGO-861, DATAMONGO-1542
@Test
public void nestedCriteriaShouldRenderCorrectly() {
Cond operator = //
ConditionalOperators.when(Criteria.where("luminosity").gte(100)).thenValueOf(//
newBuilder().when(//
Criteria.where("luminosity").gte(200)).then(//
"verybright").otherwise(//
"not-so-bright")).otherwise(//
newBuilder().when(//
Criteria.where("luminosity").lt(50)).then(//
"very-dark").otherwise("not-so-dark"));
Document document = operator.toDocument(Aggregation.DEFAULT_CONTEXT);
Document trueCondition = //
new Document().append("if", //
new Document("$gte", Arrays.<Object>asList("$luminosity", 200))).append("then", //
"verybright").append("else", "not-so-bright");
Document falseCondition = //
new Document().append("if", //
new Document("$lt", Arrays.<Object>asList("$luminosity", 50))).append("then", //
"very-dark").append("else", "not-so-dark");
assertThat(document, isBsonObject().containing("$cond.then.$cond", trueCondition));
assertThat(document, isBsonObject().containing("$cond.else.$cond", falseCondition));
}
use of org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond in project spring-data-mongodb by spring-projects.
the class CondExpressionUnitTests method simpleCriteriaShouldRenderCorrectly.
// DATAMONGO-861, DATAMONGO-1542
@Test
public void simpleCriteriaShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when(Criteria.where("luminosity").gte(100)).thenValueOf("bright").otherwise("dark");
Document document = operator.toDocument(Aggregation.DEFAULT_CONTEXT);
Document expectedCondition = //
new Document().append("if", //
new Document("$gte", Arrays.<Object>asList("$luminosity", 100))).append("then", //
"bright").append("else", "dark");
assertThat(document, isBsonObject().containing("$cond", expectedCondition));
}
use of org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond in project spring-data-mongodb by spring-projects.
the class CondExpressionUnitTests method andCriteriaShouldRenderCorrectly.
// DATAMONGO-861
@Test
public void andCriteriaShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when(//
Criteria.where("luminosity").gte(100).andOperator(//
Criteria.where("hue").is(50), Criteria.where("saturation").lt(11))).thenValueOf("bright").otherwiseValueOf("dark-field");
Document document = operator.toDocument(Aggregation.DEFAULT_CONTEXT);
Document luminosity = new Document("$gte", Arrays.<Object>asList("$luminosity", 100));
Document hue = new Document("$eq", Arrays.<Object>asList("$hue", 50));
Document saturation = new Document("$lt", Arrays.<Object>asList("$saturation", 11));
Document expectedCondition = //
new Document().append("if", //
Arrays.<Object>asList(luminosity, new Document("$and", Arrays.asList(hue, saturation)))).append("then", //
"bright").append("else", "$dark-field");
assertThat(document, isBsonObject().containing("$cond", expectedCondition));
}
use of org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond in project spring-data-mongodb by spring-projects.
the class CondExpressionUnitTests method twoArgsCriteriaShouldRenderCorrectly.
// DATAMONGO-861, DATAMONGO-1542
@Test
public void twoArgsCriteriaShouldRenderCorrectly() {
Criteria criteria = //
Criteria.where("luminosity").gte(100).and("saturation").and("chroma").is(200);
Cond operator = ConditionalOperators.when(criteria).thenValueOf("bright").otherwise("dark");
Document document = operator.toDocument(Aggregation.DEFAULT_CONTEXT);
Document gte = new Document("$gte", Arrays.<Object>asList("$luminosity", 100));
Document is = new Document("$eq", Arrays.<Object>asList("$chroma", 200));
Document expectedCondition = //
new Document().append("if", //
Arrays.asList(gte, is)).append("then", //
"bright").append("else", "dark");
assertThat(document, isBsonObject().containing("$cond", expectedCondition));
}
use of org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond in project spring-data-mongodb by spring-projects.
the class CondExpressionUnitTests method simpleBuilderShouldRenderCorrectly.
// DATAMONGO-861, DATAMONGO-1542
@Test
public void simpleBuilderShouldRenderCorrectly() {
Cond operator = ConditionalOperators.when("isYellow").thenValueOf("bright").otherwise("dark");
Document document = operator.toDocument(Aggregation.DEFAULT_CONTEXT);
Document expectedCondition = //
new Document().append("if", //
"$isYellow").append("then", //
"bright").append("else", "dark");
assertThat(document, isBsonObject().containing("$cond", expectedCondition));
}
Aggregations