diff --git a/src/test/java/com/uber/cadence/internal/compatibility/ProtoObjects.java b/src/test/java/com/uber/cadence/internal/compatibility/ProtoObjects.java index 314e0f42d..08b5d668d 100644 --- a/src/test/java/com/uber/cadence/internal/compatibility/ProtoObjects.java +++ b/src/test/java/com/uber/cadence/internal/compatibility/ProtoObjects.java @@ -201,6 +201,14 @@ public final class ProtoObjects { .setScheduledTimeOfThisAttempt(timestampNanos(3)) .setTaskToken(utf8("taskToken")) .build(); + public static final BadBinaryInfo BAD_BINARY_INFO = + BadBinaryInfo.newBuilder() + .setReason("reason") + .setOperator("operator") + .setCreatedTime(timestampNanos(3)) + .build(); + public static final BadBinaries BAD_BINARIES = + BadBinaries.newBuilder().putBinaries("badBinaryKey", BAD_BINARY_INFO).build(); public static final Domain DOMAIN = Domain.newBuilder() .setId("uuid") @@ -210,15 +218,7 @@ public final class ProtoObjects { .setOwnerEmail("email") .putAllData(DATA) .setWorkflowExecutionRetentionPeriod(days(2)) - .setBadBinaries( - BadBinaries.newBuilder() - .putBinaries( - "badBinaryKey", - BadBinaryInfo.newBuilder() - .setReason("reason") - .setOperator("operator") - .setCreatedTime(timestampNanos(3)) - .build())) + .setBadBinaries(BAD_BINARIES) .setHistoryArchivalStatus(ArchivalStatus.ARCHIVAL_STATUS_ENABLED) .setHistoryArchivalUri("historyArchivalUri") .setVisibilityArchivalStatus(ArchivalStatus.ARCHIVAL_STATUS_DISABLED) @@ -228,6 +228,24 @@ public final class ProtoObjects { .setFailoverVersion(1) .setIsGlobalDomain(true) .build(); + public static final WorkerVersionInfo WORKER_VERSION_INFO = + WorkerVersionInfo.newBuilder().setFeatureVersion("featureVersion").setImpl("impl").build(); + public static final StartTimeFilter START_TIME_FILTER = + StartTimeFilter.newBuilder() + .setEarliestTime(timestampNanos(2)) + .setLatestTime(timestampNanos(3)) + .build(); + public static final WorkflowExecutionFilter WORKFLOW_EXECUTION_FILTER = + WorkflowExecutionFilter.newBuilder() + .setWorkflowId(WORKFLOW_EXECUTION.getWorkflowId()) + .setRunId(WORKFLOW_EXECUTION.getRunId()) + .build(); + public static final WorkflowTypeFilter WORKFLOW_TYPE_FILTER = + WorkflowTypeFilter.newBuilder().setName(WORKFLOW_TYPE.getName()).build(); + public static final StatusFilter STATUS_FILTER = + StatusFilter.newBuilder() + .setStatus(WorkflowExecutionCloseStatus.WORKFLOW_EXECUTION_CLOSE_STATUS_COMPLETED) + .build(); public static Decision DECISION_SCHEDULE_ACTIVITY_TASK = Decision.newBuilder() @@ -930,8 +948,7 @@ public final class ProtoObjects { .setResultType(QueryResultType.QUERY_RESULT_TYPE_ANSWERED) .setAnswer(payload("queryResult")) .setErrorMessage("errorMessage")) - .setWorkerVersionInfo( - WorkerVersionInfo.newBuilder().setFeatureVersion("featureVersion").setImpl("impl")) + .setWorkerVersionInfo(WORKER_VERSION_INFO) .setTaskToken(utf8("taskToken")) .build(); public static final ScanWorkflowExecutionsRequest SCAN_WORKFLOW_EXECUTIONS_REQUEST = @@ -1144,36 +1161,21 @@ public final class ProtoObjects { ListClosedWorkflowExecutionsRequest.newBuilder() .setDomain("domain") .setPageSize(1) - .setExecutionFilter( - WorkflowExecutionFilter.newBuilder() - .setWorkflowId(WORKFLOW_EXECUTION.getWorkflowId()) - .setRunId(WORKFLOW_EXECUTION.getRunId())) - .setTypeFilter(WorkflowTypeFilter.newBuilder().setName(WORKFLOW_TYPE.getName())) - .setStatusFilter( - StatusFilter.newBuilder() - .setStatus( - WorkflowExecutionCloseStatus.WORKFLOW_EXECUTION_CLOSE_STATUS_COMPLETED)) + .setExecutionFilter(WORKFLOW_EXECUTION_FILTER) + .setTypeFilter(WORKFLOW_TYPE_FILTER) + .setStatusFilter(STATUS_FILTER) .setNextPageToken(utf8("nextPageToken")) - .setStartTimeFilter( - StartTimeFilter.newBuilder() - .setEarliestTime(timestampNanos(2)) - .setLatestTime(timestampNanos(3))) + .setStartTimeFilter(START_TIME_FILTER) .build(); public static final ListOpenWorkflowExecutionsRequest LIST_OPEN_WORKFLOW_EXECUTIONS_REQUEST = ListOpenWorkflowExecutionsRequest.newBuilder() .setDomain("domain") .setPageSize(1) - .setExecutionFilter( - WorkflowExecutionFilter.newBuilder() - .setWorkflowId(WORKFLOW_EXECUTION.getWorkflowId()) - .setRunId(WORKFLOW_EXECUTION.getRunId())) - .setTypeFilter(WorkflowTypeFilter.newBuilder().setName(WORKFLOW_TYPE.getName())) + .setExecutionFilter(WORKFLOW_EXECUTION_FILTER) + .setTypeFilter(WORKFLOW_TYPE_FILTER) .setNextPageToken(utf8("nextPageToken")) - .setStartTimeFilter( - StartTimeFilter.newBuilder() - .setEarliestTime(timestampNanos(2)) - .setLatestTime(timestampNanos(3))) + .setStartTimeFilter(START_TIME_FILTER) .build(); public static final StartWorkflowExecutionResponse START_WORKFLOW_EXECUTION_RESPONSE = @@ -1385,7 +1387,7 @@ public final class ProtoObjects { private ProtoObjects() {} - private static Payload payload(String value) { + public static Payload payload(String value) { return Payload.newBuilder().setData(utf8(value)).build(); } diff --git a/src/test/java/com/uber/cadence/internal/compatibility/ThriftObjects.java b/src/test/java/com/uber/cadence/internal/compatibility/ThriftObjects.java index 63cc6ca1a..2c157ec87 100644 --- a/src/test/java/com/uber/cadence/internal/compatibility/ThriftObjects.java +++ b/src/test/java/com/uber/cadence/internal/compatibility/ThriftObjects.java @@ -138,6 +138,8 @@ public final class ThriftObjects { .setStartedTimestamp(2) .setAttempt(3) .setOriginalScheduledTimestamp(4); + public static final WorkerVersionInfo WORKER_VERSION_INFO = + new WorkerVersionInfo().setFeatureVersion("featureVersion").setImpl("impl"); public static final SupportedClientVersions SUPPORTED_CLIENT_VERSIONS = new SupportedClientVersions().setGoSdk("goSdk").setJavaSdk("javaSdk"); public static final Map INDEXED_VALUES = @@ -161,23 +163,25 @@ public final class ThriftObjects { .setOwnerEmail("email") .setData(DATA) .setUuid("uuid"); + public static final BadBinaryInfo BAD_BINARY_INFO = + new BadBinaryInfo().setReason("reason").setOperator("operator").setCreatedTimeNano(3); + public static final BadBinaries BAD_BINARIES = + new BadBinaries().setBinaries(ImmutableMap.of("badBinaryKey", BAD_BINARY_INFO)); public static final DomainConfiguration DOMAIN_CONFIGURATION = new DomainConfiguration() .setWorkflowExecutionRetentionPeriodInDays(2) - .setBadBinaries( - new BadBinaries() - .setBinaries( - ImmutableMap.of( - "badBinaryKey", - new BadBinaryInfo() - .setReason("reason") - .setOperator("operator") - .setCreatedTimeNano(3)))) + .setBadBinaries(BAD_BINARIES) .setHistoryArchivalStatus(ArchivalStatus.ENABLED) .setHistoryArchivalURI("historyArchivalUri") .setVisibilityArchivalStatus(ArchivalStatus.DISABLED) .setVisibilityArchivalURI("visibilityArchivalUri") .setEmitMetric(true); + public static final StartTimeFilter START_TIME_FILTER = + new StartTimeFilter().setEarliestTime(2).setLatestTime(3); + public static final WorkflowExecutionFilter WORKFLOW_EXECUTION_FILTER = + new WorkflowExecutionFilter().setWorkflowId(WORKFLOW_ID).setRunId(RUN_ID); + public static final WorkflowTypeFilter WORKFLOW_TYPE_FILTER = + new WorkflowTypeFilter().setName(WORKFLOW_TYPE.getName()); public static final DomainReplicationConfiguration DOMAIN_REPLICATION_CONFIGURATION = new DomainReplicationConfiguration() @@ -814,8 +818,7 @@ public final class ThriftObjects { .setCompletedType(QueryTaskCompletedType.COMPLETED) .setQueryResult(utf8("queryResult")) .setErrorMessage("errorMessage") - .setWorkerVersionInfo( - new WorkerVersionInfo().setFeatureVersion("featureVersion").setImpl("impl")) + .setWorkerVersionInfo(WORKER_VERSION_INFO) .setTaskToken(utf8("taskToken")); public static final ListWorkflowExecutionsRequest LIST_WORKFLOW_EXECUTIONS_REQUEST = @@ -990,22 +993,20 @@ public final class ThriftObjects { new ListClosedWorkflowExecutionsRequest() .setDomain("domain") .setMaximumPageSize(1) - .setExecutionFilter( - new WorkflowExecutionFilter().setWorkflowId(WORKFLOW_ID).setRunId(RUN_ID)) - .setTypeFilter(new WorkflowTypeFilter().setName(WORKFLOW_TYPE.getName())) + .setExecutionFilter(WORKFLOW_EXECUTION_FILTER) + .setTypeFilter(WORKFLOW_TYPE_FILTER) .setStatusFilter(WorkflowExecutionCloseStatus.COMPLETED) .setNextPageToken(utf8("nextPageToken")) - .setStartTimeFilter(new StartTimeFilter().setEarliestTime(2).setLatestTime(3)); + .setStartTimeFilter(START_TIME_FILTER); public static final ListOpenWorkflowExecutionsRequest LIST_OPEN_WORKFLOW_EXECUTIONS_REQUEST = new ListOpenWorkflowExecutionsRequest() .setDomain("domain") .setMaximumPageSize(1) - .setExecutionFilter( - new WorkflowExecutionFilter().setWorkflowId(WORKFLOW_ID).setRunId(RUN_ID)) - .setTypeFilter(new WorkflowTypeFilter().setName(WORKFLOW_TYPE.getName())) + .setExecutionFilter(WORKFLOW_EXECUTION_FILTER) + .setTypeFilter(WORKFLOW_TYPE_FILTER) .setNextPageToken(utf8("nextPageToken")) - .setStartTimeFilter(new StartTimeFilter().setEarliestTime(2).setLatestTime(3)); + .setStartTimeFilter(START_TIME_FILTER); public static final StartWorkflowExecutionResponse START_WORKFLOW_EXECUTION_RESPONSE = new StartWorkflowExecutionResponse().setRunId(RUN_ID); diff --git a/src/test/java/com/uber/cadence/internal/compatibility/proto/TypeMapperTest.java b/src/test/java/com/uber/cadence/internal/compatibility/proto/TypeMapperTest.java new file mode 100644 index 000000000..9b0bc1440 --- /dev/null +++ b/src/test/java/com/uber/cadence/internal/compatibility/proto/TypeMapperTest.java @@ -0,0 +1,157 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package com.uber.cadence.internal.compatibility.proto; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.protobuf.Message; +import com.uber.cadence.WorkflowExecutionCloseStatus; +import com.uber.cadence.internal.compatibility.ProtoObjects; +import com.uber.cadence.internal.compatibility.ThriftObjects; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class TypeMapperTest { + + @Parameterized.Parameter(0) + public String testName; + + @Parameterized.Parameter(1) + public T from; + + @Parameterized.Parameter(2) + public P to; + + @Parameterized.Parameter(3) + public Function via; + + @Test + public void testMapper() { + P actual = via.apply(from); + assertEquals(to, actual); + } + + @Test + public void testHandlesNull() { + P actual = via.apply(null); + + if (actual instanceof List) { + assertTrue( + "Mapper functions returning a list should return an empty list", + ((List) actual).isEmpty()); + } else if (actual instanceof Map) { + assertTrue( + "Mapper functions returning a map should return an empty map", + ((Map) actual).isEmpty()); + } else if (actual instanceof Message) { + assertEquals( + "Mapper functions returning a Message should return the default value", + ((Message) actual).getDefaultInstanceForType(), + actual); + } else { + assertNull("Mapper functions should accept null, returning null", actual); + } + } + + @Parameterized.Parameters(name = "{0}") + public static Iterable cases() { + return Arrays.asList( + testCase( + ThriftObjects.BAD_BINARY_INFO, ProtoObjects.BAD_BINARY_INFO, TypeMapper::badBinaryInfo), + testCase( + ThriftObjects.utf8Bytes("data"), ProtoObjects.payload("data"), TypeMapper::payload), + testCase(ThriftObjects.ACTIVITY_TYPE, ProtoObjects.ACTIVITY_TYPE, TypeMapper::activityType), + testCase(ThriftObjects.WORKFLOW_TYPE, ProtoObjects.WORKFLOW_TYPE, TypeMapper::workflowType), + testCase(ThriftObjects.TASK_LIST, ProtoObjects.TASK_LIST, TypeMapper::taskList), + testCase( + ThriftObjects.TASK_LIST_METADATA, + ProtoObjects.TASK_LIST_METADATA, + TypeMapper::taskListMetadata), + testCase(ThriftObjects.RETRY_POLICY, ProtoObjects.RETRY_POLICY, TypeMapper::retryPolicy), + testCase(ThriftObjects.HEADER, ProtoObjects.HEADER, TypeMapper::header), + testCase(ThriftObjects.MEMO, ProtoObjects.MEMO, TypeMapper::memo), + testCase( + ThriftObjects.SEARCH_ATTRIBUTES, + ProtoObjects.SEARCH_ATTRIBUTES, + TypeMapper::searchAttributes), + testCase(ThriftObjects.BAD_BINARIES, ProtoObjects.BAD_BINARIES, TypeMapper::badBinaries), + testCase( + ThriftObjects.CLUSTER_REPLICATION_CONFIGURATION, + ProtoObjects.CLUSTER_REPLICATION_CONFIGURATION, + TypeMapper::clusterReplicationConfiguration), + testCase( + ThriftObjects.WORKFLOW_QUERY, ProtoObjects.WORKFLOW_QUERY, TypeMapper::workflowQuery), + testCase( + ThriftObjects.WORKFLOW_QUERY_RESULT, + ProtoObjects.WORKFLOW_QUERY_RESULT, + TypeMapper::workflowQueryResult), + testCase( + ThriftObjects.STICKY_EXECUTION_ATTRIBUTES, + ProtoObjects.STICKY_EXECUTION_ATTRIBUTES, + TypeMapper::stickyExecutionAttributes), + testCase( + ThriftObjects.WORKER_VERSION_INFO, + ProtoObjects.WORKER_VERSION_INFO, + TypeMapper::workerVersionInfo), + testCase( + ThriftObjects.START_TIME_FILTER, + ProtoObjects.START_TIME_FILTER, + TypeMapper::startTimeFilter), + testCase( + ThriftObjects.WORKFLOW_EXECUTION_FILTER, + ProtoObjects.WORKFLOW_EXECUTION_FILTER, + TypeMapper::workflowExecutionFilter), + testCase( + ThriftObjects.WORKFLOW_TYPE_FILTER, + ProtoObjects.WORKFLOW_TYPE_FILTER, + TypeMapper::workflowTypeFilter), + testCase( + WorkflowExecutionCloseStatus.COMPLETED, + ProtoObjects.STATUS_FILTER, + TypeMapper::statusFilter), + testCase( + ImmutableMap.of("key", ThriftObjects.utf8("data")), + ImmutableMap.of("key", ProtoObjects.payload("data")), + TypeMapper::payloadByteBufferMap), + testCase( + ImmutableMap.of("key", ThriftObjects.BAD_BINARY_INFO), + ImmutableMap.of("key", ProtoObjects.BAD_BINARY_INFO), + TypeMapper::badBinaryInfoMap), + testCase( + ImmutableList.of(ThriftObjects.CLUSTER_REPLICATION_CONFIGURATION), + ImmutableList.of(ProtoObjects.CLUSTER_REPLICATION_CONFIGURATION), + TypeMapper::clusterReplicationConfigurationArray), + testCase( + ImmutableMap.of("key", ThriftObjects.WORKFLOW_QUERY_RESULT), + ImmutableMap.of("key", ProtoObjects.WORKFLOW_QUERY_RESULT), + TypeMapper::workflowQueryResultMap)); + } + + private static Object[] testCase(T from, P to, Function via) { + return new Object[] {from.getClass().getSimpleName(), from, to, via}; + } +}