Search in sources :

Example 1 with QueryMethod

use of org.springframework.data.repository.query.QueryMethod in project spring-data-keyvalue by spring-projects.

the class KeyValuePartTreeQueryUnitTests method shouldApplyPageableParameterToCollectionQuery.

// DATAKV-142
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldApplyPageableParameterToCollectionQuery() throws SecurityException, NoSuchMethodException {
    when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
    when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
    QueryMethod qm = new QueryMethod(Repo.class.getMethod("findBy", Pageable.class), metadataMock, projectionFactoryMock);
    KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, kvOpsMock, SpelQueryCreator.class);
    KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { PageRequest.of(2, 3) });
    assertThat(query.getOffset(), is(6L));
    assertThat(query.getRows(), is(3));
}
Also used : Pageable(org.springframework.data.domain.Pageable) QueryMethod(org.springframework.data.repository.query.QueryMethod) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) Person(org.springframework.data.keyvalue.Person) Test(org.junit.Test)

Example 2 with QueryMethod

use of org.springframework.data.repository.query.QueryMethod in project spring-data-keyvalue by spring-projects.

the class KeyValuePartTreeQueryUnitTests method shouldApplyDerivedMaxResultsToQuery.

// DATAKV-142
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldApplyDerivedMaxResultsToQuery() throws SecurityException, NoSuchMethodException {
    when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
    when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
    QueryMethod qm = new QueryMethod(Repo.class.getMethod("findTop3By"), metadataMock, projectionFactoryMock);
    KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, kvOpsMock, SpelQueryCreator.class);
    KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] {});
    assertThat(query.getRows(), is(3));
}
Also used : QueryMethod(org.springframework.data.repository.query.QueryMethod) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) Person(org.springframework.data.keyvalue.Person) Test(org.junit.Test)

Example 3 with QueryMethod

use of org.springframework.data.repository.query.QueryMethod in project spring-data-keyvalue by spring-projects.

the class SpelQueryEngineUnitTests method createQueryForMethodWithArgs.

private static SpelCriteria createQueryForMethodWithArgs(String methodName, Object... args) throws Exception {
    List<Class<?>> types = new ArrayList<>(args.length);
    for (Object arg : args) {
        types.add(arg.getClass());
    }
    Method method = PersonRepository.class.getMethod(methodName, types.toArray(new Class<?>[types.size()]));
    RepositoryMetadata metadata = mock(RepositoryMetadata.class);
    doReturn(method.getReturnType()).when(metadata).getReturnedDomainClass(method);
    PartTree partTree = new PartTree(method.getName(), method.getReturnType());
    SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(new QueryMethod(method, metadata, new SpelAwareProxyProjectionFactory()).getParameters(), args));
    return new SpelCriteria(creator.createQuery().getCriteria(), new StandardEvaluationContext(args));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) QueryMethod(org.springframework.data.repository.query.QueryMethod) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) ArrayList(java.util.ArrayList) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) RepositoryMetadata(org.springframework.data.repository.core.RepositoryMetadata) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) PartTree(org.springframework.data.repository.query.parser.PartTree) SpelQueryCreator(org.springframework.data.keyvalue.repository.query.SpelQueryCreator)

Example 4 with QueryMethod

use of org.springframework.data.repository.query.QueryMethod in project spring-data-jdbc by spring-projects.

the class CriteriaFactoryUnitTests method shouldConsiderArrayValuesInInOperator.

// DATAJDBC-539
@Test
void shouldConsiderArrayValuesInInOperator() {
    QueryMethod queryMethod = getQueryMethod("findAllByNameIn", String[].class);
    RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { new String[] { "foo", "bar" } });
    ParameterMetadataProvider parameterMetadata = new ParameterMetadataProvider(accessor);
    CriteriaFactory criteriaFactory = new CriteriaFactory(parameterMetadata);
    Part part = new Part("NameIn", User.class);
    Criteria criteria = criteriaFactory.createCriteria(part);
    assertThat(criteria.getValue()).isEqualTo(Arrays.asList("foo", "bar"));
}
Also used : QueryMethod(org.springframework.data.repository.query.QueryMethod) Part(org.springframework.data.repository.query.parser.Part) Criteria(org.springframework.data.relational.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 5 with QueryMethod

use of org.springframework.data.repository.query.QueryMethod in project spring-data-keyvalue by spring-projects.

the class KeyValuePartTreeQueryUnitTests method spelExpressionAndContextShouldNotBeReused.

// DATAKV-115
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void spelExpressionAndContextShouldNotBeReused() throws NoSuchMethodException, SecurityException {
    when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
    when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
    QueryMethod qm = new QueryMethod(Repo.class.getMethod("findByFirstname", String.class), metadataMock, projectionFactoryMock);
    KeyValuePartTreeQuery query = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, kvOpsMock, SpelQueryCreator.class);
    Object[] args = new Object[] { "foo" };
    Object first = query.prepareQuery(args).getCriteria();
    Object second = query.prepareQuery(args).getCriteria();
    assertThat(first, not(sameInstance(second)));
}
Also used : QueryMethod(org.springframework.data.repository.query.QueryMethod) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) Person(org.springframework.data.keyvalue.Person) Test(org.junit.Test)

Aggregations

QueryMethod (org.springframework.data.repository.query.QueryMethod)10 Method (java.lang.reflect.Method)6 Test (org.junit.Test)5 Person (org.springframework.data.keyvalue.Person)4 Test (org.junit.jupiter.api.Test)2 SpelCriteria (org.springframework.data.keyvalue.core.SpelCriteria)2 SpelAwareProxyProjectionFactory (org.springframework.data.projection.SpelAwareProxyProjectionFactory)2 Criteria (org.springframework.data.relational.core.query.Criteria)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 Part (org.springframework.data.repository.query.parser.Part)2 PartTree (org.springframework.data.repository.query.parser.PartTree)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 ArrayList (java.util.ArrayList)1 Pageable (org.springframework.data.domain.Pageable)1 SpelQueryCreator (org.springframework.data.keyvalue.repository.query.SpelQueryCreator)1 RepositoryMetadata (org.springframework.data.repository.core.RepositoryMetadata)1 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)1