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);
}
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");
}
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");
}
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;
}
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;
}
Aggregations