Skip to content

Commit

Permalink
[Fix apache#2158] Adding support for querying workflow variables (apa…
Browse files Browse the repository at this point in the history
…che#2161)

* [Fix apache#2158] Adding support for querying workflow variables

* [Fix apache#2158] Adding more filter conditions

* [Fix apache#2158] Gonzalos comment

Rolling back unneded pom change

* [Fix apache#2158] Gonzalos comment

Rolling back unneded pom change
  • Loading branch information
fjtirado authored and rgdoliveira committed Dec 17, 2024
1 parent e20ed9c commit f48b511
Show file tree
Hide file tree
Showing 15 changed files with 531 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.kie.kogito.jackson.utils.JsonObjectUtils;
import org.kie.kogito.jackson.utils.MergeUtils;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
import org.kie.kogito.persistence.api.query.AttributeFilter;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -58,4 +59,11 @@ private static ObjectNode createObjectNode(String variableName, Object variableV
}
return result;
}

public static <T> AttributeFilter<T> jsonFilter(AttributeFilter<T> filter) {
if (filter != null) {
filter.setJson(true);
}
return filter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static graphql.schema.GraphQLTypeUtil.unwrapNonNull;
import static graphql.schema.GraphQLTypeUtil.unwrapOne;
import static java.util.stream.Collectors.toList;
import static org.kie.kogito.index.json.JsonUtils.jsonFilter;
import static org.kie.kogito.persistence.api.query.FilterCondition.NOT;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.and;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.between;
Expand Down Expand Up @@ -110,15 +111,65 @@ public GraphQLQueryParser apply(GraphQLInputObjectType type) {
case "KogitoMetadataArgument":
parser.mapAttribute(field.getName(), mapSubEntityArgument(field.getName(), GraphQLQueryParserRegistry.get().getParser("KogitoMetadataArgument")));
break;
case "JSON":
parser.mapAttribute(field.getName(), mapJsonArgument(field.getName()));
break;
default:
parser.mapAttribute(field.getName(), mapSubEntityArgument(field.getName(), new GraphQLQueryMapper().apply((GraphQLInputObjectType) field.getType())));
if (field.getType() instanceof GraphQLInputObjectType) {
parser.mapAttribute(field.getName(), mapSubEntityArgument(field.getName(), new GraphQLQueryMapper().apply((GraphQLInputObjectType) field.getType())));
}
}
}
});

return parser;
}

Function<Object, Stream<AttributeFilter<?>>> mapJsonArgument(String attribute) {
return argument -> ((Map<String, Object>) argument).entrySet().stream().map(e -> mapJsonArgument(attribute, e.getKey(), e.getValue()));
}

private AttributeFilter<?> mapJsonArgument(String attribute, String key, Object value) {
StringBuilder sb = new StringBuilder(attribute);
FilterCondition condition = FilterCondition.fromLabel(key);
while (condition == null && value instanceof Map) {
sb.append('.').append(key);
Map.Entry<String, Object> entry = ((Map<String, Object>) value).entrySet().iterator().next();
key = entry.getKey();
value = entry.getValue();
condition = FilterCondition.fromLabel(key);
}
if (condition != null) {
switch (condition) {
case GT:
return jsonFilter(greaterThan(sb.toString(), value));
case GTE:
return jsonFilter(greaterThanEqual(sb.toString(), value));
case LT:
return jsonFilter(lessThan(sb.toString(), value));
case LTE:
return jsonFilter(lessThanEqual(sb.toString(), value));
case BETWEEN:
return jsonFilter(filterValueMap(value, val -> between(sb.toString(), val.get("from"), val.get("to"))));
case IN:
return jsonFilter(filterValueList(value, val -> in(sb.toString(), val)));
case IS_NULL:
return jsonFilter(Boolean.TRUE.equals(value) ? isNull(sb.toString()) : notNull(sb.toString()));
case CONTAINS:
return jsonFilter(contains(sb.toString(), value));
case LIKE:
return jsonFilter(like(sb.toString(), value.toString()));
case CONTAINS_ALL:
return filterValueList(value, val -> containsAll(sb.toString(), val));
case CONTAINS_ANY:
return filterValueList(value, val -> containsAny(sb.toString(), val));
case EQUAL:
default:
return jsonFilter(equalTo(sb.toString(), value));
}
}
return null;
}

private boolean isListOfType(GraphQLInputType source, String type) {
if (isList(source)) {
return ((GraphQLNamedType) unwrapNonNull(unwrapOne(source))).getName().equals(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ input ProcessInstanceArgument {
id: IdArgument
processId: StringArgument
processName: StringArgument
variables: JSON
parentProcessInstanceId: IdArgument
rootProcessInstanceId: IdArgument
rootProcessId: StringArgument
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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.index.graphql.query;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.kie.kogito.index.json.JsonUtils.jsonFilter;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.*;

public class GraphQLQueryMapperTest {

private GraphQLQueryMapper mapper;

@BeforeEach
void setup() {
mapper = new GraphQLQueryMapper();
}

@Test
void testJsonMapperEqual() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("equal", 1))))).containsExactly(
jsonFilter(equalTo("variables.workflowdata.number", 1)));
}

@Test
void testJsonMapperGreater() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("greaterThan", 1))))).containsExactly(
jsonFilter(greaterThan("variables.workflowdata.number", 1)));
}

@Test
void testJsonMapperLess() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("lessThan", 1))))).containsExactly(
jsonFilter(lessThan("variables.workflowdata.number", 1)));
}

@Test
void testJsonMapperGreaterEqual() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("greaterThanEqual", 1))))).containsExactly(
jsonFilter(greaterThanEqual("variables.workflowdata.number", 1)));
}

@Test
void testJsonMapperLessEqual() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("lessThanEqual", 1))))).containsExactly(
jsonFilter(lessThanEqual("variables.workflowdata.number", 1)));
}

@Test
void testJsonMapperBetween() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("between", Map.of("from", 1, "to", 3)))))).containsExactly(
jsonFilter(between("variables.workflowdata.number", 1, 3)));
}

@Test
void testJsonMapperIn() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("in", List.of(1, 3)))))).containsExactly(
jsonFilter(in("variables.workflowdata.number", Arrays.asList(1, 3))));
}

@Test
void testJsonMapperContains() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("contains", 1))))).containsExactly(
jsonFilter(contains("variables.workflowdata.number", 1)));
}

@Test
void testJsonMapperLike() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("like", "kk"))))).containsExactly(
jsonFilter(like("variables.workflowdata.number", "kk")));
}

@Test
void testJsonMapperNull() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("isNull", true))))).containsExactly(
jsonFilter(isNull("variables.workflowdata.number")));
}

@Test
void testJsonMapperNotNull() {
assertThat(mapper.mapJsonArgument("variables").apply(Map.of("workflowdata", Map.of("number", Map.of("isNull", false))))).containsExactly(
jsonFilter(notNull("variables.workflowdata.number")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public GraphQLSchema createSchema() {
typeDefinitionRegistry.merge(loadSchemaDefinitionFile("domain.schema.graphqls"));

RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.scalar(ExtendedScalars.Json)
.type("Query", builder -> {
builder.dataFetcher("ProcessDefinitions", this::getProcessDefinitionsValues);
builder.dataFetcher("ProcessInstances", this::getProcessInstancesValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public static BiConsumer<List<ObjectNode>, String[]> assertWithObjectNodeInOrder
public static BiConsumer<List<ObjectNode>, String[]> assertWithObjectNode() {
return (instances, ids) -> assertThat(instances).hasSize(ids == null ? 0 : ids.length).extracting(n -> n.get("id").asText()).containsExactlyInAnyOrder(ids);
}

public static <V> BiConsumer<List<V>, String[]> assertNotId() {
return (instances, ids) -> assertThat(instances).extracting("id").doesNotContainAnyElementsOf(List.of(ids));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import org.kie.kogito.jackson.utils.ObjectMapperFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;

import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -83,6 +85,20 @@ public static ProcessInstanceVariableDataEvent createProcessInstanceVariableEven
return event;
}

public static ProcessInstanceVariableDataEvent createProcessInstanceVariableEvent(String processInstance,
String processId, String name, int age, boolean isMartian, List<String> aliases) {
ProcessInstanceVariableDataEvent event = new ProcessInstanceVariableDataEvent();
event.setKogitoProcessId(processId);
event.setKogitoProcessInstanceId(processInstance);
ArrayNode node = ObjectMapperFactory.get().createArrayNode();
aliases.forEach(s -> node.add(new TextNode(s)));
event.setData(ProcessInstanceVariableEventBody.create().processId(processId).processInstanceId(processInstance)
.variableName("traveller").variableValue(ObjectMapperFactory.get().createObjectNode().put("name", name).put("age", age).put("isMartian", isMartian)
.set("aliases", node))
.build());
return event;
}

public static ProcessInstanceNodeDataEvent createProcessInstanceNodeDataEvent(String processInstance, String processId, String nodeDefinitionId, String nodeInstanceId, String nodeName,
String nodeType, int eventType) {

Expand Down
Loading

0 comments on commit f48b511

Please sign in to comment.