Skip to content

Commit b8ea6a4

Browse files
committed
[Fix #3590] Adding $input support
1 parent 8613c8e commit b8ea6a4

File tree

7 files changed

+79
-1
lines changed

7 files changed

+79
-1
lines changed

kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/models/JsonNodeModel.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
public class JsonNodeModel implements Model, MapInput, MapInputId, MapOutput, MappableToModel<JsonNodeModelOutput> {
4040

4141
private JsonNode workflowdata;
42+
private JsonNode input;
4243
private String id;
4344
private Map<String, Object> additionalProperties = Collections.emptyMap();
4445

@@ -57,6 +58,7 @@ public JsonNodeModel(String id, Object workflowdata) {
5758
ObjectMapper mapper = ObjectMapperFactory.listenerAware();
5859
this.workflowdata = workflowdata == null ? mapper.createObjectNode() : mapper.convertValue(workflowdata, JsonNode.class);
5960
}
61+
this.input = this.workflowdata.deepCopy();
6062
}
6163

6264
public String getId() {
@@ -122,6 +124,7 @@ public MapInput fromMap(Map<String, Object> params) {
122124
public Map<String, Object> toMap() {
123125
Map<String, Object> map = new HashMap<>();
124126
map.put(SWFConstants.DEFAULT_WORKFLOW_VAR, workflowdata);
127+
map.put("input", input);
125128
map.putAll(additionalProperties);
126129
return map;
127130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.kie.kogito.serverless.workflow.utils;
20+
21+
import java.util.Map;
22+
import java.util.function.Function;
23+
24+
import org.kie.kogito.internal.process.runtime.KogitoProcessContext;
25+
26+
public class InputKogitoProcessContextResolver implements KogitoProcessContextResolverExtension {
27+
28+
@Override
29+
public Map<String, Function<KogitoProcessContext, Object>> getKogitoProcessContextResolver() {
30+
return Map.of("input", this::getInputVariables);
31+
}
32+
33+
private Object getInputVariables(KogitoProcessContext context) {
34+
return context.getVariable("input");
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
org.kie.kogito.serverless.workflow.utils.InputKogitoProcessContextResolver

kogito-serverless-workflow/kogito-serverless-workflow-utils/src/test/java/org/kie/kogito/serverless/workflow/test/MockBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
3333
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
3434

35+
import com.fasterxml.jackson.databind.JsonNode;
36+
3537
import io.serverlessworkflow.api.Workflow;
3638
import io.serverlessworkflow.api.functions.FunctionDefinition;
3739
import io.serverlessworkflow.api.workflow.Constants;
@@ -89,6 +91,7 @@ public Workflow build() {
8991
public static class KogitoProcessContextMockBuilder {
9092
private Consumer<KogitoProcessInstance> processInstanceMockManipulation;
9193
private Map<String, Object> constants = new HashMap<>();
94+
private JsonNode input;
9295

9396
public KogitoProcessContextMockBuilder withProcessInstanceMock(Consumer<KogitoProcessInstance> processInstanceMockManipulation) {
9497
this.processInstanceMockManipulation = processInstanceMockManipulation;
@@ -100,6 +103,11 @@ public KogitoProcessContextMockBuilder withConstants(Map<String, Object> constan
100103
return this;
101104
}
102105

106+
public KogitoProcessContextMockBuilder withInput(JsonNode input) {
107+
this.input = input;
108+
return this;
109+
}
110+
103111
public KogitoProcessContext build() {
104112
KogitoProcessContext context = mock(KogitoProcessContext.class);
105113
KogitoProcessInstance kogitoProcessInstanceMock = mock(KogitoProcessInstance.class);
@@ -112,6 +120,7 @@ public KogitoProcessContext build() {
112120
if (constants != null && !constants.isEmpty()) {
113121
when(processMock.getMetaData()).thenReturn(Collections.singletonMap(Metadata.CONSTANTS, ObjectMapperFactory.get().valueToTree(constants)));
114122
}
123+
when(context.getVariable("input")).thenReturn(input);
115124
return context;
116125
}
117126
}

kogito-serverless-workflow/kogito-serverless-workflow-utils/src/test/java/org/kie/kogito/serverless/workflow/utils/KogitoProcessContextResolverTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@
2020

2121
import org.junit.jupiter.api.Test;
2222
import org.kie.kogito.internal.process.runtime.KogitoProcessContext;
23+
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
2324
import org.kie.kogito.serverless.workflow.test.MockBuilder;
2425

26+
import com.fasterxml.jackson.databind.JsonNode;
27+
2528
import static org.assertj.core.api.Assertions.assertThat;
2629
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2730
import static org.mockito.Mockito.when;
2831

2932
public class KogitoProcessContextResolverTest {
3033

3134
KogitoProcessContext context = MockBuilder.kogitoProcessContext()
35+
.withInput(ObjectMapperFactory.get().createObjectNode().put("pepe", "pepe"))
3236
.withProcessInstanceMock(it -> {
3337
when(it.getId()).thenReturn("value-id");
3438
when(it.getProcessId()).thenReturn("value-process-id");
@@ -50,6 +54,11 @@ void testGetName() {
5054
assertThat(KogitoProcessContextResolver.get().readKey(context, "name")).isEqualTo("value-name");
5155
}
5256

57+
@Test
58+
void testGetInput() {
59+
assertThat(KogitoProcessContextResolver.get().readKey(context, "input")).isInstanceOf(JsonNode.class);
60+
}
61+
5362
@Test
5463
void testGetNonExistentKey() {
5564
assertThatIllegalArgumentException().isThrownBy(() -> KogitoProcessContextResolver.get().readKey(context, "nonexistent"));

quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/expression.sw.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"name": "finish",
8686
"type": "operation",
8787
"stateDataFilter": {
88-
"input": "{result: .number, message: .message}"
88+
"input": "{result: .number, message: .message, originalFirstX: $WORKFLOW.input.numbers[0].x}"
8989
},
9090
"actions": [
9191
{

quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ExpressionRestIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void testExpressionRest() {
4343
.then()
4444
.statusCode(201)
4545
.body("workflowdata.result", is(4))
46+
.body("workflowdata.originalFirstX", is(2))
4647
.body("workflowdata.number", nullValue())
4748
.body("workflowdata.message", is("my name is javierito and in my native language dog is translated to perro and the header pepe is pepa"))
4849
.body("workflowdata.user", is("anonymous"))

0 commit comments

Comments
 (0)