Search in sources :

Example 1 with DatastorePersistentEntity

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

the class DatastoreTemplateTests method setUpConverters.

private void setUpConverters(Entity ce1, Query childTestEntityQuery, QueryResults childTestEntityQueryResults, QueryResults testEntityQueryResults) {
    // mocking the converter to return the final objects corresponding to their
    // specific entities.
    DatastorePersistentEntity testPersistentEntity = new DatastoreMappingContext().getDatastorePersistentEntity(TestEntity.class);
    DatastorePersistentEntity childPersistentEntity = new DatastoreMappingContext().getDatastorePersistentEntity(ChildEntity.class);
    when(this.datastoreEntityConverter.read(TestEntity.class, this.e1)).thenReturn(this.ob1);
    when(this.datastoreEntityConverter.getDiscriminationPersistentEntity(TestEntity.class, this.e1)).thenReturn(testPersistentEntity);
    when(this.datastoreEntityConverter.read(TestEntity.class, this.e2)).thenReturn(this.ob2);
    when(this.datastoreEntityConverter.getDiscriminationPersistentEntity(TestEntity.class, this.e2)).thenReturn(testPersistentEntity);
    when(this.datastoreEntityConverter.read(eq(ChildEntity.class), same(ce1))).thenAnswer(invocationOnMock -> createChildEntity());
    when(this.datastoreEntityConverter.getDiscriminationPersistentEntity(eq(ChildEntity.class), same(ce1))).thenReturn(childPersistentEntity);
    doAnswer(invocation -> {
        FullEntity.Builder builder = invocation.getArgument(1);
        builder.set("color", "simple_test_color");
        builder.set("int_field", 1);
        return null;
    }).when(this.datastoreEntityConverter).write(same(this.simpleTestEntity), any());
    doAnswer(invocation -> {
        FullEntity.Builder builder = invocation.getArgument(1);
        builder.set("color", NullValue.of());
        builder.set("int_field", NullValue.of());
        return null;
    }).when(this.datastoreEntityConverter).write(same(this.simpleTestEntityNullVallues), any());
    when(this.datastore.run(this.testEntityQuery)).thenReturn(testEntityQueryResults);
    when(this.datastore.run(this.findAllTestEntityQuery)).thenReturn(testEntityQueryResults);
    when(this.datastore.run(childTestEntityQuery)).thenReturn(childTestEntityQueryResults);
    // Because get() takes varags, there is difficulty in matching the single param
    // case using just thenReturn.
    doAnswer(invocation -> {
        Object key = invocation.getArgument(0);
        List<Entity> result = new ArrayList<>();
        if (key instanceof Key) {
            if (key == this.key1) {
                result.add(this.e1);
            } else if (key == this.keyChild1) {
                result.add(ce1);
            } else {
                result.add(null);
            }
        }
        return result;
    }).when(this.datastore).fetch((Key[]) any());
    when(this.objectToKeyFactory.getKeyFromId(eq(this.key1), any())).thenReturn(this.key1);
    when(this.objectToKeyFactory.getKeyFromId(eq(this.key2), any())).thenReturn(this.key2);
    when(this.objectToKeyFactory.getKeyFromId(eq(this.keyChild1), any())).thenReturn(this.keyChild1);
    when(this.objectToKeyFactory.getKeyFromId(eq(this.badKey), any())).thenReturn(this.badKey);
    when(this.objectToKeyFactory.getKeyFromObject(eq(this.ob1), any())).thenReturn(this.key1);
    when(this.objectToKeyFactory.getKeyFromObject(eq(this.ob2), any())).thenReturn(this.key2);
    this.childKey2 = createFakeKey("child_id2");
    when(this.objectToKeyFactory.allocateKeyForObject(same(this.childEntity2), any(), eq(this.key1))).thenReturn(this.childKey2);
    this.childKey3 = createFakeKey("child_id3");
    when(this.objectToKeyFactory.allocateKeyForObject(same(this.childEntity3), any(), eq(this.key1))).thenReturn(this.childKey3);
    this.childKey4 = createFakeKey("child_id4");
    when(this.objectToKeyFactory.allocateKeyForObject(same(this.childEntity4), any(), any())).thenReturn(this.childKey4);
    when(this.objectToKeyFactory.getKeyFromObject(same(this.childEntity4), any())).thenReturn(this.childKey4);
    this.childKey5 = createFakeKey("child_id5");
    when(this.objectToKeyFactory.allocateKeyForObject(same(this.childEntity5), any(), any())).thenReturn(this.childKey5);
    when(this.objectToKeyFactory.getKeyFromObject(same(this.childEntity5), any())).thenReturn(this.childKey5);
    this.childKey6 = createFakeKey("child_id6");
    when(this.objectToKeyFactory.allocateKeyForObject(same(this.childEntity6), any(), any())).thenReturn(this.childKey6);
    when(this.objectToKeyFactory.getKeyFromObject(same(this.childEntity6), any())).thenReturn(this.childKey6);
    this.childKey7 = createFakeKey("child_id7");
    when(this.objectToKeyFactory.allocateKeyForObject(same(this.childEntity7), any(), any())).thenReturn(this.childKey7);
    when(this.objectToKeyFactory.getKeyFromObject(same(this.childEntity7), any())).thenReturn(this.childKey7);
}
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) ArrayList(java.util.ArrayList) FullEntity(com.google.cloud.datastore.FullEntity) Key(com.google.cloud.datastore.Key)

Example 2 with DatastorePersistentEntity

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

the class DatastoreServiceObjectToKeyFactoryTests method allocateIdForObjectNonKeyIdTest.

@Test
void allocateIdForObjectNonKeyIdTest() {
    TestEntityWithId testEntityWithId = new TestEntityWithId();
    KeyFactory keyFactory = new KeyFactory("project").setKind("kind");
    Key testKey = keyFactory.newKey("ancestor");
    DatastorePersistentEntity dpeTest = this.datastoreMappingContext.getPersistentEntity(testEntityWithId.getClass());
    when(this.datastore.newKeyFactory()).thenReturn(keyFactory);
    assertThatThrownBy(() -> this.datastoreServiceObjectToKeyFactory.allocateKeyForObject(testEntityWithId, dpeTest, testKey)).isInstanceOf(DatastoreDataException.class).hasMessage("Only Key types are allowed for descendants id");
}
Also used : DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Test(org.junit.jupiter.api.Test)

Example 3 with DatastorePersistentEntity

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

the class DatastoreServiceObjectToKeyFactoryTests method allocateIdForObjectUnsupportedKeyTypeIdTest.

@Test
void allocateIdForObjectUnsupportedKeyTypeIdTest() {
    TestEntityWithStringId testEntityWithStringId = new TestEntityWithStringId();
    KeyFactory keyFactory = new KeyFactory("project").setKind("kind");
    Key testKey = keyFactory.newKey("key");
    when(this.datastore.newKeyFactory()).thenReturn(keyFactory);
    DatastorePersistentEntity dpeTest = this.datastoreMappingContext.getPersistentEntity(testEntityWithStringId.getClass());
    assertThatThrownBy(() -> this.datastoreServiceObjectToKeyFactory.allocateKeyForObject(testEntityWithStringId, dpeTest, testKey)).isInstanceOf(DatastoreDataException.class).hasMessage("Cloud Datastore can only allocate IDs for Long and Key properties. " + "Cannot allocate for type: class java.lang.String");
}
Also used : DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Test(org.junit.jupiter.api.Test)

Example 4 with DatastorePersistentEntity

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

the class DatastoreTemplate method getReferenceEntitiesForSave.

private List<Entity> getReferenceEntitiesForSave(Object entity, Builder builder, Set<Key> persistedEntities) {
    DatastorePersistentEntity<?> datastorePersistentEntity = getPersistentEntity(entity.getClass());
    List<Entity> entitiesToSave = new ArrayList<>();
    datastorePersistentEntity.doWithAssociations((AssociationHandler) association -> {
        PersistentProperty persistentProperty = association.getInverse();
        PersistentPropertyAccessor accessor = datastorePersistentEntity.getPropertyAccessor(entity);
        Object val = accessor.getProperty(persistentProperty);
        if (val == null) {
            return;
        }
        Value<?> value;
        if (LazyUtil.isLazyAndNotLoaded(val)) {
            value = LazyUtil.getKeys(val);
        } else if (persistentProperty.isCollectionLike()) {
            Iterable<?> iterableVal = (Iterable<?>) ValueUtil.toListIfArray(val);
            entitiesToSave.addAll(getEntitiesForSave(iterableVal, persistedEntities));
            List<KeyValue> keyValues = StreamSupport.stream((iterableVal).spliterator(), false).map(o -> KeyValue.of(this.getKey(o, false))).collect(Collectors.toList());
            value = ListValue.of(keyValues);
        } else {
            entitiesToSave.addAll(getEntitiesForSave(Collections.singletonList(val), persistedEntities));
            Key key = getKey(val, false);
            value = KeyValue.of(key);
        }
        builder.set(((DatastorePersistentProperty) persistentProperty).getFieldName(), value);
    });
    return entitiesToSave;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Query(com.google.cloud.datastore.Query) Builder(com.google.cloud.datastore.Entity.Builder) PathElement(com.google.cloud.datastore.PathElement) ObjectToKeyFactory(com.google.cloud.spring.data.datastore.core.convert.ObjectToKeyFactory) ValueUtil(com.google.cloud.spring.data.datastore.core.util.ValueUtil) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) Filter(com.google.cloud.datastore.StructuredQuery.Filter) KeyQuery(com.google.cloud.datastore.KeyQuery) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) QueryResults(com.google.cloud.datastore.QueryResults) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) AfterDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BaseEntity(com.google.cloud.datastore.BaseEntity) KeyValue(com.google.cloud.datastore.KeyValue) DatastorePageable(com.google.cloud.spring.data.datastore.repository.query.DatastorePageable) Collection(java.util.Collection) Set(java.util.Set) AfterQueryEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterQueryEvent) Example(org.springframework.data.domain.Example) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Collectors(java.util.stream.Collectors) Slice(org.springframework.data.domain.Slice) Objects(java.util.Objects) BeforeSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeSaveEvent) List(java.util.List) SliceUtil(com.google.cloud.spring.data.datastore.core.util.SliceUtil) Optional(java.util.Optional) NonNull(org.springframework.lang.NonNull) ListValue(com.google.cloud.datastore.ListValue) Key(com.google.cloud.datastore.Key) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) SliceImpl(org.springframework.data.domain.SliceImpl) AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) HashMap(java.util.HashMap) Datastore(com.google.cloud.datastore.Datastore) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Cursor(com.google.cloud.datastore.Cursor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) Entity(com.google.cloud.datastore.Entity) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) BeforeDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeDeleteEvent) NullValue(com.google.cloud.datastore.NullValue) DatastoreEntityConverter(com.google.cloud.spring.data.datastore.core.convert.DatastoreEntityConverter) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) StreamSupport(java.util.stream.StreamSupport) Nullable(org.springframework.lang.Nullable) LinkedList(java.util.LinkedList) StructuredQuery(com.google.cloud.datastore.StructuredQuery) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Iterator(java.util.Iterator) KeyUtil(com.google.cloud.spring.data.datastore.core.util.KeyUtil) TypeUtils(org.springframework.util.TypeUtils) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) EntityQuery(com.google.cloud.datastore.EntityQuery) ApplicationEvent(org.springframework.context.ApplicationEvent) AssociationHandler(org.springframework.data.mapping.AssociationHandler) BaseKey(com.google.cloud.datastore.BaseKey) Value(com.google.cloud.datastore.Value) AfterSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterSaveEvent) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Collections(java.util.Collections) Assert(org.springframework.util.Assert) 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) KeyValue(com.google.cloud.datastore.KeyValue) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ArrayList(java.util.ArrayList) PersistentProperty(org.springframework.data.mapping.PersistentProperty) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) KeyValue(com.google.cloud.datastore.KeyValue) ListValue(com.google.cloud.datastore.ListValue) NullValue(com.google.cloud.datastore.NullValue) Value(com.google.cloud.datastore.Value) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) BaseKey(com.google.cloud.datastore.BaseKey)

Example 5 with DatastorePersistentEntity

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

the class DatastoreTemplate method getDescendantEntitiesForSave.

private List<Entity> getDescendantEntitiesForSave(Object entity, Key key, Set<Key> persistedEntities) {
    DatastorePersistentEntity<?> datastorePersistentEntity = getPersistentEntity(entity.getClass());
    List<Entity> entitiesToSave = new ArrayList<>();
    datastorePersistentEntity.doWithDescendantProperties((DatastorePersistentProperty persistentProperty) -> {
        // Convert and write descendants, applying ancestor from parent entry
        PersistentPropertyAccessor accessor = datastorePersistentEntity.getPropertyAccessor(entity);
        Object val = accessor.getProperty(persistentProperty);
        if (val != null) {
            // we can be sure that the property is an array or an iterable,
            // because we check it in isDescendant
            entitiesToSave.addAll(getEntitiesForSave((Iterable<?>) ValueUtil.toListIfArray(val), persistedEntities, key));
        }
    });
    return entitiesToSave;
}
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) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ArrayList(java.util.ArrayList) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)

Aggregations

DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)14 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)10 Key (com.google.cloud.datastore.Key)8 Entity (com.google.cloud.datastore.Entity)7 FullEntity (com.google.cloud.datastore.FullEntity)7 IncompleteKey (com.google.cloud.datastore.IncompleteKey)7 ArrayList (java.util.ArrayList)7 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)6 DatastorePersistentProperty (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)6 BaseEntity (com.google.cloud.datastore.BaseEntity)5 BaseKey (com.google.cloud.datastore.BaseKey)5 EntityQuery (com.google.cloud.datastore.EntityQuery)5 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)5 Filter (com.google.cloud.datastore.StructuredQuery.Filter)5 PropertyFilter (com.google.cloud.datastore.StructuredQuery.PropertyFilter)5 AfterFindByKeyEvent (com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent)5 Cursor (com.google.cloud.datastore.Cursor)4 Datastore (com.google.cloud.datastore.Datastore)4 DatastoreReaderWriter (com.google.cloud.datastore.DatastoreReaderWriter)4 Builder (com.google.cloud.datastore.Entity.Builder)4