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

temporalio / sdk-java / #201

16 Oct 2023 03:47PM UTC coverage: 77.389% (+0.02%) from 77.368%
#201

push

github-actions

web-flow
Apply data converter context in more places (#1896)

Add data converter context to memo, lastFailure and schedules

23 of 23 new or added lines in 5 files covered. (100.0%)

18718 of 24187 relevant lines covered (77.39%)

0.77 hits per line

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

95.74
/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.SerializerUtils.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.taskqueue.v1.TaskQueue;
32
import io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryRequest;
33
import io.temporal.api.workflowservice.v1.SignalWithStartWorkflowExecutionRequest;
34
import io.temporal.api.workflowservice.v1.StartWorkflowExecutionRequest;
35
import io.temporal.api.workflowservice.v1.StartWorkflowExecutionRequestOrBuilder;
36
import io.temporal.client.WorkflowClientOptions;
37
import io.temporal.client.WorkflowOptions;
38
import io.temporal.common.RetryOptions;
39
import io.temporal.common.context.ContextPropagator;
40
import io.temporal.internal.common.ProtobufTimeUtils;
41
import io.temporal.internal.common.SearchAttributesUtil;
42
import java.util.*;
43
import javax.annotation.Nonnull;
44
import javax.annotation.Nullable;
45

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

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

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

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

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

85
    String taskQueue = options.getTaskQueue();
1✔
86
    if (taskQueue != null && !taskQueue.isEmpty()) {
1✔
87
      request.setTaskQueue(TaskQueue.newBuilder().setName(taskQueue).build());
1✔
88
    }
89

90
    RetryOptions retryOptions = options.getRetryOptions();
1✔
91
    if (retryOptions != null) {
1✔
92
      request.setRetryPolicy(toRetryPolicy(retryOptions));
1✔
93
    }
94

95
    if (!Strings.isNullOrEmpty(options.getCronSchedule())) {
1✔
96
      request.setCronSchedule(options.getCronSchedule());
1✔
97
    }
98

99
    if (memo != null) {
1✔
100
      request.setMemo(memo);
1✔
101
    }
102

103
    if (options.getStartDelay() != null) {
1✔
104
      request.setWorkflowStartDelay(ProtobufTimeUtils.toProtoDuration(options.getStartDelay()));
1✔
105
    }
106

107
    if (options.getSearchAttributes() != null && !options.getSearchAttributes().isEmpty()) {
1✔
108
      if (options.getTypedSearchAttributes() != null) {
1✔
109
        throw new IllegalArgumentException(
×
110
            "Cannot have search attributes and typed search attributes");
111
      }
112
      request.setSearchAttributes(SearchAttributesUtil.encode(options.getSearchAttributes()));
1✔
113
    } else if (options.getTypedSearchAttributes() != null) {
1✔
114
      request.setSearchAttributes(
1✔
115
          SearchAttributesUtil.encodeTyped(options.getTypedSearchAttributes()));
1✔
116
    }
117

118
    Header grpcHeader =
1✔
119
        toHeaderGrpc(header, extractContextsAndConvertToBytes(options.getContextPropagators()));
1✔
120
    request.setHeader(grpcHeader);
1✔
121

122
    return request;
1✔
123
  }
124

125
  @Nonnull
126
  SignalWithStartWorkflowExecutionRequest.Builder newSignalWithStartWorkflowExecutionRequest(
127
      @Nonnull StartWorkflowExecutionRequestOrBuilder startParameters,
128
      @Nonnull String signalName,
129
      @Nullable Payloads signalInput) {
130
    SignalWithStartWorkflowExecutionRequest.Builder request =
131
        SignalWithStartWorkflowExecutionRequest.newBuilder()
1✔
132
            .setNamespace(clientOptions.getNamespace())
1✔
133
            .setRequestId(generateUniqueId())
1✔
134
            .setIdentity(clientOptions.getIdentity())
1✔
135
            .setSignalName(signalName)
1✔
136
            .setWorkflowRunTimeout(startParameters.getWorkflowRunTimeout())
1✔
137
            .setWorkflowExecutionTimeout(startParameters.getWorkflowExecutionTimeout())
1✔
138
            .setWorkflowTaskTimeout(startParameters.getWorkflowTaskTimeout())
1✔
139
            .setWorkflowType(startParameters.getWorkflowType())
1✔
140
            .setWorkflowIdReusePolicy(startParameters.getWorkflowIdReusePolicy())
1✔
141
            .setCronSchedule(startParameters.getCronSchedule());
1✔
142

143
    String workflowId = startParameters.getWorkflowId();
1✔
144
    if (workflowId.isEmpty()) {
1✔
145
      workflowId = generateUniqueId();
×
146
    }
147
    request.setWorkflowId(workflowId);
1✔
148

149
    if (signalInput != null) {
1✔
150
      request.setSignalInput(signalInput);
1✔
151
    }
152

153
    if (startParameters.hasInput()) {
1✔
154
      request.setInput(startParameters.getInput());
1✔
155
    }
156

157
    if (startParameters.hasTaskQueue()) {
1✔
158
      request.setTaskQueue(startParameters.getTaskQueue());
1✔
159
    }
160

161
    if (startParameters.hasRetryPolicy()) {
1✔
162
      request.setRetryPolicy(startParameters.getRetryPolicy());
×
163
    }
164

165
    if (startParameters.hasMemo()) {
1✔
166
      request.setMemo(startParameters.getMemo());
×
167
    }
168

169
    if (startParameters.hasSearchAttributes()) {
1✔
170
      request.setSearchAttributes(startParameters.getSearchAttributes());
1✔
171
    }
172

173
    if (startParameters.hasHeader()) {
1✔
174
      request.setHeader(startParameters.getHeader());
1✔
175
    }
176

177
    if (startParameters.hasWorkflowStartDelay()) {
1✔
178
      request.setWorkflowStartDelay(startParameters.getWorkflowStartDelay());
1✔
179
    }
180

181
    return request;
1✔
182
  }
183

184
  @Nonnull
185
  GetWorkflowExecutionHistoryRequest newHistoryLongPollRequest(
186
      WorkflowExecution workflowExecution, ByteString pageToken) {
187
    return GetWorkflowExecutionHistoryRequest.newBuilder()
1✔
188
        .setNamespace(clientOptions.getNamespace())
1✔
189
        .setExecution(workflowExecution)
1✔
190
        .setHistoryEventFilterType(HistoryEventFilterType.HISTORY_EVENT_FILTER_TYPE_CLOSE_EVENT)
1✔
191
        .setWaitNewEvent(true)
1✔
192
        .setNextPageToken(pageToken)
1✔
193
        .build();
1✔
194
  }
195

196
  private io.temporal.common.interceptors.Header extractContextsAndConvertToBytes(
197
      List<ContextPropagator> workflowOptionsContextPropagators) {
198
    List<ContextPropagator> workflowClientContextPropagators =
1✔
199
        clientOptions.getContextPropagators();
1✔
200
    if ((workflowClientContextPropagators.isEmpty() && workflowOptionsContextPropagators == null)
1✔
201
        || (workflowOptionsContextPropagators != null
202
            && workflowOptionsContextPropagators.isEmpty())) {
1✔
203
      return null;
1✔
204
    }
205

206
    List<ContextPropagator> listToUse =
1✔
207
        MoreObjects.firstNonNull(
1✔
208
            workflowOptionsContextPropagators, workflowClientContextPropagators);
209
    Map<String, Payload> result = new HashMap<>();
1✔
210
    for (ContextPropagator propagator : listToUse) {
1✔
211
      result.putAll(propagator.serializeContext(propagator.getCurrentContext()));
1✔
212
    }
1✔
213
    return new io.temporal.common.interceptors.Header(result);
1✔
214
  }
215

216
  private static String generateUniqueId() {
217
    return UUID.randomUUID().toString();
1✔
218
  }
219
}
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