Search in sources :

Example 11 with DefaultParameters

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

the class SpannerStatementQueryTests method compoundNameConventionTest.

@Test
public void compoundNameConventionTest() throws NoSuchMethodException {
    when(this.queryMethod.getName()).thenReturn("findTop3DistinctByActionIgnoreCaseAndSymbolOrTraderIdAndPriceLessThanOrPriceGreater" + "ThanEqualAndIdIsNotNullAndTraderIdIsNullAndTraderIdLikeAndPriceTrueAndPriceFalse" + "AndPriceGreaterThanAndPriceLessThanEqualAndPriceIn" + "AndValueLessThanOrderByIdDesc");
    this.partTreeSpannerQuery = spy(createQuery());
    Object[] params = new Object[] { Trade.Action.BUY, "abcd", "abc123", // an int is not a natively supported type, and is intentionally used to use custom
    8, // converters
    3.33, "ignored", "ignored", "blahblah", "ignored", "ignored", 1.11, 2.22, Arrays.asList(1, 2), BigDecimal.ONE };
    when(this.spannerTemplate.query((Class<Object>) any(), any(), any())).thenAnswer((invocation) -> {
        Statement statement = invocation.getArgument(1);
        String expectedQuery = "SELECT DISTINCT shares, trader_id, ticker, price, action, id, value " + "FROM trades WHERE ( LOWER(action)=LOWER(@tag0) " + "AND ticker=@tag1 ) OR " + "( trader_id=@tag2 AND price<@tag3 ) OR ( price>=@tag4 AND id<>NULL AND " + "trader_id=NULL AND trader_id LIKE @tag7 AND price=TRUE AND price=FALSE AND " + "price>@tag10 AND price<=@tag11 AND price IN UNNEST(@tag12) " + "AND value<@tag13 ) ORDER BY id DESC LIMIT 3";
        assertThat(statement.getSql()).isEqualTo(expectedQuery);
        Map<String, Value> paramMap = statement.getParameters();
        assertThat(paramMap.get("tag0").getString()).isEqualTo(params[0].toString());
        assertThat(paramMap.get("tag1").getString()).isEqualTo(params[1]);
        assertThat(paramMap.get("tag2").getString()).isEqualTo(params[2]);
        assertThat(paramMap.get("tag3").getInt64()).isEqualTo(8L);
        assertThat(paramMap.get("tag4").getFloat64()).isEqualTo(params[4]);
        assertThat(paramMap.get("tag5").getString()).isEqualTo(params[5]);
        assertThat(paramMap.get("tag6").getString()).isEqualTo(params[6]);
        assertThat(paramMap.get("tag7").getString()).isEqualTo(params[7]);
        assertThat(paramMap.get("tag8").getString()).isEqualTo(params[8]);
        assertThat(paramMap.get("tag9").getString()).isEqualTo(params[9]);
        assertThat(paramMap.get("tag10").getFloat64()).isEqualTo(params[10]);
        assertThat(paramMap.get("tag11").getFloat64()).isEqualTo(params[11]);
        assertThat(paramMap.get("tag12").getInt64Array()).isEqualTo(((List<Integer>) params[12]).stream().map(Long::valueOf).collect(Collectors.toList()));
        assertThat(paramMap.get("tag13").getNumeric()).isEqualTo(params[13]);
        return null;
    });
    doReturn(Object.class).when(this.partTreeSpannerQuery).getReturnedSimpleConvertableItemType();
    doReturn(null).when(this.partTreeSpannerQuery).convertToSimpleReturnType(any(), any());
    // This dummy method was created so the metadata for the ARRAY param inner type is
    // provided.
    Method method = QueryHolder.class.getMethod("repositoryMethod1", Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, List.class, BigDecimal.class);
    when(this.queryMethod.getMethod()).thenReturn(method);
    doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
    this.partTreeSpannerQuery.execute(params);
    verify(this.spannerTemplate, times(1)).query((Class<Object>) any(), any(), any());
}
Also used : DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Statement(com.google.cloud.spanner.Statement) Value(com.google.cloud.spanner.Value) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 12 with DefaultParameters

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

the class SqlSpannerQueryTests method sortAndPageableQueryTest.

@Test
public void sortAndPageableQueryTest() throws NoSuchMethodException {
    String sql = "SELECT * FROM :org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQueryTests$Child:" + " WHERE id = @id AND trader_id = @trader_id";
    // @formatter:off
    String entityResolvedSql = "SELECT *, " + "ARRAY (SELECT AS STRUCT canceled, documentId, id, childId, content " + "FROM documents WHERE (documents.id = children.id AND documents.childId = children.childId) " + "AND (canceled = false)) AS documents " + "FROM (SELECT * FROM children WHERE id = @id AND trader_id = @trader_id) children " + "WHERE disabled = false ORDER BY trader_id ASC LIMIT 2 OFFSET 2";
    // @formatter:on
    Object[] params = new Object[] { "ID", "TRADER_ID", Sort.by(Order.asc("trader_id")), PageRequest.of(1, 2) };
    String[] paramNames = new String[] { "id", "trader_id", "ignoredSort", "pageable" };
    when(queryMethod.isCollectionQuery()).thenReturn(false);
    when(queryMethod.getReturnedObjectType()).thenReturn((Class) Child.class);
    EvaluationContext evaluationContext = new StandardEvaluationContext();
    for (int i = 0; i < params.length; i++) {
        evaluationContext.setVariable(paramNames[i], params[i]);
    }
    when(this.evaluationContextProvider.getEvaluationContext(any(), any())).thenReturn(evaluationContext);
    SqlSpannerQuery sqlSpannerQuery = createQuery(sql, Child.class, false);
    doAnswer((invocation) -> {
        Statement statement = invocation.getArgument(0);
        SpannerQueryOptions queryOptions = invocation.getArgument(1);
        assertThat(queryOptions.isAllowPartialRead()).isTrue();
        assertThat(statement.getSql()).isEqualTo(entityResolvedSql);
        Map<String, Value> paramMap = statement.getParameters();
        assertThat(paramMap.get("id").getString()).isEqualTo(params[0]);
        assertThat(paramMap.get("trader_id").getString()).isEqualTo(params[1]);
        assertThat(paramMap.get("ignoredSort")).isNull();
        assertThat(paramMap.get("pageable")).isNull();
        return null;
    }).when(this.spannerTemplate).executeQuery(any(), any());
    Method method = QueryHolder.class.getMethod("sortAndPageable", String.class, String.class, Sort.class, Pageable.class);
    when(this.queryMethod.getMethod()).thenReturn(method);
    Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(new DefaultParameters(method));
    sqlSpannerQuery.execute(params);
    verify(this.spannerTemplate, times(1)).executeQuery(any(), any());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Statement(com.google.cloud.spanner.Statement) Method(java.lang.reflect.Method) SpannerQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerQueryOptions) DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Value(com.google.cloud.spanner.Value) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 13 with DefaultParameters

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

the class SqlSpannerQueryTests method noPageableParamQueryTest.

@Test
public void noPageableParamQueryTest() throws NoSuchMethodException {
    String sql = "SELECT DISTINCT * FROM " + ":org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQueryTests$Trade:";
    // @formatter:off
    String entityResolvedSql = "SELECT *, " + "ARRAY (SELECT AS STRUCT disabled, id, childId, value, " + "ARRAY (SELECT AS STRUCT canceled, documentId, id, childId, content " + "FROM documents WHERE (documents.id = children.id AND documents.childId = children.childId) " + "AND (canceled = false)) AS documents " + "FROM children WHERE (children.id = trades.id) AND (disabled = false)) AS children " + "FROM (SELECT DISTINCT * FROM trades) trades";
    // @formatter:on
    final Class toReturn = Trade.class;
    when(queryMethod.isCollectionQuery()).thenReturn(false);
    when(queryMethod.getReturnedObjectType()).thenReturn(toReturn);
    EvaluationContext evaluationContext = new StandardEvaluationContext();
    when(this.evaluationContextProvider.getEvaluationContext(any(), any())).thenReturn(evaluationContext);
    SqlSpannerQuery sqlSpannerQuery = createQuery(sql, toReturn, false);
    doAnswer((invocation) -> {
        Statement statement = invocation.getArgument(0);
        SpannerQueryOptions queryOptions = invocation.getArgument(1);
        assertThat(queryOptions.isAllowPartialRead()).isTrue();
        assertThat(statement.getSql()).isEqualTo(entityResolvedSql);
        return null;
    }).when(this.spannerTemplate).executeQuery(any(), any());
    // This dummy method was created so the metadata for the ARRAY param inner type is
    // provided.
    Method method = QueryHolder.class.getMethod("dummyMethod2");
    when(this.queryMethod.getMethod()).thenReturn(method);
    Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(new DefaultParameters(method));
    sqlSpannerQuery.execute(new Object[] {});
    verify(this.spannerTemplate, times(1)).executeQuery(any(), any());
}
Also used : SpannerQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerQueryOptions) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Statement(com.google.cloud.spanner.Statement) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 14 with DefaultParameters

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

the class SqlSpannerQueryTests method sqlCountWithWhereTest.

@Test
public void sqlCountWithWhereTest() throws NoSuchMethodException {
    String sql = "SELECT count(1) FROM :org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQueryTests$Child:" + " WHERE id = @id AND trader_id = @trader_id";
    String entityResolvedSql = "SELECT count(1) FROM children WHERE id = @id AND trader_id = @trader_id";
    Object[] params = new Object[] { "ID", "TRADER_ID" };
    String[] paramNames = new String[] { "id", "trader_id" };
    when(queryMethod.isCollectionQuery()).thenReturn(false);
    when(queryMethod.getReturnedObjectType()).thenReturn((Class) long.class);
    EvaluationContext evaluationContext = new StandardEvaluationContext();
    for (int i = 0; i < params.length; i++) {
        evaluationContext.setVariable(paramNames[i], params[i]);
    }
    when(this.evaluationContextProvider.getEvaluationContext(any(), any())).thenReturn(evaluationContext);
    SqlSpannerQuery sqlSpannerQuery = createQuery(sql, long.class, false);
    doAnswer((invocation) -> {
        Statement statement = invocation.getArgument(0);
        SpannerQueryOptions queryOptions = invocation.getArgument(1);
        assertThat(queryOptions.isAllowPartialRead()).isTrue();
        assertThat(statement.getSql()).isEqualTo(entityResolvedSql);
        Map<String, Value> paramMap = statement.getParameters();
        assertThat(paramMap.get("id").getString()).isEqualTo(params[0]);
        assertThat(paramMap.get("trader_id").getString()).isEqualTo(params[1]);
        return null;
    }).when(this.spannerTemplate).executeQuery(any(), any());
    // This dummy method was created so the metadata for the ARRAY param inner type is
    // provided.
    Method method = QueryHolder.class.getMethod("dummyMethod3", String.class, String.class);
    when(this.queryMethod.getMethod()).thenReturn(method);
    Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(new DefaultParameters(method));
    sqlSpannerQuery.execute(params);
    verify(this.spannerTemplate, times(1)).executeQuery(any(), any());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Statement(com.google.cloud.spanner.Statement) Method(java.lang.reflect.Method) SpannerQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerQueryOptions) DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Value(com.google.cloud.spanner.Value) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 15 with DefaultParameters

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

the class PartTreeDatastoreQueryTests method queryWithMockResult.

private void queryWithMockResult(String queryName, List results, Method m, boolean mockOptionalNullable, ProjectionInformation projectionInformation) {
    when(this.queryMethod.getName()).thenReturn(queryName);
    doReturn(new DefaultParameters(m)).when(this.queryMethod).getParameters();
    if (mockOptionalNullable) {
        DefaultRepositoryMetadata mockMetadata = mock(DefaultRepositoryMetadata.class);
        doReturn(m.getReturnType()).when(mockMetadata).getReturnedDomainClass(m);
        DatastoreQueryMethod datastoreQueryMethod = new DatastoreQueryMethod(m, mockMetadata, mock(SpelAwareProxyProjectionFactory.class));
        doReturn(datastoreQueryMethod.isOptionalReturnType()).when(this.queryMethod).isOptionalReturnType();
        doReturn(datastoreQueryMethod.isNullable()).when(this.queryMethod).isNullable();
    }
    this.partTreeDatastoreQuery = createQuery(false, false, projectionInformation);
    when(this.datastoreTemplate.queryKeysOrEntities(any(), Mockito.<Class<Trade>>any())).thenReturn(new DatastoreResultsIterable<>(results != null ? results.iterator() : Collections.emptyIterator(), null));
}
Also used : DefaultParameters(org.springframework.data.repository.query.DefaultParameters) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) DefaultRepositoryMetadata(org.springframework.data.repository.core.support.DefaultRepositoryMetadata)

Aggregations

DefaultParameters (org.springframework.data.repository.query.DefaultParameters)15 Method (java.lang.reflect.Method)12 Test (org.junit.Test)12 Statement (com.google.cloud.spanner.Statement)9 Value (com.google.cloud.spanner.Value)8 SpannerQueryOptions (org.springframework.cloud.gcp.data.spanner.core.SpannerQueryOptions)6 EvaluationContext (org.springframework.expression.EvaluationContext)6 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)6 Struct (com.google.cloud.spanner.Struct)2 TransactionContext (com.google.cloud.spanner.TransactionContext)1 TransactionRunner (com.google.cloud.spanner.TransactionRunner)1 Before (org.junit.Before)1 JdbcMappingContext (org.springframework.data.jdbc.mapping.model.JdbcMappingContext)1 SpelAwareProxyProjectionFactory (org.springframework.data.projection.SpelAwareProxyProjectionFactory)1 DefaultRepositoryMetadata (org.springframework.data.repository.core.support.DefaultRepositoryMetadata)1 Parameters (org.springframework.data.repository.query.Parameters)1 RowMapper (org.springframework.jdbc.core.RowMapper)1