Skip to content

Commit 2c598b1

Browse files
committed
[WIP] add support for externalResource
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent 0dc641d commit 2c598b1

File tree

4 files changed

+125
-50
lines changed

4 files changed

+125
-50
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/executors/RunScriptExecutor.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
import java.util.Arrays;
3434
import java.util.HashMap;
3535
import java.util.Map;
36+
import java.util.Objects;
3637
import java.util.concurrent.CompletableFuture;
3738
import java.util.concurrent.ExecutorService;
39+
40+
import io.serverlessworkflow.impl.resources.ResourceLoaderUtils;
3841
import org.graalvm.polyglot.Context;
3942
import org.graalvm.polyglot.PolyglotException;
4043
import org.graalvm.polyglot.Value;
@@ -92,6 +95,19 @@ public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
9295

9396
fnExecutor =
9497
(workflowContext, taskContext) -> {
98+
99+
String source;
100+
if (scriptUnion.getInlineScript() != null) {
101+
source = scriptUnion.getInlineScript().getCode();
102+
} else if (scriptUnion.getExternalScript() == null) {
103+
throw new WorkflowException(WorkflowError.runtime(taskContext, new IllegalStateException(
104+
"No script source defined."
105+
)).build());
106+
} else {
107+
source = definition.resourceLoader().load(scriptUnion.getExternalScript().getSource(),
108+
ResourceLoaderUtils::readString, workflowContext, taskContext, taskContext.input());
109+
}
110+
95111
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
96112
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
97113

@@ -156,14 +172,14 @@ public void init(RunScript taskConfiguration, WorkflowDefinition definition) {
156172
() -> {
157173
context.eval(
158174
scriptUnion.getInlineScript().getLanguage(),
159-
scriptUnion.getInlineScript().getCode());
175+
source);
160176
});
161177
return application.modelFactory().fromAny(taskContext.input());
162178
}
163179

164180
context.eval(
165-
scriptUnion.getInlineScript().getLanguage(),
166-
scriptUnion.getInlineScript().getCode());
181+
lowerLang,
182+
source);
167183

168184
WorkflowModelFactory modelFactory = application.modelFactory();
169185

impl/test/src/test/java/io/serverlessworkflow/impl/test/RunShellScriptTest.java

Lines changed: 92 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,59 +19,104 @@
1919
import io.serverlessworkflow.api.types.Workflow;
2020
import io.serverlessworkflow.impl.WorkflowApplication;
2121
import io.serverlessworkflow.impl.WorkflowModel;
22-
import java.io.IOException;
23-
import java.util.Map;
22+
import okhttp3.mockwebserver.MockResponse;
23+
import okhttp3.mockwebserver.MockWebServer;
24+
import okio.Buffer;
2425
import org.assertj.core.api.SoftAssertions;
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.BeforeEach;
2528
import org.junit.jupiter.api.Test;
2629

30+
import java.io.IOException;
31+
import java.util.Map;
32+
2733
public class RunShellScriptTest {
2834

29-
@Test
30-
void testConsoleLog() throws IOException {
31-
Workflow workflow =
32-
WorkflowReader.readWorkflowFromClasspath("workflows-samples/run-script/console-log.yaml");
33-
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
34-
WorkflowModel model = appl.workflowDefinition(workflow).instance(Map.of()).start().join();
35-
36-
SoftAssertions.assertSoftly(
37-
softly -> {
38-
softly.assertThat(model.asText()).isPresent();
39-
softly.assertThat(model.asText().get()).isEqualTo("hello from script");
40-
});
35+
private MockWebServer fileServer;
36+
37+
@BeforeEach
38+
void setUp() throws IOException {
39+
fileServer = new MockWebServer();
40+
fileServer.start(8886);
41+
}
42+
43+
@AfterEach
44+
void tearDown() throws IOException {
45+
fileServer.shutdown();
46+
}
47+
48+
@Test
49+
void testConsoleLog() throws IOException {
50+
Workflow workflow =
51+
WorkflowReader.readWorkflowFromClasspath("workflows-samples/run-script/console-log.yaml");
52+
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
53+
WorkflowModel model = appl.workflowDefinition(workflow).instance(Map.of()).start().join();
54+
55+
SoftAssertions.assertSoftly(
56+
softly -> {
57+
softly.assertThat(model.asText()).isPresent();
58+
softly.assertThat(model.asText().get()).isEqualTo("hello from script");
59+
});
60+
}
4161
}
42-
}
43-
44-
@Test
45-
void testConsoleLogWithArgs() throws IOException {
46-
Workflow workflow =
47-
WorkflowReader.readWorkflowFromClasspath(
48-
"workflows-samples/run-script/console-log-args.yaml");
49-
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
50-
WorkflowModel model = appl.workflowDefinition(workflow).instance(Map.of()).start().join();
51-
52-
SoftAssertions.assertSoftly(
53-
softly -> {
54-
softly.assertThat(model.asText()).isPresent();
55-
softly.assertThat(model.asText().get()).isEqualTo("Hello, world!");
56-
});
62+
63+
@Test
64+
void testConsoleLogWithArgs() throws IOException {
65+
Workflow workflow =
66+
WorkflowReader.readWorkflowFromClasspath(
67+
"workflows-samples/run-script/console-log-args.yaml");
68+
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
69+
WorkflowModel model = appl.workflowDefinition(workflow).instance(Map.of()).start().join();
70+
71+
SoftAssertions.assertSoftly(
72+
softly -> {
73+
softly.assertThat(model.asText()).isPresent();
74+
softly.assertThat(model.asText().get()).isEqualTo("Hello, world!");
75+
});
76+
}
77+
}
78+
79+
@Test
80+
void testConsoleLogWithEnvs() throws IOException {
81+
Workflow workflow =
82+
WorkflowReader.readWorkflowFromClasspath(
83+
"workflows-samples/run-script/console-log-envs.yaml");
84+
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
85+
WorkflowModel model = appl.workflowDefinition(workflow).instance(Map.of()).start().join();
86+
87+
SoftAssertions.assertSoftly(
88+
softly -> {
89+
softly.assertThat(model.asText()).isPresent();
90+
softly
91+
.assertThat(model.asText().get())
92+
.isEqualTo("Running JavaScript code using Serverless Workflow!");
93+
});
94+
}
5795
}
58-
}
59-
60-
@Test
61-
void testConsoleLogWithEnvs() throws IOException {
62-
Workflow workflow =
63-
WorkflowReader.readWorkflowFromClasspath(
64-
"workflows-samples/run-script/console-log-envs.yaml");
65-
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
66-
WorkflowModel model = appl.workflowDefinition(workflow).instance(Map.of()).start().join();
67-
68-
SoftAssertions.assertSoftly(
69-
softly -> {
70-
softly.assertThat(model.asText()).isPresent();
71-
softly
72-
.assertThat(model.asText().get())
73-
.isEqualTo("Running JavaScript code using Serverless Workflow!");
74-
});
96+
97+
@Test
98+
void testConsoleLogWithExternalSource() throws IOException {
99+
Workflow workflow =
100+
WorkflowReader.readWorkflowFromClasspath(
101+
"workflows-samples/run-script/console-log-external-source.yaml");
102+
103+
fileServer.enqueue(new MockResponse().setBody("""
104+
console.log("hello from script");
105+
""")
106+
.setHeader("Content-Type", "application/javascript")
107+
.setResponseCode(200)
108+
);
109+
110+
try (WorkflowApplication appl = WorkflowApplication.builder().build()) {
111+
WorkflowModel model = appl.workflowDefinition(workflow).instance(Map.of()).start().join();
112+
113+
SoftAssertions.assertSoftly(
114+
softly -> {
115+
softly.assertThat(model.asText()).isPresent();
116+
softly
117+
.assertThat(model.asText().get())
118+
.isEqualTo("hello from script");
119+
});
120+
}
75121
}
76-
}
77122
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
document:
2+
dsl: '1.0.2'
3+
namespace: test
4+
name: run-script-example
5+
version: '0.1.0'
6+
do:
7+
- runScript:
8+
run:
9+
script:
10+
language: js
11+
source:
12+
endpoint:
13+
uri: http://localhost:8886/script.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello from script");

0 commit comments

Comments
 (0)