|
19 | 19 | import io.serverlessworkflow.api.types.Workflow; |
20 | 20 | import io.serverlessworkflow.impl.WorkflowApplication; |
21 | 21 | 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; |
24 | 25 | import org.assertj.core.api.SoftAssertions; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
25 | 28 | import org.junit.jupiter.api.Test; |
26 | 29 |
|
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Map; |
| 32 | + |
27 | 33 | public class RunShellScriptTest { |
28 | 34 |
|
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 | + } |
41 | 61 | } |
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 | + } |
57 | 95 | } |
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 | + } |
75 | 121 | } |
76 | | - } |
77 | 122 | } |
0 commit comments