Skip to content

Commit 8008003

Browse files
committed
Update method to return aggregate
Signed-off-by: Otavio Santana <[email protected]>
1 parent 0adeecb commit 8008003

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

mongodb-driver/src/test/java/org/eclipse/jnosql/communication/mongodb/document/MongoDBSpecificFeaturesTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,31 @@
1414
*/
1515
package org.eclipse.jnosql.communication.mongodb.document;
1616

17+
import com.mongodb.client.model.Accumulators;
18+
import com.mongodb.client.model.Aggregates;
1719
import com.mongodb.client.model.Filters;
1820
import jakarta.nosql.document.Document;
1921
import jakarta.nosql.document.DocumentCollectionManager;
2022
import jakarta.nosql.document.DocumentDeleteQuery;
2123
import jakarta.nosql.document.DocumentEntity;
2224
import jakarta.nosql.document.DocumentQuery;
25+
import org.bson.BsonValue;
26+
import org.bson.conversions.Bson;
2327
import org.eclipse.jnosql.communication.document.Documents;
2428
import org.junit.jupiter.api.Assertions;
2529
import org.junit.jupiter.api.BeforeAll;
2630
import org.junit.jupiter.api.BeforeEach;
2731
import org.junit.jupiter.api.Test;
2832

2933
import java.io.IOException;
34+
import java.util.Arrays;
3035
import java.util.Collections;
3136
import java.util.HashMap;
3237
import java.util.List;
3338
import java.util.Map;
3439
import java.util.Optional;
3540
import java.util.stream.Collectors;
41+
import java.util.stream.Stream;
3642

3743
import static com.mongodb.client.model.Filters.eq;
3844
import static jakarta.nosql.document.DocumentQuery.select;
@@ -112,6 +118,25 @@ public void shouldReturnErrorOnAggregateWhenThereIsNullParameter(){
112118
Collections.singletonList(eq("name", "Poliana"))));
113119
}
114120

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+
115140

116141
private DocumentEntity getEntity() {
117142
DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME);

0 commit comments

Comments
 (0)