|
| 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.proto; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNull; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.common.collect.ImmutableList; |
| 24 | +import com.google.common.collect.ImmutableMap; |
| 25 | +import com.google.protobuf.Message; |
| 26 | +import com.uber.cadence.WorkflowExecutionCloseStatus; |
| 27 | +import com.uber.cadence.internal.compatibility.ProtoObjects; |
| 28 | +import com.uber.cadence.internal.compatibility.ThriftObjects; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.function.Function; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.Parameterized; |
| 36 | + |
| 37 | +@RunWith(Parameterized.class) |
| 38 | +public class TypeMapperTest<T, P> { |
| 39 | + |
| 40 | + @Parameterized.Parameter(0) |
| 41 | + public String testName; |
| 42 | + |
| 43 | + @Parameterized.Parameter(1) |
| 44 | + public T from; |
| 45 | + |
| 46 | + @Parameterized.Parameter(2) |
| 47 | + public P to; |
| 48 | + |
| 49 | + @Parameterized.Parameter(3) |
| 50 | + public Function<T, P> via; |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testMapper() { |
| 54 | + P actual = via.apply(from); |
| 55 | + assertEquals(to, actual); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testHandlesNull() { |
| 60 | + P actual = via.apply(null); |
| 61 | + |
| 62 | + if (actual instanceof List<?>) { |
| 63 | + assertTrue( |
| 64 | + "Mapper functions returning a list should return an empty list", |
| 65 | + ((List<?>) actual).isEmpty()); |
| 66 | + } else if (actual instanceof Map<?, ?>) { |
| 67 | + assertTrue( |
| 68 | + "Mapper functions returning a map should return an empty map", |
| 69 | + ((Map<?, ?>) actual).isEmpty()); |
| 70 | + } else if (actual instanceof Message) { |
| 71 | + assertEquals( |
| 72 | + "Mapper functions returning a Message should return the default value", |
| 73 | + ((Message) actual).getDefaultInstanceForType(), |
| 74 | + actual); |
| 75 | + } else { |
| 76 | + assertNull("Mapper functions should accept null, returning null", actual); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Parameterized.Parameters(name = "{0}") |
| 81 | + public static Iterable<Object[]> cases() { |
| 82 | + return Arrays.asList( |
| 83 | + testCase( |
| 84 | + ThriftObjects.BAD_BINARY_INFO, ProtoObjects.BAD_BINARY_INFO, TypeMapper::badBinaryInfo), |
| 85 | + testCase( |
| 86 | + ThriftObjects.utf8Bytes("data"), ProtoObjects.payload("data"), TypeMapper::payload), |
| 87 | + testCase(ThriftObjects.ACTIVITY_TYPE, ProtoObjects.ACTIVITY_TYPE, TypeMapper::activityType), |
| 88 | + testCase(ThriftObjects.WORKFLOW_TYPE, ProtoObjects.WORKFLOW_TYPE, TypeMapper::workflowType), |
| 89 | + testCase(ThriftObjects.TASK_LIST, ProtoObjects.TASK_LIST, TypeMapper::taskList), |
| 90 | + testCase( |
| 91 | + ThriftObjects.TASK_LIST_METADATA, |
| 92 | + ProtoObjects.TASK_LIST_METADATA, |
| 93 | + TypeMapper::taskListMetadata), |
| 94 | + testCase(ThriftObjects.RETRY_POLICY, ProtoObjects.RETRY_POLICY, TypeMapper::retryPolicy), |
| 95 | + testCase(ThriftObjects.HEADER, ProtoObjects.HEADER, TypeMapper::header), |
| 96 | + testCase(ThriftObjects.MEMO, ProtoObjects.MEMO, TypeMapper::memo), |
| 97 | + testCase( |
| 98 | + ThriftObjects.SEARCH_ATTRIBUTES, |
| 99 | + ProtoObjects.SEARCH_ATTRIBUTES, |
| 100 | + TypeMapper::searchAttributes), |
| 101 | + testCase(ThriftObjects.BAD_BINARIES, ProtoObjects.BAD_BINARIES, TypeMapper::badBinaries), |
| 102 | + testCase( |
| 103 | + ThriftObjects.CLUSTER_REPLICATION_CONFIGURATION, |
| 104 | + ProtoObjects.CLUSTER_REPLICATION_CONFIGURATION, |
| 105 | + TypeMapper::clusterReplicationConfiguration), |
| 106 | + testCase( |
| 107 | + ThriftObjects.WORKFLOW_QUERY, ProtoObjects.WORKFLOW_QUERY, TypeMapper::workflowQuery), |
| 108 | + testCase( |
| 109 | + ThriftObjects.WORKFLOW_QUERY_RESULT, |
| 110 | + ProtoObjects.WORKFLOW_QUERY_RESULT, |
| 111 | + TypeMapper::workflowQueryResult), |
| 112 | + testCase( |
| 113 | + ThriftObjects.STICKY_EXECUTION_ATTRIBUTES, |
| 114 | + ProtoObjects.STICKY_EXECUTION_ATTRIBUTES, |
| 115 | + TypeMapper::stickyExecutionAttributes), |
| 116 | + testCase( |
| 117 | + ThriftObjects.WORKER_VERSION_INFO, |
| 118 | + ProtoObjects.WORKER_VERSION_INFO, |
| 119 | + TypeMapper::workerVersionInfo), |
| 120 | + testCase( |
| 121 | + ThriftObjects.START_TIME_FILTER, |
| 122 | + ProtoObjects.START_TIME_FILTER, |
| 123 | + TypeMapper::startTimeFilter), |
| 124 | + testCase( |
| 125 | + ThriftObjects.WORKFLOW_EXECUTION_FILTER, |
| 126 | + ProtoObjects.WORKFLOW_EXECUTION_FILTER, |
| 127 | + TypeMapper::workflowExecutionFilter), |
| 128 | + testCase( |
| 129 | + ThriftObjects.WORKFLOW_TYPE_FILTER, |
| 130 | + ProtoObjects.WORKFLOW_TYPE_FILTER, |
| 131 | + TypeMapper::workflowTypeFilter), |
| 132 | + testCase( |
| 133 | + WorkflowExecutionCloseStatus.COMPLETED, |
| 134 | + ProtoObjects.STATUS_FILTER, |
| 135 | + TypeMapper::statusFilter), |
| 136 | + testCase( |
| 137 | + ImmutableMap.of("key", ThriftObjects.utf8("data")), |
| 138 | + ImmutableMap.of("key", ProtoObjects.payload("data")), |
| 139 | + TypeMapper::payloadByteBufferMap), |
| 140 | + testCase( |
| 141 | + ImmutableMap.of("key", ThriftObjects.BAD_BINARY_INFO), |
| 142 | + ImmutableMap.of("key", ProtoObjects.BAD_BINARY_INFO), |
| 143 | + TypeMapper::badBinaryInfoMap), |
| 144 | + testCase( |
| 145 | + ImmutableList.of(ThriftObjects.CLUSTER_REPLICATION_CONFIGURATION), |
| 146 | + ImmutableList.of(ProtoObjects.CLUSTER_REPLICATION_CONFIGURATION), |
| 147 | + TypeMapper::clusterReplicationConfigurationArray), |
| 148 | + testCase( |
| 149 | + ImmutableMap.of("key", ThriftObjects.WORKFLOW_QUERY_RESULT), |
| 150 | + ImmutableMap.of("key", ProtoObjects.WORKFLOW_QUERY_RESULT), |
| 151 | + TypeMapper::workflowQueryResultMap)); |
| 152 | + } |
| 153 | + |
| 154 | + private static <T, P> Object[] testCase(T from, P to, Function<T, P> via) { |
| 155 | + return new Object[] {from.getClass().getSimpleName(), from, to, via}; |
| 156 | + } |
| 157 | +} |
0 commit comments