|
| 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.dsl; |
| 17 | + |
| 18 | +import static io.serverlessworkflow.fluent.spec.dsl.DSL.call; |
| 19 | +import static io.serverlessworkflow.fluent.spec.dsl.DSL.http; |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +import io.serverlessworkflow.api.types.CallHTTP; |
| 23 | +import io.serverlessworkflow.api.types.HTTPArguments; |
| 24 | +import io.serverlessworkflow.fluent.spec.WorkflowBuilder; |
| 25 | +import java.util.Map; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +class CallHttpDslExtensionsTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + void when_put_method_shortcut() { |
| 32 | + var wf = |
| 33 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 34 | + .tasks(call(http().PUT().uri("http://test.com"))) |
| 35 | + .build(); |
| 36 | + CallHTTP call = wf.getDo().get(0).getTask().getCallTask().getCallHTTP(); |
| 37 | + assertThat(call.getWith().getMethod()).isEqualTo("PUT"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void when_delete_method_shortcut() { |
| 42 | + var wf = |
| 43 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 44 | + .tasks(call(http().DELETE().uri("http://test.com"))) |
| 45 | + .build(); |
| 46 | + CallHTTP call = wf.getDo().get(0).getTask().getCallTask().getCallHTTP(); |
| 47 | + assertThat(call.getWith().getMethod()).isEqualTo("DELETE"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void when_patch_method_shortcut() { |
| 52 | + var wf = |
| 53 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 54 | + .tasks(call(http().PATCH().uri("http://test.com"))) |
| 55 | + .build(); |
| 56 | + CallHTTP call = wf.getDo().get(0).getTask().getCallTask().getCallHTTP(); |
| 57 | + assertThat(call.getWith().getMethod()).isEqualTo("PATCH"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void when_head_method_shortcut() { |
| 62 | + var wf = |
| 63 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 64 | + .tasks(call(http().HEAD().uri("http://test.com"))) |
| 65 | + .build(); |
| 66 | + CallHTTP call = wf.getDo().get(0).getTask().getCallTask().getCallHTTP(); |
| 67 | + assertThat(call.getWith().getMethod()).isEqualTo("HEAD"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void when_options_method_shortcut() { |
| 72 | + var wf = |
| 73 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 74 | + .tasks(call(http().OPTIONS().uri("http://test.com"))) |
| 75 | + .build(); |
| 76 | + CallHTTP call = wf.getDo().get(0).getTask().getCallTask().getCallHTTP(); |
| 77 | + assertThat(call.getWith().getMethod()).isEqualTo("OPTIONS"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void when_redirect_true() { |
| 82 | + var wf = |
| 83 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 84 | + .tasks(call(http().redirect(true).POST().uri("http://test.com"))) |
| 85 | + .build(); |
| 86 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 87 | + assertThat(args.isRedirect()).isTrue(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void when_redirect_false() { |
| 92 | + var wf = |
| 93 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 94 | + .tasks(call(http().redirect(false).POST().uri("http://test.com"))) |
| 95 | + .build(); |
| 96 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 97 | + assertThat(args.isRedirect()).isFalse(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void when_acceptXML() { |
| 102 | + var wf = |
| 103 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 104 | + .tasks(call(http().acceptXML().POST().uri("http://test.com"))) |
| 105 | + .build(); |
| 106 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 107 | + assertThat(args.getHeaders().getHTTPHeaders().getAdditionalProperties().get("Accept")) |
| 108 | + .isEqualTo("application/xml"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void when_acceptForm() { |
| 113 | + var wf = |
| 114 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 115 | + .tasks(call(http().acceptForm().POST().uri("http://test.com"))) |
| 116 | + .build(); |
| 117 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 118 | + assertThat(args.getHeaders().getHTTPHeaders().getAdditionalProperties().get("Accept")) |
| 119 | + .isEqualTo("application/x-www-form-urlencoded"); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void when_acceptText() { |
| 124 | + var wf = |
| 125 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 126 | + .tasks(call(http().acceptText().POST().uri("http://test.com"))) |
| 127 | + .build(); |
| 128 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 129 | + assertThat(args.getHeaders().getHTTPHeaders().getAdditionalProperties().get("Accept")) |
| 130 | + .isEqualTo("text/plain"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void when_contentTypeXML() { |
| 135 | + var wf = |
| 136 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 137 | + .tasks(call(http().contentTypeXML().POST().uri("http://test.com"))) |
| 138 | + .build(); |
| 139 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 140 | + assertThat(args.getHeaders().getHTTPHeaders().getAdditionalProperties().get("Content-Type")) |
| 141 | + .isEqualTo("application/xml"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void when_contentTypeForm() { |
| 146 | + var wf = |
| 147 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 148 | + .tasks(call(http().contentTypeForm().POST().uri("http://test.com"))) |
| 149 | + .build(); |
| 150 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 151 | + assertThat(args.getHeaders().getHTTPHeaders().getAdditionalProperties().get("Content-Type")) |
| 152 | + .isEqualTo("application/x-www-form-urlencoded"); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void when_contentTypeText() { |
| 157 | + var wf = |
| 158 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 159 | + .tasks(call(http().contentTypeText().POST().uri("http://test.com"))) |
| 160 | + .build(); |
| 161 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 162 | + assertThat(args.getHeaders().getHTTPHeaders().getAdditionalProperties().get("Content-Type")) |
| 163 | + .isEqualTo("text/plain"); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + void when_andThen_adds_query() { |
| 168 | + var wf = |
| 169 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 170 | + .tasks( |
| 171 | + call( |
| 172 | + http() |
| 173 | + .GET() |
| 174 | + .uri("http://test.com") |
| 175 | + .andThen(b -> b.query(Map.of("key", "value"))))) |
| 176 | + .build(); |
| 177 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 178 | + assertThat(args.getQuery().getHTTPQuery().getAdditionalProperties().get("key")) |
| 179 | + .isEqualTo("value"); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void when_andThen_adds_body() { |
| 184 | + var wf = |
| 185 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 186 | + .tasks( |
| 187 | + call( |
| 188 | + http() |
| 189 | + .POST() |
| 190 | + .uri("http://test.com") |
| 191 | + .andThen(b -> b.body(Map.of("foo", "bar"))))) |
| 192 | + .build(); |
| 193 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 194 | + assertThat(args.getBody()).isInstanceOf(Map.class); |
| 195 | + @SuppressWarnings("unchecked") |
| 196 | + Map<String, Object> body = (Map<String, Object>) args.getBody(); |
| 197 | + assertThat(body).containsEntry("foo", "bar"); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + void when_andThen_chained_multiple_times() { |
| 202 | + var wf = |
| 203 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 204 | + .tasks( |
| 205 | + call( |
| 206 | + http() |
| 207 | + .GET() |
| 208 | + .uri("http://test.com") |
| 209 | + .andThen(b -> b.redirect(false)) |
| 210 | + .andThen(b -> b.query(Map.of("q", "1"))) |
| 211 | + .andThen(b -> b.body(Map.of("key", "value"))))) |
| 212 | + .build(); |
| 213 | + HTTPArguments args = wf.getDo().get(0).getTask().getCallTask().getCallHTTP().getWith(); |
| 214 | + assertThat(args.isRedirect()).isFalse(); |
| 215 | + assertThat(args.getQuery().getHTTPQuery().getAdditionalProperties().get("q")).isEqualTo("1"); |
| 216 | + assertThat(args.getBody()).isInstanceOf(Map.class); |
| 217 | + @SuppressWarnings("unchecked") |
| 218 | + Map<String, Object> body = (Map<String, Object>) args.getBody(); |
| 219 | + assertThat(body).containsEntry("key", "value"); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + void when_outputAs_with_expression() { |
| 224 | + var wf = |
| 225 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 226 | + .tasks( |
| 227 | + call(http().POST().uri("http://test.com").andThen(b -> b.outputAs("$.firstName")))) |
| 228 | + .build(); |
| 229 | + // output.as is set on the CallHTTP task itself |
| 230 | + CallHTTP call = wf.getDo().get(0).getTask().getCallTask().getCallHTTP(); |
| 231 | + assertThat(call.getOutput().getAs().getString()).isEqualTo("$.firstName"); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + void when_http_call_with_all_features() { |
| 236 | + var wf = |
| 237 | + WorkflowBuilder.workflow("test", "ns", "1") |
| 238 | + .tasks( |
| 239 | + call( |
| 240 | + "myTask", |
| 241 | + http() |
| 242 | + .contentTypeJSON() |
| 243 | + .acceptJSON() |
| 244 | + .redirect(true) |
| 245 | + .POST() |
| 246 | + .uri("http://localhost:9876/api/v1/authors") |
| 247 | + .body(Map.of("firstName", "John", "lastName", "Doe")) |
| 248 | + .query(Map.of("sort", "asc")) |
| 249 | + .header("X-Custom", "value") |
| 250 | + .andThen(b -> b.outputAs("$.id")))) |
| 251 | + .build(); |
| 252 | + |
| 253 | + var taskItem = wf.getDo().get(0); |
| 254 | + assertThat(taskItem.getName()).isEqualTo("myTask"); |
| 255 | + |
| 256 | + CallHTTP call = taskItem.getTask().getCallTask().getCallHTTP(); |
| 257 | + HTTPArguments args = call.getWith(); |
| 258 | + |
| 259 | + assertThat(args.getMethod()).isEqualTo("POST"); |
| 260 | + assertThat(args.isRedirect()).isTrue(); |
| 261 | + assertThat(args.getBody()).isInstanceOf(Map.class); |
| 262 | + @SuppressWarnings("unchecked") |
| 263 | + Map<String, Object> body = (Map<String, Object>) args.getBody(); |
| 264 | + assertThat(body).containsEntry("firstName", "John").containsEntry("lastName", "Doe"); |
| 265 | + assertThat(args.getQuery().getHTTPQuery().getAdditionalProperties().get("sort")) |
| 266 | + .isEqualTo("asc"); |
| 267 | + assertThat(args.getHeaders().getHTTPHeaders().getAdditionalProperties().get("Content-Type")) |
| 268 | + .isEqualTo("application/json"); |
| 269 | + assertThat(args.getHeaders().getHTTPHeaders().getAdditionalProperties().get("Accept")) |
| 270 | + .isEqualTo("application/json"); |
| 271 | + assertThat(args.getHeaders().getHTTPHeaders().getAdditionalProperties().get("X-Custom")) |
| 272 | + .isEqualTo("value"); |
| 273 | + |
| 274 | + assertThat(call.getOutput().getAs().getString()).isEqualTo("$.id"); |
| 275 | + } |
| 276 | +} |
0 commit comments