Search in sources :

Example 1 with AfterFindByKeyEvent

use of org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplateTests method findByIdNotFoundTest.

@Test
public 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(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 2 with AfterFindByKeyEvent

use of org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplateTests method findAllReferenceLoopTest.

@Test
public 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(eq(this.key1))).thenReturn(Collections.singletonList(referenceTestDatastoreEntity));
    when(this.datastore.fetch(eq(this.key2))).thenReturn(Collections.singletonList(child));
    when(this.datastore.fetch(eq(this.childKey2))).thenReturn(Collections.singletonList(child2));
    ReferenceTestEntity referenceTestEntity = new ReferenceTestEntity();
    ReferenceTestEntity childEntity = new ReferenceTestEntity();
    ReferenceTestEntity childEntity2 = new ReferenceTestEntity();
    when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(referenceTestDatastoreEntity))).thenAnswer(invocationOnMock -> referenceTestEntity);
    when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(child))).thenAnswer(invocationOnMock -> childEntity);
    when(this.datastoreEntityConverter.read(eq(ReferenceTestEntity.class), same(child2))).thenAnswer(invocationOnMock -> childEntity2);
    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.size()).isEqualTo(1);
        verify(this.datastore, times(2)).fetch(any());
        verify(this.datastore, times(1)).fetch(eq(this.key1));
        verify(this.datastore, times(1)).fetch(eq(this.key2));
        assertThat(readReferenceTestEntity.lazyChild.toString()).isNotNull();
        verify(this.datastore, times(3)).fetch(any());
        verify(this.datastore, times(1)).fetch(eq(this.childKey2));
    }, x -> {
    });
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) AfterFindByKeyEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent) Test(org.junit.Test)

Example 3 with AfterFindByKeyEvent

use of org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplateTests method findAllByIdTest.

@Test
public void findAllByIdTest() {
    when(this.datastore.fetch(eq(this.key2), eq(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(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent) Key(com.google.cloud.datastore.Key) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with AfterFindByKeyEvent

use of org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplateTests method findByIdTest.

@Test
public 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(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent) Test(org.junit.Test)

Example 5 with AfterFindByKeyEvent

use of org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent in project spring-cloud-gcp by spring-cloud.

the class DatastoreTemplateTests method findAllByIdReferenceConsistencyTest.

@Test
public void findAllByIdReferenceConsistencyTest() {
    when(this.objectToKeyFactory.getKeyFromObject(eq(this.childEntity1), any())).thenReturn(this.childEntity1.id);
    when(this.datastore.fetch(eq(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(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent) Test(org.junit.Test)

Aggregations

AfterFindByKeyEvent (org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent)6 Test (org.junit.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 HashSet (java.util.HashSet)1