Skip to content

Commit

Permalink
flowable#3692: Query jobs by case definition key
Browse files Browse the repository at this point in the history
  • Loading branch information
amporsim committed Nov 27, 2023
1 parent c0a4253 commit 0c61c2a
Show file tree
Hide file tree
Showing 17 changed files with 391 additions and 2 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,46 @@
/* 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.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();

assertThat(cmmnManagementService.createJobQuery().caseDefinitionKey("timerJobQueryTest").count()).isEqualTo(1);
assertThat(cmmnManagementService.createJobQuery().caseDefinitionKey("timerJobQueryTest").singleResult().getScopeId()).isEqualTo(caseInstance.getId());
assertThat(cmmnManagementService.createJobQuery().caseDefinitionKey("timerJobQueryTest").list()).extracting(Job::getId)
.containsExactly(executableJob.getId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* 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.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;
});

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* 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())
);
}

}
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 @@ -121,6 +121,11 @@ public interface BaseJobQuery<U extends BaseJobQuery<U, T>, T extends Job> exten
*/
U scopeDefinitionId(String scopeDefinitionId);

/**
* Only select tasks for the given case definition key.
*/
U caseDefinitionKey(String caseDefinitionKey);

/**
* Only select jobs for the given case instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class DeadLetterJobQueryImpl extends AbstractQuery<DeadLetterJobQuery, Jo
protected String scopeType;
protected boolean withoutScopeType;
protected String scopeDefinitionId;
protected String caseDefinitionKey;
protected String correlationId;
protected boolean executable;
protected boolean onlyTimers;
Expand Down Expand Up @@ -217,7 +218,7 @@ public DeadLetterJobQueryImpl scopeDefinitionId(String scopeDefinitionId) {
this.scopeDefinitionId = scopeDefinitionId;
return this;
}

@Override
public DeadLetterJobQuery caseInstanceId(String caseInstanceId) {
if (caseInstanceId == null) {
Expand All @@ -237,6 +238,15 @@ public DeadLetterJobQuery caseDefinitionId(String caseDefinitionId) {
scopeType(ScopeTypes.CMMN);
return this;
}

@Override
public DeadLetterJobQueryImpl caseDefinitionKey(String caseDefinitionKey) {
if (caseDefinitionKey == null) {
throw new FlowableIllegalArgumentException("Provided case definition key null");
}
this.caseDefinitionKey = caseDefinitionKey;
return this;
}

@Override
public DeadLetterJobQuery planItemInstanceId(String planItemInstanceId) {
Expand Down Expand Up @@ -515,7 +525,7 @@ public String getElementName() {
public String getScopeId() {
return scopeId;
}

public boolean isWithoutScopeId() {
return withoutScopeId;
}
Expand All @@ -536,6 +546,10 @@ public String getScopeDefinitionId() {
return scopeDefinitionId;
}

public String getCaseDefinitionKey() {
return caseDefinitionKey;
}

public String getCorrelationId() {
return correlationId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class ExternalWorkerJobQueryImpl extends AbstractQuery<ExternalWorkerJobQ
protected String scopeType;
protected boolean withoutScopeType;
protected String scopeDefinitionId;
protected String caseDefinitionKey;
protected String correlationId;
protected Date duedateHigherThan;
protected Date duedateLowerThan;
Expand Down Expand Up @@ -240,6 +241,15 @@ public ExternalWorkerJobQuery caseDefinitionId(String caseDefinitionId) {
return this;
}

@Override
public ExternalWorkerJobQuery caseDefinitionKey(String caseDefinitionKey) {
if (caseDefinitionKey == null) {
throw new FlowableIllegalArgumentException("Provided case definition key is null");
}
this.caseDefinitionKey = caseDefinitionKey;
return this;
}

@Override
public ExternalWorkerJobQuery planItemInstanceId(String planItemInstanceId) {
if (planItemInstanceId == null) {
Expand Down Expand Up @@ -524,6 +534,10 @@ public String getScopeDefinitionId() {
return scopeDefinitionId;
}

public String getCaseDefinitionKey() {
return caseDefinitionKey;
}

public String getCorrelationId() {
return correlationId;
}
Expand Down

0 comments on commit 0c61c2a

Please sign in to comment.