Skip to content

Commit 628fb21

Browse files
authored
Adding more tests to WorkflowExecutionUtils (#970)
Should cover all the following path almost 100%: ``` prettyPrintDecisions prettyPrintDecision prettyPrintObject prettyPrintJson fixStackTrace ``` Nit change: moved methods around in the source-code to group better.
1 parent c6a7a00 commit 628fb21

File tree

2 files changed

+113
-36
lines changed

2 files changed

+113
-36
lines changed

src/main/java/com/uber/cadence/internal/common/WorkflowExecutionUtils.java

+33-25
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,8 @@
5757
import java.nio.ByteBuffer;
5858
import java.nio.file.Files;
5959
import java.time.Duration;
60-
import java.util.Collection;
61-
import java.util.Date;
62-
import java.util.Iterator;
63-
import java.util.List;
64-
import java.util.Map;
60+
import java.util.*;
6561
import java.util.Map.Entry;
66-
import java.util.Optional;
6762
import java.util.concurrent.CancellationException;
6863
import java.util.concurrent.CompletableFuture;
6964
import java.util.concurrent.TimeUnit;
@@ -506,24 +501,6 @@ public static String prettyPrintHistory(
506501
return result.toString();
507502
}
508503

509-
public static String prettyPrintDecisions(Iterable<Decision> decisions) {
510-
StringBuilder result = new StringBuilder();
511-
result.append("{");
512-
boolean first = true;
513-
for (Decision decision : decisions) {
514-
if (first) {
515-
first = false;
516-
} else {
517-
result.append(",");
518-
}
519-
result.append("\n");
520-
result.append(INDENTATION);
521-
result.append(prettyPrintDecision(decision));
522-
}
523-
result.append("\n}");
524-
return result.toString();
525-
}
526-
527504
/**
528505
* Returns single event in a human readable format
529506
*
@@ -561,6 +538,29 @@ private static Object getEventAttributes(HistoryEvent event) {
561538
}
562539
}
563540

541+
/**
542+
* Returns decisions in a human readable format
543+
*
544+
* @param decisions decisions to pretty print
545+
*/
546+
public static String prettyPrintDecisions(Iterable<Decision> decisions) {
547+
StringBuilder result = new StringBuilder();
548+
result.append("{");
549+
boolean first = true;
550+
for (Decision decision : decisions) {
551+
if (first) {
552+
first = false;
553+
} else {
554+
result.append(",");
555+
}
556+
result.append("\n");
557+
result.append(INDENTATION);
558+
result.append(prettyPrintDecision(decision));
559+
}
560+
result.append("\n}");
561+
return result.toString();
562+
}
563+
564564
/**
565565
* Returns single decision in a human readable format
566566
*
@@ -638,7 +638,8 @@ private static String prettyPrintObject(
638638
result.append("{ ");
639639

640640
String prefix = "";
641-
for (Object entry : ((Map) object).entrySet()) {
641+
Map<?, ?> sortedMap = new TreeMap<>((Map<?, ?>) object); // Automatically sorts by keys
642+
for (Map.Entry<?, ?> entry : sortedMap.entrySet()) {
642643
result.append(prefix);
643644
prefix = ", ";
644645
result.append(
@@ -652,14 +653,21 @@ private static String prettyPrintObject(
652653
if (Collection.class.isAssignableFrom(clz)) {
653654
return String.valueOf(object);
654655
}
656+
655657
if (!skipLevel) {
656658
if (printTypeName) {
657659
result.append(object.getClass().getSimpleName());
658660
result.append(" ");
659661
}
660662
result.append("{");
661663
}
664+
665+
// walk through getter methods without params and dump them as
666+
// key (method-name) = value (getter-result)
667+
662668
Method[] eventMethods = object.getClass().getDeclaredMethods();
669+
Arrays.sort(eventMethods, Comparator.comparing(Method::getName));
670+
663671
boolean first = true;
664672
for (Method method : eventMethods) {
665673
String name = method.getName();

src/test/java/com/uber/cadence/internal/common/WorkflowExecutionUtilsTest.java

+80-11
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,13 @@
1717
import static org.junit.Assert.*;
1818
import static org.mockito.Mockito.*;
1919

20-
import com.uber.cadence.EventType;
21-
import com.uber.cadence.GetWorkflowExecutionHistoryResponse;
22-
import com.uber.cadence.History;
23-
import com.uber.cadence.HistoryEvent;
24-
import com.uber.cadence.StartChildWorkflowExecutionFailedEventAttributes;
25-
import com.uber.cadence.WorkflowExecution;
26-
import com.uber.cadence.WorkflowExecutionCloseStatus;
20+
import com.google.common.collect.ImmutableMap;
21+
import com.uber.cadence.*;
2722
import com.uber.cadence.client.WorkflowTerminatedException;
2823
import com.uber.cadence.client.WorkflowTimedOutException;
24+
import com.uber.cadence.internal.compatibility.ThriftObjects;
2925
import com.uber.cadence.serviceclient.IWorkflowService;
30-
import java.util.Arrays;
31-
import java.util.Collections;
32-
import java.util.Iterator;
33-
import java.util.Optional;
26+
import java.util.*;
3427
import java.util.concurrent.CancellationException;
3528
import java.util.concurrent.TimeUnit;
3629
import java.util.concurrent.TimeoutException;
@@ -42,6 +35,33 @@ public class WorkflowExecutionUtilsTest {
4235

4336
private IWorkflowService mockService;
4437
private WorkflowExecution workflowExecution;
38+
private final String DECISIONS_PRETTY_PRINT =
39+
"{\n"
40+
+ " ScheduleActivityTaskDecisionAttributes {\n"
41+
+ " ActivityId = activityId;\n"
42+
+ " ActivityType = activityName;\n"
43+
+ " Domain = domain;\n"
44+
+ " Header = {\n"
45+
+ " Fields = { key1=value1, key2=value2 };\n"
46+
+ " FieldsSize = 2\n"
47+
+ " };\n"
48+
+ " HeartbeatTimeoutSeconds = 4;\n"
49+
+ " Input = input;\n"
50+
+ " ScheduleToCloseTimeoutSeconds = 1;\n"
51+
+ " ScheduleToStartTimeoutSeconds = 2;\n"
52+
+ " StartToCloseTimeoutSeconds = 3;\n"
53+
+ " TaskList = taskList\n"
54+
+ " },\n"
55+
+ " FailWorkflowExecutionDecisionAttributes {\n"
56+
+ " Details = {\n"
57+
+ " \"error\": \"panic\",\n"
58+
+ " \"stackTrace\": \"fn()\n"
59+
+ " main()\"\n"
60+
+ " };\n"
61+
+ " Reason = failure reason\n"
62+
+ " },\n"
63+
+ " null\n"
64+
+ "}";
4565

4666
@Before
4767
public void setUp() {
@@ -400,4 +420,53 @@ public void testGetHistoryPage_ExceptionWhileRetrievingExecutionHistory() throws
400420

401421
assertTrue(exception.getMessage().contains(errMessage));
402422
}
423+
424+
@Test
425+
public void testPrettyPrintDecisions() throws Exception {
426+
final TaskList taskList =
427+
new com.uber.cadence.TaskList()
428+
.setName("taskList")
429+
.setKind(com.uber.cadence.TaskListKind.NORMAL);
430+
431+
final ActivityType activityType = new ActivityType().setName("activityName");
432+
final Header activityHeader =
433+
new Header()
434+
.setFields(
435+
ImmutableMap.of(
436+
"key1", ThriftObjects.utf8("value1"),
437+
"key2", ThriftObjects.utf8("value2")));
438+
439+
final Decision decisionScheduleActivity =
440+
new Decision()
441+
.setDecisionType(DecisionType.ScheduleActivityTask)
442+
.setScheduleActivityTaskDecisionAttributes(
443+
new ScheduleActivityTaskDecisionAttributes()
444+
.setActivityId("activityId")
445+
.setActivityType(activityType)
446+
.setTaskList(taskList)
447+
.setInput(ThriftObjects.utf8("input"))
448+
.setScheduleToCloseTimeoutSeconds(1)
449+
.setScheduleToStartTimeoutSeconds(2)
450+
.setStartToCloseTimeoutSeconds(3)
451+
.setHeartbeatTimeoutSeconds(4)
452+
.setHeader(activityHeader)
453+
.setRequestLocalDispatch(true)
454+
.setDomain("domain"));
455+
456+
final Decision decisionFailWorkflow =
457+
new Decision()
458+
.setDecisionType(DecisionType.FailWorkflowExecution)
459+
.setFailWorkflowExecutionDecisionAttributes(
460+
new FailWorkflowExecutionDecisionAttributes()
461+
.setReason("failure reason")
462+
.setDetails(
463+
ThriftObjects.utf8(
464+
"{\"error\":\"panic\", \"stackTrace\":\"fn()\\nmain()\"}")));
465+
466+
ArrayList<Decision> decisions =
467+
new ArrayList<>(Arrays.asList(decisionScheduleActivity, decisionFailWorkflow, null));
468+
469+
String result = WorkflowExecutionUtils.prettyPrintDecisions(decisions);
470+
assertEquals(DECISIONS_PRETTY_PRINT, result);
471+
}
403472
}

0 commit comments

Comments
 (0)