Skip to content

Commit

Permalink
[Fix #2165] Adding IT test
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Dec 20, 2024
1 parent e85f688 commit 8c13cca
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
*/
package org.kie.kogito.index;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;

public class CommonUtils {

public static final int ERROR_STATE = 5;
Expand Down Expand Up @@ -51,6 +57,16 @@ public static String getServiceUrl(String endpoint, String processId) {
}
}

public static TypeDefinitionRegistry loadSchemaDefinitionFile(String fileName) {
SchemaParser schemaParser = new SchemaParser();
try (InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
InputStreamReader reader = new InputStreamReader(stream)) {
return schemaParser.parse(reader);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static String getContext(String processId) {
return processId != null && processId.contains(".") ? processId.substring(processId.lastIndexOf('.') + 1) : processId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/
package org.kie.kogito.index.graphql;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -51,7 +48,6 @@
import graphql.schema.GraphQLNamedType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import graphql.schema.idl.TypeRuntimeWiring.Builder;
import jakarta.annotation.PostConstruct;
Expand Down Expand Up @@ -83,8 +79,11 @@ public abstract class AbstractGraphQLSchemaManager implements GraphQLSchemaManag

private GraphQLSchema schema;

private Collection<GraphQLMutationsProvider> mutations;

@PostConstruct
public void setup() {
mutations = ServiceLoader.load(GraphQLMutationsProvider.class).stream().map(Provider::get).collect(Collectors.toList());
schema = createSchema();
GraphQLQueryParserRegistry.get().registerParsers(
(GraphQLInputObjectType) schema.getType("ProcessDefinitionArgument"),
Expand All @@ -94,18 +93,16 @@ public void setup() {
}

protected final void loadAdditionalMutations(Builder builder) {
ServiceLoader.load(GraphQLMutationsProvider.class).stream().map(Provider::get).map(m -> m.mutations(this)).flatMap(map -> map.entrySet().stream())
mutations.stream().map(m -> m.mutations(this)).flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2)).forEach(builder::dataFetcher);
}

protected final void loadAdditionalMutations(TypeDefinitionRegistry typeRegistry) {
mutations.stream().map(GraphQLMutationsProvider::registry).forEach(typeRegistry::merge);
}

protected TypeDefinitionRegistry loadSchemaDefinitionFile(String fileName) {
SchemaParser schemaParser = new SchemaParser();
try (InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
InputStreamReader reader = new InputStreamReader(stream)) {
return schemaParser.parse(reader);
} catch (IOException e) {
throw new RuntimeException(e);
}
return CommonUtils.loadSchemaDefinitionFile(fileName);
}

public abstract GraphQLSchema createSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.util.concurrent.CompletableFuture;

import graphql.schema.DataFetcher;
import graphql.schema.idl.TypeDefinitionRegistry;

public interface GraphQLMutationsProvider {
Map<String, DataFetcher<CompletableFuture<?>>> mutations(AbstractGraphQLSchemaManager schemaManager);

TypeDefinitionRegistry registry();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.kie.kogito.index.CommonUtils;
import org.kie.kogito.index.api.ExecuteArgs;
import org.kie.kogito.index.graphql.AbstractGraphQLSchemaManager;
import org.kie.kogito.index.graphql.GraphQLMutationsProvider;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessDefinitionKey;
import org.kie.kogito.index.model.ProcessInstance;
import org.kie.kogito.index.storage.DataIndexStorageService;
import org.kie.kogito.jackson.utils.JsonObjectUtils;
import org.kie.kogito.jackson.utils.MergeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -36,6 +38,7 @@

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.idl.TypeDefinitionRegistry;

public class OutputGraphQLMutationProvider implements GraphQLMutationsProvider {

Expand All @@ -54,7 +57,7 @@ private CompletableFuture<String> sharedOutput(AbstractGraphQLSchemaManager sche
if (processDefinition == null) {
throw new IllegalArgumentException(key + "does not correspond to any existing process definition");
}
JsonNode input = env.getArgument("input");
JsonNode input = JsonObjectUtils.fromValue(env.getArgument("input"));
String completedInstanceId = env.getArgument(COMPLETED_INSTANCE_ID);
if (completedInstanceId != null) {
ProcessInstance processInstance = cacheService.getProcessInstanceStorage().get(completedInstanceId);
Expand All @@ -76,4 +79,9 @@ private static <T> T mandatoryArgument(DataFetchingEnvironment env, String name)
}
return result;
}

@Override
public TypeDefinitionRegistry registry() {
return CommonUtils.loadSchemaDefinitionFile("mutation.schema.graphqls");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extend type Mutation {
ExecuteAfter(completedInstanceId: String, processId: String, processVersion: String, input: JSON): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public GraphQLSchema createSchema() {
TypeDefinitionRegistry typeDefinitionRegistry = new TypeDefinitionRegistry();
typeDefinitionRegistry.merge(loadSchemaDefinitionFile("basic.schema.graphqls"));
typeDefinitionRegistry.merge(loadSchemaDefinitionFile("domain.schema.graphqls"));
loadAdditionalMutations(typeDefinitionRegistry);

RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.scalar(ExtendedScalars.Json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ void testProcessExecuteInstance() {
final String infraProcessId = "infra";
ProcessDefinitionDataEvent definitionEvent = TestUtils.getProcessDefinitionDataEvent(infraProcessId);
indexProcessCloudEvent(definitionEvent);
checkOkResponse("{ \"query\" : \"mutation{ ExecuteAfter ( " + fragment("completedProcessId", assesmentInstanceId) + "," + fragment("processId", infraProcessId) +
"," + fragment("processVersion", TestUtils.PROCESS_VERSION) + "," + "input: {" + fragment(assesmentVarName, assesmentVarValue) + "})}\"}");
checkOkResponse("{ \"query\" : \"mutation{ ExecuteAfter ( " + fragment("completedInstanceId", assesmentInstanceId) + "," + fragment("processId", infraProcessId) +
"," + fragment("processVersion", TestUtils.PROCESS_VERSION) + "," + "input: {" + fragment(infraVarName, infraVarValue) + "})}\"}");
verify(dataIndexApiClient).executeProcessIntance(getProcessDefinition(infraProcessId), ExecuteArgs.of(ObjectMapperFactory.get().createObjectNode().put(assesmentVarName, assesmentVarValue)
.put(infraVarName,infraVarValue)));
.put(infraVarName, infraVarValue)));
}

private String fragment(String name, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class GraphQLAddonSchemaManagerImpl extends AbstractGraphQLSchemaManager
public GraphQLSchema createSchema() {
TypeDefinitionRegistry typeDefinitionRegistry = new TypeDefinitionRegistry();
typeDefinitionRegistry.merge(loadSchemaDefinitionFile("basic.schema.graphqls"));
loadAdditionalMutations(typeDefinitionRegistry);

RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", builder -> {
Expand Down

0 comments on commit 8c13cca

Please sign in to comment.