Skip to content

Commit

Permalink
[Fix apache#3590] Adding $input support (apache#3591)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado authored and rgdoliveira committed Aug 6, 2024
1 parent 9504b50 commit c80081d
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
public class JsonNodeModel implements Model, MapInput, MapInputId, MapOutput, MappableToModel<JsonNodeModelOutput> {

private JsonNode workflowdata;
private JsonNode input;
private String id;
private Map<String, Object> additionalProperties = Collections.emptyMap();

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

public String getId() {
Expand Down Expand Up @@ -122,6 +124,9 @@ public MapInput fromMap(Map<String, Object> params) {
public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put(SWFConstants.DEFAULT_WORKFLOW_VAR, workflowdata);
if (input != null) {
map.put(SWFConstants.INPUT_WORKFLOW_VAR, input);
}
map.putAll(additionalProperties);
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class SWFConstants {

public static final String DEFAULT_WORKFLOW_VAR = "workflowdata";
public static final String INPUT_WORKFLOW_VAR = "workflowdatainput";
public static final String RESULT = "Result";
public static final String MODEL_WORKFLOW_VAR = "Parameter";
public static final String CONTENT_DATA = "ContentData";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.serverless.workflow.utils;

import java.util.Map;
import java.util.function.Function;

import org.kie.kogito.internal.process.runtime.KogitoProcessContext;
import org.kie.kogito.serverless.workflow.SWFConstants;

public class InputKogitoProcessContextResolver implements KogitoProcessContextResolverExtension {

@Override
public Map<String, Function<KogitoProcessContext, Object>> getKogitoProcessContextResolver() {
return Map.of("input", this::getInputVariables);
}

private Object getInputVariables(KogitoProcessContext context) {
return context.getVariable(SWFConstants.INPUT_WORKFLOW_VAR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#

org.kie.kogito.serverless.workflow.utils.InputKogitoProcessContextResolver
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import org.kie.kogito.internal.process.runtime.KogitoProcessContext;
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
import org.kie.kogito.serverless.workflow.SWFConstants;

import com.fasterxml.jackson.databind.JsonNode;

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

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

public KogitoProcessContextMockBuilder withInput(JsonNode input) {
this.input = input;
return this;
}

public KogitoProcessContext build() {
KogitoProcessContext context = mock(KogitoProcessContext.class);
KogitoProcessInstance kogitoProcessInstanceMock = mock(KogitoProcessInstance.class);
Expand All @@ -112,6 +121,7 @@ public KogitoProcessContext build() {
if (constants != null && !constants.isEmpty()) {
when(processMock.getMetaData()).thenReturn(Collections.singletonMap(Metadata.CONSTANTS, ObjectMapperFactory.get().valueToTree(constants)));
}
when(context.getVariable(SWFConstants.INPUT_WORKFLOW_VAR)).thenReturn(input);
return context;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@

import org.junit.jupiter.api.Test;
import org.kie.kogito.internal.process.runtime.KogitoProcessContext;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
import org.kie.kogito.serverless.workflow.test.MockBuilder;

import com.fasterxml.jackson.databind.JsonNode;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.when;

public class KogitoProcessContextResolverTest {

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

@Test
void testGetInput() {
assertThat(KogitoProcessContextResolver.get().readKey(context, "input")).isInstanceOf(JsonNode.class);
}

@Test
void testGetNonExistentKey() {
assertThatIllegalArgumentException().isThrownBy(() -> KogitoProcessContextResolver.get().readKey(context, "nonexistent"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"name": "finish",
"type": "operation",
"stateDataFilter": {
"input": "{result: .number, message: .message}"
"input": "{result: .number, message: .message, originalFirstX: $WORKFLOW.input.numbers[0].x}"
},
"actions": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void testExpressionRest() {
.then()
.statusCode(201)
.body("workflowdata.result", is(4))
.body("workflowdata.originalFirstX", is(2))
.body("workflowdata.number", nullValue())
.body("workflowdata.message", is("my name is javierito and in my native language dog is translated to perro and the header pepe is pepa"))
.body("workflowdata.user", is("anonymous"))
Expand Down

0 comments on commit c80081d

Please sign in to comment.