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

temporalio / sdk-java / #330

10 Oct 2024 04:35PM CUT coverage: 78.113% (-0.1%) from 78.238%
#330

push

github

web-flow
Test server support for bidi links (#2258)

* Test server support for bidi links

* typo

* license

* feedback

* link validation

* describe fields

* link validation

103 of 163 new or added lines in 3 files covered. (63.19%)

11 existing lines in 4 files now uncovered.

21332 of 27309 relevant lines covered (78.11%)

0.78 hits per line

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

90.32
/temporal-test-server/src/main/java/io/temporal/internal/testservice/LinkConverter.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.testservice;
22

23
import io.temporal.api.common.v1.Link;
24
import io.temporal.api.enums.v1.EventType;
25
import java.net.URI;
26
import java.net.URLDecoder;
27
import java.net.URLEncoder;
28
import java.nio.charset.StandardCharsets;
29
import java.util.StringTokenizer;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

NEW
33
public class LinkConverter {
×
34

35
  private static final Logger log = LoggerFactory.getLogger(StateMachines.class);
1✔
36

37
  private static final String linkPathFormat = "temporal:///namespaces/%s/workflows/%s/%s/history";
38

39
  public static io.temporal.api.nexus.v1.Link workflowEventToNexusLink(Link.WorkflowEvent we) {
40
    try {
41
      String url =
1✔
42
          String.format(
1✔
43
              linkPathFormat,
44
              URLEncoder.encode(we.getNamespace(), StandardCharsets.UTF_8.toString()),
1✔
45
              URLEncoder.encode(we.getWorkflowId(), StandardCharsets.UTF_8.toString()),
1✔
46
              URLEncoder.encode(we.getRunId(), StandardCharsets.UTF_8.toString()));
1✔
47

48
      if (we.hasEventRef()) {
1✔
49
        url += "?";
1✔
50
        if (we.getEventRef().getEventId() > 0) {
1✔
51
          url += "eventID=" + we.getEventRef().getEventId() + "&";
1✔
52
        }
53
        url +=
1✔
54
            "eventType="
55
                + URLEncoder.encode(
1✔
56
                    we.getEventRef().getEventType().name(), StandardCharsets.UTF_8.toString())
1✔
57
                + "&";
58
        url += "referenceType=EventReference";
1✔
59
      }
60

61
      return io.temporal.api.nexus.v1.Link.newBuilder()
1✔
62
          .setUrl(url)
1✔
63
          .setType(we.getDescriptorForType().getFullName())
1✔
64
          .build();
1✔
NEW
65
    } catch (Exception e) {
×
NEW
66
      log.error("Failed to encode Nexus link URL", e);
×
67
    }
NEW
68
    return null;
×
69
  }
70

71
  public static Link nexusLinkToWorkflowEvent(io.temporal.api.nexus.v1.Link nexusLink) {
72
    Link.Builder link = Link.newBuilder();
1✔
73
    try {
74
      URI uri = new URI(nexusLink.getUrl());
1✔
75

76
      if (!uri.getScheme().equals("temporal")) {
1✔
77
        log.error("Failed to parse Nexus link URL: invalid scheme: {}", uri.getScheme());
1✔
78
        return null;
1✔
79
      }
80

81
      StringTokenizer st = new StringTokenizer(uri.getRawPath(), "/");
1✔
82
      if (!st.nextToken().equals("namespaces")) {
1✔
NEW
83
        log.error("Failed to parse Nexus link URL: invalid path: {}", uri.getRawPath());
×
NEW
84
        return null;
×
85
      }
86
      String namespace = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8.toString());
1✔
87
      if (!st.nextToken().equals("workflows")) {
1✔
88
        log.error("Failed to parse Nexus link URL: invalid path: {}", uri.getRawPath());
1✔
89
        return null;
1✔
90
      }
91
      String workflowID = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8.toString());
1✔
92
      String runID = URLDecoder.decode(st.nextToken(), StandardCharsets.UTF_8.toString());
1✔
93
      if (!st.hasMoreTokens() || !st.nextToken().equals("history")) {
1✔
94
        log.error("Failed to parse Nexus link URL: invalid path: {}", uri.getRawPath());
1✔
95
        return null;
1✔
96
      }
97

98
      Link.WorkflowEvent.Builder we =
99
          Link.WorkflowEvent.newBuilder()
1✔
100
              .setNamespace(namespace)
1✔
101
              .setWorkflowId(workflowID)
1✔
102
              .setRunId(runID);
1✔
103

104
      if (uri.getQuery() != null) {
1✔
105
        Link.WorkflowEvent.EventReference.Builder eventRef =
106
            Link.WorkflowEvent.EventReference.newBuilder();
1✔
107
        String query = URLDecoder.decode(uri.getQuery(), StandardCharsets.UTF_8.toString());
1✔
108
        st = new StringTokenizer(query, "&");
1✔
109
        while (st.hasMoreTokens()) {
1✔
110
          String[] param = st.nextToken().split("=");
1✔
111
          switch (param[0]) {
1✔
112
            case "eventID":
113
              eventRef.setEventId(Long.parseLong(param[1]));
1✔
114
              continue;
1✔
115
            case "eventType":
116
              eventRef.setEventType(EventType.valueOf(param[1]));
1✔
117
          }
118
        }
1✔
119
        we.setEventRef(eventRef);
1✔
120
        link.setWorkflowEvent(we);
1✔
121
      }
122
    } catch (Exception e) {
1✔
123
      // Swallow un-parsable links since they are not critical to processing
124
      log.error("Failed to parse Nexus link URL", e);
1✔
125
      return null;
1✔
126
    }
1✔
127
    return link.build();
1✔
128
  }
129
}
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