Search in sources :

Example 1 with IsBsonObject

use of org.springframework.data.mongodb.test.util.IsBsonObject 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 IsBsonObject

use of org.springframework.data.mongodb.test.util.IsBsonObject in project spring-data-mongodb by spring-projects.

the class AggregationTests method graphLookupShouldBeAppliedCorrectly.

// DATAMONGO-1551
@Test
@MongoVersion(asOf = "3.4")
public void graphLookupShouldBeAppliedCorrectly() {
    Employee em1 = Employee.builder().id(1).name("Dev").build();
    Employee em2 = Employee.builder().id(2).name("Eliot").reportsTo("Dev").build();
    Employee em4 = Employee.builder().id(4).name("Andrew").reportsTo("Eliot").build();
    mongoTemplate.insert(Arrays.asList(em1, em2, em4), Employee.class);
    TypedAggregation<Employee> agg = Aggregation.newAggregation(Employee.class, // 
    match(Criteria.where("name").is("Andrew")), // 
    Aggregation.graphLookup("employee").startWith(// 
    "reportsTo").connectFrom(// 
    "reportsTo").connectTo(// 
    "name").depthField(// 
    "depth").maxDepth(// 
    5).as("reportingHierarchy"));
    AggregationResults<Document> result = mongoTemplate.aggregate(agg, Document.class);
    Document object = result.getUniqueMappedResult();
    List<Object> list = (List<Object>) object.get("reportingHierarchy");
    assertThat(object, isBsonObject().containing("reportingHierarchy", List.class));
    assertThat((Document) list.get(0), isBsonObject().containing("name", "Dev").containing("depth", 1L));
    assertThat((Document) list.get(1), isBsonObject().containing("name", "Eliot").containing("depth", 0L));
}
Also used : IsBsonObject(org.springframework.data.mongodb.test.util.IsBsonObject) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) Test(org.junit.Test) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion)

Example 3 with IsBsonObject

use of org.springframework.data.mongodb.test.util.IsBsonObject 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)

Aggregations

Document (org.bson.Document)3 Test (org.junit.Test)3 IsBsonObject (org.springframework.data.mongodb.test.util.IsBsonObject)3 Cond (org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)1