Search in sources :

Example 1 with BeforeSaveEvent

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

the class SpannerTemplate method applySaveMutations.

private void applySaveMutations(Supplier<List<Mutation>> mutationsSupplier, Iterable<?> entities, Set<String> includeProperties) {
    maybeEmitEvent(new BeforeSaveEvent(entities, includeProperties));
    List<Mutation> mutations = mutationsSupplier.get();
    applyMutations(mutations);
    maybeEmitEvent(new AfterSaveEvent(mutations, entities, includeProperties));
}
Also used : AfterSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeSaveEvent) Mutation(com.google.cloud.spanner.Mutation)

Example 2 with BeforeSaveEvent

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

the class SpannerTemplateTests method upsertTest.

@Test
public void upsertTest() {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    List<Mutation> mutations = Collections.singletonList(mutation);
    when(this.mutationFactory.upsert(same(entity), isNull())).thenReturn(mutations);
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(Collections.singletonList(entity), null), new AfterSaveEvent(mutations, Collections.singletonList(entity), null), () -> this.spannerTemplate.upsert(entity), x -> x.verify(this.databaseClient, times(1)).write(eq(mutations)));
}
Also used : AfterSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeSaveEvent) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 3 with BeforeSaveEvent

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

the class SpannerTemplateTests method insertAllTest.

@Test
public void insertAllTest() {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    List<Mutation> mutations = Arrays.asList(mutation, mutation, mutation);
    List entities = Arrays.asList(entity, entity, entity);
    when(this.mutationFactory.insert(same(entity))).thenReturn(Collections.singletonList(mutation));
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(entities, null), new AfterSaveEvent(mutations, entities, null), () -> this.spannerTemplate.insertAll(entities), x -> x.verify(this.databaseClient, times(1)).write(eq(mutations)));
}
Also used : AfterSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeSaveEvent) List(java.util.List) ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 4 with BeforeSaveEvent

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

the class SpannerTemplateTests method upsertAllTest.

@Test
public void upsertAllTest() {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    List<Mutation> mutations = Arrays.asList(mutation, mutation, mutation);
    List entities = Arrays.asList(entity, entity, entity);
    when(this.mutationFactory.upsert(same(entity), isNull())).thenReturn(Collections.singletonList(mutation));
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(entities, null), new AfterSaveEvent(mutations, entities, null), () -> this.spannerTemplate.upsertAll(entities), x -> x.verify(this.databaseClient, times(1)).write(eq(mutations)));
}
Also used : AfterSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeSaveEvent) List(java.util.List) ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 5 with BeforeSaveEvent

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

the class SpannerTemplateTests method upsertColumnsArrayTest.

@Test
public void upsertColumnsArrayTest() {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    Set<String> cols = new HashSet<>(Arrays.asList("a", "b"));
    List<Mutation> mutations = Collections.singletonList(mutation);
    when(this.mutationFactory.upsert(same(entity), eq(cols))).thenReturn(Collections.singletonList(mutation));
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(Collections.singletonList(entity), cols), new AfterSaveEvent(mutations, Collections.singletonList(entity), cols), () -> this.spannerTemplate.upsert(entity, "a", "b"), x -> x.verify(this.databaseClient, times(1)).write(eq(mutations)));
}
Also used : AfterSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeSaveEvent) Mutation(com.google.cloud.spanner.Mutation) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Mutation (com.google.cloud.spanner.Mutation)11 AfterSaveEvent (org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterSaveEvent)11 BeforeSaveEvent (org.springframework.cloud.gcp.data.spanner.core.mapping.event.BeforeSaveEvent)11 Test (org.junit.Test)10 HashSet (java.util.HashSet)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3