Search in sources :

Example 6 with DefaultParameters

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

the class SpannerStatementQueryTests method unsupportedParamTypeTest.

@Test
public void unsupportedParamTypeTest() throws NoSuchMethodException {
    this.expectedEx.expect(IllegalArgumentException.class);
    this.expectedEx.expectMessage("is not a supported type: class org.springframework." + "cloud.gcp.data.spanner.repository.query.SpannerStatementQueryTests$Trade");
    when(this.queryMethod.getName()).thenReturn("findTop3DistinctIdActionPriceByActionAndSymbolOrTraderIdAndPriceLessThanOrPriceGreater" + "ThanEqualAndIdIsNotNullAndTraderIdIsNullOrderByIdDesc");
    this.partTreeSpannerQuery = createQuery();
    Method method = QueryHolder.class.getMethod("repositoryMethod2", Object.class, Object.class, Object.class, Object.class, Object.class, Trade.class, Object.class);
    doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
    // This parameter is an unsupported type for Spanner SQL.
    Object[] params = new Object[] { "BUY", "abcd", "abc123", 8.88, 3.33, new Trade(), "ignored" };
    this.partTreeSpannerQuery.execute(params);
}
Also used : DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 7 with DefaultParameters

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

the class SpannerStatementQueryTests method compoundNameConventionCountTest.

@Test
public void compoundNameConventionCountTest() throws NoSuchMethodException {
    when(this.queryMethod.getName()).thenReturn("existsDistinctByActionIgnoreCaseAndSymbolOrTraderIdAndPriceLessThanOrPriceGreater" + "ThanEqualAndIdIsNotNullAndTraderIdIsNullAndTraderIdLikeAndPriceTrueAndPriceFalse" + "AndPriceGreaterThanAndPriceLessThanEqualOrderByIdDesc");
    this.partTreeSpannerQuery = spy(createQuery());
    when(this.spannerTemplate.query((Function<Struct, Object>) any(), any(), any())).thenReturn(Collections.singletonList(1L));
    Object[] params = new Object[] { Trade.Action.BUY, "abcd", "abc123", 8.88, 3.33, "ignored", "ignored", "blahblah", "ignored", "ignored", 1.11, 2.22 };
    Method method = QueryHolder.class.getMethod("repositoryMethod3", Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class);
    doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
    when(this.spannerTemplate.query((Function<Struct, Object>) any(), any(), any())).thenAnswer((invocation) -> {
        Statement statement = invocation.getArgument(1);
        String expectedSql = "SELECT EXISTS" + "(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 ) ORDER BY id DESC LIMIT 1)";
        assertThat(statement.getSql()).isEqualTo(expectedSql);
        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").getFloat64()).isEqualTo(params[3]);
        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]);
        return null;
    });
    doReturn(Object.class).when(this.partTreeSpannerQuery).getReturnedSimpleConvertableItemType();
    doReturn(null).when(this.partTreeSpannerQuery).convertToSimpleReturnType(any(), any());
    this.partTreeSpannerQuery.execute(params);
    verify(this.spannerTemplate, times(1)).query((Function<Struct, 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) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 8 with DefaultParameters

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

the class SpannerStatementQueryTests method unspecifiedParametersTest.

@Test
public void unspecifiedParametersTest() throws NoSuchMethodException {
    this.expectedEx.expect(IllegalArgumentException.class);
    this.expectedEx.expectMessage("The number of tags does not match the number of params.");
    when(this.queryMethod.getName()).thenReturn("findTop3DistinctIdActionPriceByActionAndSymbolOrTraderIdAndPriceLessThanOrPriceGreater" + "ThanEqualAndIdIsNotNullAndTraderIdIsNullOrderByIdDesc");
    this.partTreeSpannerQuery = createQuery();
    Method method = QueryHolder.class.getMethod("repositoryMethod4", Object.class, Object.class, Object.class);
    doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
    // There are too few params specified, so the exception will occur.
    Object[] params = new Object[] { "BUY", "abcd", "abc123" };
    this.partTreeSpannerQuery.execute(params);
}
Also used : DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 9 with DefaultParameters

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

the class SpannerStatementQueryTests method unSupportedPredicateTest.

@Test
public void unSupportedPredicateTest() throws NoSuchMethodException {
    this.expectedEx.expect(UnsupportedOperationException.class);
    this.expectedEx.expectMessage("The statement type: BETWEEN (2): [IsBetween, " + "Between] is not supported.");
    when(this.queryMethod.getName()).thenReturn("countByTraderIdBetween");
    Method method = Object.class.getMethod("toString");
    doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
    this.partTreeSpannerQuery = createQuery();
    this.partTreeSpannerQuery.execute(EMPTY_PARAMETERS);
}
Also used : DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 10 with DefaultParameters

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

the class JdbcRepositoryQueryUnitTests method setup.

@Before
public void setup() throws NoSuchMethodException {
    Parameters parameters = new DefaultParameters(JdbcRepositoryQueryUnitTests.class.getDeclaredMethod("dummyMethod"));
    queryMethod = mock(JdbcQueryMethod.class);
    when(queryMethod.getParameters()).thenReturn(parameters);
    context = mock(JdbcMappingContext.class, RETURNS_DEEP_STUBS);
    defaultRowMapper = mock(RowMapper.class);
}
Also used : DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Parameters(org.springframework.data.repository.query.Parameters) DefaultParameters(org.springframework.data.repository.query.DefaultParameters) JdbcMappingContext(org.springframework.data.jdbc.mapping.model.JdbcMappingContext) RowMapper(org.springframework.jdbc.core.RowMapper) Before(org.junit.Before)

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