Skip to content

Commit f3b5083

Browse files
Fix #730 - Introduce static imports for the spec DSL (#758)
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent 4eb9f75 commit f3b5083

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2207
-116
lines changed

fluent/spec/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
<artifactId>junit-jupiter-api</artifactId>
2828
<scope>test</scope>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.assertj</groupId>
32+
<artifactId>assertj-core</artifactId>
33+
<scope>test</scope>
34+
</dependency>
3035
</dependencies>
3136

3237
</project>

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/AbstractEventConsumptionStrategyBuilder.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,22 @@ public SELF one(Consumer<F> c) {
5858

5959
protected abstract void setOne(OneEventConsumptionStrategy strategy);
6060

61-
public SELF all(Consumer<F> c) {
61+
@SafeVarargs
62+
public final SELF all(Consumer<F>... consumers) {
6263
ensureNoneSet();
6364
allSet = true;
64-
F fb = this.newEventFilterBuilder();
65-
c.accept(fb);
65+
66+
List<EventFilter> built = new ArrayList<>(consumers.length);
67+
68+
for (Consumer<? super F> c : consumers) {
69+
Objects.requireNonNull(c, "consumer");
70+
F fb = this.newEventFilterBuilder();
71+
c.accept(fb);
72+
built.add(fb.build());
73+
}
74+
6675
AllEventConsumptionStrategy strat = new AllEventConsumptionStrategy();
67-
strat.setAll(List.of(fb.build()));
76+
strat.setAll(built);
6877
this.setAll(strat);
6978
return this.self();
7079
}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/CallHTTPTaskBuilder.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
import io.serverlessworkflow.api.types.CallHTTP;
1919
import io.serverlessworkflow.api.types.Endpoint;
20+
import io.serverlessworkflow.api.types.EndpointConfiguration;
2021
import io.serverlessworkflow.api.types.HTTPArguments;
2122
import io.serverlessworkflow.api.types.HTTPHeaders;
2223
import io.serverlessworkflow.api.types.HTTPQuery;
2324
import io.serverlessworkflow.api.types.Headers;
2425
import io.serverlessworkflow.api.types.Query;
26+
import io.serverlessworkflow.api.types.ReferenceableAuthenticationPolicy;
2527
import io.serverlessworkflow.api.types.UriTemplate;
2628
import java.net.URI;
2729
import java.util.Map;
@@ -55,12 +57,44 @@ public CallHTTPTaskBuilder endpoint(URI endpoint) {
5557
return this;
5658
}
5759

60+
public CallHTTPTaskBuilder endpoint(
61+
URI endpoint, Consumer<AuthenticationPolicyUnionBuilder> auth) {
62+
final AuthenticationPolicyUnionBuilder policy = new AuthenticationPolicyUnionBuilder();
63+
auth.accept(policy);
64+
this.callHTTP
65+
.getWith()
66+
.setEndpoint(
67+
new Endpoint()
68+
.withEndpointConfiguration(
69+
new EndpointConfiguration()
70+
.withAuthentication(
71+
new ReferenceableAuthenticationPolicy()
72+
.withAuthenticationPolicy(policy.build())))
73+
.withUriTemplate(new UriTemplate().withLiteralUri(endpoint)));
74+
return this;
75+
}
76+
5877
public CallHTTPTaskBuilder endpoint(String expr) {
5978
this.callHTTP.getWith().setEndpoint(new Endpoint().withRuntimeExpression(expr));
6079
return this;
6180
}
6281

63-
// TODO: add endpoint configuration to support authentication
82+
public CallHTTPTaskBuilder endpoint(
83+
String expr, Consumer<AuthenticationPolicyUnionBuilder> auth) {
84+
final AuthenticationPolicyUnionBuilder policy = new AuthenticationPolicyUnionBuilder();
85+
auth.accept(policy);
86+
this.callHTTP
87+
.getWith()
88+
.setEndpoint(
89+
new Endpoint()
90+
.withEndpointConfiguration(
91+
new EndpointConfiguration()
92+
.withAuthentication(
93+
new ReferenceableAuthenticationPolicy()
94+
.withAuthenticationPolicy(policy.build())))
95+
.withRuntimeExpression(expr));
96+
return this;
97+
}
6498

6599
public CallHTTPTaskBuilder headers(String expr) {
66100
this.callHTTP.getWith().setHeaders(new Headers().withRuntimeExpression(expr));
@@ -70,7 +104,18 @@ public CallHTTPTaskBuilder headers(String expr) {
70104
public CallHTTPTaskBuilder headers(Consumer<HTTPHeadersBuilder> consumer) {
71105
HTTPHeadersBuilder hb = new HTTPHeadersBuilder();
72106
consumer.accept(hb);
73-
callHTTP.getWith().setHeaders(hb.build());
107+
if (callHTTP.getWith().getHeaders() != null
108+
&& callHTTP.getWith().getHeaders().getHTTPHeaders() != null) {
109+
Headers h = callHTTP.getWith().getHeaders();
110+
Headers built = hb.build();
111+
built
112+
.getHTTPHeaders()
113+
.getAdditionalProperties()
114+
.forEach((k, v) -> h.getHTTPHeaders().setAdditionalProperty(k, v));
115+
} else {
116+
callHTTP.getWith().setHeaders(hb.build());
117+
}
118+
74119
return this;
75120
}
76121

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/SetTaskBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public SetTaskBuilder expr(String expression) {
4040
return this;
4141
}
4242

43-
public SetTaskBuilder put(String key, String value) {
43+
public SetTaskBuilder put(String key, Object value) {
4444
setTaskConfiguration.withAdditionalProperty(key, value);
4545
return this;
4646
}

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/TryTaskBuilder.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ public TryTaskBuilder<T> tryHandler(Consumer<T> consumer) {
5757
return this;
5858
}
5959

60-
public TryTaskBuilder<T> catchHandler(Consumer<TryTaskCatchBuilder> consumer) {
61-
final TryTaskCatchBuilder catchBuilder = new TryTaskCatchBuilder();
60+
public TryTaskBuilder<T> catchHandler(Consumer<TryTaskCatchBuilder<T>> consumer) {
61+
final TryTaskCatchBuilder<T> catchBuilder =
62+
new TryTaskCatchBuilder<>(this.doTaskBuilderFactory);
6263
consumer.accept(catchBuilder);
6364
this.tryTask.setCatch(catchBuilder.build());
6465
return this;
@@ -68,42 +69,51 @@ public TryTask build() {
6869
return tryTask;
6970
}
7071

71-
public static final class TryTaskCatchBuilder {
72+
public static final class TryTaskCatchBuilder<T extends BaseTaskItemListBuilder<T>> {
7273
private final TryTaskCatch tryTaskCatch;
74+
private final T doTaskBuilderFactory;
7375

74-
TryTaskCatchBuilder() {
76+
TryTaskCatchBuilder(T doTaskBuilderFactory) {
77+
this.doTaskBuilderFactory = doTaskBuilderFactory;
7578
this.tryTaskCatch = new TryTaskCatch();
7679
}
7780

78-
public TryTaskCatchBuilder as(final String as) {
81+
public TryTaskCatchBuilder<T> as(final String as) {
7982
this.tryTaskCatch.setAs(as);
8083
return this;
8184
}
8285

83-
public TryTaskCatchBuilder when(final String when) {
86+
public TryTaskCatchBuilder<T> when(final String when) {
8487
this.tryTaskCatch.setWhen(when);
8588
return this;
8689
}
8790

88-
public TryTaskCatchBuilder exceptWhen(final String exceptWhen) {
91+
public TryTaskCatchBuilder<T> exceptWhen(final String exceptWhen) {
8992
this.tryTaskCatch.setExceptWhen(exceptWhen);
9093
return this;
9194
}
9295

93-
public TryTaskCatchBuilder retry(Consumer<RetryPolicyBuilder> consumer) {
96+
public TryTaskCatchBuilder<T> retry(Consumer<RetryPolicyBuilder> consumer) {
9497
final RetryPolicyBuilder retryPolicyBuilder = new RetryPolicyBuilder();
9598
consumer.accept(retryPolicyBuilder);
9699
this.tryTaskCatch.setRetry(new Retry().withRetryPolicyDefinition(retryPolicyBuilder.build()));
97100
return this;
98101
}
99102

100-
public TryTaskCatchBuilder errorsWith(Consumer<CatchErrorsBuilder> consumer) {
103+
public TryTaskCatchBuilder<T> errorsWith(Consumer<CatchErrorsBuilder> consumer) {
101104
final CatchErrorsBuilder catchErrorsBuilder = new CatchErrorsBuilder();
102105
consumer.accept(catchErrorsBuilder);
103106
this.tryTaskCatch.setErrors(catchErrorsBuilder.build());
104107
return this;
105108
}
106109

110+
public TryTaskCatchBuilder<T> doTasks(Consumer<T> consumer) {
111+
final T taskItemListBuilder = this.doTaskBuilderFactory.newItemListBuilder();
112+
consumer.accept(taskItemListBuilder);
113+
this.tryTaskCatch.setDo(taskItemListBuilder.build());
114+
return this;
115+
}
116+
107117
public TryTaskCatch build() {
108118
return tryTaskCatch;
109119
}
@@ -153,30 +163,30 @@ public static final class RetryPolicyJitterBuilder {
153163
this.retryPolicyJitter = new RetryPolicyJitter();
154164
}
155165

156-
public RetryPolicyJitter to(Consumer<DurationInlineBuilder> consumer) {
166+
public RetryPolicyJitterBuilder to(Consumer<DurationInlineBuilder> consumer) {
157167
final DurationInlineBuilder durationInlineBuilder = new DurationInlineBuilder();
158168
consumer.accept(durationInlineBuilder);
159169
this.retryPolicyJitter.setTo(
160170
new TimeoutAfter().withDurationInline(durationInlineBuilder.build()));
161-
return retryPolicyJitter;
171+
return this;
162172
}
163173

164-
public RetryPolicyJitter to(String expression) {
174+
public RetryPolicyJitterBuilder to(String expression) {
165175
this.retryPolicyJitter.setTo(new TimeoutAfter().withDurationExpression(expression));
166-
return retryPolicyJitter;
176+
return this;
167177
}
168178

169-
public RetryPolicyJitter from(Consumer<DurationInlineBuilder> consumer) {
179+
public RetryPolicyJitterBuilder from(Consumer<DurationInlineBuilder> consumer) {
170180
final DurationInlineBuilder durationInlineBuilder = new DurationInlineBuilder();
171181
consumer.accept(durationInlineBuilder);
172182
this.retryPolicyJitter.setFrom(
173183
new TimeoutAfter().withDurationInline(durationInlineBuilder.build()));
174-
return retryPolicyJitter;
184+
return this;
175185
}
176186

177-
public RetryPolicyJitter from(String expression) {
187+
public RetryPolicyJitterBuilder from(String expression) {
178188
this.retryPolicyJitter.setFrom(new TimeoutAfter().withDurationExpression(expression));
179-
return retryPolicyJitter;
189+
return this;
180190
}
181191

182192
public RetryPolicyJitter build() {

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/WorkflowBuilderConsumers.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.fluent.spec.configurers;
17+
18+
import io.serverlessworkflow.fluent.spec.AuthenticationPolicyUnionBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface AuthenticationConfigurer extends Consumer<AuthenticationPolicyUnionBuilder> {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.fluent.spec.configurers;
17+
18+
import io.serverlessworkflow.fluent.spec.CallHTTPTaskBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface CallHTTPConfigurer extends Consumer<CallHTTPTaskBuilder> {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.fluent.spec.configurers;
17+
18+
import io.serverlessworkflow.fluent.spec.EmitTaskBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface EmitConfigurer extends Consumer<EmitTaskBuilder> {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.fluent.spec.configurers;
17+
18+
import io.serverlessworkflow.fluent.spec.EventPropertiesBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface EventConfigurer extends Consumer<EventPropertiesBuilder> {}

0 commit comments

Comments
 (0)