Skip to content

Commit 88ef33e

Browse files
authored
Merge pull request #179 from eclipse/add_mongodb_funcionalities
Add mongodb Specific features
2 parents 4557395 + 54cf33e commit 88ef33e

File tree

4 files changed

+223
-4
lines changed

4 files changed

+223
-4
lines changed

mongodb-driver/src/main/java/org/eclipse/jnosql/communication/mongodb/document/MongoDBDocumentCollectionManager.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,28 @@
1515

1616
package org.eclipse.jnosql.communication.mongodb.document;
1717

18+
import com.mongodb.client.AggregateIterable;
1819
import com.mongodb.client.FindIterable;
1920
import com.mongodb.client.MongoCollection;
2021
import com.mongodb.client.MongoDatabase;
2122
import com.mongodb.client.model.Projections;
2223
import com.mongodb.client.model.Sorts;
24+
import com.mongodb.client.result.DeleteResult;
2325
import jakarta.nosql.Sort;
2426
import jakarta.nosql.SortType;
2527
import jakarta.nosql.document.DocumentCollectionManager;
2628
import jakarta.nosql.document.DocumentDeleteQuery;
2729
import jakarta.nosql.document.DocumentEntity;
2830
import jakarta.nosql.document.DocumentQuery;
2931
import org.bson.BsonDocument;
32+
import org.bson.BsonValue;
3033
import org.bson.Document;
3134
import org.bson.conversions.Bson;
3235
import org.eclipse.jnosql.communication.document.Documents;
3336

3437
import java.time.Duration;
38+
import java.util.List;
39+
import java.util.Map;
3540
import java.util.Objects;
3641
import java.util.stream.Stream;
3742
import java.util.stream.StreamSupport;
@@ -131,6 +136,7 @@ public void delete(DocumentDeleteQuery query) {
131136
collection.deleteMany(mongoDBQuery);
132137
}
133138

139+
134140
@Override
135141
public Stream<DocumentEntity> select(DocumentQuery query) {
136142
Objects.requireNonNull(query, "query is required");
@@ -155,6 +161,59 @@ public Stream<DocumentEntity> select(DocumentQuery query) {
155161

156162
}
157163

164+
/**
165+
* Removes all documents from the collection that match the given query filter.
166+
* If no documents match, the collection is not modified.
167+
*
168+
* @param collectionName the collection name
169+
* @param filter the delete filter
170+
* @return the number of documents deleted.
171+
* @throws NullPointerException when filter or collectionName is null
172+
*/
173+
public long delete(String collectionName, Bson filter) {
174+
Objects.requireNonNull(filter, "filter is required");
175+
Objects.requireNonNull(collectionName, "collectionName is required");
176+
177+
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
178+
DeleteResult result = collection.deleteMany(filter);
179+
return result.getDeletedCount();
180+
}
181+
182+
/**
183+
* Aggregates documents according to the specified aggregation pipeline.
184+
*
185+
* @param collectionName the collection name
186+
* @param pipeline the aggregation pipeline
187+
* @return the number of documents deleted.
188+
* @throws NullPointerException when filter or collectionName is null
189+
*/
190+
public Stream<Map<String, BsonValue>> aggregate(String collectionName, List<Bson> pipeline) {
191+
Objects.requireNonNull(pipeline, "filter is required");
192+
Objects.requireNonNull(collectionName, "collectionName is required");
193+
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
194+
AggregateIterable<Document> aggregate = collection.aggregate(pipeline);
195+
return stream(aggregate.spliterator(), false)
196+
.map(Document::toBsonDocument);
197+
}
198+
199+
/**
200+
* Finds all documents in the collection.
201+
*
202+
* @param collectionName the collection name
203+
* @param filter the query filter
204+
* @return the stream result
205+
* @throws NullPointerException when filter or collectionName is null
206+
*/
207+
public Stream<DocumentEntity> select(String collectionName, Bson filter) {
208+
Objects.requireNonNull(filter, "filter is required");
209+
Objects.requireNonNull(collectionName, "collectionName is required");
210+
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
211+
FindIterable<Document> documents = collection.find(filter);
212+
return stream(documents.spliterator(), false).map(MongoDBUtils::of)
213+
.map(ds -> DocumentEntity.of(collectionName, ds));
214+
}
215+
216+
158217
@Override
159218
public long count(String documentCollection) {
160219
Objects.requireNonNull(documentCollection, "documentCollection is required");
@@ -164,7 +223,7 @@ public long count(String documentCollection) {
164223

165224
private Bson getSort(Sort sort) {
166225
boolean isAscending = SortType.ASC.equals(sort.getType());
167-
return isAscending?Sorts.ascending(sort.getName()): Sorts.descending(sort.getName());
226+
return isAscending ? Sorts.ascending(sort.getName()) : Sorts.descending(sort.getName());
168227
}
169228

170229
@Override

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.eclipse.jnosql.communication.mongodb.document.type.Money;
2626
import org.junit.jupiter.api.Assertions;
2727
import org.junit.jupiter.api.BeforeAll;
28+
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
2930

3031
import java.io.IOException;
@@ -59,6 +60,11 @@ public static void setUp() throws IOException {
5960
entityManager = ManagerFactorySupplier.INSTANCE.get("database");
6061
}
6162

63+
@BeforeEach
64+
public void beforeEach() {
65+
DocumentDeleteQuery.delete().from(COLLECTION_NAME).delete(entityManager);
66+
}
67+
6268
@Test
6369
public void shouldInsert() {
6470
DocumentEntity entity = getEntity();

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@
1515

1616
package org.eclipse.jnosql.communication.mongodb.document;
1717

18-
import jakarta.nosql.Sort;
1918
import jakarta.nosql.document.Document;
2019
import jakarta.nosql.document.DocumentCollectionManager;
21-
import jakarta.nosql.document.DocumentCondition;
2220
import jakarta.nosql.document.DocumentDeleteQuery;
2321
import jakarta.nosql.document.DocumentEntity;
2422
import jakarta.nosql.document.DocumentQuery;
2523
import org.eclipse.jnosql.communication.document.Documents;
2624
import org.junit.jupiter.api.Assertions;
2725
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.BeforeEach;
2827
import org.junit.jupiter.api.DisplayName;
2928
import org.junit.jupiter.api.Test;
3029

3130
import java.io.IOException;
3231
import java.util.HashMap;
3332
import java.util.List;
3433
import java.util.Map;
35-
import java.util.Optional;
3634
import java.util.stream.Stream;
3735

3836
import static jakarta.nosql.document.DocumentDeleteQuery.delete;
@@ -48,6 +46,11 @@ public static void setUp() throws IOException {
4846
entityManager = ManagerFactorySupplier.INSTANCE.get("database");
4947
}
5048

49+
@BeforeEach
50+
public void beforeEach() {
51+
DocumentDeleteQuery.delete().from(COLLECTION_NAME).delete(entityManager);
52+
}
53+
5154
@Test
5255
@DisplayName("The query should execute A or B")
5356
public void shouldQuery() {
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright (c) 2022 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.communication.mongodb.document;
16+
17+
import com.mongodb.client.model.Accumulators;
18+
import com.mongodb.client.model.Aggregates;
19+
import com.mongodb.client.model.Filters;
20+
import jakarta.nosql.document.Document;
21+
import jakarta.nosql.document.DocumentCollectionManager;
22+
import jakarta.nosql.document.DocumentDeleteQuery;
23+
import jakarta.nosql.document.DocumentEntity;
24+
import jakarta.nosql.document.DocumentQuery;
25+
import org.bson.BsonValue;
26+
import org.bson.conversions.Bson;
27+
import org.eclipse.jnosql.communication.document.Documents;
28+
import org.junit.jupiter.api.Assertions;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Test;
32+
33+
import java.io.IOException;
34+
import java.util.Arrays;
35+
import java.util.Collections;
36+
import java.util.HashMap;
37+
import java.util.List;
38+
import java.util.Map;
39+
import java.util.Optional;
40+
import java.util.stream.Collectors;
41+
import java.util.stream.Stream;
42+
43+
import static com.mongodb.client.model.Filters.eq;
44+
import static jakarta.nosql.document.DocumentQuery.select;
45+
import static org.hamcrest.MatcherAssert.assertThat;
46+
import static org.hamcrest.Matchers.contains;
47+
import static org.junit.jupiter.api.Assertions.assertFalse;
48+
49+
public class MongoDBSpecificFeaturesTest {
50+
51+
public static final String COLLECTION_NAME = "person";
52+
private static MongoDBDocumentCollectionManager entityManager;
53+
54+
@BeforeAll
55+
public static void setUp() throws IOException {
56+
entityManager = ManagerFactorySupplier.INSTANCE.get("database");
57+
}
58+
59+
@BeforeEach
60+
public void beforeEach() {
61+
DocumentDeleteQuery.delete().from(COLLECTION_NAME).delete(entityManager);
62+
}
63+
64+
@Test
65+
public void shouldReturnErrorOnSelectWhenThereIsNullParameter(){
66+
Assertions.assertThrows(NullPointerException.class,
67+
() -> entityManager.select(null, null));
68+
Assertions.assertThrows(NullPointerException.class,
69+
() -> entityManager.select(COLLECTION_NAME, null));
70+
71+
Assertions.assertThrows(NullPointerException.class,
72+
() -> entityManager.select(null, eq("name", "Poliana")));
73+
}
74+
75+
@Test
76+
public void shouldFindDocument() {
77+
DocumentEntity entity = entityManager.insert(getEntity());
78+
79+
List<DocumentEntity> entities = entityManager.select(COLLECTION_NAME,
80+
eq("name", "Poliana")).collect(Collectors.toList());
81+
assertFalse(entities.isEmpty());
82+
assertThat(entities, contains(entity));
83+
}
84+
85+
@Test
86+
public void shouldReturnErrorOnDeleteWhenThereIsNullParameter(){
87+
Assertions.assertThrows(NullPointerException.class,
88+
() -> entityManager.delete(null, null));
89+
Assertions.assertThrows(NullPointerException.class,
90+
() -> entityManager.delete(COLLECTION_NAME, null));
91+
92+
Assertions.assertThrows(NullPointerException.class,
93+
() -> entityManager.delete(null, eq("name", "Poliana")));
94+
}
95+
96+
@Test
97+
public void shouldDelete() {
98+
entityManager.insert(getEntity());
99+
100+
long result = entityManager.delete(COLLECTION_NAME,
101+
eq("name", "Poliana"));
102+
103+
Assertions.assertEquals(1L, result);
104+
List<DocumentEntity> entities = entityManager.select(COLLECTION_NAME,
105+
eq("name", "Poliana")).collect(Collectors.toList());
106+
Assertions.assertTrue(entities.isEmpty());
107+
}
108+
109+
@Test
110+
public void shouldReturnErrorOnAggregateWhenThereIsNullParameter(){
111+
Assertions.assertThrows(NullPointerException.class,
112+
() -> entityManager.aggregate(null, null));
113+
Assertions.assertThrows(NullPointerException.class,
114+
() -> entityManager.aggregate(COLLECTION_NAME, null));
115+
116+
Assertions.assertThrows(NullPointerException.class,
117+
() -> entityManager.aggregate(null,
118+
Collections.singletonList(eq("name", "Poliana"))));
119+
}
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+
140+
141+
private DocumentEntity getEntity() {
142+
DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME);
143+
Map<String, Object> map = new HashMap<>();
144+
map.put("name", "Poliana");
145+
map.put("city", "Salvador");
146+
List<Document> documents = Documents.of(map);
147+
documents.forEach(entity::add);
148+
return entity;
149+
}
150+
151+
}

0 commit comments

Comments
 (0)