use of org.springframework.data.repository.query.Parameters in project spring-cloud-gcp by spring-cloud.
the class SpannerRepositoryFactory method delegateContextProvider.
private QueryMethodEvaluationContextProvider delegateContextProvider(QueryMethodEvaluationContextProvider evaluationContextProvider) {
return new QueryMethodEvaluationContextProvider() {
@Override
public <T extends Parameters<?, ?>> EvaluationContext getEvaluationContext(T parameters, Object[] parameterValues) {
StandardEvaluationContext evaluationContext = (StandardEvaluationContext) evaluationContextProvider.getEvaluationContext(parameters, parameterValues);
evaluationContext.setRootObject(SpannerRepositoryFactory.this.applicationContext);
evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());
evaluationContext.setBeanResolver(new BeanFactoryResolver(SpannerRepositoryFactory.this.applicationContext));
return evaluationContext;
}
};
}
use of org.springframework.data.repository.query.Parameters in project spring-cloud-gcp by spring-cloud.
the class SpannerQueryLookupStrategyTests method resolveSqlQueryTest.
@Test
public void resolveSqlQueryTest() {
String queryName = "fakeNamedQueryName";
String query = "fake query";
when(this.queryMethod.getNamedQueryName()).thenReturn(queryName);
NamedQueries namedQueries = mock(NamedQueries.class);
Parameters parameters = mock(Parameters.class);
// @formatter:off
Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(parameters);
// @formatter:off
when(parameters.getNumberOfParameters()).thenReturn(1);
when(parameters.getParameter(anyInt())).thenAnswer((invocation) -> {
Parameter param = mock(Parameter.class);
when(param.getName()).thenReturn(Optional.of("tag"));
// @formatter:off
Mockito.<Class>when(param.getType()).thenReturn(Object.class);
// @formatter:on
return param;
});
when(namedQueries.hasQuery(eq(queryName))).thenReturn(true);
when(namedQueries.getQuery(eq(queryName))).thenReturn(query);
this.spannerQueryLookupStrategy.resolveQuery(null, null, null, namedQueries);
verify(this.spannerQueryLookupStrategy, times(1)).createSqlSpannerQuery(eq(Object.class), same(this.queryMethod), eq(query), eq(false));
}
use of org.springframework.data.repository.query.Parameters in project spring-cloud-gcp by spring-cloud.
the class GqlDatastoreQueryTests method pageableTestPage.
@Test
public void pageableTestPage() {
String gql = "SELECT * FROM trades WHERE price=@price";
String expected = "SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset";
Object[] paramVals = new Object[] { 1, PageRequest.of(0, 2) };
String[] paramNames = new String[] { "price", null };
Parameters parameters = buildParameters(paramVals, paramNames);
Mockito.<Class>when(this.queryMethod.getReturnedObjectType()).thenReturn(Trade.class);
when(parameters.hasPageableParameter()).thenReturn(true);
when(parameters.getPageableIndex()).thenReturn(1);
GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, true, true);
Cursor cursor = Cursor.copyFrom("abc".getBytes());
doAnswer((invocation) -> {
GqlQuery statement = invocation.getArgument(0);
assertThat(statement.getQueryString().equals(gql) || statement.getQueryString().equals(expected)).isEqualTo(true);
Map<String, Value> paramMap = statement.getNamedBindings();
if (statement.getQueryString().equals(expected)) {
assertThat(paramMap.size()).isEqualTo(3);
assertThat(paramMap.get("price").get()).isEqualTo(1L);
assertThat(paramMap.get("limit").get()).isEqualTo(2L);
assertThat(paramMap.get("offset").get()).isEqualTo(0L);
return new DatastoreResultsIterable(Collections.emptyList(), cursor);
} else if (statement.getQueryString().equals(gql)) {
assertThat(paramMap.size()).isEqualTo(1);
assertThat(paramMap.get("price").get()).isEqualTo(1L);
return new DatastoreResultsIterable(Arrays.asList(1L, 2L), cursor);
}
return null;
}).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
doAnswer((invocation) -> invocation.getArgument(0)).when(gqlDatastoreQuery).processRawObjectForProjection(any());
Slice result = (Page) gqlDatastoreQuery.execute(paramVals);
assertThat(((DatastorePageable) result.getPageable()).toCursor()).isEqualTo(cursor);
assertThat(((DatastorePageable) result.getPageable()).getTotalCount()).isEqualTo(2L);
assertThat(((Page) result).getTotalElements()).isEqualTo(2L);
verify(this.datastoreTemplate, times(2)).queryKeysOrEntities(any(), eq(Trade.class));
}
use of org.springframework.data.repository.query.Parameters in project spring-cloud-gcp by spring-cloud.
the class GqlDatastoreQueryTests method buildParameters.
private Parameters buildParameters(Object[] params, String[] paramNames) {
Parameters parameters = mock(Parameters.class);
Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(parameters);
when(parameters.getNumberOfParameters()).thenReturn(paramNames.length);
when(parameters.getParameter(anyInt())).thenAnswer((invocation) -> {
int index = invocation.getArgument(0);
Parameter param = mock(Parameter.class);
when(param.getName()).thenReturn(paramNames[index] == null ? Optional.empty() : Optional.of(paramNames[index]));
Mockito.<Class>when(param.getType()).thenReturn(params[index].getClass());
return param;
});
return parameters;
}
use of org.springframework.data.repository.query.Parameters in project spring-cloud-gcp by spring-cloud.
the class GqlDatastoreQueryTests method pageableTest.
@Test
public void pageableTest() {
String gql = "SELECT * FROM trades WHERE price=@price";
Object[] paramVals = new Object[] { 1, PageRequest.of(0, 2) };
String[] paramNames = new String[] { "price", null };
Parameters parameters = buildParameters(paramVals, paramNames);
when(parameters.hasPageableParameter()).thenReturn(true);
when(parameters.getPageableIndex()).thenReturn(1);
GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, false, false);
doAnswer((invocation) -> {
GqlQuery statement = invocation.getArgument(0);
assertThat(statement.getQueryString()).isEqualTo("SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset");
Map<String, Value> paramMap = statement.getNamedBindings();
assertThat(paramMap.get("price").get()).isEqualTo(1L);
assertThat(paramMap.get("limit").get()).isEqualTo(2L);
assertThat(paramMap.get("offset").get()).isEqualTo(0L);
return null;
}).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
gqlDatastoreQuery.execute(paramVals);
verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), eq(Trade.class));
}
Aggregations