Skip to content

Commit 2da5fe4

Browse files
authored
Test for TestWorkflowMutableStateImpl (#979)
1 parent fb7d322 commit 2da5fe4

5 files changed

+422
-70
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
*
3+
* * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
* *
5+
* * Modifications copyright (C) 2017 Uber Technologies, Inc.
6+
* *
7+
* * Licensed under the Apache License, Version 2.0 (the "License"). You may not
8+
* * use this file except in compliance with the License. A copy of the License is
9+
* * located at
10+
* *
11+
* * http://aws.amazon.com/apache2.0
12+
* *
13+
* * or in the "license" file accompanying this file. This file is distributed on
14+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15+
* * express or implied. See the License for the specific language governing
16+
* * permissions and limitations under the License.
17+
*
18+
*/
19+
20+
package com.uber.cadence.internal.testservice;
21+
22+
import com.uber.cadence.*;
23+
24+
public class TestWorkflowMutableStateAttrUtil {
25+
26+
static void validateScheduleActivityTask(ScheduleActivityTaskDecisionAttributes a)
27+
throws BadRequestError {
28+
if (a == null) {
29+
throw new BadRequestError("ScheduleActivityTaskDecisionAttributes is not set on decision.");
30+
}
31+
32+
if (a.getTaskList() == null || a.getTaskList().getName().isEmpty()) {
33+
throw new BadRequestError("TaskList is not set on decision.");
34+
}
35+
if (a.getActivityId() == null || a.getActivityId().isEmpty()) {
36+
throw new BadRequestError("ActivityId is not set on decision.");
37+
}
38+
if (a.getActivityType() == null
39+
|| a.getActivityType().getName() == null
40+
|| a.getActivityType().getName().isEmpty()) {
41+
throw new BadRequestError("ActivityType is not set on decision.");
42+
}
43+
if (a.getStartToCloseTimeoutSeconds() <= 0) {
44+
throw new BadRequestError("A valid StartToCloseTimeoutSeconds is not set on decision.");
45+
}
46+
if (a.getScheduleToStartTimeoutSeconds() <= 0) {
47+
throw new BadRequestError("A valid ScheduleToStartTimeoutSeconds is not set on decision.");
48+
}
49+
if (a.getScheduleToCloseTimeoutSeconds() <= 0) {
50+
throw new BadRequestError("A valid ScheduleToCloseTimeoutSeconds is not set on decision.");
51+
}
52+
if (a.getHeartbeatTimeoutSeconds() < 0) {
53+
throw new BadRequestError("Ac valid HeartbeatTimeoutSeconds is not set on decision.");
54+
}
55+
}
56+
57+
static void validateStartChildExecutionAttributes(StartChildWorkflowExecutionDecisionAttributes a)
58+
throws BadRequestError {
59+
if (a == null) {
60+
throw new BadRequestError(
61+
"StartChildWorkflowExecutionDecisionAttributes is not set on decision.");
62+
}
63+
64+
if (a.getWorkflowId().isEmpty()) {
65+
throw new BadRequestError("Required field WorkflowID is not set on decision.");
66+
}
67+
68+
if (a.getWorkflowType() == null || a.getWorkflowType().getName().isEmpty()) {
69+
throw new BadRequestError("Required field WorkflowType is not set on decision.");
70+
}
71+
72+
RetryPolicy retryPolicy = a.getRetryPolicy();
73+
if (retryPolicy != null) {
74+
RetryState.validateRetryPolicy(retryPolicy);
75+
}
76+
}
77+
78+
public static void inheritUnsetPropertiesFromParentWorkflow(
79+
StartWorkflowExecutionRequest startRequest, StartChildWorkflowExecutionDecisionAttributes a) {
80+
// Inherit tasklist from parent workflow execution if not provided on decision
81+
if (a.getTaskList() == null || a.getTaskList().getName().isEmpty()) {
82+
a.setTaskList(startRequest.getTaskList());
83+
}
84+
85+
// Inherit workflow timeout from parent workflow execution if not provided on decision
86+
if (a.getExecutionStartToCloseTimeoutSeconds() <= 0) {
87+
a.setExecutionStartToCloseTimeoutSeconds(
88+
startRequest.getExecutionStartToCloseTimeoutSeconds());
89+
}
90+
91+
// Inherit decision task timeout from parent workflow execution if not provided on decision
92+
if (a.getTaskStartToCloseTimeoutSeconds() <= 0) {
93+
a.setTaskStartToCloseTimeoutSeconds(startRequest.getTaskStartToCloseTimeoutSeconds());
94+
}
95+
}
96+
}

src/main/java/com/uber/cadence/internal/testservice/TestWorkflowMutableStateImpl.java

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.uber.cadence.internal.testservice;
1919

20+
import static com.uber.cadence.internal.testservice.TestWorkflowMutableStateAttrUtil.*;
21+
2022
import com.cronutils.model.Cron;
2123
import com.cronutils.model.CronType;
2224
import com.cronutils.model.definition.CronDefinition;
@@ -72,7 +74,6 @@
7274
import com.uber.cadence.RespondDecisionTaskCompletedRequest;
7375
import com.uber.cadence.RespondDecisionTaskFailedRequest;
7476
import com.uber.cadence.RespondQueryTaskCompletedRequest;
75-
import com.uber.cadence.RetryPolicy;
7677
import com.uber.cadence.ScheduleActivityTaskDecisionAttributes;
7778
import com.uber.cadence.SignalExternalWorkflowExecutionDecisionAttributes;
7879
import com.uber.cadence.SignalExternalWorkflowExecutionFailedCause;
@@ -592,87 +593,19 @@ private void processScheduleActivityTask(
592593
ctx.lockTimer();
593594
}
594595

595-
private void validateScheduleActivityTask(ScheduleActivityTaskDecisionAttributes a)
596-
throws BadRequestError {
597-
if (a == null) {
598-
throw new BadRequestError("ScheduleActivityTaskDecisionAttributes is not set on decision.");
599-
}
600-
601-
if (a.getTaskList() == null || a.getTaskList().getName().isEmpty()) {
602-
throw new BadRequestError("TaskList is not set on decision.");
603-
}
604-
if (a.getActivityId() == null || a.getActivityId().isEmpty()) {
605-
throw new BadRequestError("ActivityId is not set on decision.");
606-
}
607-
if (a.getActivityType() == null
608-
|| a.getActivityType().getName() == null
609-
|| a.getActivityType().getName().isEmpty()) {
610-
throw new BadRequestError("ActivityType is not set on decision.");
611-
}
612-
if (a.getStartToCloseTimeoutSeconds() <= 0) {
613-
throw new BadRequestError("A valid StartToCloseTimeoutSeconds is not set on decision.");
614-
}
615-
if (a.getScheduleToStartTimeoutSeconds() <= 0) {
616-
throw new BadRequestError("A valid ScheduleToStartTimeoutSeconds is not set on decision.");
617-
}
618-
if (a.getScheduleToCloseTimeoutSeconds() <= 0) {
619-
throw new BadRequestError("A valid ScheduleToCloseTimeoutSeconds is not set on decision.");
620-
}
621-
if (a.getHeartbeatTimeoutSeconds() < 0) {
622-
throw new BadRequestError("Ac valid HeartbeatTimeoutSeconds is not set on decision.");
623-
}
624-
}
625-
626596
private void processStartChildWorkflow(
627597
RequestContext ctx,
628598
StartChildWorkflowExecutionDecisionAttributes a,
629599
long decisionTaskCompletedId)
630600
throws BadRequestError, InternalServiceError {
631601
validateStartChildExecutionAttributes(a);
602+
inheritUnsetPropertiesFromParentWorkflow(startRequest, a);
632603
StateMachine<ChildWorkflowData> child = StateMachines.newChildWorkflowStateMachine(service);
633604
childWorkflows.put(ctx.getNextEventId(), child);
634605
child.action(StateMachines.Action.INITIATE, ctx, a, decisionTaskCompletedId);
635606
ctx.lockTimer();
636607
}
637608

638-
/** Clone of the validateStartChildExecutionAttributes from historyEngine.go */
639-
private void validateStartChildExecutionAttributes(
640-
StartChildWorkflowExecutionDecisionAttributes a) throws BadRequestError {
641-
if (a == null) {
642-
throw new BadRequestError(
643-
"StartChildWorkflowExecutionDecisionAttributes is not set on decision.");
644-
}
645-
646-
if (a.getWorkflowId().isEmpty()) {
647-
throw new BadRequestError("Required field WorkflowID is not set on decision.");
648-
}
649-
650-
if (a.getWorkflowType() == null || a.getWorkflowType().getName().isEmpty()) {
651-
throw new BadRequestError("Required field WorkflowType is not set on decision.");
652-
}
653-
654-
// Inherit tasklist from parent workflow execution if not provided on decision
655-
if (a.getTaskList() == null || a.getTaskList().getName().isEmpty()) {
656-
a.setTaskList(startRequest.getTaskList());
657-
}
658-
659-
// Inherit workflow timeout from parent workflow execution if not provided on decision
660-
if (a.getExecutionStartToCloseTimeoutSeconds() <= 0) {
661-
a.setExecutionStartToCloseTimeoutSeconds(
662-
startRequest.getExecutionStartToCloseTimeoutSeconds());
663-
}
664-
665-
// Inherit decision task timeout from parent workflow execution if not provided on decision
666-
if (a.getTaskStartToCloseTimeoutSeconds() <= 0) {
667-
a.setTaskStartToCloseTimeoutSeconds(startRequest.getTaskStartToCloseTimeoutSeconds());
668-
}
669-
670-
RetryPolicy retryPolicy = a.getRetryPolicy();
671-
if (retryPolicy != null) {
672-
RetryState.validateRetryPolicy(retryPolicy);
673-
}
674-
}
675-
676609
private void processSignalExternalWorkflowExecution(
677610
RequestContext ctx,
678611
SignalExternalWorkflowExecutionDecisionAttributes a,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
*
3+
* * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
* *
5+
* * Modifications copyright (C) 2017 Uber Technologies, Inc.
6+
* *
7+
* * Licensed under the Apache License, Version 2.0 (the "License"). You may not
8+
* * use this file except in compliance with the License. A copy of the License is
9+
* * located at
10+
* *
11+
* * http://aws.amazon.com/apache2.0
12+
* *
13+
* * or in the "license" file accompanying this file. This file is distributed on
14+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15+
* * express or implied. See the License for the specific language governing
16+
* * permissions and limitations under the License.
17+
*
18+
*/
19+
20+
package com.uber.cadence.internal.testservice;
21+
22+
import static com.uber.cadence.internal.testservice.TestWorkflowMutableStateAttrUtil.validateStartChildExecutionAttributes;
23+
24+
import com.uber.cadence.RetryPolicy;
25+
import com.uber.cadence.StartChildWorkflowExecutionDecisionAttributes;
26+
import com.uber.cadence.WorkflowType;
27+
import java.util.Arrays;
28+
import java.util.Collection;
29+
import junit.framework.TestCase;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.Parameterized;
33+
34+
@RunWith(Parameterized.class)
35+
public class TestWorkflowMutableStateAttrUtil_inheritUnsetPropertiesFromParentWorkflow
36+
extends TestCase {
37+
38+
private final StartChildWorkflowExecutionDecisionAttributes attributes;
39+
private final String errorMessage;
40+
41+
public TestWorkflowMutableStateAttrUtil_inheritUnsetPropertiesFromParentWorkflow(
42+
String testName,
43+
StartChildWorkflowExecutionDecisionAttributes attributes,
44+
String errorMessage) {
45+
this.attributes = attributes;
46+
this.errorMessage = errorMessage;
47+
}
48+
49+
@Parameterized.Parameters(name = "{index}: {0}")
50+
public static Collection<Object[]> data() {
51+
return Arrays.asList(
52+
new Object[][] {
53+
{"valid", createAtt(), null},
54+
{"null", null, "StartChildWorkflowExecutionDecisionAttributes is not set on decision."},
55+
{
56+
"WorkflowId empty",
57+
createAtt().setWorkflowId(""),
58+
"Required field WorkflowID is not set on decision."
59+
},
60+
{
61+
"WorkflowType null",
62+
createAtt().setWorkflowType(null),
63+
"Required field WorkflowType is not set on decision."
64+
},
65+
{
66+
"WorkflowType name empty",
67+
createAtt().setWorkflowType(new WorkflowType().setName("")),
68+
"Required field WorkflowType is not set on decision."
69+
},
70+
{"RetryPolicy null", createAtt().setRetryPolicy(null), null},
71+
});
72+
}
73+
74+
@Test
75+
public void testValidateScheduleActivityTask() {
76+
try {
77+
validateStartChildExecutionAttributes(attributes);
78+
if (errorMessage != null) {
79+
fail("Expected exception");
80+
}
81+
} catch (Exception e) {
82+
assertEquals(errorMessage, e.getMessage());
83+
}
84+
}
85+
86+
private static StartChildWorkflowExecutionDecisionAttributes createAtt() {
87+
return new StartChildWorkflowExecutionDecisionAttributes()
88+
.setWorkflowId("testWorkflowId")
89+
.setWorkflowType(new WorkflowType().setName("testWorkflowType"))
90+
.setRetryPolicy(
91+
new RetryPolicy()
92+
.setInitialIntervalInSeconds(12)
93+
.setBackoffCoefficient(3.4)
94+
.setMaximumIntervalInSeconds(56)
95+
.setMaximumAttempts(78)
96+
.setExpirationIntervalInSeconds(99));
97+
}
98+
}

0 commit comments

Comments
 (0)