|
14 | 14 | */ |
15 | 15 | package org.eclipse.jnosql.communication.mongodb.document; |
16 | 16 |
|
| 17 | +import com.mongodb.client.model.Accumulators; |
| 18 | +import com.mongodb.client.model.Aggregates; |
17 | 19 | import com.mongodb.client.model.Filters; |
18 | 20 | import jakarta.nosql.document.Document; |
19 | 21 | import jakarta.nosql.document.DocumentCollectionManager; |
20 | 22 | import jakarta.nosql.document.DocumentDeleteQuery; |
21 | 23 | import jakarta.nosql.document.DocumentEntity; |
22 | 24 | import jakarta.nosql.document.DocumentQuery; |
| 25 | +import org.bson.BsonValue; |
| 26 | +import org.bson.conversions.Bson; |
23 | 27 | import org.eclipse.jnosql.communication.document.Documents; |
24 | 28 | import org.junit.jupiter.api.Assertions; |
25 | 29 | import org.junit.jupiter.api.BeforeAll; |
26 | 30 | import org.junit.jupiter.api.BeforeEach; |
27 | 31 | import org.junit.jupiter.api.Test; |
28 | 32 |
|
29 | 33 | import java.io.IOException; |
| 34 | +import java.util.Arrays; |
30 | 35 | import java.util.Collections; |
31 | 36 | import java.util.HashMap; |
32 | 37 | import java.util.List; |
33 | 38 | import java.util.Map; |
34 | 39 | import java.util.Optional; |
35 | 40 | import java.util.stream.Collectors; |
| 41 | +import java.util.stream.Stream; |
36 | 42 |
|
37 | 43 | import static com.mongodb.client.model.Filters.eq; |
38 | 44 | import static jakarta.nosql.document.DocumentQuery.select; |
@@ -112,6 +118,25 @@ public void shouldReturnErrorOnAggregateWhenThereIsNullParameter(){ |
112 | 118 | Collections.singletonList(eq("name", "Poliana")))); |
113 | 119 | } |
114 | 120 |
|
| 121 | + @Test |
| 122 | + public void shouldAggregate() { |
| 123 | + List<Bson> predicates = Arrays.asList( |
| 124 | + Aggregates.match(eq("name", "Poliana")), |
| 125 | + Aggregates.group("$stars", Accumulators.sum("count", 1)) |
| 126 | + ); |
| 127 | + entityManager.insert(getEntity()); |
| 128 | + Stream<Map<String, BsonValue>> aggregate = entityManager.aggregate(COLLECTION_NAME, predicates); |
| 129 | + Assertions.assertNotNull(aggregate); |
| 130 | + Map<String, BsonValue> result = aggregate.findFirst() |
| 131 | + .orElseThrow(() -> new IllegalStateException("There is an issue with the aggregate test result")); |
| 132 | + |
| 133 | + Assertions.assertNotNull(result); |
| 134 | + Assertions.assertFalse(result.isEmpty()); |
| 135 | + BsonValue count = result.get("count"); |
| 136 | + Assertions.assertEquals(1L, count.asNumber().longValue()); |
| 137 | + |
| 138 | + } |
| 139 | + |
115 | 140 |
|
116 | 141 | private DocumentEntity getEntity() { |
117 | 142 | DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME); |
|
0 commit comments