|
| 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.test; |
| 17 | + |
| 18 | +import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.http; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.SoftAssertions.assertSoftly; |
| 21 | + |
| 22 | +import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder; |
| 23 | +import io.serverlessworkflow.impl.WorkflowApplication; |
| 24 | +import io.serverlessworkflow.impl.WorkflowInstance; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import mockwebserver3.MockResponse; |
| 29 | +import mockwebserver3.MockWebServer; |
| 30 | +import mockwebserver3.RecordedRequest; |
| 31 | +import okhttp3.Headers; |
| 32 | +import org.junit.jupiter.api.AfterEach; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.api.DisplayName; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +public class FuncHttpTest { |
| 38 | + |
| 39 | + private WorkflowApplication app; |
| 40 | + private MockWebServer mockServer; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setup() throws IOException { |
| 44 | + app = WorkflowApplication.builder().build(); |
| 45 | + mockServer = new MockWebServer(); |
| 46 | + mockServer.start(0); |
| 47 | + mockServer.enqueue(new MockResponse(204, Headers.of("Content-Type", "application/json"), "")); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void cleanup() { |
| 52 | + mockServer.close(); |
| 53 | + app.close(); |
| 54 | + } |
| 55 | + |
| 56 | + private RecordedRequest takeRequestOrFail() throws Exception { |
| 57 | + RecordedRequest request = mockServer.takeRequest(150, TimeUnit.MILLISECONDS); |
| 58 | + assertThat(request) |
| 59 | + .as("Expected an HTTP request to be received by MockWebServer within 300 ms") |
| 60 | + .isNotNull(); |
| 61 | + return request; |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("Query method with single key-value pair") |
| 66 | + void test_query_with_single_key_value() throws Exception { |
| 67 | + var workflow = |
| 68 | + FuncWorkflowBuilder.workflow("test-query-single") |
| 69 | + .tasks( |
| 70 | + http("callHttp") |
| 71 | + .GET() |
| 72 | + .uri(mockServer.url("/api/endpoint").toString()) |
| 73 | + .query("param1", "value1")) |
| 74 | + .build(); |
| 75 | + |
| 76 | + WorkflowInstance instance = app.workflowDefinition(workflow).instance(Map.of()); |
| 77 | + |
| 78 | + instance.start().join(); |
| 79 | + |
| 80 | + RecordedRequest request = takeRequestOrFail(); |
| 81 | + |
| 82 | + assertSoftly( |
| 83 | + softly -> { |
| 84 | + softly.assertThat(request.getUrl().toString()).contains("param1=value1"); |
| 85 | + softly.assertThat(request.getMethod()).isEqualTo("GET"); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @DisplayName("Query method with multiple single key-value pairs (individually tested)") |
| 91 | + void test_query_with_multiple_single_values() throws Exception { |
| 92 | + var workflow = |
| 93 | + FuncWorkflowBuilder.workflow("test-query-single-multi") |
| 94 | + .tasks( |
| 95 | + http("callHttp") |
| 96 | + .GET() |
| 97 | + .uri(mockServer.url("/api/endpoint").toString()) |
| 98 | + .query("param1", "value1") |
| 99 | + .query("param2", "value2") |
| 100 | + .query("param3", "value3")) |
| 101 | + .build(); |
| 102 | + |
| 103 | + WorkflowInstance instance = app.workflowDefinition(workflow).instance(Map.of()); |
| 104 | + instance.start().join(); |
| 105 | + |
| 106 | + RecordedRequest request = takeRequestOrFail(); |
| 107 | + String url = request.getUrl().toString(); |
| 108 | + |
| 109 | + assertSoftly( |
| 110 | + softly -> { |
| 111 | + softly.assertThat(url).contains("param1=value1").isNotEmpty(); |
| 112 | + softly.assertThat(url).contains("param2=value2").isNotEmpty(); |
| 113 | + softly.assertThat(url).contains("param3=value3").isNotEmpty(); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @DisplayName("Query method with Map of parameters") |
| 119 | + void test_query_with_map() throws Exception { |
| 120 | + |
| 121 | + var workflow = |
| 122 | + FuncWorkflowBuilder.workflow("test-query-map") |
| 123 | + .tasks( |
| 124 | + http("callHttp") |
| 125 | + .GET() |
| 126 | + .uri(mockServer.url("/api/endpoint").toString()) |
| 127 | + .query(Map.of("userId", "123", "userName", "john", "status", "active"))) |
| 128 | + .build(); |
| 129 | + |
| 130 | + WorkflowInstance instance = app.workflowDefinition(workflow).instance(Map.of()); |
| 131 | + instance.start().join(); |
| 132 | + |
| 133 | + RecordedRequest request = takeRequestOrFail(); |
| 134 | + String url = request.getUrl().toString(); |
| 135 | + |
| 136 | + assertSoftly( |
| 137 | + softly -> { |
| 138 | + softly.assertThat(url).contains("userId=123"); |
| 139 | + softly.assertThat(url).contains("userName=john"); |
| 140 | + softly.assertThat(url).contains("status=active"); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + @DisplayName("Query method with expression string") |
| 146 | + void test_query_with_expression() throws Exception { |
| 147 | + var workflow = |
| 148 | + FuncWorkflowBuilder.workflow("test-query-expression") |
| 149 | + .tasks( |
| 150 | + http("callHttp") |
| 151 | + .GET() |
| 152 | + .uri(mockServer.url("/api/endpoint").toString()) |
| 153 | + .query("enabled", "${ .enabled }")) |
| 154 | + .build(); |
| 155 | + |
| 156 | + WorkflowInstance instance = app.workflowDefinition(workflow).instance(Map.of("enabled", true)); |
| 157 | + instance.start().join(); |
| 158 | + |
| 159 | + RecordedRequest request = takeRequestOrFail(); |
| 160 | + |
| 161 | + assertThat(request.getUrl().query()).contains("enabled=true"); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + @DisplayName("Query method with empty Map") |
| 166 | + void test_query_with_empty_map() throws Exception { |
| 167 | + var workflow = |
| 168 | + FuncWorkflowBuilder.workflow("test-query-empty-map") |
| 169 | + .tasks( |
| 170 | + http("callHttp") |
| 171 | + .GET() |
| 172 | + .uri(mockServer.url("/api/endpoint").toString()) |
| 173 | + .query(Map.of())) |
| 174 | + .build(); |
| 175 | + |
| 176 | + WorkflowInstance instance = app.workflowDefinition(workflow).instance(Map.of()); |
| 177 | + instance.start().join(); |
| 178 | + |
| 179 | + RecordedRequest request = takeRequestOrFail(); |
| 180 | + |
| 181 | + assertSoftly( |
| 182 | + softly -> { |
| 183 | + softly.assertThat(request.getUrl().encodedPath()).isEqualTo("/api/endpoint"); |
| 184 | + softly.assertThat(request.getUrl().encodedQuery()).isNull(); |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + @DisplayName("Query method with special characters in values") |
| 190 | + void test_query_with_special_characters() throws Exception { |
| 191 | + var workflow = |
| 192 | + FuncWorkflowBuilder.workflow("test-query-special-chars") |
| 193 | + .tasks( |
| 194 | + http("callHttp") |
| 195 | + .GET() |
| 196 | + .uri(mockServer.url("/api/endpoint").toString()) |
| 197 | + .query("email", "user@example.com")) |
| 198 | + .build(); |
| 199 | + |
| 200 | + WorkflowInstance instance = app.workflowDefinition(workflow).instance(Map.of()); |
| 201 | + instance.start().join(); |
| 202 | + |
| 203 | + RecordedRequest request = takeRequestOrFail(); |
| 204 | + |
| 205 | + assertSoftly( |
| 206 | + softly -> { |
| 207 | + softly.assertThat(request.getUrl().queryParameter("email")).isEqualTo("user@example.com"); |
| 208 | + softly.assertThat(request.getUrl().encodedQuery()).contains("email=user%40example.com"); |
| 209 | + }); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + @DisplayName("Query method overload - Map with multiple values") |
| 214 | + void test_query_map_multiple_values() throws Exception { |
| 215 | + var workflow = |
| 216 | + FuncWorkflowBuilder.workflow("test-query-map-multi") |
| 217 | + .tasks( |
| 218 | + http("callHttp") |
| 219 | + .GET() |
| 220 | + .uri(mockServer.url("/api/endpoint").toString()) |
| 221 | + .query(Map.of("limit", "50", "offset", "0", "sort", "name"))) |
| 222 | + .build(); |
| 223 | + |
| 224 | + WorkflowInstance instance = app.workflowDefinition(workflow).instance(Map.of()); |
| 225 | + instance.start().join(); |
| 226 | + |
| 227 | + RecordedRequest request = takeRequestOrFail(); |
| 228 | + String url = request.getUrl().toString(); |
| 229 | + |
| 230 | + assertSoftly( |
| 231 | + softly -> { |
| 232 | + softly.assertThat(url).contains("limit=50"); |
| 233 | + softly.assertThat(url).contains("offset=0"); |
| 234 | + softly.assertThat(url).contains("sort=name"); |
| 235 | + }); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + @DisplayName("Query method with headers and query parameters") |
| 240 | + void test_query_with_headers_and_query() throws Exception { |
| 241 | + var workflow = |
| 242 | + FuncWorkflowBuilder.workflow("test-query-with-headers") |
| 243 | + .tasks( |
| 244 | + http("callHttp") |
| 245 | + .GET() |
| 246 | + .uri(mockServer.url("/api/endpoint").toString()) |
| 247 | + .header("Authorization", "Bearer token123") |
| 248 | + .header("Accept", "application/json") |
| 249 | + .query("userId", "123")) |
| 250 | + .build(); |
| 251 | + |
| 252 | + WorkflowInstance instance = app.workflowDefinition(workflow).instance(Map.of()); |
| 253 | + instance.start().join(); |
| 254 | + |
| 255 | + RecordedRequest request = takeRequestOrFail(); |
| 256 | + String url = request.getUrl().toString(); |
| 257 | + assertThat(url).contains("userId=123"); |
| 258 | + } |
| 259 | +} |
0 commit comments