Search in sources :

Example 1 with AfterQueryEvent

use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplate method query.

@Override
public <T> List<T> query(Class<T> entityClass, Statement statement, SpannerQueryOptions options) {
    List<T> entities = queryAndResolveChildren(entityClass, statement, options);
    maybeEmitEvent(new AfterQueryEvent(entities, statement, options));
    return entities;
}
Also used : AfterQueryEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent)

Example 2 with AfterQueryEvent

use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method queryFuncTest.

@Test
public void queryFuncTest() {
    ResultSet resultSet = mock(ResultSet.class);
    when(resultSet.next()).thenReturn(false);
    Statement query = Statement.of("test");
    when(this.readContext.executeQuery(eq(query))).thenReturn(resultSet);
    verifyAfterEvents(new AfterQueryEvent(Collections.emptyList(), query, null), () -> assertThat(this.spannerTemplate.query(x -> null, query, null)).isEmpty(), x -> {
    });
}
Also used : TransactionCallable(com.google.cloud.spanner.TransactionRunner.TransactionCallable) Arrays(java.util.Arrays) BeforeSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeSaveEvent) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BeforeDeleteEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeDeleteEvent) Timestamp(com.google.cloud.Timestamp) DatabaseClient(com.google.cloud.spanner.DatabaseClient) CachingComposingSupplier(org.springframework.cloud.gcp.data.spanner.core.admin.CachingComposingSupplier) ResultSet(com.google.cloud.spanner.ResultSet) Column(org.springframework.cloud.gcp.data.spanner.core.mapping.Column) SpannerWriteConverter(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerWriteConverter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Key(com.google.cloud.spanner.Key) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) AfterExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterExecuteDmlEvent) Mockito.doReturn(org.mockito.Mockito.doReturn) TransactionContext(com.google.cloud.spanner.TransactionContext) Set(java.util.Set) Mutation(com.google.cloud.spanner.Mutation) Embedded(org.springframework.cloud.gcp.data.spanner.core.mapping.Embedded) List(java.util.List) AfterDeleteEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterDeleteEvent) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) AfterSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterSaveEvent) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ReadContext(com.google.cloud.spanner.ReadContext) Interleaved(org.springframework.cloud.gcp.data.spanner.core.mapping.Interleaved) SpannerEntityProcessor(org.springframework.cloud.gcp.data.spanner.core.convert.SpannerEntityProcessor) Table(org.springframework.cloud.gcp.data.spanner.core.mapping.Table) TransactionRunner(com.google.cloud.spanner.TransactionRunner) BeforeExecuteDmlEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeExecuteDmlEvent) Mockito.spy(org.mockito.Mockito.spy) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ByteArray(com.google.cloud.ByteArray) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) SpannerSchemaUtils(org.springframework.cloud.gcp.data.spanner.core.admin.SpannerSchemaUtils) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) InOrder(org.mockito.InOrder) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) TimestampBound(com.google.cloud.spanner.TimestampBound) KeySet(com.google.cloud.spanner.KeySet) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Statement(com.google.cloud.spanner.Statement) ApplicationEvent(org.springframework.context.ApplicationEvent) ReadOption(com.google.cloud.spanner.Options.ReadOption) Mockito(org.mockito.Mockito) Where(org.springframework.cloud.gcp.data.spanner.core.mapping.Where) Rule(org.junit.Rule) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) AfterQueryEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent) Collections(java.util.Collections) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) AfterQueryEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) Test(org.junit.Test)

Example 3 with AfterQueryEvent

use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplate method query.

@Override
public <A> List<A> query(Function<Struct, A> rowFunc, Statement statement, SpannerQueryOptions options) {
    ArrayList<A> result = new ArrayList<>();
    try (ResultSet resultSet = executeQuery(statement, options)) {
        while (resultSet.next()) {
            result.add(rowFunc.apply(resultSet.getCurrentRowAsStruct()));
        }
    }
    maybeEmitEvent(new AfterQueryEvent(result, statement, options));
    return result;
}
Also used : AfterQueryEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent) ArrayList(java.util.ArrayList) ResultSet(com.google.cloud.spanner.ResultSet)

Example 4 with AfterQueryEvent

use of org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method queryTest.

@Test
public void queryTest() {
    when(this.readContext.read(any(), any(), any())).thenReturn(null);
    Statement query = Statement.of("test");
    verifyAfterEvents(new AfterQueryEvent(Collections.emptyList(), query, null), () -> assertThat(this.spannerTemplate.query(TestEntity.class, query, null)).isEmpty(), x -> {
    });
    verify(this.databaseClient, times(1)).singleUse();
}
Also used : AfterQueryEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent) Statement(com.google.cloud.spanner.Statement) Test(org.junit.Test)

Aggregations

AfterQueryEvent (org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent)4 ResultSet (com.google.cloud.spanner.ResultSet)2 Statement (com.google.cloud.spanner.Statement)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ByteArray (com.google.cloud.ByteArray)1 Timestamp (com.google.cloud.Timestamp)1 DatabaseClient (com.google.cloud.spanner.DatabaseClient)1 Key (com.google.cloud.spanner.Key)1 KeySet (com.google.cloud.spanner.KeySet)1 Mutation (com.google.cloud.spanner.Mutation)1 ReadOption (com.google.cloud.spanner.Options.ReadOption)1 ReadContext (com.google.cloud.spanner.ReadContext)1 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)1 TimestampBound (com.google.cloud.spanner.TimestampBound)1 TransactionContext (com.google.cloud.spanner.TransactionContext)1 TransactionRunner (com.google.cloud.spanner.TransactionRunner)1 TransactionCallable (com.google.cloud.spanner.TransactionRunner.TransactionCallable)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1