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