use of org.springframework.cloud.gcp.core.util.MapBuilder in project spring-cloud-gcp by spring-cloud.
the class DefaultDatastoreEntityConverterTests method testUnindexedField.
@Test
public void testUnindexedField() {
UnindexedTestDatastoreItem item = new UnindexedTestDatastoreItem();
item.setIndexedField(1L);
item.setUnindexedField(2L);
item.setUnindexedStringListField(Arrays.asList("a", "b"));
item.setUnindexedMapField(new MapBuilder<String, String>().put("c", "C").put("d", "D").build());
item.setEmbeddedItem(new UnindexedTestDatastoreItem(2, new UnindexedTestDatastoreItem(3, null)));
item.setUnindexedItems(Collections.singletonList(new UnindexedTestDatastoreItem(4, new UnindexedTestDatastoreItem(5, null))));
DatastoreEntityConverter entityConverter = new DefaultDatastoreEntityConverter(new DatastoreMappingContext(), new DatastoreServiceObjectToKeyFactory(() -> this.datastore));
Entity.Builder builder = getEntityBuilder();
entityConverter.write(item, builder);
Entity entity = builder.build();
assertThat(entity.getLong("indexedField")).as("validate indexed field value").isEqualTo(1L);
assertThat(entity.getLong("unindexedField")).as("validate unindexed field value").isEqualTo(2L);
assertThat(entity.getValue("indexedField").excludeFromIndexes()).as("validate excludeFromIndexes on indexed field").isFalse();
assertThat(entity.getValue("unindexedField").excludeFromIndexes()).as("validate excludeFromIndexes on unindexed field").isTrue();
// the ListValue itself must NOT be unindexed or it will cause exception. the contents
// individually must be. Same for map.
assertThat(entity.getValue("unindexedStringListField").excludeFromIndexes()).isFalse();
assertThat(((ListValue) entity.getValue("unindexedStringListField")).get().get(0).excludeFromIndexes()).isTrue();
assertThat(((ListValue) entity.getValue("unindexedStringListField")).get().get(1).excludeFromIndexes()).isTrue();
assertThat(entity.getValue("unindexedMapField").excludeFromIndexes()).isFalse();
assertThat(((EntityValue) entity.getValue("unindexedMapField")).get().getValue("c").excludeFromIndexes()).isTrue();
assertThat(((EntityValue) entity.getValue("unindexedMapField")).get().getValue("d").excludeFromIndexes()).isTrue();
// Multi-level embedded entities - exclusion from indexes
testMultiLevelEmbeddedEntityUnindexed(((EntityValue) entity.getValue("embeddedItem")).get());
// Multi-level embedded entities in a list - exclusion from indexes
testMultiLevelEmbeddedEntityUnindexed(((EntityValue) ((ListValue) entity.getValue("unindexedItems")).get().get(0)).get());
}
Aggregations