Skip to content

Commit c39c4c5

Browse files
authored
thrift/TypeMapper: Added more unit tests (#976)
1 parent 2ec3c69 commit c39c4c5

File tree

4 files changed

+262
-16
lines changed

4 files changed

+262
-16
lines changed

src/main/java/com/uber/cadence/internal/compatibility/thrift/TypeMapper.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.uber.cadence.SupportedClientVersions;
5555
import com.uber.cadence.TaskIDBlock;
5656
import com.uber.cadence.TaskList;
57+
import com.uber.cadence.TaskListMetadata;
5758
import com.uber.cadence.TaskListPartitionMetadata;
5859
import com.uber.cadence.TaskListStatus;
5960
import com.uber.cadence.WorkflowExecution;
@@ -374,6 +375,7 @@ static WorkflowExecutionInfo workflowExecutionInfo(
374375
res.setCloseTime(timeToUnixNano(t.getCloseTime()));
375376
res.setCloseStatus(workflowExecutionCloseStatus(t.getCloseStatus()));
376377
res.setHistoryLength(t.getHistoryLength());
378+
res.setParentDomainName(parentDomainName(t.getParentExecutionInfo()));
377379
res.setParentDomainId(parentDomainId(t.getParentExecutionInfo()));
378380
res.setParentExecution(parentWorkflowExecution(t.getParentExecutionInfo()));
379381
res.setExecutionTime(timeToUnixNano(t.getExecutionTime()));
@@ -527,6 +529,15 @@ static DescribeDomainResponse describeDomainResponseDomain(com.uber.cadence.api.
527529
return res;
528530
}
529531

532+
static TaskListMetadata taskListMetadata(com.uber.cadence.api.v1.TaskListMetadata t) {
533+
if (t == null) {
534+
return null;
535+
}
536+
TaskListMetadata res = new TaskListMetadata();
537+
res.setMaxTasksPerSecond(t.getMaxTasksPerSecond().getValue());
538+
return res;
539+
}
540+
530541
static TaskListPartitionMetadata taskListPartitionMetadata(
531542
com.uber.cadence.api.v1.TaskListPartitionMetadata t) {
532543
if (t == null || t == com.uber.cadence.api.v1.TaskListPartitionMetadata.getDefaultInstance()) {
@@ -552,8 +563,8 @@ static List<PollerInfo> pollerInfoArray(List<com.uber.cadence.api.v1.PollerInfo>
552563
return null;
553564
}
554565
List<PollerInfo> v = new ArrayList<>();
555-
for (int i = 0; i < t.size(); i++) {
556-
v.add(pollerInfo(t.get(i)));
566+
for (com.uber.cadence.api.v1.PollerInfo pollerInfo : t) {
567+
v.add(pollerInfo(pollerInfo));
557568
}
558569
return v;
559570
}
@@ -563,8 +574,8 @@ static List<ResetPointInfo> resetPointInfoArray(List<com.uber.cadence.api.v1.Res
563574
return null;
564575
}
565576
List<ResetPointInfo> v = new ArrayList<>();
566-
for (int i = 0; i < t.size(); i++) {
567-
v.add(resetPointInfo(t.get(i)));
577+
for (com.uber.cadence.api.v1.ResetPointInfo resetPointInfo : t) {
578+
v.add(resetPointInfo(resetPointInfo));
568579
}
569580
return v;
570581
}
@@ -575,8 +586,8 @@ static List<PendingActivityInfo> pendingActivityInfoArray(
575586
return null;
576587
}
577588
List<PendingActivityInfo> v = new ArrayList<>();
578-
for (int i = 0; i < t.size(); i++) {
579-
v.add(pendingActivityInfo(t.get(i)));
589+
for (com.uber.cadence.api.v1.PendingActivityInfo pendingActivityInfo : t) {
590+
v.add(pendingActivityInfo(pendingActivityInfo));
580591
}
581592
return v;
582593
}
@@ -587,8 +598,8 @@ static List<PendingChildExecutionInfo> pendingChildExecutionInfoArray(
587598
return null;
588599
}
589600
List<PendingChildExecutionInfo> v = new ArrayList<>();
590-
for (int i = 0; i < t.size(); i++) {
591-
v.add(pendingChildExecutionInfo(t.get(i)));
601+
for (com.uber.cadence.api.v1.PendingChildExecutionInfo pendingChildExecutionInfo : t) {
602+
v.add(pendingChildExecutionInfo(pendingChildExecutionInfo));
592603
}
593604
return v;
594605
}
@@ -610,8 +621,8 @@ static List<DataBlob> dataBlobArray(List<com.uber.cadence.api.v1.DataBlob> t) {
610621
return null;
611622
}
612623
List<DataBlob> v = new ArrayList<>();
613-
for (int i = 0; i < t.size(); i++) {
614-
v.add(dataBlob(t.get(i)));
624+
for (com.uber.cadence.api.v1.DataBlob dataBlob : t) {
625+
v.add(dataBlob(dataBlob));
615626
}
616627
return v;
617628
}
@@ -622,8 +633,8 @@ static List<WorkflowExecutionInfo> workflowExecutionInfoArray(
622633
return null;
623634
}
624635
List<WorkflowExecutionInfo> v = new ArrayList<>();
625-
for (int i = 0; i < t.size(); i++) {
626-
v.add(workflowExecutionInfo(t.get(i)));
636+
for (com.uber.cadence.api.v1.WorkflowExecutionInfo workflowExecutionInfo : t) {
637+
v.add(workflowExecutionInfo(workflowExecutionInfo));
627638
}
628639
return v;
629640
}
@@ -634,8 +645,8 @@ static List<DescribeDomainResponse> describeDomainResponseArray(
634645
return null;
635646
}
636647
List<DescribeDomainResponse> v = new ArrayList<>();
637-
for (int i = 0; i < t.size(); i++) {
638-
v.add(describeDomainResponseDomain(t.get(i)));
648+
for (com.uber.cadence.api.v1.Domain domain : t) {
649+
v.add(describeDomainResponseDomain(domain));
639650
}
640651
return v;
641652
}
@@ -646,8 +657,8 @@ static List<TaskListPartitionMetadata> taskListPartitionMetadataArray(
646657
return null;
647658
}
648659
List<TaskListPartitionMetadata> v = new ArrayList<>();
649-
for (int i = 0; i < t.size(); i++) {
650-
v.add(taskListPartitionMetadata(t.get(i)));
660+
for (com.uber.cadence.api.v1.TaskListPartitionMetadata taskListPartitionMetadata : t) {
661+
v.add(taskListPartitionMetadata(taskListPartitionMetadata));
651662
}
652663
return v;
653664
}

src/test/java/com/uber/cadence/internal/compatibility/ProtoObjects.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public final class ProtoObjects {
4848
public static final String WORKFLOW_ID = "workflowId";
4949
public static final WorkflowExecution WORKFLOW_EXECUTION =
5050
WorkflowExecution.newBuilder().setWorkflowId("workflowId").setRunId("runId").build();
51+
public static final WorkflowExecution EXTERNAL_WORKFLOW_EXECUTION =
52+
WorkflowExecution.newBuilder()
53+
.setWorkflowId("externalWorkflowId")
54+
.setRunId("externalRunId")
55+
.build();
5156
public static final WorkflowExecution PARENT_WORKFLOW_EXECUTION =
5257
WorkflowExecution.newBuilder()
5358
.setWorkflowId("parentWorkflowId")
@@ -123,6 +128,7 @@ public final class ProtoObjects {
123128
.setHistoryLength(3)
124129
.setParentExecutionInfo(
125130
ParentExecutionInfo.newBuilder()
131+
.setDomainName("parentDomainName")
126132
.setDomainId("parentDomainId")
127133
.setWorkflowExecution(PARENT_WORKFLOW_EXECUTION)
128134
.setInitiatedId(1)
@@ -711,6 +717,12 @@ public final class ProtoObjects {
711717
.setIdentity("identity")
712718
.build();
713719

720+
public static final ExternalExecutionInfo EXTERNAL_WORKFLOW_EXECUTION_INFO =
721+
ExternalExecutionInfo.newBuilder()
722+
.setInitiatedId(1)
723+
.setWorkflowExecution(EXTERNAL_WORKFLOW_EXECUTION)
724+
.build();
725+
714726
public static final WorkflowExecutionCancelRequestedEventAttributes
715727
WORKFLOW_EXECUTION_CANCEL_REQUESTED_EVENT_ATTRIBUTES =
716728
WorkflowExecutionCancelRequestedEventAttributes.newBuilder()

src/test/java/com/uber/cadence/internal/compatibility/ThriftObjects.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public final class ThriftObjects {
5050
public static final String PARENT_RUN_ID = "parentRunId";
5151
public static final WorkflowExecution PARENT_WORKFLOW_EXECUTION =
5252
new WorkflowExecution().setWorkflowId(PARENT_WORkFLOW_ID).setRunId(PARENT_RUN_ID);
53+
public static final String EXTERNAL_WORKFLOW_ID = "externalWorkflowId";
54+
public static final String EXTERNAL_RUN_ID = "externalRunId";
55+
public static final WorkflowExecution EXTERNAL_WORKFLOW_EXECUTION =
56+
new WorkflowExecution().setWorkflowId(EXTERNAL_WORKFLOW_ID).setRunId(EXTERNAL_RUN_ID);
5357
public static final StickyExecutionAttributes STICKY_EXECUTION_ATTRIBUTES =
5458
new StickyExecutionAttributes()
5559
.setWorkerTaskList(TASK_LIST)
@@ -101,6 +105,7 @@ public final class ThriftObjects {
101105
.setCloseTime(2)
102106
.setCloseStatus(WorkflowExecutionCloseStatus.FAILED)
103107
.setHistoryLength(3)
108+
.setParentDomainName("parentDomainName")
104109
.setParentDomainId("parentDomainId")
105110
.setParentExecution(PARENT_WORKFLOW_EXECUTION)
106111
.setExecutionTime(4)
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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

Comments
 (0)