Skip to content

Commit 874c6c9

Browse files
committed
Added coverage for TestActivityExecutor private methods
1 parent eed41e4 commit 874c6c9

File tree

1 file changed

+246
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)