Skip to content

Commit 6e3a3be

Browse files
committed
tests and refactoring
Signed-off-by: Dmitrii Tikhomirov <[email protected]>
1 parent 01630a0 commit 6e3a3be

File tree

12 files changed

+399
-112
lines changed

12 files changed

+399
-112
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.executors.openapi;
17+
18+
import io.serverlessworkflow.impl.TaskContext;
19+
import io.serverlessworkflow.impl.WorkflowContext;
20+
import io.serverlessworkflow.impl.WorkflowModel;
21+
import io.serverlessworkflow.impl.WorkflowValueResolver;
22+
import java.net.URI;
23+
24+
class ExpressionURISupplier implements TargetSupplier {
25+
private WorkflowValueResolver<String> resolver;
26+
27+
ExpressionURISupplier(WorkflowValueResolver<String> resolver) {
28+
this.resolver = resolver;
29+
}
30+
31+
@Override
32+
public URI apply(WorkflowContext workflow, TaskContext task, WorkflowModel node) {
33+
return URI.create(resolver.apply(workflow, task, node));
34+
}
35+
}

impl/openapi/src/main/java/io/serverlessworkflow/impl/executors/openapi/HttpCallAdapter.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import java.util.Map;
3737

3838
@SuppressWarnings("rawtypes")
39-
public class HttpCallAdapter {
39+
class HttpCallAdapter {
4040

4141
private ReferenceableAuthenticationPolicy auth;
4242
private Map<String, Schema> body;
@@ -49,19 +49,19 @@ public class HttpCallAdapter {
4949
private URI target;
5050
private Map<String, Object> workflowParams;
5151

52-
public HttpCallAdapter auth(ReferenceableAuthenticationPolicy policy) {
52+
HttpCallAdapter auth(ReferenceableAuthenticationPolicy policy) {
5353
if (policy != null) {
5454
this.auth = policy;
5555
}
5656
return this;
5757
}
5858

59-
public HttpCallAdapter body(Map<String, Schema> body) {
59+
HttpCallAdapter body(Map<String, Schema> body) {
6060
this.body = body;
6161
return this;
6262
}
6363

64-
public CallHTTP build() {
64+
CallHTTP build() {
6565
CallHTTP callHTTP = new CallHTTP();
6666

6767
HTTPArguments httpArgs = new HTTPArguments();
@@ -96,43 +96,42 @@ public CallHTTP build() {
9696
return callHTTP;
9797
}
9898

99-
public HttpCallAdapter contentType(String contentType) {
99+
HttpCallAdapter contentType(String contentType) {
100100
this.contentType = contentType;
101101
return this;
102102
}
103103

104-
public HttpCallAdapter headers(
105-
Collection<io.swagger.v3.oas.models.parameters.Parameter> headers) {
104+
HttpCallAdapter headers(Collection<io.swagger.v3.oas.models.parameters.Parameter> headers) {
106105
this.headers = headers;
107106
return this;
108107
}
109108

110-
public HttpCallAdapter method(String method) {
109+
HttpCallAdapter method(String method) {
111110
this.method = method;
112111
return this;
113112
}
114113

115-
public HttpCallAdapter query(Collection<io.swagger.v3.oas.models.parameters.Parameter> query) {
114+
HttpCallAdapter query(Collection<io.swagger.v3.oas.models.parameters.Parameter> query) {
116115
this.query = query;
117116
return this;
118117
}
119118

120-
public HttpCallAdapter redirect(boolean redirect) {
119+
HttpCallAdapter redirect(boolean redirect) {
121120
this.redirect = redirect;
122121
return this;
123122
}
124123

125-
public HttpCallAdapter server(String server) {
124+
HttpCallAdapter server(String server) {
126125
this.server = URI.create(server);
127126
return this;
128127
}
129128

130-
public HttpCallAdapter target(URI target) {
129+
HttpCallAdapter target(URI target) {
131130
this.target = target;
132131
return this;
133132
}
134133

135-
public HttpCallAdapter workflowParams(Map<String, Object> workflowParams) {
134+
HttpCallAdapter workflowParams(Map<String, Object> workflowParams) {
136135
this.workflowParams = workflowParams;
137136
return this;
138137
}
@@ -194,7 +193,7 @@ private void addQueryParams(HTTPArguments httpArgs) {
194193
} else if (value instanceof Character asCharacter) {
195194
httpQuery.setAdditionalProperty(name, asCharacter.toString());
196195
} else {
197-
throw new IllegalArgumentException("Query parameter " + name + " must be a type of String, Number, Boolean or Character");
196+
httpQuery.setAdditionalProperty(name, value.toString());
198197
}
199198
}
200199
}

impl/openapi/src/main/java/io/serverlessworkflow/impl/executors/openapi/OpenAPIExecutor.java

Lines changed: 55 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@
2525
import io.serverlessworkflow.impl.TaskContext;
2626
import io.serverlessworkflow.impl.WorkflowApplication;
2727
import io.serverlessworkflow.impl.WorkflowContext;
28+
import io.serverlessworkflow.impl.WorkflowException;
2829
import io.serverlessworkflow.impl.WorkflowModel;
29-
import io.serverlessworkflow.impl.WorkflowValueResolver;
3030
import io.serverlessworkflow.impl.executors.CallableTask;
3131
import io.serverlessworkflow.impl.executors.http.HttpExecutor;
3232
import io.serverlessworkflow.impl.expressions.ExpressionDescriptor;
3333
import io.serverlessworkflow.impl.expressions.ExpressionFactory;
3434
import io.serverlessworkflow.impl.resources.ResourceLoader;
3535
import jakarta.ws.rs.core.UriBuilder;
3636
import java.net.URI;
37-
import java.util.Objects;
3837
import java.util.concurrent.CompletableFuture;
3938
import java.util.stream.Collectors;
4039

@@ -47,39 +46,6 @@ public class OpenAPIExecutor implements CallableTask<CallOpenAPI> {
4746

4847
private ResourceLoader resourceLoader;
4948

50-
private static TargetSupplier getTargetSupplier(
51-
Endpoint endpoint, ExpressionFactory expressionFactory) {
52-
if (endpoint.getEndpointConfiguration() != null) {
53-
EndpointUri uri = endpoint.getEndpointConfiguration().getUri();
54-
if (uri.getLiteralEndpointURI() != null) {
55-
return getURISupplier(uri.getLiteralEndpointURI());
56-
} else if (uri.getExpressionEndpointURI() != null) {
57-
return new ExpressionURISupplier(
58-
expressionFactory.resolveString(
59-
ExpressionDescriptor.from(uri.getExpressionEndpointURI())));
60-
}
61-
} else if (endpoint.getRuntimeExpression() != null) {
62-
return new ExpressionURISupplier(
63-
expressionFactory.resolveString(
64-
ExpressionDescriptor.from(endpoint.getRuntimeExpression())));
65-
} else if (endpoint.getUriTemplate() != null) {
66-
return getURISupplier(endpoint.getUriTemplate());
67-
}
68-
throw new IllegalArgumentException("Invalid endpoint definition " + endpoint);
69-
}
70-
71-
private static TargetSupplier getURISupplier(UriTemplate template) {
72-
if (template.getLiteralUri() != null) {
73-
return (w, t, n) -> template.getLiteralUri();
74-
} else if (template.getLiteralUriTemplate() != null) {
75-
return (w, t, n) ->
76-
UriBuilder.fromUri(template.getLiteralUriTemplate())
77-
.resolveTemplates(n.asMap().orElseThrow(), false)
78-
.build();
79-
}
80-
throw new IllegalArgumentException("Invalid uritemplate definition " + template);
81-
}
82-
8349
@Override
8450
public boolean accept(Class<? extends TaskBase> clazz) {
8551
return clazz.equals(CallOpenAPI.class);
@@ -88,37 +54,38 @@ public boolean accept(Class<? extends TaskBase> clazz) {
8854
@Override
8955
public CompletableFuture<WorkflowModel> apply(
9056
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
91-
9257
String operationId = task.getWith().getOperationId();
9358
URI openAPIEndpoint = targetSupplier.apply(workflowContext, taskContext, input);
9459
OpenAPIProcessor processor = new OpenAPIProcessor(operationId, openAPIEndpoint);
9560
OperationDefinition operation = processor.parse();
9661

9762
OperationPathResolver pathResolver =
98-
new OperationPathResolver(operation.getPath(), input.asMap().orElseThrow());
99-
URI resolvedPath = pathResolver.passPathParams().apply(workflowContext, taskContext, input);
100-
101-
HttpCallAdapter httpCallAdapter =
102-
new HttpCallAdapter()
103-
.auth(task.getWith().getAuthentication())
104-
.body(operation.getBody())
105-
.contentType(operation.getContentType())
106-
.headers(
107-
operation.getParameters().stream()
108-
.filter(p -> "header".equals(p.getIn()))
109-
.collect(Collectors.toUnmodifiableSet()))
110-
.method(operation.getMethod())
111-
.query(
112-
operation.getParameters().stream()
113-
.filter(p -> "query".equals(p.getIn()))
114-
.collect(Collectors.toUnmodifiableSet()))
115-
.redirect(task.getWith().isRedirect())
116-
.target(resolvedPath)
117-
.workflowParams(task.getWith().getParameters().getAdditionalProperties());
63+
new OperationPathResolver(
64+
operation.getPath(),
65+
application,
66+
task.getWith().getParameters().getAdditionalProperties());
11867

11968
return CompletableFuture.supplyAsync(
12069
() -> {
121-
RuntimeException ex = null;
70+
HttpCallAdapter httpCallAdapter =
71+
new HttpCallAdapter()
72+
.auth(task.getWith().getAuthentication())
73+
.body(operation.getBody())
74+
.contentType(operation.getContentType())
75+
.headers(
76+
operation.getParameters().stream()
77+
.filter(p -> "header".equals(p.getIn()))
78+
.collect(Collectors.toUnmodifiableSet()))
79+
.method(operation.getMethod())
80+
.query(
81+
operation.getParameters().stream()
82+
.filter(p -> "query".equals(p.getIn()))
83+
.collect(Collectors.toUnmodifiableSet()))
84+
.redirect(task.getWith().isRedirect())
85+
.target(pathResolver.resolve(workflowContext, taskContext, input))
86+
.workflowParams(task.getWith().getParameters().getAdditionalProperties());
87+
88+
WorkflowException workflowException = null;
12289

12390
for (var server : operation.getServers()) {
12491
CallHTTP callHTTP = httpCallAdapter.server(server).build();
@@ -127,14 +94,13 @@ public CompletableFuture<WorkflowModel> apply(
12794

12895
try {
12996
return executor.apply(workflowContext, taskContext, input).get();
97+
} catch (WorkflowException e) {
98+
workflowException = e;
13099
} catch (Exception e) {
131-
132-
System.out.println("Call to " + server + " failed: " + e.getMessage());
133-
ex = new RuntimeException(e);
100+
throw new RuntimeException(e);
134101
}
135102
}
136-
Objects.requireNonNull(ex, "Should have at least one exception");
137-
throw ex; // if we there, we failed all servers and ex is not null
103+
throw workflowException; // if we there, we failed all servers and ex is not null
138104
},
139105
workflowContext.definition().application().executorService());
140106
}
@@ -154,20 +120,35 @@ public void init(
154120
this.resourceLoader = resourceLoader;
155121
}
156122

157-
public interface TargetSupplier {
158-
URI apply(WorkflowContext workflow, TaskContext taskContext, WorkflowModel input);
159-
}
160-
161-
private static class ExpressionURISupplier implements TargetSupplier {
162-
private WorkflowValueResolver<String> expr;
163-
164-
public ExpressionURISupplier(WorkflowValueResolver<String> expr) {
165-
this.expr = expr;
123+
private TargetSupplier getTargetSupplier(Endpoint endpoint, ExpressionFactory expressionFactory) {
124+
if (endpoint.getEndpointConfiguration() != null) {
125+
EndpointUri uri = endpoint.getEndpointConfiguration().getUri();
126+
if (uri.getLiteralEndpointURI() != null) {
127+
return getURISupplier(uri.getLiteralEndpointURI());
128+
} else if (uri.getExpressionEndpointURI() != null) {
129+
return new ExpressionURISupplier(
130+
expressionFactory.resolveString(
131+
ExpressionDescriptor.from(uri.getExpressionEndpointURI())));
132+
}
133+
} else if (endpoint.getRuntimeExpression() != null) {
134+
return new ExpressionURISupplier(
135+
expressionFactory.resolveString(
136+
ExpressionDescriptor.from(endpoint.getRuntimeExpression())));
137+
} else if (endpoint.getUriTemplate() != null) {
138+
return getURISupplier(endpoint.getUriTemplate());
166139
}
140+
throw new IllegalArgumentException("Invalid endpoint definition " + endpoint);
141+
}
167142

168-
@Override
169-
public URI apply(WorkflowContext workflow, TaskContext task, WorkflowModel node) {
170-
return URI.create(expr.apply(workflow, task, node));
143+
private TargetSupplier getURISupplier(UriTemplate template) {
144+
if (template.getLiteralUri() != null) {
145+
return (w, t, n) -> template.getLiteralUri();
146+
} else if (template.getLiteralUriTemplate() != null) {
147+
return (w, t, n) ->
148+
UriBuilder.fromUri(template.getLiteralUriTemplate())
149+
.resolveTemplates(n.asMap().orElseThrow(), false)
150+
.build();
171151
}
152+
throw new IllegalArgumentException("Invalid uri template definition " + template);
172153
}
173154
}

impl/openapi/src/main/java/io/serverlessworkflow/impl/executors/openapi/OpenAPIProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
import java.util.List;
2525
import java.util.Set;
2626

27-
public class OpenAPIProcessor {
27+
class OpenAPIProcessor {
2828

2929
private final String operationId;
3030
private final URI openAPIEndpoint;
3131

32-
public OpenAPIProcessor(String operationId, URI openAPIEndpoint) {
32+
OpenAPIProcessor(String operationId, URI openAPIEndpoint) {
3333
this.operationId = operationId;
3434
this.openAPIEndpoint = openAPIEndpoint;
3535
}
3636

37-
public OperationDefinition parse() {
37+
OperationDefinition parse() {
3838
OpenAPIV3Parser parser = new OpenAPIV3Parser();
3939
ParseOptions opts = new ParseOptions();
4040
opts.setResolve(true);
@@ -45,7 +45,7 @@ public OperationDefinition parse() {
4545
return getOperation(openapi);
4646
}
4747

48-
public OperationDefinition getOperation(OpenAPI openAPI) {
48+
OperationDefinition getOperation(OpenAPI openAPI) {
4949
if (openAPI == null || openAPI.getPaths() == null) {
5050
throw new IllegalArgumentException("Invalid OpenAPI document");
5151
}

0 commit comments

Comments
 (0)