Skip to content

Commit

Permalink
[Fix_#1923] Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Dec 22, 2023
1 parent 8004a47 commit 773f3ac
Show file tree
Hide file tree
Showing 77 changed files with 489 additions and 504 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
<groupId>org.kie.kogito</groupId>
<artifactId>data-index-service-common</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-oracle</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>data-index-storage-oracle</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@
import java.util.List;
import java.util.function.BiConsumer;

import org.kie.kogito.persistence.api.Storage;
import org.kie.kogito.persistence.api.StorageFetcher;
import org.kie.kogito.persistence.api.query.AttributeFilter;
import org.kie.kogito.persistence.api.query.AttributeSort;

public abstract class QueryTestBase<K, V> {

public abstract Storage<K, V> getStorage();

protected Boolean isDateTimeAsLong() {
return true;
}

public void queryAndAssert(BiConsumer<List<V>, String[]> assertConsumer, Storage<K, V> storage, List<AttributeFilter<?>> filters, List<AttributeSort> sort, Integer offset, Integer limit,
public void queryAndAssert(BiConsumer<List<V>, String[]> assertConsumer, StorageFetcher<K, V> storage, List<AttributeFilter<?>> filters, List<AttributeSort> sort, Integer offset, Integer limit,
String... ids) {
assertConsumer.accept(storage.query().filter(filters).sort(sort).offset(offset).limit(limit).execute(), ids);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import java.util.UUID;

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.index.model.Attachment;
import org.kie.kogito.index.model.Comment;
import org.kie.kogito.index.model.Job;
Expand All @@ -40,6 +44,7 @@
import org.kie.kogito.index.model.ProcessInstanceError;
import org.kie.kogito.index.model.ProcessInstanceState;
import org.kie.kogito.index.model.UserTaskInstance;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand All @@ -61,6 +66,28 @@ public static ProcessDefinition createProcessDefinition(String processId, String
return pd;
}

public static ProcessInstanceStateDataEvent createProcessInstanceEvent(String processInstance,
String processId, String rootProcessInstanceId, String rootProcessId, Integer status) {
ProcessInstanceStateDataEvent event = new ProcessInstanceStateDataEvent();
event.setKogitoProcessId(processId);
event.setKogitoRootProcessId(rootProcessId);
event.setKogitoRootProcessInstanceId(rootProcessInstanceId);
event.setKogitoProcessInstanceId(processInstance);
event.setData(ProcessInstanceStateEventBody.create().processId(processId).rootProcessId(rootProcessId).rootProcessInstanceId(rootProcessInstanceId).processInstanceId(processInstance)
.state(status).build());
return event;
}

public static ProcessInstanceVariableDataEvent createProcessInstanceVariableEvent(String processInstance,
String processId, String firstName, String lastName) {
ProcessInstanceVariableDataEvent event = new ProcessInstanceVariableDataEvent();
event.setKogitoProcessId(processId);
event.setKogitoProcessInstanceId(processInstance);
event.setData(ProcessInstanceVariableEventBody.create().processId(processId).processInstanceId(processInstance)
.variableName("traveller").variableValue(ObjectMapperFactory.get().createObjectNode().put("firstName", firstName).put("lastName", lastName)).build());
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 @@ -48,6 +48,8 @@

public abstract class AbstractJobQueryIT extends QueryTestBase<String, Job> {

public abstract Storage<String, Job> getStorage();

@Test
void testJobQuery() {
String jobId1 = UUID.randomUUID().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

public abstract class AbstractProcessDefinitionQueryIT extends QueryTestBase<String, ProcessDefinition> {

public abstract Storage<String, ProcessDefinition> getStorage();

@Test
void testProcessDefinitionQuery() {
String processId = "travels";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

public abstract class AbstractProcessIdQueryIT extends QueryTestBase<String, String> {

public abstract Storage<String, String> getStorage();

@Test
void testProcessIdQuery() {
String processId = "travels";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,96 +21,32 @@
import java.util.UUID;

import org.junit.jupiter.api.Test;
import org.kie.kogito.event.process.ProcessInstanceStateDataEvent;
import org.kie.kogito.index.model.ProcessInstance;
import org.kie.kogito.index.storage.ProcessInstanceStorage;
import org.kie.kogito.index.test.QueryTestBase;
import org.kie.kogito.index.test.TestUtils;
import org.kie.kogito.persistence.api.Storage;
import org.kie.kogito.persistence.api.query.SortDirection;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.kie.kogito.index.model.ProcessInstanceState.ACTIVE;
import static org.kie.kogito.index.model.ProcessInstanceState.COMPLETED;
import static org.kie.kogito.index.test.QueryTestUtils.assertWithId;
import static org.kie.kogito.index.test.QueryTestUtils.assertWithIdInOrder;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.and;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.between;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.contains;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.containsAll;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.containsAny;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.equalTo;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.greaterThan;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.greaterThanEqual;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.in;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.isNull;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.lessThan;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.lessThanEqual;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.like;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.notNull;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.orderBy;

public abstract class AbstractProcessInstanceQueryIT extends QueryTestBase<String, ProcessInstance> {

public abstract ProcessInstanceStorage getStorage();

@Test
void testProcessInstanceQuery() {
String processId = "travels";
String processInstanceId = UUID.randomUUID().toString();
String subProcessId = processId + "_sub";
String subProcessInstanceId = UUID.randomUUID().toString();
ProcessInstance processInstance = TestUtils
.createProcessInstance(processInstanceId, processId, null, null, ACTIVE.ordinal(), 0L);
ProcessInstance subProcessInstance = TestUtils
.createProcessInstance(subProcessInstanceId, subProcessId, processInstanceId, processId, COMPLETED.ordinal(),
1000L);
Storage<String, ProcessInstance> storage = getStorage();
storage.put(processInstanceId, processInstance);
storage.put(subProcessInstanceId, subProcessInstance);

queryAndAssert(assertWithId(), storage, singletonList(in("state", asList(ACTIVE.ordinal(), COMPLETED.ordinal()))), null,
null, null, processInstanceId, subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(equalTo("state", ACTIVE.ordinal())), null, null, null,
processInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(greaterThan("state", ACTIVE.ordinal())), null, null, null,
subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(greaterThanEqual("state", ACTIVE.ordinal())), null, null, null,
processInstanceId, subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(lessThan("state", COMPLETED.ordinal())), null, null, null,
ProcessInstanceStateDataEvent processInstanceEvent = TestUtils.createProcessInstanceEvent(processInstanceId, processId, subProcessInstanceId, subProcessId, COMPLETED.ordinal());
ProcessInstanceStorage storage = getStorage();
storage.indexState(processInstanceEvent);
queryAndAssert(assertWithId(), storage, singletonList(equalTo("state", COMPLETED.ordinal())), null, null, null,
processInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(lessThanEqual("state", COMPLETED.ordinal())), null, null, null,
processInstanceId, subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(between("state", ACTIVE.ordinal(), COMPLETED.ordinal())), null,
null, null, processInstanceId, subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(isNull("rootProcessInstanceId")), null, null, null,
processInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(notNull("rootProcessInstanceId")), null, null, null,
subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(contains("roles", "admin")), null, null, null, processInstanceId,
subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(containsAny("roles", asList("admin", "kogito"))), null, null,
null, processInstanceId, subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(containsAll("roles", asList("admin", "kogito"))), null, null,
null);
queryAndAssert(assertWithId(), storage, singletonList(like("processId", "*_sub")), null, null, null,
subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(and(asList(lessThan("start", getDateTime()), lessThanEqual("start", getDateTime())))),
null, null, null, processInstanceId, subProcessInstanceId);
queryAndAssert(assertWithId(), storage, singletonList(and(asList(equalTo("rootProcessInstanceId", processInstanceId), equalTo("start", getDateTime(subProcessInstance.getStart()))))),
null, null, null, subProcessInstanceId);
queryAndAssert(assertWithId(), storage, asList(isNull("roles"), isNull("end"),
greaterThan("start", getDateTime()),
greaterThanEqual("start", getDateTime())), null, null, null);
queryAndAssert(assertWithId(), storage, singletonList(equalTo("nodes.id", processInstance.getNodes().get(0).getId())),
null, null, null, processInstanceId);

queryAndAssert(assertWithIdInOrder(), storage, asList(in("id", asList(processInstanceId, subProcessInstanceId)),
in("processId", asList(processId, subProcessId))),
singletonList(orderBy("processId", SortDirection.ASC)), 1, 1, subProcessInstanceId);
queryAndAssert(assertWithIdInOrder(), storage, null, singletonList(orderBy("processId", SortDirection.DESC)), null,
null, subProcessInstanceId, processInstanceId);
queryAndAssert(assertWithIdInOrder(), storage, null, null, 1, 1, subProcessInstanceId);
queryAndAssert(assertWithIdInOrder(), storage, null,
asList(orderBy("processId", SortDirection.ASC), orderBy("state", SortDirection.ASC)), 1, 1,
subProcessInstanceId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,96 +20,31 @@

import java.util.UUID;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;
import org.kie.kogito.event.usertask.UserTaskInstanceStateDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceStateEventBody;
import org.kie.kogito.index.model.UserTaskInstance;
import org.kie.kogito.index.storage.UserTaskInstanceStorage;
import org.kie.kogito.index.test.QueryTestBase;
import org.kie.kogito.index.test.TestUtils;
import org.kie.kogito.persistence.api.Storage;
import org.kie.kogito.persistence.api.query.SortDirection;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.kie.kogito.index.test.QueryTestUtils.assertWithId;
import static org.kie.kogito.index.test.QueryTestUtils.assertWithIdInOrder;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.and;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.between;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.contains;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.containsAll;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.containsAny;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.equalTo;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.greaterThan;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.greaterThanEqual;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.in;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.isNull;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.lessThan;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.lessThanEqual;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.like;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.notNull;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.or;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.orderBy;

public abstract class AbstractUserTaskInstanceQueryIT extends QueryTestBase<String, UserTaskInstance> {

public abstract UserTaskInstanceStorage getStorage();

@Test
void testUserTaskInstanceQuery() {
String taskId1 = UUID.randomUUID().toString();
String processInstanceId1 = UUID.randomUUID().toString();
String taskId2 = UUID.randomUUID().toString();
String processInstanceId2 = UUID.randomUUID().toString();

UserTaskInstance userTaskInstance1 = TestUtils
.createUserTaskInstance(taskId1, processInstanceId1, RandomStringUtils.randomAlphabetic(5),
UUID.randomUUID().toString(),
RandomStringUtils.randomAlphabetic(10), "InProgress", 0L);
UserTaskInstance userTaskInstance2 = TestUtils
.createUserTaskInstance(taskId2, processInstanceId2, RandomStringUtils.randomAlphabetic(5), null, null,
"Completed", 1000L);
Storage<String, UserTaskInstance> storage = getStorage();
storage.put(taskId1, userTaskInstance1);
storage.put(taskId2, userTaskInstance2);

queryAndAssert(assertWithId(), storage, singletonList(in("state", asList("InProgress", "Completed"))), null, null, null,
taskId1, taskId2);
queryAndAssert(assertWithId(), storage, singletonList(equalTo("state", "InProgress")), null, null, null, taskId1);
queryAndAssert(assertWithId(), storage,
singletonList(greaterThan("started", getDateTime())), null, null, null);
queryAndAssert(assertWithId(), storage,
singletonList(greaterThanEqual("completed", getDateTime(userTaskInstance2.getCompleted()))), null,
null, null, taskId2);
queryAndAssert(assertWithId(), storage,
singletonList(lessThan("completed", getDateTime())), null, null, null, taskId2);
queryAndAssert(assertWithId(), storage,
singletonList(lessThanEqual("started", getDateTime())), null, null,
null, taskId1, taskId2);
queryAndAssert(assertWithId(), storage, singletonList(
between("completed", getDateTime(userTaskInstance2.getCompleted()), getDateTime())),
null, null, null, taskId2);
queryAndAssert(assertWithId(), storage, singletonList(isNull("rootProcessInstanceId")), null, null, null, taskId2);
queryAndAssert(assertWithId(), storage, singletonList(notNull("rootProcessInstanceId")), null, null, null, taskId1);
queryAndAssert(assertWithId(), storage, singletonList(contains("adminUsers", "kogito")), null, null, null, taskId1,
taskId2);
queryAndAssert(assertWithId(), storage, singletonList(containsAny("adminGroups", asList("admin", "agroup"))), null,
null, null, taskId1, taskId2);
queryAndAssert(assertWithId(), storage, singletonList(containsAll("adminGroups", asList("admin", "agroup"))), null,
null, null);
queryAndAssert(assertWithId(), storage, singletonList(like("state", "*ss")), null, null, null, taskId1);
queryAndAssert(assertWithId(), storage,
singletonList(and(asList(equalTo("id", taskId1), equalTo("processInstanceId", processInstanceId1)))), null,
null, null, taskId1);
queryAndAssert(assertWithId(), storage, singletonList(or(asList(equalTo("id", taskId1), equalTo("id", taskId2)))), null,
null, null, taskId1, taskId2);
queryAndAssert(assertWithId(), storage,
asList(equalTo("id", taskId1), equalTo("processInstanceId", processInstanceId2)), null, null, null);

queryAndAssert(assertWithIdInOrder(), storage, asList(in("id", asList(taskId1, taskId2)),
in("processInstanceId", asList(processInstanceId1, processInstanceId2))),
singletonList(orderBy("state", SortDirection.ASC)), 1, 1, taskId1);
queryAndAssert(assertWithIdInOrder(), storage, null, singletonList(orderBy("state", SortDirection.DESC)), null, null,
taskId1, taskId2);
queryAndAssert(assertWithIdInOrder(), storage, null, null, 1, 1, taskId2);
queryAndAssert(assertWithIdInOrder(), storage, null,
asList(orderBy("state", SortDirection.ASC), orderBy("completed", SortDirection.ASC)), 1, 1, taskId1);
String taskId = UUID.randomUUID().toString();
String processInstanceId = UUID.randomUUID().toString();
UserTaskInstanceStorage storage = getStorage();
UserTaskInstanceStateDataEvent event = new UserTaskInstanceStateDataEvent();
event.setKogitoProcessInstanceId(processInstanceId);
event.setKogitoUserTaskInstanceId(taskId);
event.setData(UserTaskInstanceStateEventBody.create().processInstanceId(processInstanceId).state("InProgress").userTaskInstanceId(taskId).build());
storage.indexState(event);
queryAndAssert(assertWithId(), storage, singletonList(equalTo("state", "InProgress")), null, null, null, taskId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public void indexVariable(ProcessInstanceVariableDataEvent event) {
}

private <T extends ProcessInstanceDataEvent<?>> void index(T event, ProcessInstanceEventMerger merger) {
storage.put(event.getKogitoProcessId(), merger.merge(storage.get(event.getKogitoProcessInstanceId()), event));
ProcessInstance processInstance = storage.get(event.getKogitoProcessInstanceId());
if (processInstance != null) {
processInstance = new ProcessInstance();
processInstance.setId(event.getKogitoProcessInstanceId());
processInstance.setProcessId(event.getKogitoProcessId());
}
storage.put(event.getKogitoProcessInstanceId(), merger.merge(processInstance, event));
}
}
Loading

0 comments on commit 773f3ac

Please sign in to comment.