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

temporalio / sdk-java / #333

16 Oct 2024 07:28PM UTC coverage: 78.65% (+0.6%) from 78.085%
#333

push

github

web-flow
Fix code coverage (#2275)

22670 of 28824 relevant lines covered (78.65%)

0.79 hits per line

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

94.39
/temporal-sdk/src/main/java/io/temporal/internal/client/WorkflowClientRequestFactory.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.internal.client;
22

23
import static io.temporal.internal.common.HeaderUtils.toHeaderGrpc;
24
import static io.temporal.internal.common.RetryOptionsUtils.toRetryPolicy;
25

26
import com.google.common.base.MoreObjects;
27
import com.google.common.base.Strings;
28
import com.google.protobuf.ByteString;
29
import io.temporal.api.common.v1.*;
30
import io.temporal.api.enums.v1.HistoryEventFilterType;
31
import io.temporal.api.sdk.v1.UserMetadata;
32
import io.temporal.api.taskqueue.v1.TaskQueue;
33
import io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryRequest;
34
import io.temporal.api.workflowservice.v1.SignalWithStartWorkflowExecutionRequest;
35
import io.temporal.api.workflowservice.v1.StartWorkflowExecutionRequest;
36
import io.temporal.api.workflowservice.v1.StartWorkflowExecutionRequestOrBuilder;
37
import io.temporal.client.WorkflowClientOptions;
38
import io.temporal.client.WorkflowOptions;
39
import io.temporal.common.RetryOptions;
40
import io.temporal.common.context.ContextPropagator;
41
import io.temporal.internal.common.ProtobufTimeUtils;
42
import io.temporal.internal.common.SearchAttributesUtil;
43
import java.util.*;
44
import javax.annotation.Nonnull;
45
import javax.annotation.Nullable;
46

47
final class WorkflowClientRequestFactory {
48
  private final WorkflowClientOptions clientOptions;
49

50
  public WorkflowClientRequestFactory(WorkflowClientOptions clientOptions) {
1✔
51
    this.clientOptions = clientOptions;
1✔
52
  }
1✔
53

54
  // If you add anything new here, keep newSignalWithStartWorkflowExecutionRequest in sync
55
  @SuppressWarnings("deprecation")
56
  @Nonnull
57
  StartWorkflowExecutionRequest.Builder newStartWorkflowExecutionRequest(
58
      @Nonnull String workflowId,
59
      @Nonnull String workflowTypeName,
60
      @Nonnull io.temporal.common.interceptors.Header header,
61
      @Nonnull WorkflowOptions options,
62
      @Nullable Payloads inputArgs,
63
      @Nullable Memo memo,
64
      @Nullable UserMetadata userMetadata) {
65
    StartWorkflowExecutionRequest.Builder request =
66
        StartWorkflowExecutionRequest.newBuilder()
1✔
67
            .setNamespace(clientOptions.getNamespace())
1✔
68
            .setRequestId(generateUniqueId())
1✔
69
            .setIdentity(clientOptions.getIdentity())
1✔
70
            .setWorkflowId(workflowId)
1✔
71
            .setWorkflowType(WorkflowType.newBuilder().setName(workflowTypeName))
1✔
72
            .setWorkflowRunTimeout(
1✔
73
                ProtobufTimeUtils.toProtoDuration(options.getWorkflowRunTimeout()))
1✔
74
            .setWorkflowExecutionTimeout(
1✔
75
                ProtobufTimeUtils.toProtoDuration(options.getWorkflowExecutionTimeout()))
1✔
76
            .setWorkflowTaskTimeout(
1✔
77
                ProtobufTimeUtils.toProtoDuration(options.getWorkflowTaskTimeout()));
1✔
78

79
    if (inputArgs != null) {
1✔
80
      request.setInput(inputArgs);
1✔
81
    }
82

83
    if (options.getWorkflowIdReusePolicy() != null) {
1✔
84
      request.setWorkflowIdReusePolicy(options.getWorkflowIdReusePolicy());
1✔
85
    }
86

87
    if (options.getWorkflowIdConflictPolicy() != null) {
1✔
88
      request.setWorkflowIdConflictPolicy(options.getWorkflowIdConflictPolicy());
1✔
89
    }
90

91
    if (options.getRequestId() != null) {
1✔
92
      request.setRequestId(options.getRequestId());
1✔
93
    }
94

95
    if (options.getCompletionCallbacks() != null) {
1✔
96
      options.getCompletionCallbacks().forEach(request::addCompletionCallbacks);
1✔
97
    }
98

99
    if (options.getLinks() != null) {
1✔
100
      options.getLinks().forEach(request::addLinks);
1✔
101
    }
102

103
    String taskQueue = options.getTaskQueue();
1✔
104
    if (taskQueue != null && !taskQueue.isEmpty()) {
1✔
105
      request.setTaskQueue(TaskQueue.newBuilder().setName(taskQueue).build());
1✔
106
    }
107

108
    RetryOptions retryOptions = options.getRetryOptions();
1✔
109
    if (retryOptions != null) {
1✔
110
      request.setRetryPolicy(toRetryPolicy(retryOptions));
1✔
111
    }
112

113
    if (!Strings.isNullOrEmpty(options.getCronSchedule())) {
1✔
114
      request.setCronSchedule(options.getCronSchedule());
1✔
115
    }
116

117
    if (memo != null) {
1✔
118
      request.setMemo(memo);
1✔
119
    }
120

121
    if (options.getStartDelay() != null) {
1✔
122
      request.setWorkflowStartDelay(ProtobufTimeUtils.toProtoDuration(options.getStartDelay()));
1✔
123
    }
124

125
    if (userMetadata != null) {
1✔
126
      request.setUserMetadata(userMetadata);
×
127
    }
128

129
    if (options.getSearchAttributes() != null && !options.getSearchAttributes().isEmpty()) {
1✔
130
      if (options.getTypedSearchAttributes() != null) {
1✔
131
        throw new IllegalArgumentException(
×
132
            "Cannot have search attributes and typed search attributes");
133
      }
134
      request.setSearchAttributes(SearchAttributesUtil.encode(options.getSearchAttributes()));
1✔
135
    } else if (options.getTypedSearchAttributes() != null) {
1✔
136
      request.setSearchAttributes(
1✔
137
          SearchAttributesUtil.encodeTyped(options.getTypedSearchAttributes()));
1✔
138
    }
139

140
    Header grpcHeader =
1✔
141
        toHeaderGrpc(header, extractContextsAndConvertToBytes(options.getContextPropagators()));
1✔
142
    request.setHeader(grpcHeader);
1✔
143

144
    return request;
1✔
145
  }
146

147
  @Nonnull
148
  SignalWithStartWorkflowExecutionRequest.Builder newSignalWithStartWorkflowExecutionRequest(
149
      @Nonnull StartWorkflowExecutionRequestOrBuilder startParameters,
150
      @Nonnull String signalName,
151
      @Nullable Payloads signalInput) {
152
    SignalWithStartWorkflowExecutionRequest.Builder request =
153
        SignalWithStartWorkflowExecutionRequest.newBuilder()
1✔
154
            .setNamespace(clientOptions.getNamespace())
1✔
155
            .setRequestId(generateUniqueId())
1✔
156
            .setIdentity(clientOptions.getIdentity())
1✔
157
            .setSignalName(signalName)
1✔
158
            .setWorkflowRunTimeout(startParameters.getWorkflowRunTimeout())
1✔
159
            .setWorkflowExecutionTimeout(startParameters.getWorkflowExecutionTimeout())
1✔
160
            .setWorkflowTaskTimeout(startParameters.getWorkflowTaskTimeout())
1✔
161
            .setWorkflowType(startParameters.getWorkflowType())
1✔
162
            .setWorkflowIdReusePolicy(startParameters.getWorkflowIdReusePolicy())
1✔
163
            .setWorkflowIdConflictPolicy(startParameters.getWorkflowIdConflictPolicy())
1✔
164
            .setCronSchedule(startParameters.getCronSchedule());
1✔
165

166
    String workflowId = startParameters.getWorkflowId();
1✔
167
    if (workflowId.isEmpty()) {
1✔
168
      workflowId = generateUniqueId();
×
169
    }
170
    request.setWorkflowId(workflowId);
1✔
171

172
    if (signalInput != null) {
1✔
173
      request.setSignalInput(signalInput);
1✔
174
    }
175

176
    if (startParameters.hasInput()) {
1✔
177
      request.setInput(startParameters.getInput());
1✔
178
    }
179

180
    if (startParameters.hasTaskQueue()) {
1✔
181
      request.setTaskQueue(startParameters.getTaskQueue());
1✔
182
    }
183

184
    if (startParameters.hasRetryPolicy()) {
1✔
185
      request.setRetryPolicy(startParameters.getRetryPolicy());
×
186
    }
187

188
    if (startParameters.hasMemo()) {
1✔
189
      request.setMemo(startParameters.getMemo());
×
190
    }
191

192
    if (startParameters.hasSearchAttributes()) {
1✔
193
      request.setSearchAttributes(startParameters.getSearchAttributes());
1✔
194
    }
195

196
    if (startParameters.hasHeader()) {
1✔
197
      request.setHeader(startParameters.getHeader());
1✔
198
    }
199

200
    if (startParameters.hasWorkflowStartDelay()) {
1✔
201
      request.setWorkflowStartDelay(startParameters.getWorkflowStartDelay());
1✔
202
    }
203

204
    if (startParameters.hasUserMetadata()) {
1✔
205
      request.setUserMetadata(startParameters.getUserMetadata());
×
206
    }
207

208
    return request;
1✔
209
  }
210

211
  @Nonnull
212
  GetWorkflowExecutionHistoryRequest newHistoryLongPollRequest(
213
      WorkflowExecution workflowExecution, ByteString pageToken) {
214
    return GetWorkflowExecutionHistoryRequest.newBuilder()
1✔
215
        .setNamespace(clientOptions.getNamespace())
1✔
216
        .setExecution(workflowExecution)
1✔
217
        .setHistoryEventFilterType(HistoryEventFilterType.HISTORY_EVENT_FILTER_TYPE_CLOSE_EVENT)
1✔
218
        .setWaitNewEvent(true)
1✔
219
        .setNextPageToken(pageToken)
1✔
220
        .build();
1✔
221
  }
222

223
  private io.temporal.common.interceptors.Header extractContextsAndConvertToBytes(
224
      List<ContextPropagator> workflowOptionsContextPropagators) {
225
    List<ContextPropagator> workflowClientContextPropagators =
1✔
226
        clientOptions.getContextPropagators();
1✔
227
    if ((workflowClientContextPropagators.isEmpty() && workflowOptionsContextPropagators == null)
1✔
228
        || (workflowOptionsContextPropagators != null
229
            && workflowOptionsContextPropagators.isEmpty())) {
1✔
230
      return null;
1✔
231
    }
232

233
    List<ContextPropagator> listToUse =
1✔
234
        MoreObjects.firstNonNull(
1✔
235
            workflowOptionsContextPropagators, workflowClientContextPropagators);
236
    Map<String, Payload> result = new HashMap<>();
1✔
237
    for (ContextPropagator propagator : listToUse) {
1✔
238
      result.putAll(propagator.serializeContext(propagator.getCurrentContext()));
1✔
239
    }
1✔
240
    return new io.temporal.common.interceptors.Header(result);
1✔
241
  }
242

243
  private static String generateUniqueId() {
244
    return UUID.randomUUID().toString();
1✔
245
  }
246
}
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