Search in sources :

Example 11 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method assertBindsDistanceToQuery.

private void assertBindsDistanceToQuery(Point point, Distance distance, Query reference) throws Exception {
    PartTree tree = new PartTree("findByLocationNearAndFirstname", org.springframework.data.mongodb.repository.Person.class);
    Method method = PersonRepository.class.getMethod("findByLocationNearAndFirstname", Point.class, Distance.class, String.class);
    MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), new SpelAwareProxyProjectionFactory(), new MongoMappingContext());
    MongoParameterAccessor accessor = new MongoParametersParameterAccessor(queryMethod, new Object[] { point, distance, "Dave" });
    Query query = new MongoQueryCreator(tree, new ConvertingParameterAccessor(converter, accessor), context).createQuery();
    assertThat(query).isEqualTo(query);
}
Also used : DefaultRepositoryMetadata(org.springframework.data.repository.core.support.DefaultRepositoryMetadata) Query(org.springframework.data.mongodb.core.query.Query) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) DefaultRepositoryMetadata(org.springframework.data.repository.core.support.DefaultRepositoryMetadata) Method(java.lang.reflect.Method) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) PartTree(org.springframework.data.repository.query.parser.PartTree)

Example 12 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method notLikeShouldBeTreatedCorrectlyWhenUsedWithWildcardOnly.

// DATAMONGO-1424
@Test
void notLikeShouldBeTreatedCorrectlyWhenUsedWithWildcardOnly() {
    PartTree tree = new PartTree("findByUsernameNotLike", User.class);
    ConvertingParameterAccessor accessor = getAccessor(converter, "*");
    Query query = new MongoQueryCreator(tree, accessor, context).createQuery();
    assertThat(query.getQueryObject().toJson()).isEqualTo(query(where("username").not().regex(".*")).getQueryObject().toJson());
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Example 13 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method createsQueryWithStartingWithPredicateCorrectly.

// DATAMONGO-418
@Test
void createsQueryWithStartingWithPredicateCorrectly() {
    PartTree tree = new PartTree("findByUsernameStartingWith", User.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "Matt"), context);
    Query query = creator.createQuery();
    assertThat(query).isEqualTo(query(where("username").regex("^Matt")));
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Example 14 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method createsIsNullQueryCorrectly.

@Test
void createsIsNullQueryCorrectly() {
    PartTree tree = new PartTree("findByFirstNameIsNull", Person.class);
    Query query = new MongoQueryCreator(tree, getAccessor(converter), context).createQuery();
    assertThat(query).isEqualTo(new Query(Criteria.where("firstName").is(null)));
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Example 15 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method shouldOnlyGenerateLikeExpressionsForStringPropertiesIfAllIgnoreCase.

// DATAMONGO-770
@Test
void shouldOnlyGenerateLikeExpressionsForStringPropertiesIfAllIgnoreCase() {
    PartTree tree = new PartTree("findByFirstNameAndAgeAllIgnoreCase", Person.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "dave", 42), context);
    Query query = creator.createQuery();
    assertThat(query).isEqualTo(query(where("firstName").regex("^dave$", "i").and("age").is(42)));
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Aggregations

PartTree (org.springframework.data.repository.query.parser.PartTree)60 Test (org.junit.jupiter.api.Test)51 Query (org.springframework.data.mongodb.core.query.Query)44 GeoJsonPoint (org.springframework.data.mongodb.core.geo.GeoJsonPoint)8 Point (org.springframework.data.geo.Point)6 Method (java.lang.reflect.Method)4 Distance (org.springframework.data.geo.Distance)4 SpelAwareProxyProjectionFactory (org.springframework.data.projection.SpelAwareProxyProjectionFactory)4 QueryMethod (org.springframework.data.repository.query.QueryMethod)3 Part (org.springframework.data.repository.query.parser.Part)3 DefaultRepositoryMetadata (org.springframework.data.repository.core.support.DefaultRepositoryMetadata)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 MongoException (com.mongodb.MongoException)1 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 Document (org.bson.Document)1 ObjectId (org.bson.types.ObjectId)1 NotNull (org.jetbrains.annotations.NotNull)1 Test (org.junit.Test)1