Skip to content

Commit

Permalink
feat(Process instance search) add missing search options (#3295)
Browse files Browse the repository at this point in the history
* rootProcessInstanceId in engine search and REST API filters
* search keys in REST API filters
* fix useless IT test

Relates to [BPM-358](https://bonitasoft.atlassian.net/browse/BPM-358)

---------

Co-authored-by: Romain Bioteau <[email protected]>
  • Loading branch information
abirembaut and rbioteau authored Dec 26, 2024
1 parent 637b843 commit 6dd3081
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -755,48 +756,95 @@ public void searchArchivedProcessInstancesInvolvingUser() throws Exception {

@Test
public void twoPoolsWithOneWithACallActivityCaseTest() throws Exception {
final ProcessDefinitionBuilder process2DefinitionBuilder = new ProcessDefinitionBuilder()
.createNewInstance("process2", "1.0");
process2DefinitionBuilder.addActor(ACTOR_NAME);
process2DefinitionBuilder.addUserTask("User task", ACTOR_NAME);
final DesignProcessDefinition designProcess2Definition = process2DefinitionBuilder.done();
final ProcessDefinition process2Definition = deployAndEnableProcessWithActor(designProcess2Definition,
final ProcessDefinition subProcessDefinition = buildAndDeploySubprocess();

final ProcessDefinition rootProcessDefinition = buildAndDeployProcessWithCallActivity();

final ProcessInstance rootProcessInstance = getProcessAPI().startProcess(rootProcessDefinition.getId());
waitForUserTask(rootProcessInstance, "User task");

final SearchOptions opts = new SearchOptionsBuilder(0, 10).done();
final SearchResult<ProcessInstance> processInstanceSearchResult = getProcessAPI()
.searchOpenProcessInstances(opts);
Assert.assertThat(processInstanceSearchResult.getCount(), is(1L));

disableAndDeleteProcess(rootProcessDefinition);
disableAndDeleteProcess(subProcessDefinition);
}

private ProcessDefinition buildAndDeploySubprocess() throws InvalidProcessDefinitionException, BonitaException {
final ProcessDefinitionBuilder subProcessDefinitionBuilder = new ProcessDefinitionBuilder()
.createNewInstance("subprocess", "1.0");
subProcessDefinitionBuilder.addActor(ACTOR_NAME);
subProcessDefinitionBuilder.addUserTask("User task", ACTOR_NAME);
final DesignProcessDefinition designProcessDefinition = subProcessDefinitionBuilder.done();
return deployAndEnableProcessWithActor(designProcessDefinition,
ACTOR_NAME, user);
}

final ProcessDefinitionBuilder process1DefinitionBuilder = new ProcessDefinitionBuilder()
private ProcessDefinition buildAndDeployProcessWithCallActivity()
throws InvalidExpressionException, InvalidProcessDefinitionException, BonitaException {
final ProcessDefinitionBuilder rootProcessDefinitionBuilder = new ProcessDefinitionBuilder()
.createNewInstance("process1", "1.0");
process1DefinitionBuilder.addActor(ACTOR_NAME);

final Expression process2Name = new ExpressionBuilder().createConstantStringExpression("process2");
final Expression process2Version = new ExpressionBuilder().createConstantStringExpression("1.0");
process1DefinitionBuilder.addCallActivity("call process2", process2Name, process2Version);
final DesignProcessDefinition designProcess1Definition = process1DefinitionBuilder.done();
final ProcessDefinition process1Definition = deployAndEnableProcessWithActor(designProcess1Definition,
rootProcessDefinitionBuilder.addActor(ACTOR_NAME);
final Expression subProcessName = new ExpressionBuilder().createConstantStringExpression("subprocess");
final Expression subProcessVersion = new ExpressionBuilder().createConstantStringExpression("1.0");
rootProcessDefinitionBuilder.addCallActivity("call subprocess", subProcessName, subProcessVersion);
final DesignProcessDefinition designProcessDefinition = rootProcessDefinitionBuilder.done();
return deployAndEnableProcessWithActor(designProcessDefinition,
ACTOR_NAME, user);
}

@Test
public void searchProcessInstanceByRootProcessInstanceId() throws Exception {
final ProcessDefinition subProcessDefinition = buildAndDeploySubprocess();

final ProcessInstance instance1 = getProcessAPI().startProcess(process1Definition.getId());
waitForUserTask(instance1, "User task");
final ProcessDefinition rootProcessDefinition = buildAndDeployProcessWithCallActivity();

final SearchOptions opts = new SearchOptionsBuilder(0, 10).done();
final ProcessInstance rootProcessInstance = getProcessAPI().startProcess(rootProcessDefinition.getId());
waitForUserTask(rootProcessInstance, "User task");

SearchOptionsBuilder searchOptionsBuilder = new SearchOptionsBuilder(0, 10);
searchOptionsBuilder.filter(ProcessInstanceSearchDescriptor.ROOT_PROCESS_INSTANCE_ID,
rootProcessInstance.getId());
searchOptionsBuilder.differentFrom(ProcessInstanceSearchDescriptor.ID, rootProcessInstance.getId());
final SearchOptions opts = searchOptionsBuilder.done();
final SearchResult<ProcessInstance> processInstanceSearchResult = getProcessAPI()
.searchOpenProcessInstances(opts);
.searchProcessInstances(opts);
Assert.assertThat(processInstanceSearchResult.getCount(), is(1L));
Assert.assertThat(processInstanceSearchResult.getResult().get(0).getProcessDefinitionId(),
is(subProcessDefinition.getId()));

disableAndDeleteProcess(process1Definition);
disableAndDeleteProcess(process2Definition);
disableAndDeleteProcess(rootProcessDefinition);
disableAndDeleteProcess(subProcessDefinition);
}

private ProcessDefinitionBuilder createProcessDefinition(final String processName, final boolean withUserTask) {
final ProcessDefinitionBuilder designProcessDefinition = new ProcessDefinitionBuilder()
.createNewInstance(processName, "17.3");
if (withUserTask) {
designProcessDefinition.addActor(ACTOR_NAME);
designProcessDefinition.addUserTask("step1", ACTOR_NAME);
} else {
designProcessDefinition.addAutomaticTask("step1");
}
@Test
public void searchArchivedProcessInstanceByRootProcessInstanceId() throws Exception {
final ProcessDefinition subProcessDefinition = buildAndDeploySubprocess();

return designProcessDefinition;
final ProcessDefinition rootProcessDefinition = buildAndDeployProcessWithCallActivity();

final ProcessInstance rootProcessInstance = getProcessAPI().startProcess(rootProcessDefinition.getId());
waitForUserTaskAssignAndExecuteIt(rootProcessInstance, "User task", user, Collections.emptyMap());
waitForProcessToFinish(rootProcessInstance);

SearchOptionsBuilder searchOptionsBuilder = new SearchOptionsBuilder(0, 10);
searchOptionsBuilder.filter(ArchivedProcessInstancesSearchDescriptor.ROOT_PROCESS_INSTANCE_ID,
rootProcessInstance.getId());
searchOptionsBuilder.differentFrom(ArchivedProcessInstancesSearchDescriptor.SOURCE_OBJECT_ID,
rootProcessInstance.getId());
searchOptionsBuilder.filter(ArchivedProcessInstancesSearchDescriptor.STATE_ID,
ProcessInstanceState.COMPLETED.getId());
final SearchOptions opts = searchOptionsBuilder.done();
final SearchResult<ArchivedProcessInstance> archivedProcessInstanceSearchResult = getProcessAPI()
.searchArchivedProcessInstancesInAllStates(opts);
Assert.assertThat(archivedProcessInstanceSearchResult.getCount(), is(1L));
Assert.assertThat(archivedProcessInstanceSearchResult.getResult().get(0).getProcessDefinitionId(),
is(subProcessDefinition.getId()));

disableAndDeleteProcess(rootProcessDefinition);
disableAndDeleteProcess(subProcessDefinition);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
testImplementation libs.mockitoCore
testImplementation libs.assertj
testImplementation libs.restlet
testImplementation libs.awaitility
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
**/
package org.bonitasoft.test.toolkit.bpm;

import java.util.Collections;

import org.bonitasoft.engine.api.ProcessAPI;
import org.bonitasoft.engine.bpm.data.DataInstance;
import org.bonitasoft.engine.bpm.data.DataNotFoundException;
Expand Down Expand Up @@ -123,6 +125,14 @@ public TestHumanTask assignTo(final TestUser user) {
// ////////////////////////////////////////////////////////////////////////////
// / Execute
// ////////////////////////////////////////////////////////////////////////////
public void executeUserTask(final TestUser executor) {
final ProcessAPI processAPI = TestProcess.getProcessAPI(executor.getSession());
try {
processAPI.executeUserTask(humanTaskInstance.getId(), Collections.emptyMap());
} catch (final Exception e) {
throw new TestToolkitException("Can't execute user task <" + humanTaskInstance.getId() + ">.", e);
}
}

public void execute(final APISession apiSession) {
final ProcessAPI processAPI = TestProcess.getProcessAPI(apiSession);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.bonitasoft.engine.bpm.process.ProcessDefinition;
import org.bonitasoft.engine.bpm.process.ProcessInstance;
import org.bonitasoft.engine.bpm.process.ProcessInstanceSearchDescriptor;
import org.bonitasoft.engine.bpm.process.ProcessInstanceState;
import org.bonitasoft.engine.bpm.process.impl.ProcessDefinitionBuilder;
import org.bonitasoft.engine.exception.BonitaHomeNotSetException;
import org.bonitasoft.engine.exception.DeletionException;
Expand Down Expand Up @@ -447,6 +448,11 @@ public List<TestCase> listOpenCases() throws SearchException {
return convertToCasesList(processInstances);
}

public List<TestCase> listAllOpenCases() throws SearchException {
List<ProcessInstance> processInstances = searchProcessInstances();
return convertToCasesList(processInstances);
}

private List<TestCase> convertToCasesList(List<ProcessInstance> processInstances) {
List<TestCase> cases = new ArrayList<>();
for (ProcessInstance instance : processInstances) {
Expand All @@ -455,6 +461,14 @@ private List<TestCase> convertToCasesList(List<ProcessInstance> processInstances
return cases;
}

private List<ProcessInstance> searchProcessInstances() throws SearchException {
final SearchOptionsBuilder builder = new SearchOptionsBuilder(0, 100);
builder.filter(ProcessInstanceSearchDescriptor.PROCESS_DEFINITION_ID, getProcessDefinition().getId());
builder.differentFrom(ProcessInstanceSearchDescriptor.STATE_ID,
ProcessInstanceState.COMPLETED.getId());
return getProcessAPI(getSession()).searchProcessInstances(builder.done()).getResult();
}

private List<ProcessInstance> searchOpenedProcessInstances() throws SearchException {
final SearchOptionsBuilder builder = new SearchOptionsBuilder(0, 100);
builder.filter(ProcessInstanceSearchDescriptor.PROCESS_DEFINITION_ID, getProcessDefinition().getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public static ProcessDefinitionBuilder getDefaultProcessDefinitionBuilder(final
.addDescription("This a default process")
.addStartEvent("Start")
.addUserTask("Activity 1", "Employees")
.addEndEvent("Finish");
.addEndEvent("Finish")
.addTransition("Start", "Activity 1")
.addTransition("Activity 1", "Finish");
return processDefinitionBuidler;
}

Expand All @@ -111,8 +113,9 @@ protected static BusinessArchiveBuilder getBusinessArchiveWithDocumentBuilder(fi
processDefinitionBuidler.addActor("Employees", true)
.addStartEvent("Start")
.addUserTask("Activity 1", "Employees")
.addEndEvent("Finish");

.addEndEvent("Finish")
.addTransition("Start", "Activity 1")
.addTransition("Activity 1", "Finish");
try {
return new BusinessArchiveBuilder().createNewBusinessArchive()
.setFormMappings(TestProcess.createDefaultProcessFormMapping(processDefinitionBuidler.getProcess()))
Expand Down Expand Up @@ -147,7 +150,9 @@ private static ProcessDefinitionBuilder getCallActivityProcessDefinitionBuilder(
processDefinitionBuidler.addActor("Employees", true)
.addStartEvent("Start")
.addCallActivity("Call Activity", expressionName, expressionVersion)
.addEndEvent("Finish");
.addEndEvent("Finish")
.addTransition("Start", "Call Activity")
.addTransition("Call Activity", "Finish");
return processDefinitionBuidler;
}

Expand Down Expand Up @@ -180,7 +185,9 @@ public static TestHumanTask createActivityWithVariables(TestUser initiator) thro
.addData("variable3", Date.class.getName(),
new ExpressionBuilder().createConstantDateExpression("428558400000"))

.addEndEvent("Finish");
.addEndEvent("Finish")
.addTransition("Start", "Activity 1")
.addTransition("Activity 1", "Finish");
final TestProcess testProcess = new TestProcess(processDefinitionBuidler);
getInstance().getProcessList().put(processName, testProcess);
return testProcess.addActor(initiator).enable().startCase().getNextHumanTask().assignTo(initiator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
package org.bonitasoft.web.rest.server.api.bpm.flownode;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.bonitasoft.test.toolkit.bpm.TestCase;
import org.bonitasoft.test.toolkit.bpm.TestHumanTask;
import org.bonitasoft.test.toolkit.bpm.TestProcessFactory;
import org.bonitasoft.test.toolkit.organization.TestUser;
Expand Down Expand Up @@ -110,21 +112,24 @@ public void testSearch() {
/**
* Check that the paging system works fine
*/
public void testHumanTaskItemSearchPaging() {
public void testHumanTaskItemSearchPaging() throws InterruptedException {

final long before = apiHumanTask.runSearch(0, 10, null,
apiHumanTask.defineDefaultSearchOrder(),
new HashMap<>(),
new ArrayList<>(), new ArrayList<>()).getTotal();

// Setup : insert enough tasks to have 2 pages
var instances = new ArrayList<TestCase>();
for (int i = 0; i < 15; i++) {
try {
TestProcessFactory.getDefaultHumanTaskProcess().startCase();
instances.add(TestProcessFactory.getDefaultHumanTaskProcess().startCase());
} catch (final Exception e) {
fail("Can't start process [" + e.getLocalizedMessage() + "]");
}
}
await("Wait for the tasks to be created")
.until(() -> instances.stream().allMatch(testCase -> testCase.getNextHumanTask() != null));

// Setup: retrieve the needed APIs
// this.apiHumanTask = new APIHumanTask();
Expand All @@ -137,7 +142,7 @@ public void testHumanTaskItemSearchPaging() {
new HashMap<>(),
new ArrayList<>(), new ArrayList<>());

assertThat(search.getResults().size()).isGreaterThan(2);
assertThat(search.getResults()).hasSizeGreaterThan(2);
assertThat(search.getTotal()).isGreaterThan(before);
}

Expand Down
Loading

0 comments on commit 6dd3081

Please sign in to comment.