Search in sources :

Example 1 with AfterSaveEvent

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

the class DatastoreTemplateTests method saveAllTest.

@Test
void saveAllTest() {
    when(this.objectToKeyFactory.allocateKeyForObject(same(this.ob1), any())).thenReturn(this.key1);
    when(this.objectToKeyFactory.getKeyFromObject(same(this.ob2), any())).thenReturn(this.key2);
    Entity writtenEntity1 = Entity.newBuilder(this.key1).set("singularReference", this.childKey4).set("multipleReference", Arrays.asList(KeyValue.of(this.childKey5), KeyValue.of(this.childKey6))).set("lazyMultipleReference", Collections.singletonList(KeyValue.of(this.childKey7))).build();
    Entity writtenEntity2 = Entity.newBuilder(this.key2).build();
    Entity writtenChildEntity2 = Entity.newBuilder(this.childKey2).build();
    Entity writtenChildEntity3 = Entity.newBuilder(this.childKey3).build();
    Entity writtenChildEntity4 = Entity.newBuilder(this.childKey4).build();
    Entity writtenChildEntity5 = Entity.newBuilder(this.childKey5).build();
    Entity writtenChildEntity6 = Entity.newBuilder(this.childKey6).build();
    Entity writtenChildEntity7 = Entity.newBuilder(this.childKey7).build();
    doAnswer(invocation -> {
        assertThat(invocation.getArguments()).containsExactlyInAnyOrder(writtenChildEntity2, writtenChildEntity3, writtenChildEntity4, writtenChildEntity5, writtenChildEntity6, writtenEntity1, writtenEntity2, writtenChildEntity7);
        return null;
    }).when(this.datastore).put(ArgumentMatchers.<FullEntity[]>any());
    List<Entity> expected = Arrays.asList(writtenChildEntity2, writtenChildEntity3, writtenChildEntity7, writtenChildEntity4, writtenChildEntity5, writtenChildEntity6, writtenEntity1, writtenEntity2);
    List javaExpected = Arrays.asList(this.ob1, this.ob2);
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(javaExpected), new AfterSaveEvent(expected, javaExpected), () -> this.datastoreTemplate.saveAll(Arrays.asList(this.ob1, this.ob2)), x -> {
    });
    verify(this.datastoreEntityConverter, times(1)).write(same(this.ob1), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.ob2), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity2), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity3), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity4), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity5), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity6), notNull());
    verify(this.datastore, times(1)).put(ArgumentMatchers.<FullEntity[]>any());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) Entity(com.google.cloud.datastore.Entity) AfterSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeSaveEvent) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) FullEntity(com.google.cloud.datastore.FullEntity) Test(org.junit.jupiter.api.Test)

Example 2 with AfterSaveEvent

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

the class DatastoreTemplateTests method saveAllMaxWriteSizeTest.

@Test
void saveAllMaxWriteSizeTest() {
    when(this.objectToKeyFactory.allocateKeyForObject(same(this.ob1), any())).thenReturn(this.key1);
    when(this.objectToKeyFactory.getKeyFromObject(same(this.ob2), any())).thenReturn(this.key2);
    Entity writtenEntity1 = Entity.newBuilder(this.key1).set("singularReference", this.childKey4).set("multipleReference", Arrays.asList(KeyValue.of(this.childKey5), KeyValue.of(this.childKey6))).set("lazyMultipleReference", Collections.singletonList(KeyValue.of(this.childKey7))).build();
    Entity writtenEntity2 = Entity.newBuilder(this.key2).build();
    Entity writtenChildEntity2 = Entity.newBuilder(this.childKey2).build();
    Entity writtenChildEntity3 = Entity.newBuilder(this.childKey3).build();
    Entity writtenChildEntity4 = Entity.newBuilder(this.childKey4).build();
    Entity writtenChildEntity5 = Entity.newBuilder(this.childKey5).build();
    Entity writtenChildEntity6 = Entity.newBuilder(this.childKey6).build();
    Entity writtenChildEntity7 = Entity.newBuilder(this.childKey7).build();
    Set<Entity> entities = new HashSet<>();
    entities.addAll(Arrays.asList(writtenChildEntity2, writtenChildEntity3, writtenChildEntity7, writtenChildEntity4, writtenChildEntity5, writtenChildEntity6, writtenEntity1, writtenEntity2));
    doAnswer(invocation -> {
        assertThat(invocation.getArguments()).hasSize(1);
        assertThat(entities).contains((Entity) invocation.getArguments()[0]);
        entities.remove(invocation.getArguments()[0]);
        return null;
    }).when(this.datastore).put(ArgumentMatchers.<FullEntity[]>any());
    List<Entity> expected = Arrays.asList(writtenChildEntity2, writtenChildEntity3, writtenChildEntity7, writtenChildEntity4, writtenChildEntity5, writtenChildEntity6, writtenEntity1, writtenEntity2);
    List javaExpected = Arrays.asList(this.ob1, this.ob2);
    this.datastoreTemplate.setMaxWriteSize(1);
    verifyBeforeAndAfterEvents(new BeforeSaveEvent(javaExpected), new AfterSaveEvent(expected, javaExpected), () -> this.datastoreTemplate.saveAll(Arrays.asList(this.ob1, this.ob2)), x -> {
    });
    assertThat(entities).isEmpty();
    verify(this.datastoreEntityConverter, times(1)).write(same(this.ob1), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.ob2), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity2), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity3), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity4), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity5), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity6), notNull());
    verify(this.datastoreEntityConverter, times(1)).write(same(this.childEntity7), notNull());
    verify(this.datastore, times(8)).put(ArgumentMatchers.<FullEntity[]>any());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) Entity(com.google.cloud.datastore.Entity) AfterSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeSaveEvent) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) FullEntity(com.google.cloud.datastore.FullEntity) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with AfterSaveEvent

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

the class DatastoreTemplate method saveEntities.

private <T> void saveEntities(List<T> instances, Key[] ancestors) {
    if (!instances.isEmpty()) {
        maybeEmitEvent(new BeforeSaveEvent(instances));
        List<Entity> entities = getEntitiesForSave(instances, new HashSet<>(), ancestors);
        SliceUtil.sliceAndExecute(entities.toArray(new Entity[0]), this.maxWriteSize, getDatastoreReadWriter()::put);
        maybeEmitEvent(new AfterSaveEvent(entities, instances));
    }
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) Entity(com.google.cloud.datastore.Entity) AfterSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterSaveEvent) BeforeSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeSaveEvent)

Aggregations

Entity (com.google.cloud.datastore.Entity)3 FullEntity (com.google.cloud.datastore.FullEntity)3 DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)3 AfterSaveEvent (com.google.cloud.spring.data.datastore.core.mapping.event.AfterSaveEvent)3 BeforeSaveEvent (com.google.cloud.spring.data.datastore.core.mapping.event.BeforeSaveEvent)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Test (org.junit.jupiter.api.Test)2 BaseEntity (com.google.cloud.datastore.BaseEntity)1 HashSet (java.util.HashSet)1