Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aggregate function return stream of Entities #245

Merged
merged 6 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.mongodb.client.result.DeleteResult;
import jakarta.data.repository.Sort;
import org.bson.BsonDocument;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.eclipse.jnosql.communication.document.DocumentDeleteQuery;
Expand All @@ -34,6 +33,7 @@
import org.eclipse.jnosql.communication.document.Documents;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -42,6 +42,7 @@

import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
import org.bson.BsonValue;
import static org.eclipse.jnosql.databases.mongodb.communication.MongoDBUtils.ID_FIELD;
import static org.eclipse.jnosql.databases.mongodb.communication.MongoDBUtils.getDocument;

Expand Down Expand Up @@ -202,17 +203,34 @@ public long delete(String collectionName, Bson filter) {
*
* @param collectionName the collection name
* @param pipeline the aggregation pipeline
* @return the number of documents deleted.
* @return the stream of BSON Documents
* @throws NullPointerException when filter or collectionName is null
*/
public Stream<Map<String, BsonValue>> aggregate(String collectionName, List<Bson> pipeline) {
public Stream<Map<String, BsonValue>> aggregate(String collectionName, Bson... pipeline) {
Objects.requireNonNull(pipeline, "filter is required");
Objects.requireNonNull(collectionName, "collectionName is required");
MongoCollection<BsonDocument> collection = mongoDatabase.getCollection(collectionName, BsonDocument.class);
AggregateIterable aggregate = collection.aggregate(pipeline);
AggregateIterable aggregate = collection.aggregate(Arrays.asList(pipeline));
return stream(aggregate.spliterator(), false);
}

/**
* Aggregates documents according to the specified aggregation pipeline.
*
* @param collectionName the collection name
* @param pipeline the aggregation pipeline
* @return the stream result
* @throws NullPointerException when pipeline or collectionName is null
*/
public Stream<DocumentEntity> aggregate(String collectionName, List<Bson> pipeline) {
Objects.requireNonNull(pipeline, "pipeline is required");
Objects.requireNonNull(collectionName, "collectionName is required");
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
AggregateIterable<Document> aggregate = collection.aggregate(pipeline);
return stream(aggregate.spliterator(), false).map(MongoDBUtils::of)
.map(ds -> DocumentEntity.of(collectionName, ds));
}

/**
* Finds all documents in the collection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.Typed;
import jakarta.inject.Inject;
import org.bson.BsonValue;
import org.bson.conversions.Bson;
import org.eclipse.jnosql.communication.document.DocumentEntity;
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentManager;
Expand All @@ -33,6 +32,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import org.bson.BsonValue;


@ApplicationScoped
Expand Down Expand Up @@ -125,20 +125,37 @@ public <T> Stream<T> select(Class<T> entity, Bson filter) {
}

@Override
public Stream<Map<String, BsonValue>> aggregate(String collectionName, List<Bson> pipeline) {
public Stream<Map<String, BsonValue>> aggregate(String collectionName, Bson... pipeline) {
Objects.requireNonNull(collectionName, "collectionName is required");
Objects.requireNonNull(pipeline, "pipeline is required");
return this.getManager().aggregate(collectionName, pipeline);
}

@Override
public <T> Stream<Map<String, BsonValue>> aggregate(Class<T> entity, List<Bson> pipeline) {
public <T> Stream<Map<String, BsonValue>> aggregate(Class<T> entity, Bson... pipeline) {
Objects.requireNonNull(entity, "entity is required");
Objects.requireNonNull(pipeline, "pipeline is required");
EntityMetadata entityMetadata = this.entities.get(entity);
return this.getManager().aggregate(entityMetadata.name(), pipeline);
}

@Override
public <T> Stream<T> aggregate(String collectionName, List<Bson> pipeline) {
Objects.requireNonNull(collectionName, "collectionName is required");
Objects.requireNonNull(pipeline, "pipeline is required");
return this.getManager().aggregate(collectionName, pipeline)
.map(this.converter::toEntity);
}

@Override
public <T> Stream<T> aggregate(Class<T> entity, List<Bson> pipeline) {
Objects.requireNonNull(entity, "entity is required");
Objects.requireNonNull(pipeline, "pipeline is required");
EntityMetadata entityMetadata = this.entities.get(entity);
return this.getManager().aggregate(entityMetadata.name(), pipeline)
.map(this.converter::toEntity);
}

@Override
public long count(String collectionName, Bson filter) {
Objects.requireNonNull(collectionName, "collection name is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package org.eclipse.jnosql.databases.mongodb.mapping;

import jakarta.nosql.document.DocumentTemplate;
import org.bson.BsonValue;
import org.bson.conversions.Bson;
import org.eclipse.jnosql.mapping.document.JNoSQLDocumentTemplate;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.bson.BsonValue;

/**
* A MongoDB extension of {@link DocumentTemplate}
Expand Down Expand Up @@ -80,21 +80,43 @@ public interface MongoDBTemplate extends JNoSQLDocumentTemplate {
*
* @param collectionName the collection name
* @param pipeline the aggregation pipeline
* @return the number of documents deleted.
* @throws NullPointerException when filter or collectionName is null
* @return the stream result
* @throws NullPointerException when pipeline or collectionName is null
*/
Stream<Map<String, BsonValue>> aggregate(String collectionName, List<Bson> pipeline);
Stream<Map<String, BsonValue>> aggregate(String collectionName, Bson... pipeline);

/**
* Aggregates documents according to the specified aggregation pipeline.
*
* @param entity the collection name
* @param pipeline the aggregation pipeline
* @param <T> the entity type
* @return the stream result
* @throws NullPointerException when pipeline or entity is null
*/
<T> Stream<Map<String, BsonValue>> aggregate(Class<T> entity, Bson... pipeline);

/**
* Aggregates documents according to the specified aggregation pipeline.
*
* @param entity the collection name
* @param pipeline the aggregation pipeline
* @return the number of documents deleted.
* @throws NullPointerException when filter or entity is null
* @param <T> the entity type
* @return the stream result
* @throws NullPointerException when pipeline or entity is null
*/
<T> Stream<T> aggregate(Class<T> entity, List<Bson> pipeline);

/**
* Aggregates documents according to the specified aggregation pipeline.
*
* @param collectionName the collection name
* @param pipeline the aggregation pipeline
* @param <T> the entity type
* @return the stream result
* @throws NullPointerException when pipeline or collectionName is null
*/
<T> Stream<Map<String, BsonValue>> aggregate(Class<T> entity, List<Bson> pipeline);
<T> Stream<T> aggregate(String collectionName, List<Bson> pipeline);

/**
* Returns the number of items in the collection that match the given query filter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -39,6 +38,7 @@

import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
Expand Down Expand Up @@ -108,9 +108,9 @@ public void shouldDelete() {
@Test
public void shouldReturnErrorOnAggregateWhenThereIsNullParameter() {
Assertions.assertThrows(NullPointerException.class,
() -> entityManager.aggregate(null, null));
() -> entityManager.aggregate(null, (List)null));
Assertions.assertThrows(NullPointerException.class,
() -> entityManager.aggregate(COLLECTION_NAME, null));
() -> entityManager.aggregate(COLLECTION_NAME, (List)null));

Assertions.assertThrows(NullPointerException.class,
() -> entityManager.aggregate(null,
Expand All @@ -119,10 +119,10 @@ public void shouldReturnErrorOnAggregateWhenThereIsNullParameter() {

@Test
public void shouldAggregate() {
List<Bson> predicates = Arrays.asList(
Bson[] predicates = {
Aggregates.match(eq("name", "Poliana")),
Aggregates.group("$stars", Accumulators.sum("count", 1))
);
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also create a test for the new aggregate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have dockerized test. just expect it shouldn't fail...

entityManager.insert(getEntity());
Stream<Map<String, BsonValue>> aggregate = entityManager.aggregate(COLLECTION_NAME, predicates);
Assertions.assertNotNull(aggregate);
Expand All @@ -136,6 +136,20 @@ public void shouldAggregate() {

}

@Test
public void shouldAggregateEntity() {
List<Bson> predicates = Arrays.asList(
Aggregates.match(eq("name", "Poliana")),
Aggregates.limit(1)
);
entityManager.insert(getEntity());
Stream<DocumentEntity> aggregate = entityManager.aggregate(COLLECTION_NAME, predicates);
Assertions.assertNotNull(aggregate);
DocumentEntity result = aggregate.findFirst()
.orElseThrow(() -> new IllegalStateException("There is an issue with the aggregate test result"));

Assertions.assertNotNull(result);
}

private DocumentEntity getEntity() {
DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,33 +157,33 @@ public void shouldSelectWithEntity() {

@Test
public void shouldReturnErrorOnAggregateMethod() {
assertThrows(NullPointerException.class, () -> template.aggregate((String) null, null));
assertThrows(NullPointerException.class, () -> template.aggregate("Collection", null));
assertThrows(NullPointerException.class, () -> template.aggregate((String) null, (List) null));
assertThrows(NullPointerException.class, () -> template.aggregate("Collection", (List) null));
assertThrows(NullPointerException.class, () -> template.aggregate((String) null,
Collections.singletonList(eq("name", "Poliana"))));

assertThrows(NullPointerException.class, () -> template.aggregate(Person.class, null));
assertThrows(NullPointerException.class, () -> template.aggregate(Person.class, (List) null));
assertThrows(NullPointerException.class, () -> template.aggregate((Class<Object>) null,
Collections.singletonList(eq("name", "Poliana"))));
}

@Test
public void shouldAggregateWithCollectionName() {
List<Bson> predicates = Arrays.asList(
Bson[] predicates = {
Aggregates.match(eq("name", "Poliana")),
Aggregates.group("$stars", Accumulators.sum("count", 1))
);
};

template.aggregate("Person", predicates);
Mockito.verify(manager).aggregate("Person", predicates);
}

@Test
public void shouldAggregateWithEntity() {
List<Bson> predicates = Arrays.asList(
Bson[] predicates = {
Aggregates.match(eq("name", "Poliana")),
Aggregates.group("$stars", Accumulators.sum("count", 1))
);
};

template.aggregate(Person.class, predicates);
Mockito.verify(manager).aggregate("Person", predicates);
Expand Down