Search in sources :

Example 11 with DatastoreMappingContext

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

the class DefaultDatastoreEntityConverterTests method testUnsupportedTypeWrite.

@Test
void testUnsupportedTypeWrite() {
    TestItemUnsupportedFields item = new TestItemUnsupportedFields();
    item.setStringField("string value");
    item.setUnsupportedField(new TestItemUnsupportedFields.NewType(true));
    DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new TwoStepsConversions(new DatastoreCustomConversions(Arrays.asList(getIntegerToNewTypeConverter(), getNewTypeToIntegerConverter())), null, datastoreMappingContext));
    Entity.Builder builder = getEntityBuilder();
    entityConverter.write(item, builder);
    Entity entity = builder.build();
    assertThat(entity.getLong("unsupportedField")).as("validate custom conversion").isEqualTo(1L);
    assertThat(entity.getString("stringField")).as("validate string field").isEqualTo("string value");
    TestItemUnsupportedFields readItem = entityConverter.read(TestItemUnsupportedFields.class, entity);
    assertThat(item).as("read object should be equal to original").isEqualTo(readItem);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) BaseEntity(com.google.cloud.datastore.BaseEntity) EmbeddedEntity(com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) Test(org.junit.jupiter.api.Test)

Example 12 with DatastoreMappingContext

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

the class DefaultDatastoreEntityConverterTests method testCollectionFieldsUnsupportedWriteOnly.

@Test
void testCollectionFieldsUnsupportedWriteOnly() {
    TestItemUnsupportedFields.CollectionOfUnsupportedTypes item = getCollectionOfUnsupportedTypesItem();
    DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new TwoStepsConversions(new DatastoreCustomConversions(Collections.singletonList(getNewTypeToIntegerConverter())), null, datastoreMappingContext));
    Entity.Builder builder = getEntityBuilder();
    entityConverter.write(item, builder);
    Entity entity = builder.build();
    List<Value<?>> intList = entity.getList("unsupportedElts");
    assertThat(intList.stream().map(Value::get).collect(Collectors.toList())).as("validate int list values").isEqualTo(Arrays.asList(1L, 0L));
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) BaseEntity(com.google.cloud.datastore.BaseEntity) EmbeddedEntity(com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) NullValue(com.google.cloud.datastore.NullValue) EntityValue(com.google.cloud.datastore.EntityValue) DiscriminatorValue(com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorValue) Value(com.google.cloud.datastore.Value) StringValue(com.google.cloud.datastore.StringValue) ListValue(com.google.cloud.datastore.ListValue) Test(org.junit.jupiter.api.Test)

Example 13 with DatastoreMappingContext

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

the class DefaultDatastoreEntityConverterTests method testCollectionFieldsUnsupportedWriteRead.

@Test
void testCollectionFieldsUnsupportedWriteRead() {
    TestItemUnsupportedFields.CollectionOfUnsupportedTypes item = getCollectionOfUnsupportedTypesItem();
    DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new TwoStepsConversions(new DatastoreCustomConversions(Arrays.asList(getIntegerToNewTypeConverter(), getNewTypeToIntegerConverter())), null, datastoreMappingContext));
    Entity.Builder builder = getEntityBuilder();
    entityConverter.write(item, builder);
    Entity entity = builder.build();
    List<Value<?>> intList = entity.getList("unsupportedElts");
    assertThat(intList.stream().map(Value::get).collect(Collectors.toList())).as("validate long list values").isEqualTo(Arrays.asList(1L, 0L));
    TestItemUnsupportedFields.CollectionOfUnsupportedTypes read = entityConverter.read(TestItemUnsupportedFields.CollectionOfUnsupportedTypes.class, entity);
    assertThat(read).as("read object should be equal to original").isEqualTo(item);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) BaseEntity(com.google.cloud.datastore.BaseEntity) EmbeddedEntity(com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) NullValue(com.google.cloud.datastore.NullValue) EntityValue(com.google.cloud.datastore.EntityValue) DiscriminatorValue(com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorValue) Value(com.google.cloud.datastore.Value) StringValue(com.google.cloud.datastore.StringValue) ListValue(com.google.cloud.datastore.ListValue) Test(org.junit.jupiter.api.Test)

Example 14 with DatastoreMappingContext

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

the class DefaultDatastoreEntityConverterTests method testEmbeddedEntity.

@Test
void testEmbeddedEntity() {
    EmbeddedEntity embeddedEntityA = new EmbeddedEntity("item 0");
    EmbeddedEntity embeddedEntityB = new EmbeddedEntity("item 1");
    List<EmbeddedEntity> embeddedEntities = Arrays.asList(embeddedEntityA, embeddedEntityB);
    Map<String, String> mapSimpleValues = new HashMap<>();
    mapSimpleValues.put("a", "valueA");
    mapSimpleValues.put("b", "valueB");
    Map<String, String[]> mapListValues = new HashMap<>();
    mapListValues.put("a", new String[] { "valueA" });
    mapListValues.put("b", new String[] { "valueB" });
    Map<String, EmbeddedEntity> embeddedEntityMapEmbeddedEntity = new HashMap<>();
    embeddedEntityMapEmbeddedEntity.put("a", embeddedEntityA);
    embeddedEntityMapEmbeddedEntity.put("b", embeddedEntityB);
    Map<String, List<EmbeddedEntity>> embeddedEntityMapListOfEmbeddedEntities = new HashMap<>();
    embeddedEntityMapListOfEmbeddedEntities.put("a", Arrays.asList(embeddedEntityA));
    embeddedEntityMapListOfEmbeddedEntities.put("b", Arrays.asList(embeddedEntityB));
    Map<String, Map<Long, Map<String, String>>> nestedEmbeddedMap = new HashMap<>();
    Map<Long, Map<String, String>> nestedInnerEmbeddedMap = new HashMap<>();
    nestedInnerEmbeddedMap.put(1L, mapSimpleValues);
    nestedEmbeddedMap.put("outer1", nestedInnerEmbeddedMap);
    Map<TestDatastoreItem.Color, String> enumKeysMap = new HashMap<>();
    enumKeysMap.put(TestDatastoreItem.Color.BLACK, "black");
    enumKeysMap.put(TestDatastoreItem.Color.WHITE, "white");
    CustomMap customMap = new CustomMap();
    customMap.put("key1", "val1");
    TestItemWithEmbeddedEntity item = new TestItemWithEmbeddedEntity(123, new EmbeddedEntity("abc"), embeddedEntities, mapSimpleValues, mapListValues, embeddedEntityMapEmbeddedEntity, embeddedEntityMapListOfEmbeddedEntities, enumKeysMap, customMap);
    item.setNestedEmbeddedMaps(nestedEmbeddedMap);
    DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new DatastoreServiceObjectToKeyFactory(() -> this.datastore));
    Entity.Builder builder = getEntityBuilder();
    entityConverter.write(item, builder);
    Entity entity = builder.build();
    assertThat(entity.getList("listOfEmbeddedEntities").stream().map(val -> ((BaseEntity<?>) val.get()).getString("stringField")).collect(Collectors.toList())).as("validate embedded entity").isEqualTo(Arrays.asList("item 0", "item 1"));
    assertThat(entity.getEntity("embeddedEntityField").getString("stringField")).as("validate embedded entity").isEqualTo("abc");
    assertThat(entity.getLong("intField")).as("validate int field").isEqualTo(123L);
    assertThat(entity.getEntity("nestedEmbeddedMaps").getEntity("outer1").getEntity("1").getString("a")).isEqualTo("valueA");
    assertThat(entity.getEntity("nestedEmbeddedMaps").getEntity("outer1").getEntity("1").getString("b")).isEqualTo("valueB");
    assertThat(entity.getEntity("embeddedMapSimpleValues").getString("a")).isEqualTo("valueA");
    assertThat(entity.getEntity("embeddedMapSimpleValues").getString("b")).isEqualTo("valueB");
    assertThat(entity.getEntity("embeddedMapListOfValues").getList("a")).contains(StringValue.of("valueA"));
    assertThat(entity.getEntity("embeddedMapListOfValues").getList("b")).contains(StringValue.of("valueB"));
    assertThat(entity.getEntity("embeddedEntityMapEmbeddedEntity").getEntity("a").getString("stringField")).isEqualTo("item 0");
    assertThat(entity.getEntity("embeddedEntityMapEmbeddedEntity").getEntity("b").getString("stringField")).isEqualTo("item 1");
    List<Value> embeddedMapValuesEmbeddedEntityA = entity.getEntity("embeddedEntityMapListOfEmbeddedEntities").getList("a");
    List<Value> embeddedMapValuesEmbeddedEntityB = entity.getEntity("embeddedEntityMapListOfEmbeddedEntities").getList("b");
    assertThat(((BaseEntity) embeddedMapValuesEmbeddedEntityA.get(0).get()).getString("stringField")).isEqualTo("item 0");
    assertThat(embeddedMapValuesEmbeddedEntityA).hasSize(1);
    assertThat(((BaseEntity) embeddedMapValuesEmbeddedEntityB.get(0).get()).getString("stringField")).isEqualTo("item 1");
    assertThat(embeddedMapValuesEmbeddedEntityB).hasSize(1);
    TestItemWithEmbeddedEntity read = entityConverter.read(TestItemWithEmbeddedEntity.class, entity);
    assertThat(read.getNestedEmbeddedMaps().get("outer1").get(1L)).containsEntry("a", "valueA");
    assertThat(read.getNestedEmbeddedMaps().get("outer1").get(1L)).containsEntry("b", "valueB");
    assertThat(entity.getEntity("customMap").getString("key1")).isEqualTo("val1");
    assertThat(read).as("read objects equals the original one").isEqualTo(item);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) BaseEntity(com.google.cloud.datastore.BaseEntity) EmbeddedEntity(com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) HashMap(java.util.HashMap) CustomMap(com.google.cloud.spring.data.datastore.entities.CustomMap) ArrayList(java.util.ArrayList) List(java.util.List) BaseEntity(com.google.cloud.datastore.BaseEntity) EmbeddedEntity(com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) NullValue(com.google.cloud.datastore.NullValue) EntityValue(com.google.cloud.datastore.EntityValue) DiscriminatorValue(com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorValue) Value(com.google.cloud.datastore.Value) StringValue(com.google.cloud.datastore.StringValue) ListValue(com.google.cloud.datastore.ListValue) HashMap(java.util.HashMap) Map(java.util.Map) CustomMap(com.google.cloud.spring.data.datastore.entities.CustomMap) Test(org.junit.jupiter.api.Test)

Example 15 with DatastoreMappingContext

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

the class DefaultDatastoreEntityConverterTests method testCollectionFieldsUnsupportedWriteReadException.

@Test
void testCollectionFieldsUnsupportedWriteReadException() {
    TestItemUnsupportedFields.CollectionOfUnsupportedTypes item = getCollectionOfUnsupportedTypesItem();
    DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new TwoStepsConversions(new DatastoreCustomConversions(Collections.singletonList(getNewTypeToIntegerConverter())), null, datastoreMappingContext));
    Entity.Builder builder = getEntityBuilder();
    entityConverter.write(item, builder);
    Entity entity = builder.build();
    Class parameter = TestItemUnsupportedFields.CollectionOfUnsupportedTypes.class;
    assertThatThrownBy(() -> entityConverter.read(parameter, entity)).hasMessageContaining("Unable to read property unsupportedElts").hasMessageContaining("Unable process elements of a collection").hasMessageContaining("No converter found capable of converting from type [java.lang.Integer] " + "to type [com.google.cloud.spring.data.datastore.core.convert." + "TestItemUnsupportedFields$NewType]").hasMessageContaining("Unable to read com.google.cloud.spring.data.datastore.core.convert." + "TestItemUnsupportedFields$CollectionOfUnsupportedTypes entity").isInstanceOf(DatastoreDataException.class);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) BaseEntity(com.google.cloud.datastore.BaseEntity) EmbeddedEntity(com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) Test(org.junit.jupiter.api.Test)

Aggregations

DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)16 FullEntity (com.google.cloud.datastore.FullEntity)12 Entity (com.google.cloud.datastore.Entity)11 Test (org.junit.jupiter.api.Test)11 BaseEntity (com.google.cloud.datastore.BaseEntity)8 EmbeddedEntity (com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity)8 EntityValue (com.google.cloud.datastore.EntityValue)5 ListValue (com.google.cloud.datastore.ListValue)5 NullValue (com.google.cloud.datastore.NullValue)4 StringValue (com.google.cloud.datastore.StringValue)4 Value (com.google.cloud.datastore.Value)4 DiscriminatorValue (com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorValue)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 DatastoreTemplate (com.google.cloud.spring.data.datastore.core.DatastoreTemplate)3 DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)3 ArrayList (java.util.ArrayList)3 EntityQuery (com.google.cloud.datastore.EntityQuery)2 MapBuilder (com.google.cloud.spring.core.util.MapBuilder)2 List (java.util.List)2 Datastore (com.google.cloud.datastore.Datastore)1