use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext in project spring-cloud-gcp by spring-cloud.
the class DefaultDatastoreEntityConverterTests method testCollectionFieldsUnsupportedWriteRead.
@Test
public void testCollectionFieldsUnsupportedWriteRead() {
TestItemUnsupportedFields.CollectionOfUnsupportedTypes item = getCollectionOfUnsupportedTypesItem();
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();
List<Value<?>> intList = entity.getList("unsupportedElts");
assertThat(intList.stream().map(Value::get).collect(Collectors.toList())).as("validate long list values").isEqualTo(Arrays.asList(1L, 0L));
TestItemUnsupportedFields.CollectionOfUnsupportedTypes read = entityConverter.read(TestItemUnsupportedFields.CollectionOfUnsupportedTypes.class, entity);
assertThat(read.equals(item)).as("read object should be equal to original").isTrue();
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext 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();
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext in project spring-cloud-gcp by spring-cloud.
the class DefaultDatastoreEntityConverterTests method testEmbeddedEntity.
@Test
public void testEmbeddedEntity() {
EmbeddedEntity embeddedEntityA = new EmbeddedEntity("item 0");
EmbeddedEntity embeddedEntityB = new EmbeddedEntity("item 1");
List<EmbeddedEntity> embeddedEntities = Arrays.asList(embeddedEntityA, embeddedEntityB);
Map<String, String> mapSimpleValues = new HashMap<>();
mapSimpleValues.put("a", "valueA");
mapSimpleValues.put("b", "valueB");
Map<String, String[]> mapListValues = new HashMap<>();
mapListValues.put("a", new String[] { "valueA" });
mapListValues.put("b", new String[] { "valueB" });
Map<String, EmbeddedEntity> embeddedEntityMapEmbeddedEntity = new HashMap<>();
embeddedEntityMapEmbeddedEntity.put("a", embeddedEntityA);
embeddedEntityMapEmbeddedEntity.put("b", embeddedEntityB);
Map<String, List<EmbeddedEntity>> embeddedEntityMapListOfEmbeddedEntities = new HashMap<>();
embeddedEntityMapListOfEmbeddedEntities.put("a", Arrays.asList(embeddedEntityA));
embeddedEntityMapListOfEmbeddedEntities.put("b", Arrays.asList(embeddedEntityB));
Map<String, Map<Long, Map<String, String>>> nestedEmbeddedMap = new HashMap<>();
Map<Long, Map<String, String>> nestedInnerEmbeddedMap = new HashMap<>();
nestedInnerEmbeddedMap.put(1L, mapSimpleValues);
nestedEmbeddedMap.put("outer1", nestedInnerEmbeddedMap);
Map<TestDatastoreItem.Color, String> enumKeysMap = new HashMap<>();
enumKeysMap.put(TestDatastoreItem.Color.BLACK, "black");
enumKeysMap.put(TestDatastoreItem.Color.WHITE, "white");
CustomMap customMap = new CustomMap();
customMap.put("key1", "val1");
TestItemWithEmbeddedEntity item = new TestItemWithEmbeddedEntity(123, new EmbeddedEntity("abc"), embeddedEntities, mapSimpleValues, mapListValues, embeddedEntityMapEmbeddedEntity, embeddedEntityMapListOfEmbeddedEntities, enumKeysMap, customMap);
item.setNestedEmbeddedMaps(nestedEmbeddedMap);
DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new DatastoreServiceObjectToKeyFactory(() -> this.datastore));
Entity.Builder builder = getEntityBuilder();
entityConverter.write(item, builder);
Entity entity = builder.build();
assertThat(entity.getList("listOfEmbeddedEntities").stream().map((val) -> ((BaseEntity<?>) val.get()).getString("stringField")).collect(Collectors.toList())).as("validate embedded entity").isEqualTo(Arrays.asList("item 0", "item 1"));
assertThat(entity.getEntity("embeddedEntityField").getString("stringField")).as("validate embedded entity").isEqualTo("abc");
assertThat(entity.getLong("intField")).as("validate int field").isEqualTo(123L);
assertThat(entity.getEntity("nestedEmbeddedMaps").getEntity("outer1").getEntity("1").getString("a")).isEqualTo("valueA");
assertThat(entity.getEntity("nestedEmbeddedMaps").getEntity("outer1").getEntity("1").getString("b")).isEqualTo("valueB");
assertThat(entity.getEntity("embeddedMapSimpleValues").getString("a")).isEqualTo("valueA");
assertThat(entity.getEntity("embeddedMapSimpleValues").getString("b")).isEqualTo("valueB");
assertThat(entity.getEntity("embeddedMapListOfValues").getList("a")).contains(StringValue.of("valueA"));
assertThat(entity.getEntity("embeddedMapListOfValues").getList("b")).contains(StringValue.of("valueB"));
assertThat(entity.getEntity("embeddedEntityMapEmbeddedEntity").getEntity("a").getString("stringField")).isEqualTo("item 0");
assertThat(entity.getEntity("embeddedEntityMapEmbeddedEntity").getEntity("b").getString("stringField")).isEqualTo("item 1");
List<Value> embeddedMapValuesEmbeddedEntityA = entity.getEntity("embeddedEntityMapListOfEmbeddedEntities").getList("a");
List<Value> embeddedMapValuesEmbeddedEntityB = entity.getEntity("embeddedEntityMapListOfEmbeddedEntities").getList("b");
assertThat(((BaseEntity) embeddedMapValuesEmbeddedEntityA.get(0).get()).getString("stringField")).isEqualTo("item 0");
assertThat(embeddedMapValuesEmbeddedEntityA.size()).isEqualTo(1);
assertThat(((BaseEntity) embeddedMapValuesEmbeddedEntityB.get(0).get()).getString("stringField")).isEqualTo("item 1");
assertThat(embeddedMapValuesEmbeddedEntityB.size()).isEqualTo(1);
TestItemWithEmbeddedEntity read = entityConverter.read(TestItemWithEmbeddedEntity.class, entity);
assertThat(read.getNestedEmbeddedMaps().get("outer1").get(1L).get("a")).isEqualTo("valueA");
assertThat(read.getNestedEmbeddedMaps().get("outer1").get(1L).get("b")).isEqualTo("valueB");
assertThat(entity.getEntity("customMap").getString("key1")).isEqualTo("val1");
assertThat(read).as("read objects equals the original one").isEqualTo(item);
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext in project spring-cloud-gcp by spring-cloud.
the class DatastoreRepositoryFactoryTests method setUp.
@Before
public void setUp() {
DatastoreMappingContext datastoreMappingContext = new DatastoreMappingContext();
this.datastoreTemplate = mock(DatastoreTemplate.class);
this.datastoreRepositoryFactory = new DatastoreRepositoryFactory(datastoreMappingContext, this.datastoreTemplate);
}
Aggregations