Skip to content

Commit

Permalink
[Fix #2165] Adding unit test for executeAfter post call
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Dec 20, 2024
1 parent 9af8228 commit 106f98f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kie.kogito.index.api.ExecuteArgs;
import org.kie.kogito.index.api.KogitoRuntimeCommonClient;
import org.kie.kogito.index.model.Job;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessInstance;
import org.kie.kogito.index.model.UserTaskInstance;
import org.kie.kogito.index.service.DataIndexServiceException;
import org.kie.kogito.index.test.TestUtils;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
import org.kie.kogito.usertask.model.CommentInfo;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.fasterxml.jackson.databind.JsonNode;

import io.quarkus.security.credential.TokenCredential;
import io.quarkus.security.identity.SecurityIdentity;
import io.vertx.core.AsyncResult;
Expand Down Expand Up @@ -117,6 +122,20 @@ public void setup() {
client.setIdentity(identityMock);
}

@Test
public void textExecuteAfter() {
when(webClientMock.post(anyString())).thenReturn(httpRequestMock);
final String processId = "infra";
ProcessDefinition definition = TestUtils.getProcessDefinition(processId);
definition.setEndpoint(SERVICE_URL + "/" + processId);
JsonNode body = ObjectMapperFactory.get().createObjectNode().put("name", "javierito");
client.executeProcessIntance(definition, ExecuteArgs.of(body));
verify(webClientMock).post("/" + processId);
ArgumentCaptor<Object> jsonCaptor = ArgumentCaptor.forClass(Object.class);
verify(httpRequestMock).sendJson(jsonCaptor.capture(), any());
assertThat(jsonCaptor.getValue()).isEqualTo(body);
}

@Test
public void testAbortProcessInstance() {
setupIdentityMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.kie.kogito.index.api.ExecuteArgs;
import org.kie.kogito.index.api.KogitoRuntimeClient;
import org.kie.kogito.index.event.KogitoJobCloudEvent;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.UserTaskInstance;
import org.kie.kogito.index.service.AbstractIndexingIT;
import org.kie.kogito.index.service.graphql.GraphQLSchemaManagerImpl;
Expand Down Expand Up @@ -123,21 +122,15 @@ void testProcessExecuteInstance() {
indexProcessCloudEvent(definitionEvent);
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)));
verify(dataIndexApiClient).executeProcessIntance(TestUtils.getProcessDefinition(infraProcessId),
ExecuteArgs.of(ObjectMapperFactory.get().createObjectNode().put(assesmentVarName, assesmentVarValue)
.put(infraVarName, infraVarValue)));
}

private String fragment(String name, String value) {
return name + ": \\\"" + value + "\\\"";
}

private ProcessDefinition getProcessDefinition(String processId) {
ProcessDefinition def = new ProcessDefinition();
def.setId(processId);
def.setVersion(TestUtils.PROCESS_VERSION);
return def;
}

@Test
void testProcessInstanceRetry() {
String processInstanceId = UUID.randomUUID().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.kie.kogito.index.model.Milestone;
import org.kie.kogito.index.model.MilestoneStatus;
import org.kie.kogito.index.model.NodeInstance;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessInstance;
import org.kie.kogito.index.model.ProcessInstanceError;
import org.kie.kogito.index.model.ProcessInstanceState;
Expand Down Expand Up @@ -121,6 +122,13 @@ public static ProcessDefinitionDataEvent getProcessDefinitionDataEvent(String pr
return new ProcessDefinitionDataEvent(body);
}

public static ProcessDefinition getProcessDefinition(String processId) {
ProcessDefinition def = new ProcessDefinition();
def.setId(processId);
def.setVersion(TestUtils.PROCESS_VERSION);
return def;
}

public static ProcessInstanceStateDataEvent getProcessCloudEvent(String processId, String processInstanceId, ProcessInstanceState status, String rootProcessInstanceId, String rootProcessId,
String parentProcessInstanceId, String identity) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
package org.kie.kogito.index.addon.api;

import java.nio.Buffer;
import java.util.Map;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kie.kogito.Application;
import org.kie.kogito.Model;
import org.kie.kogito.index.api.ExecuteArgs;
import org.kie.kogito.index.api.KogitoRuntimeCommonClient;
import org.kie.kogito.index.model.Job;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessInstance;
import org.kie.kogito.index.test.TestUtils;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
import org.kie.kogito.process.ProcessError;
import org.kie.kogito.process.ProcessInstanceExecutionException;
import org.kie.kogito.process.ProcessInstances;
Expand All @@ -42,6 +47,8 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.fasterxml.jackson.databind.JsonNode;

import io.quarkus.security.credential.TokenCredential;
import io.quarkus.security.identity.SecurityIdentity;
import io.vertx.core.AsyncResult;
Expand Down Expand Up @@ -123,6 +130,9 @@ public class KogitoAddonRuntimeClientImplTest {
@Mock
Instance<Application> applicationInstance;

@Mock
Model model;

@Mock
private Application application;

Expand All @@ -133,6 +143,8 @@ public void setup() {
lenient().when(processesInstance.isResolvable()).thenReturn(true);
lenient().when(processesInstance.get()).thenReturn(processes);
lenient().when(processes.processById(anyString())).thenReturn(process);
lenient().when(process.createModel()).thenReturn(model);
lenient().when(process.createInstance(model)).thenReturn(processInstance);
lenient().when(process.instances()).thenReturn(instances);
lenient().when(instances.findById(anyString())).thenReturn(Optional.of(processInstance));
lenient().when(processInstance.error()).thenReturn(Optional.of(error));
Expand Down Expand Up @@ -173,6 +185,16 @@ private ProcessError mockProcessInstanceStatusActiveOnError() {
}).when(error);
}

@Test
void testExecuteAfterSuccess() {
ProcessDefinition definition = new ProcessDefinition();
definition.setId("infra");
JsonNode body = ObjectMapperFactory.get().createObjectNode().put("name", "javierito");
client.executeProcessIntance(definition, ExecuteArgs.of(body));
verify(model).update(Map.of("name", "javierito"));
verify(processInstance).start();
}

@Test
void testAbortProcessInstanceSuccess() {
ProcessInstance pI = createProcessInstance(PROCESS_INSTANCE_ID, ACTIVE);
Expand Down

0 comments on commit 106f98f

Please sign in to comment.