Search in sources :

Example 1 with DatastoreMappingContext

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

the class DatastoreTemplateTests method combineFiltersDiscrimination.

@Test
public void combineFiltersDiscrimination() {
    PropertyFilter propertyFilter = PropertyFilter.eq("field", "some value");
    EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind("test_kind").setFilter(propertyFilter);
    DatastoreTemplate.applyQueryOptions(builder, new DatastoreQueryOptions.Builder().setLimit(1).setOffset(2).build(), new DatastoreMappingContext().getPersistentEntity(SimpleDiscriminationTestEntity.class));
    assertThat(builder.build().getFilter()).isEqualTo(StructuredQuery.CompositeFilter.and(propertyFilter, PropertyFilter.eq("discrimination_field", "A")));
    assertThat(builder.build().getLimit()).isEqualTo(1);
    assertThat(builder.build().getOffset()).isEqualTo(2);
}
Also used : DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 2 with DatastoreMappingContext

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

the class DefaultDatastoreEntityConverterTests method testUnindexedField.

@Test
public void testUnindexedField() {
    UnindexedTestDatastoreItem item = new UnindexedTestDatastoreItem();
    item.setIndexedField(1L);
    item.setUnindexedField(2L);
    item.setUnindexedStringListField(Arrays.asList("a", "b"));
    item.setUnindexedMapField(new MapBuilder<String, String>().put("c", "C").put("d", "D").build());
    item.setEmbeddedItem(new UnindexedTestDatastoreItem(2, new UnindexedTestDatastoreItem(3, null)));
    item.setUnindexedItems(Collections.singletonList(new UnindexedTestDatastoreItem(4, new UnindexedTestDatastoreItem(5, null))));
    DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new DatastoreServiceObjectToKeyFactory(() -> this.datastore));
    Entity.Builder builder = getEntityBuilder();
    entityConverter.write(item, builder);
    Entity entity = builder.build();
    assertThat(entity.getLong("indexedField")).as("validate indexed field value").isEqualTo(1L);
    assertThat(entity.getLong("unindexedField")).as("validate unindexed field value").isEqualTo(2L);
    assertThat(entity.getValue("indexedField").excludeFromIndexes()).as("validate excludeFromIndexes on indexed field").isFalse();
    assertThat(entity.getValue("unindexedField").excludeFromIndexes()).as("validate excludeFromIndexes on unindexed field").isTrue();
    // the ListValue itself must NOT be unindexed or it will cause exception. the contents
    // individually must be. Same for map.
    assertThat(entity.getValue("unindexedStringListField").excludeFromIndexes()).isFalse();
    assertThat(((ListValue) entity.getValue("unindexedStringListField")).get().get(0).excludeFromIndexes()).isTrue();
    assertThat(((ListValue) entity.getValue("unindexedStringListField")).get().get(1).excludeFromIndexes()).isTrue();
    assertThat(entity.getValue("unindexedMapField").excludeFromIndexes()).isFalse();
    assertThat(((EntityValue) entity.getValue("unindexedMapField")).get().getValue("c").excludeFromIndexes()).isTrue();
    assertThat(((EntityValue) entity.getValue("unindexedMapField")).get().getValue("d").excludeFromIndexes()).isTrue();
    // Multi-level embedded entities - exclusion from indexes
    testMultiLevelEmbeddedEntityUnindexed(((EntityValue) entity.getValue("embeddedItem")).get());
    // Multi-level embedded entities in a list - exclusion from indexes
    testMultiLevelEmbeddedEntityUnindexed(((EntityValue) ((ListValue) entity.getValue("unindexedItems")).get().get(0)).get());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) EmbeddedEntity(org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) ListValue(com.google.cloud.datastore.ListValue) EntityValue(com.google.cloud.datastore.EntityValue) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) Test(org.junit.Test)

Example 3 with DatastoreMappingContext

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

the class DefaultDatastoreEntityConverterTests method testCollectionFieldsUnsupportedWriteOnly.

@Test
public 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) EmbeddedEntity(org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) DiscriminatorValue(org.springframework.cloud.gcp.data.datastore.core.mapping.DiscriminatorValue) NullValue(com.google.cloud.datastore.NullValue) EntityValue(com.google.cloud.datastore.EntityValue) Value(com.google.cloud.datastore.Value) StringValue(com.google.cloud.datastore.StringValue) ListValue(com.google.cloud.datastore.ListValue) Test(org.junit.Test)

Example 4 with DatastoreMappingContext

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

the class DefaultDatastoreEntityConverterTests method testCollectionFieldsUnsupportedWriteReadException.

@Test
public void testCollectionFieldsUnsupportedWriteReadException() {
    this.thrown.expect(DatastoreDataException.class);
    this.thrown.expectMessage("Unable to read org.springframework.cloud.gcp.data.datastore.core.convert." + "TestItemUnsupportedFields$CollectionOfUnsupportedTypes entity");
    this.thrown.expectMessage("Unable to read property unsupportedElts");
    this.thrown.expectMessage("Unable process elements of a collection");
    this.thrown.expectMessage("No converter found capable of converting from type [java.lang.Integer] " + "to type [org.springframework.cloud.gcp.data.datastore.core.convert." + "TestItemUnsupportedFields$NewType]");
    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();
    entityConverter.read(TestItemUnsupportedFields.CollectionOfUnsupportedTypes.class, entity);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) EmbeddedEntity(org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) Test(org.junit.Test)

Example 5 with DatastoreMappingContext

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

the class DefaultDatastoreEntityConverterTests method testUnsupportedTypeWrite.

@Test
public 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.equals(readItem)).as("read object should be equal to original").isTrue();
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) EmbeddedEntity(org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) Test(org.junit.Test)

Aggregations

DatastoreMappingContext (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext)14 FullEntity (com.google.cloud.datastore.FullEntity)10 Test (org.junit.Test)10 Entity (com.google.cloud.datastore.Entity)9 BaseEntity (com.google.cloud.datastore.BaseEntity)8 EmbeddedEntity (org.springframework.cloud.gcp.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 Before (org.junit.Before)4 DiscriminatorValue (org.springframework.cloud.gcp.data.datastore.core.mapping.DiscriminatorValue)4 DatastoreTemplate (org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate)3 EntityQuery (com.google.cloud.datastore.EntityQuery)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MapBuilder (org.springframework.cloud.gcp.core.util.MapBuilder)2 Datastore (com.google.cloud.datastore.Datastore)1 GqlQuery (com.google.cloud.datastore.GqlQuery)1