Skip to content

Commit ebe88fa

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

File tree

8 files changed

+82
-1
lines changed

8 files changed

+82
-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(SWFConstants.INPUT_WORKFLOW_VAR, input);
125128
map.putAll(additionalProperties);
126129
return map;
127130
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
public class SWFConstants {
2222

2323
public static final String DEFAULT_WORKFLOW_VAR = "workflowdata";
24+
public static final String INPUT_WORKFLOW_VAR = "workflowdatainput";
2425
public static final String RESULT = "Result";
2526
public static final String MODEL_WORKFLOW_VAR = "Parameter";
2627
public static final String CONTENT_DATA = "ContentData";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
import org.kie.kogito.serverless.workflow.SWFConstants;
26+
27+
public class InputKogitoProcessContextResolver implements KogitoProcessContextResolverExtension {
28+
29+
@Override
30+
public Map<String, Function<KogitoProcessContext, Object>> getKogitoProcessContextResolver() {
31+
return Map.of("input", this::getInputVariables);
32+
}
33+
34+
private Object getInputVariables(KogitoProcessContext context) {
35+
return context.getVariable(SWFConstants.INPUT_WORKFLOW_VAR);
36+
}
37+
}
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import org.kie.kogito.internal.process.runtime.KogitoProcessContext;
3232
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
3333
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
34+
import org.kie.kogito.serverless.workflow.SWFConstants;
35+
36+
import com.fasterxml.jackson.databind.JsonNode;
3437

3538
import io.serverlessworkflow.api.Workflow;
3639
import io.serverlessworkflow.api.functions.FunctionDefinition;
@@ -89,6 +92,7 @@ public Workflow build() {
8992
public static class KogitoProcessContextMockBuilder {
9093
private Consumer<KogitoProcessInstance> processInstanceMockManipulation;
9194
private Map<String, Object> constants = new HashMap<>();
95+
private JsonNode input;
9296

9397
public KogitoProcessContextMockBuilder withProcessInstanceMock(Consumer<KogitoProcessInstance> processInstanceMockManipulation) {
9498
this.processInstanceMockManipulation = processInstanceMockManipulation;
@@ -100,6 +104,11 @@ public KogitoProcessContextMockBuilder withConstants(Map<String, Object> constan
100104
return this;
101105
}
102106

107+
public KogitoProcessContextMockBuilder withInput(JsonNode input) {
108+
this.input = input;
109+
return this;
110+
}
111+
103112
public KogitoProcessContext build() {
104113
KogitoProcessContext context = mock(KogitoProcessContext.class);
105114
KogitoProcessInstance kogitoProcessInstanceMock = mock(KogitoProcessInstance.class);
@@ -112,6 +121,7 @@ public KogitoProcessContext build() {
112121
if (constants != null && !constants.isEmpty()) {
113122
when(processMock.getMetaData()).thenReturn(Collections.singletonMap(Metadata.CONSTANTS, ObjectMapperFactory.get().valueToTree(constants)));
114123
}
124+
when(context.getVariable(SWFConstants.INPUT_WORKFLOW_VAR)).thenReturn(input);
115125
return context;
116126
}
117127
}

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)