Search in sources :

Example 1 with ComparableBeanContextSupport

use of com.google.cloud.spring.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport in project spring-cloud-gcp by GoogleCloudPlatform.

the class DefaultDatastoreEntityConverterTests method testCollectionFieldsUnsupportedCollection.

@Test
void testCollectionFieldsUnsupportedCollection() {
    ComparableBeanContextSupport comparableBeanContextSupport = new ComparableBeanContextSupport();
    comparableBeanContextSupport.add("this implementation of Collection");
    comparableBeanContextSupport.add("is unsupported out of the box!");
    TestDatastoreItemCollections item = new TestDatastoreItemCollections(Arrays.asList(1, 2), comparableBeanContextSupport, new String[] { "abc", "def" }, new boolean[] { true, false }, null, null);
    Entity.Builder builder = getEntityBuilder();
    ENTITY_CONVERTER.write(item, builder);
    Entity entity = builder.build();
    assertThatThrownBy(() -> ENTITY_CONVERTER.read(TestDatastoreItemCollections.class, entity)).isInstanceOf(DatastoreDataException.class).hasMessageContaining("Unable to read" + " com.google.cloud.spring.data.datastore.core.convert.TestDatastoreItemCollections" + " entity;").hasMessageContaining("Unable to read property beanContext;").hasMessageContaining("Failed to convert from type [java.util.ArrayList<?>] to type" + " [com.google.cloud.spring.data.datastore.core.convert.TestDatastoreItemCollections$ComparableBeanContextSupport]");
}
Also used : ComparableBeanContextSupport(com.google.cloud.spring.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport) 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) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Test(org.junit.jupiter.api.Test)

Example 2 with ComparableBeanContextSupport

use of com.google.cloud.spring.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport in project spring-cloud-gcp by GoogleCloudPlatform.

the class DefaultDatastoreEntityConverterTests method testCollectionFields.

@Test
void testCollectionFields() {
    byte[][] bytes = { { 1, 2 }, { 3, 4 } };
    List<byte[]> listByteArray = Arrays.asList(bytes);
    ComparableBeanContextSupport comparableBeanContextSupport = new ComparableBeanContextSupport();
    comparableBeanContextSupport.add("this implementation of Collection");
    comparableBeanContextSupport.add("is supported through a custom converter!");
    TestDatastoreItemCollections item = new TestDatastoreItemCollections(Arrays.asList(1, 2), comparableBeanContextSupport, new String[] { "abc", "def" }, new boolean[] { true, false }, bytes, listByteArray);
    DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new TwoStepsConversions(new DatastoreCustomConversions(Arrays.asList(new Converter<List<String>, ComparableBeanContextSupport>() {

        @Override
        public ComparableBeanContextSupport convert(List<String> source) {
            ComparableBeanContextSupport bcs = new ComparableBeanContextSupport();
            source.forEach(bcs::add);
            return bcs;
        }
    }, new Converter<ComparableBeanContextSupport, List<String>>() {

        @Override
        public List<String> convert(ComparableBeanContextSupport bcs) {
            List<String> list = new ArrayList<>();
            bcs.iterator().forEachRemaining(s -> list.add((String) s));
            return list;
        }
    })), null, datastoreMappingContext));
    Entity.Builder builder = getEntityBuilder();
    entityConverter.write(item, builder);
    Entity entity = builder.build();
    List<Value<?>> intList = entity.getList("intList");
    assertThat(intList.stream().map(Value::get).collect(Collectors.toList())).as("validate int list values").isEqualTo(Arrays.asList(1L, 2L));
    List<Value<?>> stringArray = entity.getList("stringArray");
    assertThat(stringArray.stream().map(Value::get).collect(Collectors.toList())).as("validate string array values").isEqualTo(Arrays.asList("abc", "def"));
    List<Value<?>> beanContext = entity.getList("beanContext");
    assertThat(beanContext.stream().map(Value::get).collect(Collectors.toSet())).as("validate bean context values").isEqualTo(new HashSet<>(Arrays.asList("this implementation of Collection", "is supported through a custom converter!")));
    List<Value<?>> bytesVals = entity.getList("bytes");
    assertThat(bytesVals.stream().map(Value::get).collect(Collectors.toList())).as("validate array of byte[] values").isEqualTo(Arrays.asList(Blob.copyFrom(new byte[] { 1, 2 }), Blob.copyFrom(new byte[] { 3, 4 })));
    List<Value<?>> listByteArrayVals = entity.getList("listByteArray");
    assertThat(listByteArrayVals.stream().map(Value::get).collect(Collectors.toList())).as("validate list of byte[]").isEqualTo(Arrays.asList(Blob.copyFrom(new byte[] { 1, 2 }), Blob.copyFrom(new byte[] { 3, 4 })));
    TestDatastoreItemCollections readItem = entityConverter.read(TestDatastoreItemCollections.class, entity);
    assertThat(item).as("read object should be equal to original").isEqualTo(readItem);
}
Also used : ComparableBeanContextSupport(com.google.cloud.spring.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport) 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) ArrayList(java.util.ArrayList) 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) Converter(org.springframework.core.convert.converter.Converter) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

BaseEntity (com.google.cloud.datastore.BaseEntity)2 Entity (com.google.cloud.datastore.Entity)2 FullEntity (com.google.cloud.datastore.FullEntity)2 ComparableBeanContextSupport (com.google.cloud.spring.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport)2 EmbeddedEntity (com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity)2 Test (org.junit.jupiter.api.Test)2 EntityValue (com.google.cloud.datastore.EntityValue)1 ListValue (com.google.cloud.datastore.ListValue)1 NullValue (com.google.cloud.datastore.NullValue)1 StringValue (com.google.cloud.datastore.StringValue)1 Value (com.google.cloud.datastore.Value)1 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)1 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)1 DiscriminatorValue (com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorValue)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Converter (org.springframework.core.convert.converter.Converter)1