Skip to content

Commit

Permalink
#3692: Query jobs by process / case definition key
Browse files Browse the repository at this point in the history
  • Loading branch information
amporsim authored and filiphr committed Dec 4, 2023
1 parent 5abe6ba commit 988df39
Show file tree
Hide file tree
Showing 20 changed files with 613 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.flowable.cmmn.test.mgmt;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.flowable.cmmn.api.runtime.CaseInstance;
import org.flowable.cmmn.engine.test.CmmnDeployment;
import org.flowable.cmmn.engine.test.FlowableCmmnTestCase;
import org.flowable.common.engine.api.scope.ScopeTypes;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.job.api.DeadLetterJobQuery;
import org.flowable.job.api.Job;
import org.flowable.job.service.JobServiceConfiguration;
import org.flowable.job.service.impl.DeadLetterJobQueryImpl;
import org.flowable.job.service.impl.persistence.entity.DeadLetterJobEntity;
import org.flowable.job.service.impl.persistence.entity.DeadLetterJobEntityManager;
import org.flowable.job.service.impl.persistence.entity.SuspendedJobEntity;
import org.junit.Test;

/**
* @author Simon Amport
*/
public class DeadLetterJobQueryTest extends FlowableCmmnTestCase {

@Test
@CmmnDeployment(resources = "org/flowable/cmmn/test/one-human-task-model.cmmn")
public void testQueryByCaseDefinitionKey() {
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionKey("oneTaskCase")
.start();

DeadLetterJobEntity deadLetterJob = cmmnEngineConfiguration.getCommandExecutor().execute(commandContext -> {
DeadLetterJobEntityManager deadLetterJobEntityManager = cmmnEngineConfiguration.getJobServiceConfiguration().getDeadLetterJobEntityManager();
DeadLetterJobEntity deadLetterJobEntity = deadLetterJobEntityManager.create();
deadLetterJobEntity.setScopeId(caseInstance.getId());
deadLetterJobEntity.setScopeDefinitionId(caseInstance.getCaseDefinitionId());
deadLetterJobEntity.setScopeType(ScopeTypes.CMMN);
deadLetterJobEntity.setJobType(SuspendedJobEntity.JOB_TYPE_MESSAGE);
deadLetterJobEntity.setJobHandlerType("testJobHandlerType");

deadLetterJobEntityManager.insert(deadLetterJobEntity);
return deadLetterJobEntity;
});

DeadLetterJobEntity deadLetterJob2 = cmmnEngineConfiguration.getCommandExecutor().execute(commandContext -> {
DeadLetterJobEntityManager deadLetterJobEntityManager = cmmnEngineConfiguration.getJobServiceConfiguration().getDeadLetterJobEntityManager();
DeadLetterJobEntity deadLetterJobEntity = deadLetterJobEntityManager.create();
deadLetterJobEntity.setProcessInstanceId("PRC-1");
deadLetterJobEntity.setProcessDefinitionId("PRC-DEF-1");
deadLetterJobEntity.setJobType(SuspendedJobEntity.JOB_TYPE_MESSAGE);
deadLetterJobEntity.setJobHandlerType("testJobHandlerType");

deadLetterJobEntityManager.insert(deadLetterJobEntity);
return deadLetterJobEntity;
});

DeadLetterJobQuery query = cmmnEngineConfiguration.getCmmnManagementService().createDeadLetterJobQuery().caseDefinitionKey("oneTaskCase");
assertThat(query.count()).isEqualTo(1);
assertThat(query.list()).extracting(Job::getId).containsExactly(deadLetterJob.getId());
assertThat(query.singleResult().getId()).isEqualTo(deadLetterJob.getId());

query = cmmnEngineConfiguration.getCmmnManagementService().createDeadLetterJobQuery().caseDefinitionKey("invalid");
assertThat(query.count()).isZero();
assertThat(query.list()).isEmpty();
assertThat(query.singleResult()).isNull();

cmmnEngineConfiguration.getCommandExecutor().execute((Command<Void>) commandContext -> {
JobServiceConfiguration jobServiceConfiguration = cmmnEngineConfiguration.getJobServiceConfiguration();
DeadLetterJobEntityManager deadLetterJobService = jobServiceConfiguration.getDeadLetterJobEntityManager();
List<Job> jobs = deadLetterJobService.findJobsByQueryCriteria(new DeadLetterJobQueryImpl(commandContext, jobServiceConfiguration));
for (Job job : jobs) {
deadLetterJobService.delete(job.getId());
}
return null;
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ public void testQueryByCaseDefinitionId() {
assertThat(query.singleResult()).isNull();
}

@Test
@CmmnDeployment(resources = "org/flowable/cmmn/test/mgmt/ExternalWorkerJobQueryTest.cmmn")
public void testQueryByCaseDefinitionKey() {
CaseInstance caseInstance1 = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionKey("externalWorkerJobQueryTest")
.start();
CaseInstance caseInstance2 = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionKey("externalWorkerJobQueryTest")
.start();
ExternalWorkerJobQuery query = cmmnManagementService.createExternalWorkerJobQuery().caseDefinitionKey("externalWorkerJobQueryTest");
assertThat(query.count()).isEqualTo(4);
assertThat(query.list())
.extracting(ExternalWorkerJob::getScopeId)
.containsOnly(caseInstance1.getId(), caseInstance2.getId());

query = cmmnManagementService.createExternalWorkerJobQuery().caseDefinitionKey("invalid");
assertThat(query.count()).isZero();
assertThat(query.list()).isEmpty();
assertThat(query.singleResult()).isNull();
}

@Test
@CmmnDeployment(resources = "org/flowable/cmmn/test/mgmt/ExternalWorkerJobQueryTest.cmmn")
public void testQueryByPlanItemInstanceId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.flowable.cmmn.test.mgmt;

import static org.assertj.core.api.Assertions.assertThat;

import org.flowable.cmmn.api.runtime.CaseInstance;
import org.flowable.cmmn.engine.test.CmmnDeployment;
import org.flowable.cmmn.engine.test.FlowableCmmnTestCase;
import org.flowable.job.api.Job;
import org.flowable.job.api.JobQuery;
import org.junit.Test;

/**
* @author Simon Amport
*/
public class JobQueryTest extends FlowableCmmnTestCase {

@Test
@CmmnDeployment(resources = "org/flowable/cmmn/test/mgmt/TimerJobQueryTest.cmmn")
public void testQueryByCaseDefinitionKey() {
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionKey("timerJobQueryTest")
.start();

Job timerJob = cmmnManagementService.createTimerJobQuery().singleResult();

Job executableJob = cmmnManagementService.moveTimerToExecutableJob(timerJob.getId());
assertThat(executableJob).isNotNull();

JobQuery jobQuery = cmmnManagementService.createJobQuery().caseDefinitionKey("timerJobQueryTest");
assertThat(jobQuery.count()).isEqualTo(1);
assertThat(jobQuery.singleResult().getScopeId()).isEqualTo(caseInstance.getId());
assertThat(jobQuery.list()).extracting(Job::getId)
.containsExactly(executableJob.getId());

jobQuery = cmmnManagementService.createJobQuery().caseDefinitionKey("invalid");
assertThat(jobQuery.count()).isZero();
assertThat(jobQuery.singleResult()).isNull();
assertThat(jobQuery.list()).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.flowable.cmmn.test.mgmt;

import static org.assertj.core.api.Assertions.assertThat;

import org.flowable.cmmn.api.runtime.CaseInstance;
import org.flowable.cmmn.engine.test.CmmnDeployment;
import org.flowable.cmmn.engine.test.FlowableCmmnTestCase;
import org.flowable.common.engine.api.scope.ScopeTypes;
import org.flowable.job.api.Job;
import org.flowable.job.api.SuspendedJobQuery;
import org.flowable.job.service.impl.persistence.entity.SuspendedJobEntity;
import org.flowable.job.service.impl.persistence.entity.SuspendedJobEntityManager;
import org.junit.Test;

/**
* @author Simon Amport
*/
public class SuspendedJobQueryTest extends FlowableCmmnTestCase {

@Test
@CmmnDeployment(resources = "org/flowable/cmmn/test/one-task-model.cmmn")
public void testQueryByCaseDefinitionKey() {

CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("oneTaskCase").start();

SuspendedJobEntity jobEntity = cmmnEngineConfiguration.getCommandExecutor().execute(commandContext -> {
SuspendedJobEntityManager suspendedJobEntityManager = cmmnEngineConfiguration.getJobServiceConfiguration().getSuspendedJobEntityManager();
SuspendedJobEntity suspendedJobEntity = suspendedJobEntityManager.create();
suspendedJobEntity.setScopeId(caseInstance.getId());
suspendedJobEntity.setScopeDefinitionId(caseInstance.getCaseDefinitionId());
suspendedJobEntity.setScopeType(ScopeTypes.CMMN);
suspendedJobEntity.setJobType(SuspendedJobEntity.JOB_TYPE_MESSAGE);
suspendedJobEntity.setJobHandlerType("testJobHandlerType");

suspendedJobEntityManager.insert(suspendedJobEntity);
return suspendedJobEntity;
});

SuspendedJobQuery suspendedJobQuery = cmmnManagementService.createSuspendedJobQuery().caseDefinitionKey("oneTaskCase");
assertThat(suspendedJobQuery.count()).isEqualTo(1);
assertThat(suspendedJobQuery.singleResult().getScopeId()).isEqualTo(caseInstance.getId());
assertThat(suspendedJobQuery.list()).extracting(Job::getId).containsExactly(jobEntity.getId());

suspendedJobQuery = cmmnManagementService.createSuspendedJobQuery().caseDefinitionKey("invalid");
assertThat(suspendedJobQuery.count()).isZero();
assertThat(suspendedJobQuery.singleResult()).isNull();
assertThat(suspendedJobQuery.list()).isEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.flowable.cmmn.test.mgmt;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

import org.flowable.cmmn.api.runtime.CaseInstance;
import org.flowable.cmmn.engine.test.CmmnDeployment;
import org.flowable.cmmn.engine.test.FlowableCmmnTestCase;
import org.flowable.job.api.Job;
import org.flowable.job.api.TimerJobQuery;
import org.junit.Test;

/**
* @author Simon Amport
*/
public class TimerJobQueryTest extends FlowableCmmnTestCase {

@Test
@CmmnDeployment(resources = "org/flowable/cmmn/test/mgmt/TimerJobQueryTest.cmmn")
public void testQueryByCaseDefinitionKey() {
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
.caseDefinitionKey("timerJobQueryTest")
.start();

TimerJobQuery query = cmmnManagementService.createTimerJobQuery().caseDefinitionKey("timerJobQueryTest");
assertThat(query.count()).isEqualTo(1);
assertThat(query.singleResult().getScopeId()).isEqualTo(caseInstance.getId());
assertThat(query.list())
.extracting(Job::getElementId, Job::getScopeId)
.containsExactly(
tuple("timerEventListener1", caseInstance.getId())
);

query = cmmnManagementService.createTimerJobQuery().caseDefinitionKey("invalid");
assertThat(query.count()).isZero();
assertThat(query.singleResult()).isNull();
assertThat(query.list()).isEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/CMMN/20151109/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flowable="http://flowable.org/cmmn" xmlns:cmmndi="http://www.omg.org/spec/CMMN/20151109/CMMNDI" xmlns:dc="http://www.omg.org/spec/CMMN/20151109/DC" xmlns:di="http://www.omg.org/spec/CMMN/20151109/DI" targetNamespace="http://flowable.org/cmmn">
<case id="timerJobQueryTest" name="Timer Job Query Test">
<casePlanModel id="onecaseplanmodel1" name="Case plan model">
<planItem id="planItemtimerEventListener1" definitionRef="timerEventListener1"></planItem>
<planItem id="planItemhumanTask1" name="Human task" definitionRef="humanTask1">
<entryCriterion id="entryCriterion1" sentryRef="sentryentryCriterion1"></entryCriterion>
</planItem>
<sentry id="sentryentryCriterion1">
<planItemOnPart id="sentryOnPartentryCriterion1" sourceRef="planItemtimerEventListener1">
<standardEvent>occur</standardEvent>
</planItemOnPart>
</sentry>
<timerEventListener id="timerEventListener1">
<timerExpression><![CDATA[PT1H]]></timerExpression>
</timerEventListener>
<humanTask id="humanTask1" name="Human task" flowable:assignee="${initiator}" flowable:formFieldValidation="false">
</humanTask>
</casePlanModel>
</case>
</definitions>
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ public void testQueryByProcessDefinitionId() {
assertThat(query.singleResult()).isNull();
}

@Test
@Deployment(resources = "org/flowable/engine/test/api/mgmt/ExternalWorkerJobQueryTest.bpmn20.xml")
public void testQueryByProcessDefinitionKey() {
ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("externalWorkerJobQueryTest");
ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("externalWorkerJobQueryTest");
ExternalWorkerJobQuery query = managementService.createExternalWorkerJobQuery().processDefinitionKey(processInstance1.getProcessDefinitionKey());
assertThat(query.count()).isEqualTo(4);
assertThat(query.list())
.extracting(ExternalWorkerJob::getProcessInstanceId)
.containsOnly(processInstance1.getId(), processInstance2.getId());

query = managementService.createExternalWorkerJobQuery().processDefinitionKey("invalid");
assertThat(query.count()).isZero();
assertThat(query.list()).isEmpty();
assertThat(query.singleResult()).isNull();
}

@Test
@Deployment(resources = "org/flowable/engine/test/api/mgmt/ExternalWorkerJobQueryTest.bpmn20.xml")
public void testQueryByExecutionId() {
Expand Down
Loading

0 comments on commit 988df39

Please sign in to comment.