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 19, 2024
1 parent e85f688 commit 0088cea
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 14 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,6 +21,7 @@
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;
Expand All @@ -36,6 +37,7 @@

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

public class OutputGraphQLMutationProvider implements GraphQLMutationsProvider {

Expand Down Expand Up @@ -76,4 +78,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(completeProcessId: 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 @@ -122,9 +122,9 @@ void testProcessExecuteInstance() {
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) + "})}\"}");
"," + 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 0088cea

Please sign in to comment.