Skip to content

Commit

Permalink
[incubator-kie-issues#1607] Fix JPA mappings for PostgresQL jsonb col…
Browse files Browse the repository at this point in the history
…umns. (apache#2140)
  • Loading branch information
pefernan authored and rgdoliveira committed Nov 7, 2024
1 parent 0ef7b40 commit ebb5fcc
Show file tree
Hide file tree
Showing 35 changed files with 986 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,16 @@
*/
package org.kie.kogito.index.test;

import java.net.URI;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;

import org.apache.commons.lang3.RandomStringUtils;
import org.kie.kogito.event.process.ProcessInstanceStateDataEvent;
import org.kie.kogito.event.process.ProcessInstanceStateEventBody;
import org.kie.kogito.event.process.ProcessInstanceVariableDataEvent;
import org.kie.kogito.event.process.ProcessInstanceVariableEventBody;
import org.kie.kogito.event.process.*;
import org.kie.kogito.event.usertask.*;
import org.kie.kogito.index.model.Attachment;
import org.kie.kogito.index.model.Comment;
import org.kie.kogito.index.model.Job;
Expand Down Expand Up @@ -88,6 +83,137 @@ public static ProcessInstanceVariableDataEvent createProcessInstanceVariableEven
return event;
}

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

ProcessInstanceNodeEventBody body = ProcessInstanceNodeEventBody.create()
.processId(processId)
.processInstanceId(processInstance)
.nodeDefinitionId(nodeDefinitionId)
.nodeInstanceId(nodeInstanceId)
.nodeName(nodeName)
.nodeType(nodeType)
.eventDate(new Date())
.eventType(eventType)
.build();

ProcessInstanceNodeDataEvent event = new ProcessInstanceNodeDataEvent();
event.setKogitoProcessId(processId);
event.setKogitoProcessInstanceId(processInstance);
event.setData(body);

return event;
}

public static ProcessInstanceErrorDataEvent createProcessInstanceErrorDataEvent(String processInstance, String processId, String userId, String errorMessage, String nodeDefinitionId,
String nodeInstanceId) {
ProcessInstanceErrorEventBody body = ProcessInstanceErrorEventBody.create()
.eventDate(new Date())
.eventUser(userId)
.processId(processId)
.processInstanceId(processInstance)
.errorMessage(errorMessage)
.nodeDefinitionId(nodeDefinitionId)
.nodeInstanceId(nodeInstanceId)
.build();

ProcessInstanceErrorDataEvent event = new ProcessInstanceErrorDataEvent();
event.setKogitoProcessInstanceId(processInstance);
event.setKogitoProcessId(processId);
event.setData(body);
return event;
}

public static UserTaskInstanceStateDataEvent createUserTaskStateEvent(String taskId, String taskName, String processInstanceId, String processId, String state) {
UserTaskInstanceStateEventBody body = UserTaskInstanceStateEventBody.create()
.userTaskInstanceId(taskId)
.userTaskName(taskName)
.state(state)
.processInstanceId(processInstanceId)
.build();

UserTaskInstanceStateDataEvent event = new UserTaskInstanceStateDataEvent();

event.setKogitoUserTaskInstanceId(taskId);
event.setKogitoProcessInstanceId(processInstanceId);
event.setKogitoProcessId(processId);

event.setData(body);

return event;
}

public static UserTaskInstanceCommentDataEvent createUserTaskCommentEvent(String taskId, String processInstanceId, String processId, String commentId, String comment, String userId,
int eventType) {

UserTaskInstanceCommentEventBody body = UserTaskInstanceCommentEventBody.create()
.commentId(commentId)
.eventUser(userId)
.commentContent(comment)
.eventDate(new Date())
.eventType(eventType)
.userTaskInstanceId(taskId)
.build();

UserTaskInstanceCommentDataEvent event = new UserTaskInstanceCommentDataEvent();
event.setKogitoUserTaskInstanceId(taskId);
event.setKogitoProcessInstanceId(processInstanceId);
event.setKogitoProcessId(processId);
event.setData(body);
return event;
}

public static UserTaskInstanceAttachmentDataEvent createUserTaskAttachmentEvent(String taskId, String processInstanceId, String processId, String attachmentId, String attachmentName,
URI attachmentURI, String userId, int eventType) {

UserTaskInstanceAttachmentEventBody body = UserTaskInstanceAttachmentEventBody.create()
.attachmentId(attachmentId)
.eventUser(userId)
.attachmentName(attachmentName)
.attachmentURI(attachmentURI)
.eventDate(new Date())
.eventType(eventType)
.userTaskInstanceId(taskId)
.build();

UserTaskInstanceAttachmentDataEvent event = new UserTaskInstanceAttachmentDataEvent();
event.setKogitoUserTaskInstanceId(taskId);
event.setKogitoProcessInstanceId(processInstanceId);
event.setKogitoProcessId(processId);
event.setData(body);
return event;
}

public static UserTaskInstanceAssignmentDataEvent createUserTaskAssignmentEvent(String taskId, String processInstanceId, String processId, String assignmentType, String... users) {
UserTaskInstanceAssignmentEventBody body = UserTaskInstanceAssignmentEventBody.create()
.users(users)
.assignmentType(assignmentType)
.build();

UserTaskInstanceAssignmentDataEvent event = new UserTaskInstanceAssignmentDataEvent();
event.setKogitoUserTaskInstanceId(taskId);
event.setKogitoProcessInstanceId(processInstanceId);
event.setKogitoProcessId(processId);
event.setData(body);
return event;
}

public static UserTaskInstanceVariableDataEvent createUserTaskVariableEvent(String taskId, String processInstanceId, String processId, String variableName, Object variableValue,
String variableType) {
UserTaskInstanceVariableEventBody body = UserTaskInstanceVariableEventBody.create()
.variableName(variableName)
.variableValue(variableValue)
.variableType(variableType)
.build();

UserTaskInstanceVariableDataEvent event = new UserTaskInstanceVariableDataEvent();
event.setKogitoUserTaskInstanceId(taskId);
event.setKogitoProcessInstanceId(processInstanceId);
event.setKogitoProcessId(processId);
event.setData(body);
return event;
}

public static ProcessInstance createProcessInstance(String processInstanceId,
String processId,
String rootProcessInstanceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,158 @@

import java.util.UUID;

import org.apache.commons.lang3.RandomStringUtils;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.kie.kogito.index.jpa.model.ProcessInstanceEntityRepository;
import org.kie.kogito.event.process.ProcessInstanceNodeEventBody;
import org.kie.kogito.index.model.ProcessInstance;
import org.kie.kogito.index.model.ProcessInstanceState;
import org.kie.kogito.index.test.TestUtils;

import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

public abstract class AbstractProcessInstanceStorageIT {
private static final String PROCESS_ID = "travels";
private static final String TRAVELER_NAME = "John";
private static final String TRAVELER_LAST_NAME = "Doe";

@Inject
ProcessInstanceEntityRepository repository;
ProcessInstanceEntityStorage storage;

@Test
public void testProcessInstanceEntity() {
@Transactional
public void testProcessInstanceStateEvent() {
String processInstanceId = createNewProcessInstance();

ProcessInstance processInstance = storage.get(processInstanceId);
Assertions.assertThat(processInstance)
.isNotNull()
.hasFieldOrPropertyWithValue("id", processInstanceId)
.hasFieldOrPropertyWithValue("processId", PROCESS_ID)
.hasFieldOrPropertyWithValue("state", ProcessInstanceState.ACTIVE.ordinal())
.hasFieldOrPropertyWithValue("rootProcessInstanceId", null)
.hasFieldOrPropertyWithValue("rootProcessId", null)
.hasFieldOrPropertyWithValue("variables", null);

storage.indexState(TestUtils.createProcessInstanceEvent(processInstanceId, PROCESS_ID, null, null, ProcessInstanceState.COMPLETED.ordinal()));

processInstance = storage.get(processInstanceId);
Assertions.assertThat(processInstance)
.isNotNull()
.hasFieldOrPropertyWithValue("id", processInstanceId)
.hasFieldOrPropertyWithValue("processId", PROCESS_ID)
.hasFieldOrPropertyWithValue("state", ProcessInstanceState.COMPLETED.ordinal())
.hasFieldOrPropertyWithValue("rootProcessInstanceId", null)
.hasFieldOrPropertyWithValue("rootProcessId", null)
.hasFieldOrPropertyWithValue("variables", null);
}

@Test
@Transactional
public void testProcessInstanceErrorEvent() {
String processInstanceId = createNewProcessInstance();

ProcessInstance processInstance = storage.get(processInstanceId);

Assertions.assertThat(processInstance.getError())
.isNull();

String nodeDefinitionId = UUID.randomUUID().toString();
storage.indexError(TestUtils.createProcessInstanceErrorDataEvent(processInstanceId, PROCESS_ID, TRAVELER_NAME, "This is really wrong", nodeDefinitionId, UUID.randomUUID().toString()));

processInstance = storage.get(processInstanceId);

Assertions.assertThat(processInstance.getError())
.isNotNull()
.hasFieldOrPropertyWithValue("nodeDefinitionId", nodeDefinitionId)
.hasFieldOrPropertyWithValue("message", "This is really wrong");
}

@Test
@Transactional
public void testProcessInstanceNodeEvent() {
String processInstanceId = createNewProcessInstance();

ProcessInstance processInstance = storage.get(processInstanceId);

Assertions.assertThat(processInstance.getNodes())
.isEmpty();

String nodeDefinitionId = UUID.randomUUID().toString();
String nodeInstanceId = UUID.randomUUID().toString();

storage.indexNode(TestUtils.createProcessInstanceNodeDataEvent(processInstanceId, PROCESS_ID, nodeDefinitionId, nodeInstanceId, "nodeName", "BoundaryEventNode",
ProcessInstanceNodeEventBody.EVENT_TYPE_ENTER));

processInstance = storage.get(processInstanceId);

Assertions.assertThat(processInstance.getNodes())
.hasSize(1);

Assertions.assertThat(processInstance.getNodes().get(0))
.hasNoNullFieldsOrPropertiesExcept("exit")
.hasFieldOrPropertyWithValue("name", "nodeName")
.hasFieldOrPropertyWithValue("type", "BoundaryEventNode")
.hasFieldOrPropertyWithValue("definitionId", nodeDefinitionId)
.hasFieldOrPropertyWithValue("nodeId", nodeDefinitionId)
.hasFieldOrPropertyWithValue("id", nodeInstanceId);

storage.indexNode(TestUtils.createProcessInstanceNodeDataEvent(processInstanceId, PROCESS_ID, nodeDefinitionId, nodeInstanceId, "nodeName", "BoundaryEventNode",
ProcessInstanceNodeEventBody.EVENT_TYPE_EXIT));

processInstance = storage.get(processInstanceId);

Assertions.assertThat(processInstance.getNodes())
.hasSize(1);

Assertions.assertThat(processInstance.getNodes().get(0))
.hasNoNullFieldsOrProperties()
.hasFieldOrPropertyWithValue("name", "nodeName")
.hasFieldOrPropertyWithValue("type", "BoundaryEventNode")
.hasFieldOrPropertyWithValue("definitionId", nodeDefinitionId)
.hasFieldOrPropertyWithValue("nodeId", nodeDefinitionId)
.hasFieldOrPropertyWithValue("id", nodeInstanceId);
}

@Test
@Transactional
public void testProcessInstanceVariableEvent() {
String processInstanceId = createNewProcessInstance();

ProcessInstance processInstance = storage.get(processInstanceId);

Assertions.assertThat(processInstance.getVariables())
.isNull();

storage.indexVariable(TestUtils.createProcessInstanceVariableEvent(processInstanceId, PROCESS_ID, TRAVELER_NAME, TRAVELER_LAST_NAME));

processInstance = storage.get(processInstanceId);

Assertions.assertThatObject(processInstance.getVariables())
.isNotNull()
.extracting(jsonNodes -> jsonNodes.at("/traveller/firstName").asText(), jsonNodes -> jsonNodes.at("/traveller/lastName").asText())
.contains(TRAVELER_NAME, TRAVELER_LAST_NAME);
}

private String createNewProcessInstance() {
String processInstanceId = UUID.randomUUID().toString();
TestUtils
.createProcessInstance(processInstanceId, RandomStringUtils.randomAlphabetic(5), UUID.randomUUID().toString(),
RandomStringUtils.randomAlphabetic(10), ProcessInstanceState.ACTIVE.ordinal(), 0L);
TestUtils
.createProcessInstance(processInstanceId, RandomStringUtils.randomAlphabetic(5), UUID.randomUUID().toString(),
RandomStringUtils.randomAlphabetic(10), ProcessInstanceState.COMPLETED.ordinal(), 1000L);

Assertions.assertThat(storage.get(processInstanceId))
.isNull();

storage.indexState(TestUtils.createProcessInstanceEvent(processInstanceId, PROCESS_ID, null, null, ProcessInstanceState.ACTIVE.ordinal()));

ProcessInstance processInstance = storage.get(processInstanceId);
Assertions.assertThat(processInstance)
.isNotNull()
.hasFieldOrPropertyWithValue("id", processInstanceId)
.hasFieldOrPropertyWithValue("processId", PROCESS_ID)
.hasFieldOrPropertyWithValue("state", ProcessInstanceState.ACTIVE.ordinal())
.hasFieldOrPropertyWithValue("rootProcessInstanceId", null)
.hasFieldOrPropertyWithValue("rootProcessId", null)
.hasFieldOrPropertyWithValue("variables", null);

return processInstanceId;
}

}
Loading

0 comments on commit ebb5fcc

Please sign in to comment.