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

temporalio / sdk-java / #343

31 Oct 2024 06:31PM UTC coverage: 75.148% (-3.6%) from 78.794%
#343

push

github

web-flow
Fix jacoco coverage (#2304)

5139 of 8240 branches covered (62.37%)

Branch coverage included in aggregate %.

22841 of 28993 relevant lines covered (78.78%)

0.79 hits per line

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

72.73
/temporal-sdk/src/main/java/io/temporal/internal/common/WorkflowExecutionHistory.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.common;
22

23
import com.google.gson.Gson;
24
import com.google.gson.GsonBuilder;
25
import com.google.gson.JsonElement;
26
import com.google.gson.JsonParser;
27
import com.google.protobuf.InvalidProtocolBufferException;
28
import com.google.protobuf.util.JsonFormat;
29
import io.temporal.api.common.v1.WorkflowExecution;
30
import io.temporal.api.enums.v1.EventType;
31
import io.temporal.api.history.v1.History;
32
import io.temporal.api.history.v1.HistoryEvent;
33
import io.temporal.common.converter.DataConverterException;
34
import java.util.List;
35

36
// This class by mistake leaked into a public interface and is used by users, so it can't be deleted
37
// right away.
38
// To fix the mistake, it was republished in a public package as
39
// io.temporal.common.WorkflowExecutionHistory class
40
// that extends this class.
41
/**
42
 * @deprecated use {@link io.temporal.common.WorkflowExecutionHistory} instead.
43
 */
44
@Deprecated
45
public class WorkflowExecutionHistory {
46
  protected static final String DEFAULT_WORKFLOW_ID = "workflow_id_in_replay";
47
  private static final Gson GSON_PRETTY_PRINTER = new GsonBuilder().setPrettyPrinting().create();
1✔
48

49
  // we stay on using the old API that uses a JsonParser instance instead of static methods
50
  // to give users a larger range of supported version
51
  @SuppressWarnings("deprecation")
52
  private static final JsonParser GSON_PARSER = new JsonParser();
1✔
53

54
  private final History history;
55
  private final String workflowId;
56

57
  public WorkflowExecutionHistory(History history) {
58
    this(history, DEFAULT_WORKFLOW_ID);
1✔
59
  }
1✔
60

61
  public WorkflowExecutionHistory(History history, String workflowId) {
1✔
62
    checkHistory(history);
1✔
63
    this.history = history;
1✔
64
    this.workflowId = workflowId;
1✔
65
  }
1✔
66

67
  public static WorkflowExecutionHistory fromJson(String serialized) {
68
    return new WorkflowExecutionHistory(
×
69
        io.temporal.common.WorkflowExecutionHistory.fromJson(serialized).getHistory());
×
70
  }
71

72
  private static void checkHistory(History history) {
73
    List<HistoryEvent> events = history.getEventsList();
1✔
74
    if (events == null || events.size() == 0) {
1!
75
      throw new IllegalArgumentException("Empty history");
×
76
    }
77
    HistoryEvent startedEvent = events.get(0);
1✔
78
    if (startedEvent.getEventType() != EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED) {
1!
79
      throw new IllegalArgumentException(
×
80
          "First event is not WorkflowExecutionStarted but " + startedEvent);
81
    }
82
    if (!startedEvent.hasWorkflowExecutionStartedEventAttributes()) {
1!
83
      throw new IllegalArgumentException("First event is corrupted");
×
84
    }
85
  }
1✔
86

87
  /**
88
   * @param prettyPrint Whether to pretty print the JSON.
89
   * @return Full json that can be used for replay.
90
   */
91
  public String toJson(boolean prettyPrint) {
92
    return toJson(prettyPrint, false);
1✔
93
  }
94

95
  /**
96
   * @param prettyPrint Whether to pretty print the JSON.
97
   * @param legacyFormat If true, will use the older-style protobuf enum formatting as done by
98
   *     Temporal tooling in the past. This is not recommended.
99
   * @return Full JSON that can be used for replay.
100
   */
101
  public String toJson(boolean prettyPrint, boolean legacyFormat) {
102
    JsonFormat.Printer printer = JsonFormat.printer();
1✔
103
    try {
104
      String protoJson = printer.print(history);
1✔
105
      if (legacyFormat) {
1✔
106
        protoJson = HistoryJsonUtils.protoJsonToHistoryFormatJson(protoJson);
1✔
107
      }
108

109
      if (prettyPrint) {
1!
110
        @SuppressWarnings("deprecation")
111
        JsonElement je = GSON_PARSER.parse(protoJson);
1✔
112
        return GSON_PRETTY_PRINTER.toJson(je);
1✔
113
      } else {
114
        return protoJson;
×
115
      }
116
    } catch (InvalidProtocolBufferException e) {
×
117
      throw new DataConverterException(e);
×
118
    }
119
  }
120

121
  /**
122
   * Returns workflow instance history in a human-readable format.
123
   *
124
   * @param showWorkflowTasks when set to false workflow task events (command events) are not
125
   *     included
126
   */
127
  public String toProtoText(boolean showWorkflowTasks) {
128
    return HistoryProtoTextUtils.toProtoText(history, showWorkflowTasks);
×
129
  }
130

131
  public WorkflowExecution getWorkflowExecution() {
132
    return WorkflowExecution.newBuilder()
1✔
133
        .setWorkflowId(workflowId)
1✔
134
        .setRunId("run_id_in_replay")
1✔
135
        .build();
1✔
136
  }
137

138
  public List<HistoryEvent> getEvents() {
139
    return history.getEventsList();
1✔
140
  }
141

142
  public HistoryEvent getLastEvent() {
143
    int eventsCount = history.getEventsCount();
1✔
144
    return eventsCount > 0 ? history.getEvents(eventsCount - 1) : null;
1!
145
  }
146

147
  public History getHistory() {
148
    return history;
1✔
149
  }
150

151
  @Override
152
  public String toString() {
153
    return "WorkflowExecutionHistory{" + "history=" + history + '}';
1✔
154
  }
155
}
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