Search in sources :

Example 1 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerMutationFactoryImpl method delete.

@Override
public <T> Mutation delete(Class<T> entityClass, Iterable<? extends T> entities) {
    SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(entityClass);
    KeySet.Builder builder = KeySet.newBuilder();
    for (T entity : entities) {
        PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(entity);
        PersistentProperty idProperty = persistentEntity.getIdProperty();
        Key value = (Key) accessor.getProperty(idProperty);
        builder.addKey(value);
    }
    return delete(entityClass, builder.build());
}
Also used : KeySet(com.google.cloud.spanner.KeySet) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PersistentProperty(org.springframework.data.mapping.PersistentProperty) Key(com.google.cloud.spanner.Key)

Example 2 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerMutationFactoryImplTests method deleteKeyTest.

@Test
public void deleteKeyTest() {
    Key key = Key.of("key1");
    Mutation mutation = this.spannerMutationFactory.delete(TestEntity.class, key);
    assertEquals("custom_test_table", mutation.getTable());
    assertEquals(Op.DELETE, mutation.getOperation());
    List<String> keys = new ArrayList<>();
    mutation.getKeySet().getKeys().forEach((k) -> {
        keys.add((String) (k.getParts().iterator().next()));
    });
    assertThat(keys, containsInAnyOrder("key1"));
}
Also used : ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation) Key(com.google.cloud.spanner.Key) Test(org.junit.Test)

Example 3 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerRepositoryImplTests method existsByIdTestFound.

@Test
public void existsByIdTestFound() {
    SpannerOperations operations = mock(SpannerOperations.class);
    Key key = Key.of("key");
    Object ret = new Object();
    when(operations.find(eq(Object.class), eq(key))).thenReturn(ret);
    assertTrue(new SimpleSpannerRepository(operations, Object.class).existsById(key));
}
Also used : SpannerOperations(org.springframework.cloud.gcp.data.spanner.core.SpannerOperations) Key(com.google.cloud.spanner.Key) Test(org.junit.Test)

Example 4 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerRepositoryImplTests method deleteByIdTest.

@Test
public void deleteByIdTest() {
    SpannerOperations operations = mock(SpannerOperations.class);
    Key key = Key.of("key");
    new SimpleSpannerRepository(operations, Object.class).deleteById(key);
    verify(operations, times(1)).delete(eq(Object.class), eq(key));
}
Also used : SpannerOperations(org.springframework.cloud.gcp.data.spanner.core.SpannerOperations) Key(com.google.cloud.spanner.Key) Test(org.junit.Test)

Example 5 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerRepositoryImplTests method findByIdTest.

@Test
public void findByIdTest() {
    SpannerOperations operations = mock(SpannerOperations.class);
    Key key = Key.of("key");
    Object ret = new Object();
    when(operations.find(eq(Object.class), eq(key))).thenReturn(ret);
    assertEquals(ret, new SimpleSpannerRepository(operations, Object.class).findById(key).get());
    verify(operations, times(1)).find(eq(Object.class), eq(key));
}
Also used : SpannerOperations(org.springframework.cloud.gcp.data.spanner.core.SpannerOperations) Key(com.google.cloud.spanner.Key) Test(org.junit.Test)

Aggregations

Key (com.google.cloud.spanner.Key)8 Test (org.junit.Test)5 SpannerOperations (org.springframework.cloud.gcp.data.spanner.core.SpannerOperations)3 Mutation (com.google.cloud.spanner.Mutation)2 KeySet (com.google.cloud.spanner.KeySet)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Schema (org.apache.beam.sdk.schemas.Schema)1 PersistentProperty (org.springframework.data.mapping.PersistentProperty)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1