|
20 | 20 | import static org.junit.Assert.*; |
21 | 21 |
|
22 | 22 | import com.google.gson.JsonIOException; |
23 | | -import com.uber.cadence.EventType; |
24 | | -import com.uber.cadence.History; |
25 | | -import com.uber.cadence.HistoryEvent; |
26 | | -import com.uber.cadence.TaskList; |
27 | | -import com.uber.cadence.WorkflowExecutionStartedEventAttributes; |
28 | | -import com.uber.cadence.WorkflowType; |
29 | 23 | import com.uber.cadence.activity.Activity; |
30 | 24 | import java.io.File; |
31 | 25 | import java.io.FileInputStream; |
32 | 26 | import java.io.IOException; |
33 | 27 | import java.io.InputStream; |
34 | 28 | import java.lang.reflect.Method; |
35 | 29 | import java.lang.reflect.Type; |
36 | | -import java.nio.charset.StandardCharsets; |
37 | 30 | import java.util.ArrayList; |
38 | 31 | import java.util.List; |
39 | | -import java.util.Objects; |
40 | 32 | import java.util.UUID; |
41 | 33 | import org.junit.Test; |
42 | 34 |
|
43 | 35 | public class JsonDataConverterTest { |
44 | 36 |
|
45 | 37 | private final DataConverter converter = JsonDataConverter.getInstance(); |
46 | 38 |
|
47 | | - static class TestData { |
48 | | - String val1; |
49 | | - // TBase value; |
50 | | - HistoryEvent val2; |
51 | | - // TEnum value; |
52 | | - EventType val3; |
53 | | - |
54 | | - public TestData(String val1, HistoryEvent val2, EventType val3) { |
55 | | - this.val1 = val1; |
56 | | - this.val2 = val2; |
57 | | - this.val3 = val3; |
58 | | - } |
59 | | - |
60 | | - @Override |
61 | | - public boolean equals(Object o) { |
62 | | - if (this == o) return true; |
63 | | - if (!(o instanceof TestData)) return false; |
64 | | - TestData testData = (TestData) o; |
65 | | - return Objects.equals(val1, testData.val1) |
66 | | - && Objects.equals(val2, testData.val2) |
67 | | - && val3 == testData.val3; |
68 | | - } |
69 | | - |
70 | | - @Override |
71 | | - public int hashCode() { |
72 | | - |
73 | | - return Objects.hash(val1, val2, val3); |
74 | | - } |
75 | | - } |
76 | | - |
77 | | - @Test |
78 | | - public void testThrift() { |
79 | | - List<HistoryEvent> events = new ArrayList<>(); |
80 | | - WorkflowExecutionStartedEventAttributes started = |
81 | | - new WorkflowExecutionStartedEventAttributes() |
82 | | - .setExecutionStartToCloseTimeoutSeconds(11) |
83 | | - .setIdentity("testIdentity") |
84 | | - .setInput("input".getBytes(StandardCharsets.UTF_8)) |
85 | | - .setWorkflowType(new WorkflowType().setName("workflowType1")) |
86 | | - .setTaskList(new TaskList().setName("taskList1")); |
87 | | - events.add( |
88 | | - new HistoryEvent() |
89 | | - .setTimestamp(1234567) |
90 | | - .setEventId(321) |
91 | | - .setWorkflowExecutionStartedEventAttributes(started)); |
92 | | - History history = new History().setEvents(events); |
93 | | - byte[] converted = converter.toData(history); |
94 | | - History fromConverted = converter.fromData(converted, History.class, History.class); |
95 | | - assertEquals(new String(converted, StandardCharsets.UTF_8), history, fromConverted); |
96 | | - } |
97 | | - |
98 | | - @Test |
99 | | - public void testThriftArray() { |
100 | | - List<HistoryEvent> events = new ArrayList<>(); |
101 | | - WorkflowExecutionStartedEventAttributes started = |
102 | | - new WorkflowExecutionStartedEventAttributes() |
103 | | - .setExecutionStartToCloseTimeoutSeconds(11) |
104 | | - .setIdentity("testIdentity") |
105 | | - .setInput("input".getBytes(StandardCharsets.UTF_8)) |
106 | | - .setWorkflowType(new WorkflowType().setName("workflowType1")) |
107 | | - .setTaskList(new TaskList().setName("taskList1")); |
108 | | - events.add( |
109 | | - new HistoryEvent() |
110 | | - .setTimestamp(1234567) |
111 | | - .setEventId(321) |
112 | | - .setWorkflowExecutionStartedEventAttributes(started)); |
113 | | - History history = new History().setEvents(events); |
114 | | - byte[] converted = converter.toData("abc", history); |
115 | | - Object[] fromConverted = converter.fromDataArray(converted, String.class, History.class); |
116 | | - assertEquals(new String(converted, StandardCharsets.UTF_8), "abc", fromConverted[0]); |
117 | | - assertEquals(new String(converted, StandardCharsets.UTF_8), history, fromConverted[1]); |
118 | | - } |
119 | | - |
120 | | - @Test |
121 | | - public void testThriftFieldsInPOJO() { |
122 | | - WorkflowExecutionStartedEventAttributes started = |
123 | | - new WorkflowExecutionStartedEventAttributes() |
124 | | - .setExecutionStartToCloseTimeoutSeconds(11) |
125 | | - .setIdentity("testIdentity") |
126 | | - .setInput("input".getBytes(StandardCharsets.UTF_8)) |
127 | | - .setWorkflowType(new WorkflowType().setName("workflowType1")) |
128 | | - .setTaskList(new TaskList().setName("taskList1")); |
129 | | - |
130 | | - HistoryEvent historyEvent = |
131 | | - new HistoryEvent() |
132 | | - .setTimestamp(1234567) |
133 | | - .setEventId(321) |
134 | | - .setWorkflowExecutionStartedEventAttributes(started); |
135 | | - |
136 | | - TestData testData = new TestData("test-thrift", historyEvent, EventType.ActivityTaskCompleted); |
137 | | - |
138 | | - byte[] converted = converter.toData(testData); |
139 | | - TestData fromConverted = converter.fromData(converted, TestData.class, TestData.class); |
140 | | - assertEquals(new String(converted, StandardCharsets.UTF_8), testData, fromConverted); |
141 | | - } |
142 | | - |
143 | | - @Test |
144 | | - public void testThriftFieldsInPOJOArray() { |
145 | | - WorkflowExecutionStartedEventAttributes started = |
146 | | - new WorkflowExecutionStartedEventAttributes() |
147 | | - .setExecutionStartToCloseTimeoutSeconds(11) |
148 | | - .setIdentity("testIdentity") |
149 | | - .setInput("input".getBytes(StandardCharsets.UTF_8)) |
150 | | - .setWorkflowType(new WorkflowType().setName("workflowType1")) |
151 | | - .setTaskList(new TaskList().setName("taskList1")); |
152 | | - |
153 | | - HistoryEvent historyEvent = |
154 | | - new HistoryEvent() |
155 | | - .setTimestamp(1234567) |
156 | | - .setEventId(321) |
157 | | - .setWorkflowExecutionStartedEventAttributes(started); |
158 | | - |
159 | | - TestData testData = new TestData("test-thrift", historyEvent, EventType.ActivityTaskCompleted); |
160 | | - |
161 | | - byte[] converted = converter.toData("abc", testData); |
162 | | - Object[] fromConverted = converter.fromDataArray(converted, String.class, TestData.class); |
163 | | - assertEquals(new String(converted, StandardCharsets.UTF_8), "abc", fromConverted[0]); |
164 | | - assertEquals(new String(converted, StandardCharsets.UTF_8), testData, fromConverted[1]); |
165 | | - } |
166 | | - |
167 | 39 | public static void foo(List<UUID> arg) {} |
168 | 40 |
|
169 | 41 | @Test |
|
0 commit comments