|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 7 | + * use this file except in compliance with the License. A copy of the License is |
| 8 | + * located at |
| 9 | + * |
| 10 | + * http://aws.amazon.com/apache2.0 |
| 11 | + * |
| 12 | + * or in the "license" file accompanying this file. This file is distributed on |
| 13 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | + * express or implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + */ |
| 17 | +package com.uber.cadence.internal.compatibility.thrift; |
| 18 | + |
| 19 | +import static com.uber.cadence.internal.compatibility.thrift.Helpers.byteStringToArray; |
| 20 | +import static org.junit.Assert.*; |
| 21 | + |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import com.uber.cadence.internal.compatibility.ProtoObjects; |
| 25 | +import com.uber.cadence.internal.compatibility.ThriftObjects; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.function.Function; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.junit.runners.Parameterized; |
| 34 | + |
| 35 | +@RunWith(Parameterized.class) |
| 36 | +public class TypeMapperTest<T, P> { |
| 37 | + |
| 38 | + @Parameterized.Parameter(0) |
| 39 | + public String testName; |
| 40 | + |
| 41 | + @Parameterized.Parameter(1) |
| 42 | + public T from; |
| 43 | + |
| 44 | + @Parameterized.Parameter(2) |
| 45 | + public P to; |
| 46 | + |
| 47 | + @Parameterized.Parameter(3) |
| 48 | + public Function<T, P> via; |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testMapper() { |
| 52 | + P actual = via.apply(from); |
| 53 | + if (actual instanceof byte[] && to instanceof byte[]) { |
| 54 | + // Handle the byte[] comparison |
| 55 | + assertArrayEquals((byte[]) to, (byte[]) actual); |
| 56 | + } else { |
| 57 | + // Handle all other types |
| 58 | + assertEquals(to, actual); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testHandlesNull() { |
| 64 | + P actual = via.apply(null); |
| 65 | + |
| 66 | + if (actual instanceof List<?>) { |
| 67 | + assertTrue( |
| 68 | + "Mapper functions returning a list should return an empty list", |
| 69 | + ((List<?>) actual).isEmpty()); |
| 70 | + } else if (actual instanceof Map<?, ?>) { |
| 71 | + assertTrue( |
| 72 | + "Mapper functions returning a map should return an empty map", |
| 73 | + ((Map<?, ?>) actual).isEmpty()); |
| 74 | + } else if (actual instanceof Long) { |
| 75 | + assertEquals("For long we expect -1", -1L, actual); |
| 76 | + } else { |
| 77 | + assertNull("Mapper functions should accept null, returning null", actual); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Parameterized.Parameters(name = "{0}") |
| 82 | + public static Iterable<Object[]> cases() { |
| 83 | + return Arrays.asList( |
| 84 | + testCase( |
| 85 | + ProtoObjects.BAD_BINARY_INFO, ThriftObjects.BAD_BINARY_INFO, TypeMapper::badBinaryInfo), |
| 86 | + testCase(ProtoObjects.FAILURE, "reason", TypeMapper::failureReason), |
| 87 | + testCase(ProtoObjects.DATA_BLOB, ThriftObjects.DATA_BLOB, TypeMapper::dataBlob), |
| 88 | + testCase( |
| 89 | + ProtoObjects.EXTERNAL_WORKFLOW_EXECUTION_INFO, |
| 90 | + ThriftObjects.EXTERNAL_WORKFLOW_EXECUTION, |
| 91 | + TypeMapper::externalWorkflowExecution), |
| 92 | + testCase( |
| 93 | + ProtoObjects.FAILURE, |
| 94 | + byteStringToArray(ProtoObjects.FAILURE.getDetails()), |
| 95 | + TypeMapper::failureDetails), |
| 96 | + testCase(ProtoObjects.ACTIVITY_TYPE, ThriftObjects.ACTIVITY_TYPE, TypeMapper::activityType), |
| 97 | + testCase(ProtoObjects.WORKFLOW_TYPE, ThriftObjects.WORKFLOW_TYPE, TypeMapper::workflowType), |
| 98 | + testCase(ProtoObjects.RESET_POINTS, ThriftObjects.RESET_POINTS, TypeMapper::resetPoints), |
| 99 | + testCase( |
| 100 | + ProtoObjects.RESET_POINT_INFO, |
| 101 | + ThriftObjects.RESET_POINT_INFO, |
| 102 | + TypeMapper::resetPointInfo), |
| 103 | + testCase(ProtoObjects.POLLER_INFO, ThriftObjects.POLLER_INFO, TypeMapper::pollerInfo), |
| 104 | + testCase( |
| 105 | + Collections.singletonList(ProtoObjects.POLLER_INFO), |
| 106 | + Collections.singletonList(ThriftObjects.POLLER_INFO), |
| 107 | + TypeMapper::pollerInfoArray), |
| 108 | + testCase( |
| 109 | + ProtoObjects.SUPPORTED_CLIENT_VERSIONS, |
| 110 | + ThriftObjects.SUPPORTED_CLIENT_VERSIONS, |
| 111 | + TypeMapper::supportedClientVersions), |
| 112 | + testCase( |
| 113 | + ProtoObjects.TASK_LIST_STATUS, |
| 114 | + ThriftObjects.TASK_LIST_STATUS, |
| 115 | + TypeMapper::taskListStatus), |
| 116 | + testCase( |
| 117 | + ProtoObjects.WORKFLOW_EXECUTION, |
| 118 | + ThriftObjects.WORKFLOW_EXECUTION, |
| 119 | + TypeMapper::workflowExecution), |
| 120 | + testCase(ProtoObjects.WORKFLOW_EXECUTION, "workflowId", TypeMapper::workflowId), |
| 121 | + testCase(ProtoObjects.WORKFLOW_EXECUTION, "runId", TypeMapper::runId), |
| 122 | + testCase( |
| 123 | + ProtoObjects.WORKFLOW_EXECUTION_INFO, |
| 124 | + ThriftObjects.WORKFLOW_EXECUTION_INFO, |
| 125 | + TypeMapper::workflowExecutionInfo), |
| 126 | + testCase( |
| 127 | + Collections.singletonList(ProtoObjects.WORKFLOW_EXECUTION_INFO), |
| 128 | + Collections.singletonList(ThriftObjects.WORKFLOW_EXECUTION_INFO), |
| 129 | + TypeMapper::workflowExecutionInfoArray), |
| 130 | + testCase( |
| 131 | + ProtoObjects.INDEXED_VALUES, |
| 132 | + ThriftObjects.INDEXED_VALUES, |
| 133 | + TypeMapper::indexedValueTypeMap), |
| 134 | + testCase( |
| 135 | + ProtoObjects.WORKFLOW_EXECUTION_INFO.getParentExecutionInfo(), |
| 136 | + "parentDomainId", |
| 137 | + TypeMapper::parentDomainId), |
| 138 | + testCase( |
| 139 | + ProtoObjects.WORKFLOW_EXECUTION_INFO.getParentExecutionInfo(), |
| 140 | + "parentDomainName", |
| 141 | + TypeMapper::parentDomainName), |
| 142 | + testCase( |
| 143 | + ProtoObjects.WORKFLOW_EXECUTION_INFO.getParentExecutionInfo(), |
| 144 | + 1L, |
| 145 | + TypeMapper::parentInitiatedId), |
| 146 | + testCase( |
| 147 | + ProtoObjects.WORKFLOW_EXECUTION_INFO.getParentExecutionInfo(), |
| 148 | + ThriftObjects.PARENT_WORKFLOW_EXECUTION, |
| 149 | + TypeMapper::parentWorkflowExecution), |
| 150 | + testCase( |
| 151 | + Collections.singletonList(ProtoObjects.PENDING_CHILD_EXECUTION_INFO), |
| 152 | + Collections.singletonList(ThriftObjects.PENDING_CHILD_EXECUTION_INFO), |
| 153 | + TypeMapper::pendingChildExecutionInfoArray), |
| 154 | + testCase( |
| 155 | + Collections.singletonList(ProtoObjects.PENDING_ACTIVITY_INFO), |
| 156 | + Collections.singletonList(ThriftObjects.PENDING_ACTIVITY_INFO), |
| 157 | + TypeMapper::pendingActivityInfoArray), |
| 158 | + testCase( |
| 159 | + Collections.singletonList(ProtoObjects.RESET_POINT_INFO), |
| 160 | + Collections.singletonList(ThriftObjects.RESET_POINT_INFO), |
| 161 | + TypeMapper::resetPointInfoArray), |
| 162 | + testCase(ProtoObjects.TASK_LIST, ThriftObjects.TASK_LIST, TypeMapper::taskList), |
| 163 | + testCase( |
| 164 | + ProtoObjects.TASK_LIST_METADATA, |
| 165 | + ThriftObjects.TASK_LIST_METADATA, |
| 166 | + TypeMapper::taskListMetadata), |
| 167 | + testCase(ProtoObjects.RETRY_POLICY, ThriftObjects.RETRY_POLICY, TypeMapper::retryPolicy), |
| 168 | + testCase(ProtoObjects.HEADER, ThriftObjects.HEADER, TypeMapper::header), |
| 169 | + testCase(ProtoObjects.MEMO, ThriftObjects.MEMO, TypeMapper::memo), |
| 170 | + testCase( |
| 171 | + ProtoObjects.SEARCH_ATTRIBUTES, |
| 172 | + ThriftObjects.SEARCH_ATTRIBUTES, |
| 173 | + TypeMapper::searchAttributes), |
| 174 | + testCase(ProtoObjects.BAD_BINARIES, ThriftObjects.BAD_BINARIES, TypeMapper::badBinaries), |
| 175 | + testCase( |
| 176 | + ProtoObjects.CLUSTER_REPLICATION_CONFIGURATION, |
| 177 | + ThriftObjects.CLUSTER_REPLICATION_CONFIGURATION, |
| 178 | + TypeMapper::clusterReplicationConfiguration), |
| 179 | + testCase( |
| 180 | + ProtoObjects.WORKFLOW_QUERY, ThriftObjects.WORKFLOW_QUERY, TypeMapper::workflowQuery), |
| 181 | + testCase( |
| 182 | + ImmutableMap.of("key", ProtoObjects.BAD_BINARY_INFO), |
| 183 | + ImmutableMap.of("key", ThriftObjects.BAD_BINARY_INFO), |
| 184 | + TypeMapper::badBinaryInfoMap), |
| 185 | + testCase( |
| 186 | + ImmutableList.of(ProtoObjects.CLUSTER_REPLICATION_CONFIGURATION), |
| 187 | + ImmutableList.of(ThriftObjects.CLUSTER_REPLICATION_CONFIGURATION), |
| 188 | + TypeMapper::clusterReplicationConfigurationArray), |
| 189 | + testCase( |
| 190 | + ImmutableMap.of("key", ProtoObjects.WORKFLOW_QUERY), |
| 191 | + ImmutableMap.of("key", ThriftObjects.WORKFLOW_QUERY), |
| 192 | + TypeMapper::workflowQueryMap), |
| 193 | + testCase( |
| 194 | + ImmutableMap.of("key", ProtoObjects.ACTIVITY_LOCAL_DISPATCH_INFO), |
| 195 | + ImmutableMap.of("key", ThriftObjects.ACTIVITY_LOCAL_DISPATCH_INFO), |
| 196 | + TypeMapper::activityLocalDispatchInfoMap), |
| 197 | + testCase( |
| 198 | + Collections.singletonList(ProtoObjects.DATA_BLOB), |
| 199 | + Collections.singletonList(ThriftObjects.DATA_BLOB), |
| 200 | + TypeMapper::dataBlobArray), |
| 201 | + testCase( |
| 202 | + ProtoObjects.DOMAIN, |
| 203 | + ThriftObjects.DESCRIBE_DOMAIN_RESPONSE, |
| 204 | + TypeMapper::describeDomainResponseDomain), |
| 205 | + testCase( |
| 206 | + Collections.singletonList(ProtoObjects.DOMAIN), |
| 207 | + Collections.singletonList(ThriftObjects.DESCRIBE_DOMAIN_RESPONSE), |
| 208 | + TypeMapper::describeDomainResponseArray), |
| 209 | + testCase( |
| 210 | + Collections.singletonList(ProtoObjects.TASK_LIST_PARTITION_METADATA), |
| 211 | + Collections.singletonList(ThriftObjects.TASK_LIST_PARTITION_METADATA), |
| 212 | + TypeMapper::taskListPartitionMetadataArray)); |
| 213 | + } |
| 214 | + |
| 215 | + private static <T, P> Object[] testCase(T from, P to, Function<T, P> via) { |
| 216 | + return new Object[] {from.getClass().getSimpleName(), from, to, via}; |
| 217 | + } |
| 218 | +} |
0 commit comments