Search in sources :

Example 1 with AfterDeleteEvent

use of com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreTemplate method performDelete.

private void performDelete(Key[] keys, Iterable ids, Iterable entities, Class entityClass) {
    maybeEmitEvent(new BeforeDeleteEvent(keys, entityClass, ids, entities));
    SliceUtil.sliceAndExecute(keys, this.maxWriteSize, getDatastoreReadWriter()::delete);
    maybeEmitEvent(new AfterDeleteEvent(keys, entityClass, ids, entities));
}
Also used : AfterDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent) BeforeDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeDeleteEvent)

Example 2 with AfterDeleteEvent

use of com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreTemplateTests method deleteByIdTest.

@Test
void deleteByIdTest() {
    when(this.objectToKeyFactory.getKeyFromId(same(this.key1), any())).thenReturn(this.key1);
    verifyBeforeAndAfterEvents(new BeforeDeleteEvent(new Key[] { this.key1 }, TestEntity.class, Collections.singletonList(this.key1), null), new AfterDeleteEvent(new Key[] { this.key1 }, TestEntity.class, Collections.singletonList(this.key1), null), () -> this.datastoreTemplate.deleteById(this.key1, TestEntity.class), x -> x.verify(this.datastore, times(1)).delete(same(this.key1)));
}
Also used : AfterDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent) BeforeDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeDeleteEvent) Key(com.google.cloud.datastore.Key) Test(org.junit.jupiter.api.Test)

Example 3 with AfterDeleteEvent

use of com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreTemplateTests method deleteAllTest.

@Test
void deleteAllTest() {
    QueryResults<Key> queryResults = mock(QueryResults.class);
    when(queryResults.getResultClass()).thenReturn((Class) Key.class);
    doAnswer(invocation -> {
        Arrays.asList(this.key1, this.key2).iterator().forEachRemaining(invocation.getArgument(0));
        return null;
    }).when(queryResults).forEachRemaining(any());
    when(this.datastore.run(Query.newKeyQueryBuilder().setKind("custom_test_kind").build())).thenReturn(queryResults);
    verifyBeforeAndAfterEvents(new BeforeDeleteEvent(new Key[] { this.key1, this.key2 }, TestEntity.class, null, null), new AfterDeleteEvent(new Key[] { this.key1, this.key2 }, TestEntity.class, null, null), () -> assertThat(this.datastoreTemplate.deleteAll(TestEntity.class)).isEqualTo(2), x -> x.verify(this.datastore, times(1)).delete(same(this.key1), same(this.key2)));
}
Also used : AfterDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent) BeforeDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeDeleteEvent) Key(com.google.cloud.datastore.Key) Test(org.junit.jupiter.api.Test)

Example 4 with AfterDeleteEvent

use of com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreTemplateTests method deleteAllByIdTest.

@Test
void deleteAllByIdTest() {
    when(this.objectToKeyFactory.getKeyFromId(same(this.key1), any())).thenReturn(this.key1);
    when(this.objectToKeyFactory.getKeyFromId(same(this.key2), any())).thenReturn(this.key2);
    verifyBeforeAndAfterEvents(new BeforeDeleteEvent(new Key[] { this.key2, this.key1 }, TestEntity.class, Arrays.asList(this.key1, this.key2), null), new AfterDeleteEvent(new Key[] { this.key2, this.key1 }, TestEntity.class, Arrays.asList(this.key1, this.key2), null), () -> this.datastoreTemplate.deleteAllById(Arrays.asList(this.key1, this.key2), TestEntity.class), x -> x.verify(this.datastore, times(1)).delete(same(this.key2), same(this.key1)));
}
Also used : AfterDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent) BeforeDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeDeleteEvent) Key(com.google.cloud.datastore.Key) Test(org.junit.jupiter.api.Test)

Aggregations

AfterDeleteEvent (com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent)4 BeforeDeleteEvent (com.google.cloud.spring.data.datastore.core.mapping.event.BeforeDeleteEvent)4 Key (com.google.cloud.datastore.Key)3 Test (org.junit.jupiter.api.Test)3