Skip to content

Commit 31dd17f

Browse files
authored
Added coverage for TestActivityExecutor private methods (#971)
* Added coverage for TestActivityExecutor private methods
1 parent eed41e4 commit 31dd17f

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
18+
package com.uber.cadence.internal.sync;
19+
20+
import static org.junit.Assert.fail;
21+
import static org.mockito.Mockito.mock;
22+
23+
import com.uber.cadence.WorkflowExecution;
24+
import com.uber.cadence.serviceclient.IWorkflowService;
25+
import com.uber.cadence.workflow.Functions;
26+
import com.uber.cadence.workflow.WorkflowInterceptorBase;
27+
import java.lang.reflect.Constructor;
28+
import java.lang.reflect.Method;
29+
import java.lang.reflect.Type;
30+
import java.time.Duration;
31+
import java.util.Map;
32+
import java.util.Optional;
33+
import java.util.function.BiPredicate;
34+
import java.util.function.Supplier;
35+
import org.junit.Before;
36+
import org.junit.Test;
37+
import org.mockito.Mock;
38+
import org.mockito.MockitoAnnotations;
39+
40+
public class TestActivityEnvironmentInternalTest {
41+
@Mock private IWorkflowService mockWorkflowService;
42+
43+
@Mock private WorkflowInterceptorBase mockNext;
44+
45+
private Object testActivityExecutor;
46+
47+
// Helper method to find the inner class
48+
private Class<?> findTestActivityExecutorClass() {
49+
for (Class<?> declaredClass : TestActivityEnvironmentInternal.class.getDeclaredClasses()) {
50+
if (declaredClass.getSimpleName().equals("TestActivityExecutor")) {
51+
return declaredClass;
52+
}
53+
}
54+
throw new RuntimeException("Could not find TestActivityExecutor inner class");
55+
}
56+
57+
// Helper method to print all methods
58+
private void printMethods(Class<?> clazz) {
59+
System.out.println("Methods for " + clazz.getName() + ":");
60+
for (Method method : clazz.getDeclaredMethods()) {
61+
System.out.println(" " + method);
62+
}
63+
}
64+
65+
@Before
66+
public void setUp() {
67+
MockitoAnnotations.openMocks(this);
68+
69+
try {
70+
// Find the inner class first
71+
Class<?> innerClass = findTestActivityExecutorClass();
72+
73+
// Get the constructor with the specific parameter types
74+
Constructor<?> constructor =
75+
innerClass.getDeclaredConstructor(
76+
TestActivityEnvironmentInternal.class,
77+
IWorkflowService.class,
78+
WorkflowInterceptorBase.class);
79+
constructor.setAccessible(true);
80+
81+
// Create an instance of the outer class
82+
TestActivityEnvironmentInternal outerInstance = mock(TestActivityEnvironmentInternal.class);
83+
84+
// Create the instance
85+
testActivityExecutor = constructor.newInstance(outerInstance, mockWorkflowService, mockNext);
86+
87+
// Debug print the class and methods
88+
System.out.println("TestActivityExecutor class: " + innerClass);
89+
printMethods(innerClass);
90+
} catch (Exception e) {
91+
e.printStackTrace();
92+
throw new RuntimeException("Failed to set up test: " + e.getMessage(), e);
93+
}
94+
}
95+
96+
@Test
97+
public void testAllMethodsThrowUnsupportedOperationException() throws Exception {
98+
// Define test cases for different methods
99+
MethodTestCase[] methodCases = {
100+
// Signature: newRandom()
101+
new MethodTestCase("newRandom", new Class<?>[0], new Object[0]),
102+
103+
// Signature: signalExternalWorkflow(String, WorkflowExecution, String, Object[])
104+
new MethodTestCase(
105+
"signalExternalWorkflow",
106+
new Class<?>[] {String.class, WorkflowExecution.class, String.class, Object[].class},
107+
new Object[] {
108+
"testSignal", mock(WorkflowExecution.class), "signalName", new Object[] {}
109+
}),
110+
111+
// Signature: signalExternalWorkflow(WorkflowExecution, String, Object[])
112+
new MethodTestCase(
113+
"signalExternalWorkflow",
114+
new Class<?>[] {WorkflowExecution.class, String.class, Object[].class},
115+
new Object[] {mock(WorkflowExecution.class), "signalName", new Object[] {}}),
116+
117+
// Signature: cancelWorkflow(WorkflowExecution)
118+
new MethodTestCase(
119+
"cancelWorkflow",
120+
new Class<?>[] {WorkflowExecution.class},
121+
new Object[] {mock(WorkflowExecution.class)}),
122+
123+
// Signature: sleep(Duration)
124+
new MethodTestCase(
125+
"sleep", new Class<?>[] {Duration.class}, new Object[] {Duration.ofSeconds(1)}),
126+
127+
// Signature: await(Duration, String, Supplier)
128+
new MethodTestCase(
129+
"await",
130+
new Class<?>[] {Duration.class, String.class, Supplier.class},
131+
new Object[] {Duration.ofSeconds(1), "testReason", (Supplier<?>) () -> true}),
132+
133+
// Signature: await(String, Supplier)
134+
new MethodTestCase(
135+
"await",
136+
new Class<?>[] {String.class, Supplier.class},
137+
new Object[] {"testReason", (Supplier<?>) () -> true}),
138+
139+
// Signature: newTimer(Duration)
140+
new MethodTestCase(
141+
"newTimer", new Class<?>[] {Duration.class}, new Object[] {Duration.ofSeconds(1)}),
142+
143+
// Signature: sideEffect(Class, Type, Functions.Func)
144+
new MethodTestCase(
145+
"sideEffect",
146+
new Class<?>[] {Class.class, Type.class, Functions.Func.class},
147+
new Object[] {String.class, String.class, (Functions.Func<String>) () -> "test"}),
148+
149+
// Signature: mutableSideEffect(String, Class, Type, BiPredicate, Functions.Func)
150+
new MethodTestCase(
151+
"mutableSideEffect",
152+
new Class<?>[] {
153+
String.class, Class.class, Type.class, BiPredicate.class, Functions.Func.class
154+
},
155+
new Object[] {
156+
"testId",
157+
String.class,
158+
String.class,
159+
(BiPredicate<String, String>) (a, b) -> false,
160+
(Functions.Func<String>) () -> "test"
161+
}),
162+
163+
// Signature: getVersion(String, int, int)
164+
new MethodTestCase(
165+
"getVersion",
166+
new Class<?>[] {String.class, int.class, int.class},
167+
new Object[] {"changeId", 0, 1}),
168+
169+
// Signature: continueAsNew(Optional, Optional, Object[])
170+
new MethodTestCase(
171+
"continueAsNew",
172+
new Class<?>[] {Optional.class, Optional.class, Object[].class},
173+
new Object[] {Optional.empty(), Optional.empty(), new Object[] {}}),
174+
175+
// Signature: registerQuery(String, Type[], Func1)
176+
new MethodTestCase(
177+
"registerQuery",
178+
new Class<?>[] {String.class, Type[].class, Functions.Func1.class},
179+
new Object[] {
180+
"queryType",
181+
new Type[] {String.class},
182+
(Functions.Func1<Object[], Object>) args -> "result"
183+
}),
184+
185+
// Signature: randomUUID()
186+
new MethodTestCase("randomUUID", new Class<?>[0], new Object[0]),
187+
188+
// Signature: upsertSearchAttributes(Map)
189+
new MethodTestCase(
190+
"upsertSearchAttributes",
191+
new Class<?>[] {Map.class},
192+
new Object[] {java.util.Collections.emptyMap()})
193+
};
194+
195+
// Test each method
196+
for (MethodTestCase testCase : methodCases) {
197+
try {
198+
// Find the method
199+
Method method =
200+
testActivityExecutor
201+
.getClass()
202+
.getDeclaredMethod(testCase.methodName, testCase.parameterTypes);
203+
method.setAccessible(true);
204+
205+
// Invoke the method
206+
Object result = method.invoke(testActivityExecutor, testCase.arguments);
207+
208+
// If we get here, the method did not throw UnsupportedOperationException
209+
fail("Expected UnsupportedOperationException for method " + testCase.methodName);
210+
211+
} catch (Exception e) {
212+
// Check if the cause is UnsupportedOperationException
213+
if (!(e.getCause() instanceof UnsupportedOperationException)) {
214+
// If it's not the expected exception, rethrow
215+
throw new RuntimeException("Unexpected exception for method " + testCase.methodName, e);
216+
}
217+
// Expected behavior - UnsupportedOperationException was thrown
218+
// Continue to next method
219+
}
220+
}
221+
}
222+
223+
// Helper class to encapsulate method test cases
224+
private static class MethodTestCase {
225+
String methodName;
226+
Class<?>[] parameterTypes;
227+
Object[] arguments;
228+
229+
MethodTestCase(String methodName, Class<?>[] parameterTypes, Object[] arguments) {
230+
this.methodName = methodName;
231+
this.parameterTypes = parameterTypes;
232+
this.arguments = arguments;
233+
}
234+
}
235+
}

0 commit comments

Comments
 (0)