Search in sources :

Example 6 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 upsertColumnsSetTest.

@Test
public void upsertColumnsSetTest() {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    List<Mutation> mutations = Collections.singletonList(mutation);
    Set<String> cols = new HashSet<>(Arrays.asList("a", "b"));
    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, cols), 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)

Example 7 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 insertTest.

@Test
public void insertTest() {
    Mutation mutation = Mutation.newInsertBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    List<Mutation> mutations = Collections.singletonList(mutation);
    when(this.mutationFactory.insert(entity)).thenReturn(mutations);
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(Collections.singletonList(entity), null), new AfterSaveEvent(mutations, Collections.singletonList(entity), null), () -> this.spannerTemplate.insert(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 8 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 updateTest.

@Test
public void updateTest() {
    Mutation mutation = Mutation.newUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    List<Mutation> mutations = Collections.singletonList(mutation);
    when(this.mutationFactory.update(entity, null)).thenReturn(mutations);
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(Collections.singletonList(entity), null), new AfterSaveEvent(mutations, Collections.singletonList(entity), null), () -> this.spannerTemplate.update(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 9 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 updateColumnsSetTest.

@Test
public void updateColumnsSetTest() {
    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.update(same(entity), eq(cols))).thenReturn(mutations);
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(Collections.singletonList(entity), cols), new AfterSaveEvent(mutations, Collections.singletonList(entity), cols), () -> this.spannerTemplate.update(entity, cols), 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)

Example 10 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 updateAllTest.

@Test
public void updateAllTest() {
    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.update(same(entity), isNull())).thenReturn(Collections.singletonList(mutation));
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(entities, null), new AfterSaveEvent(mutations, entities, null), () -> this.spannerTemplate.updateAll(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)

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