Search in sources :

Example 1 with AfterFindByKeyEvent

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

the class DatastoreTemplateTests method findByIdNotFoundTest.

@Test
void findByIdNotFoundTest() {
    when(this.datastore.fetch(ArgumentMatchers.<Key[]>any())).thenReturn(Collections.singletonList(null));
    verifyBeforeAndAfterEvents(null, new AfterFindByKeyEvent(Collections.emptyList(), Collections.singleton(null)), () -> assertThat(this.datastoreTemplate.findById(createFakeKey("key0"), TestEntity.class)).isNull(), x -> {
    });
}
Also used : AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) Key(com.google.cloud.datastore.Key) Test(org.junit.jupiter.api.Test)

Example 2 with AfterFindByKeyEvent

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

the class DatastoreTemplateTests method findAllByIdTest.

@Test
void findAllByIdTest() {
    when(this.datastore.fetch(this.key2, this.key1)).thenReturn(Arrays.asList(this.e1, this.e2));
    List<Key> keys = Arrays.asList(this.key1, this.key2);
    verifyBeforeAndAfterEvents(null, new AfterFindByKeyEvent(Arrays.asList(this.ob1, this.ob2), new HashSet<>(keys)), () -> assertThat(this.datastoreTemplate.findAllById(keys, TestEntity.class)).containsExactly(this.ob1, this.ob2), x -> {
    });
}
Also used : AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) Key(com.google.cloud.datastore.Key) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with AfterFindByKeyEvent

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

the class DatastoreTemplateTests method findAllReferenceLoopTest.

@Test
void findAllReferenceLoopTest() {
    Entity referenceTestDatastoreEntity = Entity.newBuilder(this.key1).set("sibling", this.key1).set("lazyChildren", ListValue.of(this.key2)).set("lazyChild", this.childKey2).build();
    Entity child = Entity.newBuilder(this.key1).build();
    Entity child2 = Entity.newBuilder(this.childKey2).build();
    when(this.datastore.fetch(this.key1)).thenReturn(Collections.singletonList(referenceTestDatastoreEntity));
    when(this.datastore.fetch(this.key2)).thenReturn(Collections.singletonList(child));
    when(this.datastore.fetch(this.childKey2)).thenReturn(Collections.singletonList(child2));
    ReferenceTestEntity referenceTestEntity = new ReferenceTestEntity();
    ReferenceTestEntity childEntity = new ReferenceTestEntity();
    ReferenceTestEntity childEntity2 = new ReferenceTestEntity();
    DatastorePersistentEntity referenceTestPersistentEntity = new DatastoreMappingContext().getDatastorePersistentEntity(ReferenceTestEntity.class);
    when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(referenceTestDatastoreEntity))).thenAnswer(invocationOnMock -> referenceTestEntity);
    when(this.datastoreEntityConverter.getDiscriminationPersistentEntity(eq(ReferenceTestEntity.class), same(referenceTestDatastoreEntity))).thenReturn(referenceTestPersistentEntity);
    when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(child))).thenAnswer(invocationOnMock -> childEntity);
    when(this.datastoreEntityConverter.getDiscriminationPersistentEntity(eq(ReferenceTestEntity.class), same(child))).thenReturn(referenceTestPersistentEntity);
    when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(child2))).thenAnswer(invocationOnMock -> childEntity2);
    when(this.datastoreEntityConverter.getDiscriminationPersistentEntity(eq(ReferenceTestEntity.class), same(child2))).thenReturn(referenceTestPersistentEntity);
    verifyBeforeAndAfterEvents(null, new AfterFindByKeyEvent(Collections.singletonList(referenceTestEntity), Collections.singleton(this.key1)), () -> {
        ReferenceTestEntity readReferenceTestEntity = this.datastoreTemplate.findById(this.key1, ReferenceTestEntity.class);
        assertThat(readReferenceTestEntity.sibling).isSameAs(readReferenceTestEntity);
        verify(this.datastore, times(1)).fetch(any());
        assertThat(readReferenceTestEntity.lazyChildren).hasSize(1);
        verify(this.datastore, times(2)).fetch(any());
        verify(this.datastore, times(1)).fetch(this.key1);
        verify(this.datastore, times(1)).fetch(this.key2);
        assertThat(readReferenceTestEntity.lazyChild.toString()).isNotNull();
        verify(this.datastore, times(3)).fetch(any());
        verify(this.datastore, times(1)).fetch(this.childKey2);
    }, x -> {
    });
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) Entity(com.google.cloud.datastore.Entity) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) Test(org.junit.jupiter.api.Test)

Example 4 with AfterFindByKeyEvent

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

the class DatastoreTemplateTests method findAllByIdReferenceConsistencyTest.

@Test
void findAllByIdReferenceConsistencyTest() {
    when(this.objectToKeyFactory.getKeyFromObject(eq(this.childEntity1), any())).thenReturn(this.childEntity1.id);
    when(this.datastore.fetch(this.key1)).thenReturn(Collections.singletonList(this.e1));
    verifyBeforeAndAfterEvents(null, new AfterFindByKeyEvent(Collections.singletonList(this.ob1), Collections.singleton(this.key1)), () -> {
        TestEntity parentEntity1 = this.datastoreTemplate.findById(this.key1, TestEntity.class);
        assertThat(parentEntity1).isSameAs(this.ob1);
        ChildEntity singularReference1 = parentEntity1.singularReference;
        ChildEntity childEntity1 = parentEntity1.childEntities.get(0);
        assertThat(singularReference1).isSameAs(childEntity1);
        TestEntity parentEntity2 = this.datastoreTemplate.findById(this.key1, TestEntity.class);
        assertThat(parentEntity2).isSameAs(this.ob1);
        ChildEntity singularReference2 = parentEntity2.singularReference;
        ChildEntity childEntity2 = parentEntity2.childEntities.get(0);
        assertThat(singularReference2).isSameAs(childEntity2);
        assertThat(childEntity1).isNotSameAs(childEntity2);
    }, x -> {
    });
}
Also used : AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) Test(org.junit.jupiter.api.Test)

Example 5 with AfterFindByKeyEvent

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

the class DatastoreTemplateTests method findByIdTest.

@Test
void findByIdTest() {
    verifyBeforeAndAfterEvents(null, new AfterFindByKeyEvent(Collections.singletonList(this.ob1), Collections.singleton(this.key1)), () -> {
        TestEntity result = this.datastoreTemplate.findById(this.key1, TestEntity.class);
        assertThat(result).isEqualTo(this.ob1);
        assertThat(result.childEntities).contains(this.childEntity1);
        assertThat(this.childEntity1).isEqualTo(result.singularReference);
        assertThat(result.multipleReference).contains(this.childEntity1);
    }, x -> {
    });
}
Also used : AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) Test(org.junit.jupiter.api.Test)

Aggregations

AfterFindByKeyEvent (com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent)6 Test (org.junit.jupiter.api.Test)5 Key (com.google.cloud.datastore.Key)3 BaseKey (com.google.cloud.datastore.BaseKey)1 Entity (com.google.cloud.datastore.Entity)1 FullEntity (com.google.cloud.datastore.FullEntity)1 IncompleteKey (com.google.cloud.datastore.IncompleteKey)1 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)1 DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)1 HashSet (java.util.HashSet)1