Search in sources :

Example 1 with MapBuilder

use of com.google.cloud.spring.core.util.MapBuilder in project spring-cloud-gcp by GoogleCloudPlatform.

the class DefaultDatastoreEntityConverterTests method testUnindexedField.

@Test
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());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) BaseEntity(com.google.cloud.datastore.BaseEntity) EmbeddedEntity(com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) ListValue(com.google.cloud.datastore.ListValue) EntityValue(com.google.cloud.datastore.EntityValue) MapBuilder(com.google.cloud.spring.core.util.MapBuilder) Test(org.junit.jupiter.api.Test)

Example 2 with MapBuilder

use of com.google.cloud.spring.core.util.MapBuilder in project spring-cloud-gcp by GoogleCloudPlatform.

the class TracingMessageReceiverTest method testReceiverMessage.

@Test
void testReceiverMessage() {
    PubsubMessage.Builder pubSubMessageBuilder = PubsubMessage.newBuilder().putAllAttributes(new MapBuilder<String, String>().put("b3", "80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90").build()).setData(ByteString.copyFrom("test".getBytes()));
    AckReplyConsumer mockAckReplyConsumer = mock(AckReplyConsumer.class);
    tracingMessageReceiver.receiveMessage(pubSubMessageBuilder.build(), mockAckReplyConsumer);
    // we expect tracing header to be stripped
    verify(mockMesageReceiver, times(1)).receiveMessage(eq(pubSubMessageBuilder.clearAttributes().build()), refEq(mockAckReplyConsumer));
}
Also used : MapBuilder(com.google.cloud.spring.core.util.MapBuilder) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.jupiter.api.Test)

Aggregations

MapBuilder (com.google.cloud.spring.core.util.MapBuilder)2 Test (org.junit.jupiter.api.Test)2 BaseEntity (com.google.cloud.datastore.BaseEntity)1 Entity (com.google.cloud.datastore.Entity)1 EntityValue (com.google.cloud.datastore.EntityValue)1 FullEntity (com.google.cloud.datastore.FullEntity)1 ListValue (com.google.cloud.datastore.ListValue)1 AckReplyConsumer (com.google.cloud.pubsub.v1.AckReplyConsumer)1 EmbeddedEntity (com.google.cloud.spring.data.datastore.core.convert.TestItemWithEmbeddedEntity.EmbeddedEntity)1 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)1 PubsubMessage (com.google.pubsub.v1.PubsubMessage)1