|
| 1 | +package io.temporal.internal.testservice; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import com.google.protobuf.ByteString; |
| 6 | +import io.temporal.api.common.v1.Payload; |
| 7 | +import io.temporal.api.common.v1.SearchAttributes; |
| 8 | +import io.temporal.api.enums.v1.IndexedValueType; |
| 9 | +import io.temporal.internal.common.ProtoEnumNameUtils; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +public class TestVisibilityStoreImplTest { |
| 13 | + private static final String METADATA_TYPE_KEY = "type"; |
| 14 | + private static final String DEFAULT_KEY_INTEGER = "CustomIntField"; |
| 15 | + |
| 16 | + @Test |
| 17 | + public void testTypeMetadataIsAddedToPayloads() { |
| 18 | + TestVisibilityStoreImpl visibilityStore = new TestVisibilityStoreImpl(); |
| 19 | + Payload payloadWithoutType = |
| 20 | + Payload.newBuilder().setData(ByteString.copyFromUtf8("test data")).build(); |
| 21 | + |
| 22 | + assertFalse( |
| 23 | + "Payload should not have type metadata initially", |
| 24 | + payloadWithoutType.getMetadataMap().containsKey(METADATA_TYPE_KEY)); |
| 25 | + |
| 26 | + SearchAttributes.Builder searchAttributesBuilder = SearchAttributes.newBuilder(); |
| 27 | + searchAttributesBuilder.putIndexedFields(DEFAULT_KEY_INTEGER, payloadWithoutType); |
| 28 | + SearchAttributes searchAttributes = searchAttributesBuilder.build(); |
| 29 | + ExecutionId executionId = new ExecutionId("test-namespace", "test-workflow-id", "test-run-id"); |
| 30 | + try { |
| 31 | + SearchAttributes result = |
| 32 | + visibilityStore.upsertSearchAttributesForExecution(executionId, searchAttributes); |
| 33 | + assertNotNull("Result should not be null", result); |
| 34 | + |
| 35 | + if (result.containsIndexedFields(DEFAULT_KEY_INTEGER)) { |
| 36 | + Payload resultPayload = result.getIndexedFieldsOrThrow(DEFAULT_KEY_INTEGER); |
| 37 | + assertTrue( |
| 38 | + "Type metadata should be present", |
| 39 | + resultPayload.getMetadataMap().containsKey(METADATA_TYPE_KEY)); |
| 40 | + |
| 41 | + ByteString typeMetadata = resultPayload.getMetadataMap().get(METADATA_TYPE_KEY); |
| 42 | + String typeString = typeMetadata.toStringUtf8(); |
| 43 | + String expectedTypeString = |
| 44 | + ProtoEnumNameUtils.uniqueToSimplifiedName(IndexedValueType.INDEXED_VALUE_TYPE_INT); |
| 45 | + |
| 46 | + assertEquals("Type metadata should match expected type", expectedTypeString, typeString); |
| 47 | + } |
| 48 | + } catch (Exception e) { |
| 49 | + assertTrue("Should be a validation error", e.getMessage().contains("invalid value")); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testExistingTypeMetadataIsPreserved() { |
| 55 | + TestVisibilityStoreImpl visibilityStore = new TestVisibilityStoreImpl(); |
| 56 | + Payload payloadWithType = |
| 57 | + Payload.newBuilder() |
| 58 | + .setData(ByteString.copyFromUtf8("test data")) |
| 59 | + .putMetadata(METADATA_TYPE_KEY, ByteString.copyFromUtf8("Int")) |
| 60 | + .build(); |
| 61 | + |
| 62 | + assertTrue( |
| 63 | + "Payload should have type metadata initially", |
| 64 | + payloadWithType.getMetadataMap().containsKey(METADATA_TYPE_KEY)); |
| 65 | + |
| 66 | + SearchAttributes.Builder searchAttributesBuilder = SearchAttributes.newBuilder(); |
| 67 | + searchAttributesBuilder.putIndexedFields(DEFAULT_KEY_INTEGER, payloadWithType); |
| 68 | + SearchAttributes searchAttributes = searchAttributesBuilder.build(); |
| 69 | + ExecutionId executionId = new ExecutionId("test-namespace", "test-workflow-id", "test-run-id"); |
| 70 | + |
| 71 | + try { |
| 72 | + SearchAttributes result = |
| 73 | + visibilityStore.upsertSearchAttributesForExecution(executionId, searchAttributes); |
| 74 | + |
| 75 | + assertNotNull("Result should not be null", result); |
| 76 | + |
| 77 | + if (result.containsIndexedFields(DEFAULT_KEY_INTEGER)) { |
| 78 | + Payload resultPayload = result.getIndexedFieldsOrThrow(DEFAULT_KEY_INTEGER); |
| 79 | + assertTrue( |
| 80 | + "Type metadata should be present", |
| 81 | + resultPayload.getMetadataMap().containsKey(METADATA_TYPE_KEY)); |
| 82 | + |
| 83 | + ByteString typeMetadata = resultPayload.getMetadataMap().get(METADATA_TYPE_KEY); |
| 84 | + String typeString = typeMetadata.toStringUtf8(); |
| 85 | + |
| 86 | + assertEquals("Existing type metadata should be preserved", "Int", typeString); |
| 87 | + } |
| 88 | + } catch (Exception e) { |
| 89 | + assertTrue("Should be a validation error", e.getMessage().contains("invalid value")); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments