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