• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uber / cadence-java-client / 2629

06 Nov 2024 11:16PM UTC coverage: 79.654% (+0.1%) from 79.511%
2629

push

buildkite

web-flow
fix bug in QueryWorkflowParameters.toString (#948)

What changed?

Fixed a bug that toString will error if input length is smaller than 512
remove unused method (with method has been replaced with setter already)
add test coverage
Why?
part of unit test coverage

How did you test it?
unit test

2 of 2 new or added lines in 1 file covered. (100.0%)

2 existing lines in 2 files now uncovered.

15441 of 19385 relevant lines covered (79.65%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.61
/src/main/java/com/uber/cadence/internal/common/InternalUtils.java
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.common;
19

20
import com.google.common.base.Defaults;
21
import com.google.common.collect.Lists;
22
import com.uber.cadence.DataBlob;
23
import com.uber.cadence.History;
24
import com.uber.cadence.HistoryEvent;
25
import com.uber.cadence.HistoryEventFilterType;
26
import com.uber.cadence.Memo;
27
import com.uber.cadence.SearchAttributes;
28
import com.uber.cadence.TaskList;
29
import com.uber.cadence.TaskListKind;
30
import com.uber.cadence.converter.DataConverter;
31
import com.uber.cadence.converter.JsonDataConverter;
32
import com.uber.cadence.internal.worker.Shutdownable;
33
import com.uber.cadence.workflow.WorkflowMethod;
34
import java.lang.reflect.Method;
35
import java.nio.ByteBuffer;
36
import java.util.Arrays;
37
import java.util.HashMap;
38
import java.util.List;
39
import java.util.Map;
40
import java.util.concurrent.ExecutorService;
41
import java.util.concurrent.TimeUnit;
42
import org.apache.thrift.TDeserializer;
43
import org.apache.thrift.TException;
44
import org.apache.thrift.TSerializer;
45

46
/** Utility functions shared by the implementation code. */
47
public final class InternalUtils {
48

49
  /**
50
   * Used to construct default name of an activity or workflow type from a method it implements.
51
   *
52
   * @return "Simple class name"::"methodName"
53
   */
54
  public static String getSimpleName(Method method) {
55
    return method.getDeclaringClass().getSimpleName() + "::" + method.getName();
1✔
56
  }
57

58
  public static String getWorkflowType(Method method, WorkflowMethod workflowMethod) {
59
    String workflowName = workflowMethod.name();
1✔
60
    if (workflowName.isEmpty()) {
1✔
61
      return InternalUtils.getSimpleName(method);
1✔
62
    } else {
63
      return workflowName;
1✔
64
    }
65
  }
66

67
  public static Method getWorkflowMethod(Class<?> workflowInterface) {
68
    Method result = null;
1✔
69
    for (Method m : workflowInterface.getMethods()) {
1✔
70
      if (m.getAnnotation(WorkflowMethod.class) != null) {
1✔
71
        if (result != null) {
1✔
72
          throw new IllegalArgumentException(
×
73
              "Workflow interface must have exactly one method "
74
                  + "annotated with @WorkflowMethod. Found \""
75
                  + result
76
                  + "\" and \""
77
                  + m
78
                  + "\"");
79
        }
80
        result = m;
1✔
81
      }
82
    }
83
    if (result == null) {
1✔
84
      throw new IllegalArgumentException(
×
85
          "Method annotated with @WorkflowMethod is not " + "found at " + workflowInterface);
86
    }
87
    return result;
1✔
88
  }
89

90
  public static TaskList createStickyTaskList(String taskListName) {
91
    TaskList tl = new TaskList();
1✔
92
    tl.setName(taskListName);
1✔
93
    tl.setKind(TaskListKind.STICKY);
1✔
94
    return tl;
1✔
95
  }
96

97
  public static TaskList createNormalTaskList(String taskListName) {
98
    TaskList tl = new TaskList();
1✔
99
    tl.setName(taskListName);
1✔
100
    tl.setKind(TaskListKind.NORMAL);
1✔
101
    return tl;
1✔
102
  }
103

104
  public static long awaitTermination(Shutdownable s, long timeoutMillis) {
105
    if (s == null) {
1✔
106
      return timeoutMillis;
1✔
107
    }
108
    return awaitTermination(
1✔
109
        timeoutMillis,
110
        () -> {
111
          s.awaitTermination(timeoutMillis, TimeUnit.MILLISECONDS);
1✔
112
        });
1✔
113
  }
114

115
  public static long awaitTermination(ExecutorService s, long timeoutMillis) {
116
    if (s == null) {
1✔
117
      return timeoutMillis;
×
118
    }
119
    return awaitTermination(
1✔
120
        timeoutMillis,
121
        () -> {
122
          try {
123
            s.awaitTermination(timeoutMillis, TimeUnit.MILLISECONDS);
1✔
124
          } catch (InterruptedException e) {
×
125
          }
1✔
126
        });
1✔
127
  }
128

129
  public static long awaitTermination(long timeoutMillis, Runnable toTerminate) {
130
    long started = System.currentTimeMillis();
1✔
131
    toTerminate.run();
1✔
132
    long remainingTimeout = timeoutMillis - (System.currentTimeMillis() - started);
1✔
133
    if (remainingTimeout < 0) {
1✔
UNCOV
134
      remainingTimeout = 0;
×
135
    }
136
    return remainingTimeout;
1✔
137
  }
138

139
  public static Object getValueOrDefault(Object value, Class<?> valueClass) {
140
    if (value != null) {
1✔
141
      return value;
1✔
142
    }
143
    return Defaults.defaultValue(valueClass);
1✔
144
  }
145

146
  public static Memo convertMapToMemo(Map<String, Object> memo) {
147
    DataConverter converter = JsonDataConverter.getInstance();
×
148
    Map<String, ByteBuffer> mapOfByteBuffer = new HashMap<>();
×
149
    memo.forEach(
×
150
        (key, value) -> {
151
          mapOfByteBuffer.put(key, ByteBuffer.wrap(converter.toData(value)));
×
152
        });
×
153
    return new Memo().setFields(mapOfByteBuffer);
×
154
  }
155

156
  public static SearchAttributes convertMapToSearchAttributes(
157
      Map<String, Object> searchAttributes) {
158
    DataConverter converter = JsonDataConverter.getInstance();
1✔
159
    Map<String, ByteBuffer> mapOfByteBuffer = new HashMap<>();
1✔
160
    searchAttributes.forEach(
1✔
161
        (key, value) -> {
162
          mapOfByteBuffer.put(key, ByteBuffer.wrap(converter.toData(value)));
1✔
163
        });
1✔
164
    return new SearchAttributes().setIndexedFields(mapOfByteBuffer);
1✔
165
  }
166

167
  // This method serializes history to blob data
168
  public static DataBlob SerializeFromHistoryToBlobData(History history) {
169

170
    // TODO: move to global dependency after https://issues.apache.org/jira/browse/THRIFT-2218
171
    TSerializer serializer = new TSerializer();
1✔
172
    DataBlob blob = new DataBlob();
1✔
173
    try {
174
      blob.setData(serializer.serialize(history));
1✔
175
    } catch (org.apache.thrift.TException err) {
×
176
      throw new RuntimeException("Serialize history to blob data failed", err);
×
177
    }
1✔
178

179
    return blob;
1✔
180
  }
181

182
  // This method deserialize the DataBlob data to the History data
183
  public static History DeserializeFromBlobDataToHistory(
184
      List<DataBlob> blobData, HistoryEventFilterType historyEventFilterType) throws TException {
185

186
    // TODO: move to global dependency after https://issues.apache.org/jira/browse/THRIFT-2218
187
    TDeserializer deSerializer = new TDeserializer();
1✔
188
    List<HistoryEvent> events = Lists.newArrayList();
1✔
189
    for (DataBlob data : blobData) {
1✔
190
      History history = new History();
1✔
191
      try {
192
        byte[] dataByte = data.getData();
1✔
193
        // TODO: verify the beginning index
194
        dataByte = Arrays.copyOfRange(dataByte, 0, dataByte.length);
1✔
195
        deSerializer.deserialize(history, dataByte);
1✔
196

197
        if (history == null || history.getEvents() == null || history.getEvents().size() == 0) {
1✔
198
          return null;
×
199
        }
200
      } catch (org.apache.thrift.TException err) {
×
201
        throw new TException("Deserialize blob data to history failed with unknown error");
×
202
      }
1✔
203

204
      events.addAll(history.getEvents());
1✔
205
    }
1✔
206

207
    if (events.size() > 0 && historyEventFilterType == HistoryEventFilterType.CLOSE_EVENT) {
1✔
208
      events = events.subList(events.size() - 1, events.size());
×
209
    }
210

211
    return new History().setEvents(events);
1✔
212
  }
213

214
  // This method serializes history event to blob data
215
  public static List<DataBlob> SerializeFromHistoryEventToBlobData(List<HistoryEvent> events) {
216

217
    // TODO: move to global dependency after https://issues.apache.org/jira/browse/THRIFT-2218
218
    TSerializer serializer = new TSerializer();
1✔
219
    List<DataBlob> blobs = Lists.newArrayListWithCapacity(events.size());
1✔
220
    for (HistoryEvent event : events) {
1✔
221
      DataBlob blob = new DataBlob();
1✔
222
      try {
223
        blob.setData(serializer.serialize(event));
1✔
224
      } catch (org.apache.thrift.TException err) {
×
225
        throw new RuntimeException("Serialize history event to blob data failed", err);
×
226
      }
1✔
227
      blobs.add(blob);
1✔
228
    }
1✔
229
    return blobs;
1✔
230
  }
231

232
  // This method serializes blob data to history event
233
  public static List<HistoryEvent> DeserializeFromBlobDataToHistoryEvents(List<DataBlob> blobData)
234
      throws TException {
235

236
    // TODO: move to global dependency after https://issues.apache.org/jira/browse/THRIFT-2218
237
    TDeserializer deSerializer = new TDeserializer();
1✔
238
    List<HistoryEvent> events = Lists.newArrayList();
1✔
239
    for (DataBlob data : blobData) {
1✔
240
      try {
241
        HistoryEvent event = new HistoryEvent();
1✔
242
        byte[] dataByte = data.getData();
1✔
243
        // TODO: verify the beginning index
244
        dataByte = Arrays.copyOfRange(dataByte, 0, dataByte.length);
1✔
245
        deSerializer.deserialize(event, dataByte);
1✔
246
        events.add(event);
1✔
247
      } catch (org.apache.thrift.TException err) {
×
248
        throw new TException("Deserialize blob data to history event failed with unknown error");
×
249
      }
1✔
250
    }
1✔
251
    return events;
1✔
252
  }
253

254
  /** Prohibit instantiation */
255
  private InternalUtils() {}
256
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc