Search in sources :

Example 1 with ComparableBeanContextSupport

use of org.springframework.cloud.gcp.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport in project spring-cloud-gcp by spring-cloud.

the class DefaultDatastoreEntityConverterTests method testCollectionFieldsUnsupportedCollection.

@Test
public void testCollectionFieldsUnsupportedCollection() {
    this.thrown.expect(DatastoreDataException.class);
    this.thrown.expectMessage("Unable to read " + "org.springframework.cloud.gcp.data.datastore.core.convert.TestDatastoreItemCollections entity;");
    this.thrown.expectMessage("Unable to read property beanContext;");
    this.thrown.expectMessage("Failed to convert from type [java.util.ArrayList<?>] " + "to type [org.springframework.cloud.gcp.data.datastore.core.convert.TestDatastoreItemCollections$ComparableBeanContextSupport]");
    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();
    TestDatastoreItemCollections result = ENTITY_CONVERTER.read(TestDatastoreItemCollections.class, entity);
}
Also used : ComparableBeanContextSupport(org.springframework.cloud.gcp.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport) 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) Test(org.junit.Test)

Example 2 with ComparableBeanContextSupport

use of org.springframework.cloud.gcp.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport in project spring-cloud-gcp by spring-cloud.

the class DefaultDatastoreEntityConverterTests method testCollectionFields.

@Test
public 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.equals(readItem)).as("read object should be equal to original").isTrue();
}
Also used : ComparableBeanContextSupport(org.springframework.cloud.gcp.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport) 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) ArrayList(java.util.ArrayList) 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) Converter(org.springframework.core.convert.converter.Converter) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

BaseEntity (com.google.cloud.datastore.BaseEntity)2 Entity (com.google.cloud.datastore.Entity)2 FullEntity (com.google.cloud.datastore.FullEntity)2 Test (org.junit.Test)2 ComparableBeanContextSupport (org.springframework.cloud.gcp.data.datastore.core.convert.TestDatastoreItemCollections.ComparableBeanContextSupport)2 EmbeddedEntity (org.springframework.cloud.gcp.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity)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 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DatastoreMappingContext (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext)1 DiscriminatorValue (org.springframework.cloud.gcp.data.datastore.core.mapping.DiscriminatorValue)1 Converter (org.springframework.core.convert.converter.Converter)1