-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KOGITO-8410 Added support to GET method to Knative custom function
Signed-off-by: Helber Belmiro <[email protected]>
- Loading branch information
Showing
8 changed files
with
307 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...us/addons/knative/serving/integration-tests/src/main/resources/getKnativeFunction.sw.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"id": "getKnativeFunction", | ||
"version": "1.0", | ||
"name": "Test Knative function", | ||
"description": "This workflow tests a Knative function", | ||
"start": "invokeFunction", | ||
"functions": [ | ||
{ | ||
"name": "greet", | ||
"type": "custom", | ||
"operation": "knative:services.v1.serving.knative.dev/default/serverless-workflow-greeting-quarkus?path=/plainJsonFunction&method=GET" | ||
} | ||
], | ||
"states": [ | ||
{ | ||
"name": "invokeFunction", | ||
"type": "operation", | ||
"actions": [ | ||
{ | ||
"functionRef": { | ||
"refName": "greet", | ||
"arguments": { | ||
"name": ".name" | ||
} | ||
} | ||
} | ||
], | ||
"end": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...gito/addons/quarkus/knative/serving/customfunctions/GetRequestKnativeParamsDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.kie.kogito.addons.quarkus.knative.serving.customfunctions; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Map; | ||
|
||
import org.kie.kogito.internal.process.runtime.KogitoWorkItem; | ||
import org.kogito.workitem.rest.decorators.ParamsDecorator; | ||
|
||
import io.vertx.mutiny.ext.web.client.HttpRequest; | ||
|
||
public final class GetRequestKnativeParamsDecorator implements ParamsDecorator { | ||
|
||
@Override | ||
public void decorate(KogitoWorkItem workItem, Map<String, Object> parameters, HttpRequest<?> request) { | ||
KnativeFunctionPayloadSupplier.getPayload(parameters).forEach((key, value) -> { | ||
if (value instanceof String) { | ||
request.addQueryParam(key, (String) value); | ||
} else { | ||
String message = "Knative functions support only GET requests with String attributes. {0} has a {1} value."; | ||
throw new IllegalArgumentException(MessageFormat.format(message, key, value.getClass())); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
.../addons/quarkus/knative/serving/customfunctions/GetRequestKnativeParamsDecoratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.kie.kogito.addons.quarkus.knative.serving.customfunctions; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.mutiny.core.buffer.Buffer; | ||
import io.vertx.mutiny.ext.web.client.HttpRequest; | ||
import io.vertx.mutiny.ext.web.client.WebClient; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler.PAYLOAD_FIELDS_DELIMITER; | ||
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler.PAYLOAD_FIELDS_PROPERTY_NAME; | ||
|
||
@QuarkusTest | ||
class GetRequestKnativeParamsDecoratorTest { | ||
|
||
@Inject | ||
WebClient webClient; | ||
|
||
final GetRequestKnativeParamsDecorator decorator = new GetRequestKnativeParamsDecorator(); | ||
|
||
@Test | ||
void decorate() { | ||
Map<String, String> expectedParams = Map.of( | ||
"key1", "value1", | ||
"key2", "value2"); | ||
|
||
HttpRequest<?> request = createRequest(); | ||
|
||
HashMap<String, Object> parameters = new HashMap<>(expectedParams); | ||
parameters.put(PAYLOAD_FIELDS_PROPERTY_NAME, String.join(PAYLOAD_FIELDS_DELIMITER, expectedParams.keySet())); | ||
|
||
decorator.decorate(null, parameters, request); | ||
|
||
assertThat(request.queryParams()).hasSize(2); | ||
expectedParams.forEach((k, v) -> assertThat(request.queryParams().get(k)).isEqualTo(v)); | ||
} | ||
|
||
@Test | ||
void decorateNonStringValuesShouldThrowException() { | ||
Map<String, Object> expectedParams = Map.of( | ||
"key1", "value1", | ||
"key2", new Object()); | ||
|
||
HttpRequest<?> request = createRequest(); | ||
|
||
HashMap<String, Object> parameters = new HashMap<>(expectedParams); | ||
parameters.put(PAYLOAD_FIELDS_PROPERTY_NAME, String.join(PAYLOAD_FIELDS_DELIMITER, expectedParams.keySet())); | ||
|
||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> decorator.decorate(null, parameters, request)); | ||
} | ||
|
||
private HttpRequest<Buffer> createRequest() { | ||
return webClient.request(HttpMethod.GET, 8080, "localhost", "/path"); | ||
} | ||
} |
Oops, something went wrong.