Search in sources :

Example 1 with Cond

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));
}
Also used : Cond(org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond) IsBsonObject(org.springframework.data.mongodb.test.util.IsBsonObject) Document(org.bson.Document) Test(org.junit.Test)

Example 2 with Cond

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));
}
Also used : Cond(org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond) IsBsonObject(org.springframework.data.mongodb.test.util.IsBsonObject) Document(org.bson.Document) Test(org.junit.Test)

Example 3 with Cond

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));
}
Also used : Cond(org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond) Document(org.bson.Document) Test(org.junit.Test)

Example 4 with Cond

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));
}
Also used : Cond(org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond) Criteria(org.springframework.data.mongodb.core.query.Criteria) Document(org.bson.Document) Test(org.junit.Test)

Example 5 with Cond

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));
}
Also used : Cond(org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

Document (org.bson.Document)5 Test (org.junit.Test)5 Cond (org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond)5 IsBsonObject (org.springframework.data.mongodb.test.util.IsBsonObject)2 Criteria (org.springframework.data.mongodb.core.query.Criteria)1