Search in sources :

Example 11 with Parameters

use of org.springframework.data.repository.query.Parameters in project spring-cloud-gcp by spring-cloud.

the class GqlDatastoreQueryTests method pageableTestPageCursor.

@Test
public void pageableTestPageCursor() {
    String gql = "SELECT * FROM trades WHERE price=@price";
    String expected = "SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset";
    Cursor cursorInPageable = Cursor.copyFrom("cde".getBytes());
    long countInPageable = 123L;
    Object[] paramVals = new Object[] { 1, new DatastorePageable(PageRequest.of(0, 2), cursorInPageable, countInPageable) };
    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()).isEqualTo(expected);
        Map<String, Object> paramMap = statement.getNamedBindings();
        assertThat(paramMap.size()).isEqualTo(3);
        assertThat(((Value) paramMap.get("price")).get()).isEqualTo(1L);
        assertThat(((Value) paramMap.get("limit")).get()).isEqualTo(2L);
        assertThat(paramMap.get("offset")).isEqualTo(cursorInPageable);
        return new DatastoreResultsIterable(Collections.emptyList(), cursor);
    }).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(countInPageable);
    assertThat(((Page) result).getTotalElements()).isEqualTo(countInPageable);
    verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), eq(Trade.class));
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) Page(org.springframework.data.domain.Page) Cursor(com.google.cloud.datastore.Cursor) GqlQuery(com.google.cloud.datastore.GqlQuery) Slice(org.springframework.data.domain.Slice) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Test(org.junit.Test)

Example 12 with Parameters

use of org.springframework.data.repository.query.Parameters in project spring-cloud-gcp by spring-cloud.

the class GqlDatastoreQueryTests method pageableTestSort.

@Test
public void pageableTestSort() {
    String gql = "SELECT * FROM trades WHERE price=@price";
    Object[] paramVals = new Object[] { 1, Sort.by(Sort.Order.asc("p1"), Sort.Order.desc("p2")) };
    String[] paramNames = new String[] { "price", null };
    Parameters parameters = buildParameters(paramVals, paramNames);
    when(parameters.hasSortParameter()).thenReturn(true);
    when(parameters.getSortIndex()).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 ORDER BY p1 ASC, p2 DESC");
        Map<String, Value> paramMap = statement.getNamedBindings();
        assertThat(paramMap.get("price").get()).isEqualTo(1L);
        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));
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) GqlQuery(com.google.cloud.datastore.GqlQuery) Test(org.junit.Test)

Example 13 with Parameters

use of org.springframework.data.repository.query.Parameters in project spring-cloud-gcp by spring-cloud.

the class GqlDatastoreQueryTests method pageableTestSlice.

@Test
public void pageableTestSlice() {
    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);
    Mockito.<Class>when(this.queryMethod.getReturnedObjectType()).thenReturn(Trade.class);
    when(parameters.hasPageableParameter()).thenReturn(true);
    when(parameters.getPageableIndex()).thenReturn(1);
    GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, false, true);
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    List<Map> params = new ArrayList<>();
    doAnswer((invocation) -> {
        GqlQuery statement = invocation.getArgument(0);
        assertThat(statement.getQueryString()).isEqualTo("SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset");
        Map paramMap = statement.getNamedBindings();
        params.add(paramMap);
        return new DatastoreResultsIterable(Collections.emptyList(), cursor);
    }).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 = (Slice) gqlDatastoreQuery.execute(paramVals);
    assertThat(((DatastorePageable) result.getPageable()).toCursor()).isEqualTo(cursor);
    verify(this.datastoreTemplate, times(2)).queryKeysOrEntities(any(), eq(Trade.class));
    assertThat(((Value) params.get(0).get("price")).get()).isEqualTo(1L);
    assertThat(((Value) params.get(0).get("limit")).get()).isEqualTo(2L);
    assertThat(((Value) params.get(0).get("offset")).get()).isEqualTo(0L);
    assertThat(((Value) params.get(1).get("price")).get()).isEqualTo(1L);
    assertThat(((Value) params.get(1).get("limit")).get()).isEqualTo(1L);
    assertThat(params.get(1).get("offset")).isEqualTo(cursor);
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) ArrayList(java.util.ArrayList) Cursor(com.google.cloud.datastore.Cursor) GqlQuery(com.google.cloud.datastore.GqlQuery) Slice(org.springframework.data.domain.Slice) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Parameters (org.springframework.data.repository.query.Parameters)13 Test (org.junit.Test)7 DoubleValue (com.google.cloud.datastore.DoubleValue)5 GqlQuery (com.google.cloud.datastore.GqlQuery)5 KeyValue (com.google.cloud.datastore.KeyValue)5 LongValue (com.google.cloud.datastore.LongValue)5 Value (com.google.cloud.datastore.Value)5 Parameter (org.springframework.data.repository.query.Parameter)4 Cursor (com.google.cloud.datastore.Cursor)3 DatastoreResultsIterable (org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable)3 Slice (org.springframework.data.domain.Slice)3 BeanFactoryAccessor (org.springframework.context.expression.BeanFactoryAccessor)2 BeanFactoryResolver (org.springframework.context.expression.BeanFactoryResolver)2 Page (org.springframework.data.domain.Page)2 NamedQueries (org.springframework.data.repository.core.NamedQueries)2 QueryMethodEvaluationContextProvider (org.springframework.data.repository.query.QueryMethodEvaluationContextProvider)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1