Skip to content

Commit

Permalink
Fixes for bugs 49,50, 51
Browse files Browse the repository at this point in the history
  • Loading branch information
krraghavan committed Jul 26, 2024
1 parent 18c9b10 commit a345b94
Show file tree
Hide file tree
Showing 92 changed files with 865 additions and 1,602 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Java CI with Maven](https://github.com/krraghavan/mongodb-aggregate-query-support/actions/workflows/maven.yml/badge.svg?branch=master)](https://github.com/krraghavan/mongodb-aggregate-query-support/actions/workflows/maven.yml)[![Release Version](https://img.shields.io/badge/version-v0.9.3-red.svg)](https://github.com/krraghavan/mongodb-aggregate-query-support) [![License](https://img.shields.io/hexpm/l/plug.svg)](https://img.shields.io/hexpm/l/plug.svg)
[![Java CI with Maven](https://github.com/krraghavan/mongodb-aggregate-query-support/actions/workflows/maven.yml/badge.svg?branch=master)](https://github.com/krraghavan/mongodb-aggregate-query-support/actions/workflows/maven.yml)[![Release Version](https://img.shields.io/badge/version-v0.9.4-red.svg)](https://github.com/krraghavan/mongodb-aggregate-query-support) [![License](https://img.shields.io/hexpm/l/plug.svg)](https://img.shields.io/hexpm/l/plug.svg)

# MONGO DB AGGREGATE QUERY SUPPORT
This module provides annotated support for MongoDB aggregate queries much like the @Query annotation provided by the
Expand All @@ -8,6 +8,11 @@ The @Query annotation provided by Spring Data MongoDb allows queries to be execu
It is highly desirable to have a similar mechanism for MongoDB aggregate queries which allow us to execute sophisticated
queries with practically no code being written.

## New in 0.9.4 version
1. [Bug 50](https://github.com/krraghavan/mongodb-aggregate-query-support/issues/50): Added ```@@``` support for ```@Project``` and ```@Group``` stages
1. [Bug 51](https://github.com/krraghavan/mongodb-aggregate-query-support/issues/51): Facets with ```@@@``` annotations in ```@Project``` do not parse correctly


## New in 0.9.3 version
1. [Bug 49](https://github.com/krraghavan/mongodb-aggregate-query-support/issues/49): Fixed Date parsing when msecs are separated by dot (.).

Expand Down
2 changes: 1 addition & 1 deletion mongodb-aggregate-query-support-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>mongodb-aggregate-query-support</artifactId>
<groupId>com.github.krraghavan</groupId>
<version>0.9.4-SNAPSHOT</version>
<version>0.9.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public interface BoundParameterValue {
String getValue(Object[] methodParameterValues, String query, BiFunction<Integer, Class<?>, String> valueProviderFn);

default boolean isString(Object[] methodParameterValues, int index) {
int length = methodParameterValues.length;
validateIndex(methodParameterValues, index);
return (methodParameterValues[index] instanceof String);
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Created by rkolliva on 10/24/2015.
* Used for query execution failures
*/
public class MongoQueryException extends Exception {
public class MongoQueryException extends RuntimeException {

public MongoQueryException() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Object[][] atPhFixtures() {
}

@Test(dataProvider = "questionMarkFixtures")
public void mustParseQuestionMarkPh(String query, AggregationType.QUOTE_TYPE quoteType) {
void mustParseQuestionMarkPh(String query, AggregationType.QUOTE_TYPE quoteType) {
Matcher matcher = AggregationType.PLACEHOLDER_REGEX.matcher(query);
boolean found = matcher.find();
assertTrue(found);
Expand All @@ -51,7 +51,7 @@ public void mustParseQuestionMarkPh(String query, AggregationType.QUOTE_TYPE quo
}

@Test(dataProvider = "atPhFixtures")
public void mustParseAtPh(String query, boolean valid, AggregationType.QUOTE_TYPE quoteType) {
void mustParseAtPh(String query, boolean valid, AggregationType.QUOTE_TYPE quoteType) {
Matcher matcher = AggregationType.PLACEHOLDER_REGEX.matcher(query);
boolean found = matcher.find();
assertTrue(found);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package com.github.krr.mongodb.aggregate.support.enums;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.function.BiFunction;

import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

public class BoundValueImplTest {

@DataProvider
public static Object[][] invalidNumericOnlyPhFixtures() {
Object[] args = {randomAlphabetic(5), RandomStringUtils.randomAlphabetic(10)};
return new Object[][]{
new Object[]{AggregationType.LIMIT, args},
new Object[]{AggregationType.SKIP, args},
};
}

@DataProvider
private Object[][] queryFixtures() {
String randomValues1 = randomAlphabetic(10);
Expand Down Expand Up @@ -175,6 +188,24 @@ public void mustProcessValidAtAtParameterForMatch() {
assertEquals(query, expectedQuery);
}

// Resolves: https://github.com/krraghavan/mongodb-aggregate-query-support/issues/51
@Test
public void mustProcessValidAtAtParameterForProject() {
String expectedQuery = "{'" + randomAlphabetic(5) + "':'" + randomAlphabetic(10) + "'}";
Object[] args = {randomAlphabetic(5), expectedQuery};
String query = AggregationType.PROJECT.getValue(args, "@@1", (index, valueClass) -> (String) args[index]);
assertEquals(query, expectedQuery);
}

// Resolves: https://github.com/krraghavan/mongodb-aggregate-query-support/issues/51
@Test
public void mustProcessValidAtAtParameterForGroup() {
String expectedQuery = "{'" + randomAlphabetic(5) + "':'" + randomAlphabetic(10) + "'}";
Object[] args = {randomAlphabetic(5), expectedQuery};
String query = AggregationType.GROUP.getValue(args, "@@1", (index, valueClass) -> (String) args[index]);
assertEquals(query, expectedQuery);
}

@Test
public void mustProcessValidAtAtParameterForEmptyMatch() {
String expectedQuery = "{ }";
Expand All @@ -183,4 +214,34 @@ public void mustProcessValidAtAtParameterForEmptyMatch() {
assertNull(query);
}

@DataProvider
public static Object[][] numericOnlyPhFixtures() {
int integer = RandomUtils.nextInt();
long longValue = RandomUtils.nextLong();
String longStr = String.valueOf(longValue);
String intStr = String.valueOf(integer);
return new Object[][]{
new Object[]{AggregationType.LIMIT, new Object[]{longValue}, longStr,
(BiFunction<Integer, Class<?>, String>) (v, c) -> longStr},
new Object[]{AggregationType.LIMIT, new Object[]{integer}, intStr,
(BiFunction<Integer, Class<?>, String>) (v, c) -> intStr},
new Object[]{AggregationType.SKIP, new Object[]{integer}, longStr,
(BiFunction<Integer, Class<?>, String>) (v, c) -> longStr},
new Object[]{AggregationType.SKIP, new Object[]{integer}, intStr,
(BiFunction<Integer, Class<?>, String>) (v, c) -> intStr},
};
}


@Test(dataProvider = "numericOnlyPhFixtures")
public void mustProcessNumericOnlyPhValues(AggregationType aggregationType, Object [] args, String expectedResult,
BiFunction<Integer, Class<?>, String> cbFn) {
String query = AggregationType.getIntOrLongValueForQmarkPh(aggregationType, args, expectedResult, cbFn);
assertEquals(query, expectedResult);
}

@Test(dataProvider = "invalidNumericOnlyPhFixtures", expectedExceptions = IllegalArgumentException.class)
public void mustNotProcessNonNumericValuesOnNumericOnlyPhValues(AggregationType aggregationType, Object [] args) {
String query = aggregationType.getValue(args, "?0", (index, valueClass) -> (String) args[index]);
}
}
2 changes: 1 addition & 1 deletion mongodb-aggregate-query-support-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>com.github.krraghavan</groupId>
<artifactId>mongodb-aggregate-query-support</artifactId>
<version>0.9.4-SNAPSHOT</version>
<version>0.9.4</version>
<relativePath>../../mongodb-aggregate-query-support/pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.github.krr.mongodb.aggregate.support.annotations.Aggregate;
import com.github.krr.mongodb.aggregate.support.api.ReactiveMongoQueryExecutor;
import com.github.krr.mongodb.aggregate.support.exceptions.InvalidAggregationQueryException;
import com.github.krr.mongodb.aggregate.support.exceptions.MongoQueryException;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -90,10 +89,6 @@ public Publisher<Object> execute(Object[] parameters) {
parameterAccessor);
return (Publisher<Object>) queryExecutor.executeQuery(aggregateQueryProvider);
}
catch (MongoQueryException e) {
LOGGER.error("Error executing aggregate query", e);
throw new IllegalStateException(e);
}
catch (InvalidAggregationQueryException e) {
LOGGER.error("Invalid aggregation query", e);
throw new IllegalArgumentException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.krr.mongodb.aggregate.support.api.QueryProvider;
import com.github.krr.mongodb.aggregate.support.exceptions.MongoQueryException;
import com.mongodb.reactivestreams.client.AggregatePublisher;
import com.mongodb.reactivestreams.client.MongoCollection;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -76,7 +77,12 @@ public Publisher<?> executeQuery(QueryProvider queryProvider) {
Mono<AggregatePublisher<Document>> aggregatePublisherMono = collectionMono.map(collection -> collection.aggregate(pipelineStages)
.allowDiskUse(queryProvider.isAllowDiskUse())
.maxTime(queryProvider.getMaxTimeMS(),
MILLISECONDS));
MILLISECONDS))
.doOnError(e -> {
LOGGER.error("Error executing query {}",
pipelineStages, e);
throw new MongoQueryException(e);
});
Class<?> methodReturnType = queryProvider.getMethodReturnType();
boolean isFlux = Flux.class.isAssignableFrom(methodReturnType);
boolean isMono = Mono.class.isAssignableFrom(methodReturnType);
Expand All @@ -89,7 +95,11 @@ public Publisher<?> executeQuery(QueryProvider queryProvider) {

if (isFlux) {
LOGGER.trace("Return type is Flux<{}>", outputClass);
Flux<Document> retval = aggregatePublisherMono.flatMapMany(ap -> ap);
Flux<Document> retval = aggregatePublisherMono.flatMapMany(ap -> ap)
.doOnError(e -> {
LOGGER.error("Error executing query {}", pipelineStages, e);
throw new MongoQueryException(e);
});
if (outputClass != null) {
return adaptPipeline(queryProvider, outputClass, retval);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.krr.mongodb.aggregate.support.beans;

public class DummyBean {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package com.github.krr.mongodb.aggregate.support.config;

import com.github.krr.mongodb.aggregate.support.factory.ReactiveAggregateQuerySupportingRepositoryFactoryBean;
import com.github.krr.mongodb.aggregate.support.repository.reactive.ReactiveTestAggregateRepositoryMarker;
import com.github.krr.mongodb.aggregate.support.repository.ReactiveTestAggregateRepositoryMarker;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package com.github.krr.mongodb.aggregate.support.repository.reactive;
package com.github.krr.mongodb.aggregate.support.repository;

import com.github.krr.mongodb.aggregate.support.annotations.Aggregate;
import com.github.krr.mongodb.aggregate.support.annotations.Match;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package com.github.krr.mongodb.aggregate.support.repository.reactive;
package com.github.krr.mongodb.aggregate.support.repository;

import com.github.krr.mongodb.aggregate.support.annotations.*;
import com.github.krr.mongodb.aggregate.support.beans.Artwork;
Expand Down Expand Up @@ -56,6 +56,17 @@ public interface ReactiveArtworkRepository extends ReactiveMongoRepository<Artwo
, order = 0)
Mono<Document> getFacetResults2();

// fixture created for bug: https://github.com/krraghavan/mongodb-aggregate-query-support/issues/50
@Aggregate(inputType = Artwork.class)
@Facet(pipelines = {
@FacetPipeline(name = "categorizedByTags",
stages = {
@FacetPipelineStage(stageType = Skip.class, query = "?0"),
@FacetPipelineStage(stageType = Limit.class, query = "?1"),
@FacetPipelineStage(stageType = Project.class, query = "@@2")
})}, order = 0)
Mono<Document> facetPipelineStageWithAtAtPh(Long arg1, Long arg2, String arg3);

@Aggregate(inputType = Artwork.class)
@Facet(pipelines = {
@FacetPipeline(name = "categorizedByTags",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package com.github.krr.mongodb.aggregate.support.repository.reactive;
package com.github.krr.mongodb.aggregate.support.repository;

import com.github.krr.mongodb.aggregate.support.annotations.*;
import com.github.krr.mongodb.aggregate.support.beans.Artwork;
Expand Down
Loading

0 comments on commit a345b94

Please sign in to comment.