Skip to content

Commit

Permalink
feat(engine): query incidents by process definition key
Browse files Browse the repository at this point in the history
Related to CAM-10939
  • Loading branch information
ThorbenLindhauer authored and koevskinikola committed Oct 30, 2019
1 parent 5b31bfb commit 9458be7
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.camunda.bpm.engine.rest.dto.AbstractQueryDto;
import org.camunda.bpm.engine.rest.dto.CamundaQueryParam;
import org.camunda.bpm.engine.rest.dto.converter.BooleanConverter;
import org.camunda.bpm.engine.rest.dto.converter.StringArrayConverter;
import org.camunda.bpm.engine.rest.dto.converter.StringListConverter;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class HistoricIncidentQueryDto extends AbstractQueryDto<HistoricIncidentQ
protected String incidentType;
protected String incidentMessage;
protected String processDefinitionId;
protected String[] processDefinitionKeyIn;
protected String processInstanceId;
protected String executionId;
protected String activityId;
Expand Down Expand Up @@ -117,6 +119,11 @@ public void setProcessDefinitionId(String processDefinitionId) {
this.processDefinitionId = processDefinitionId;
}

@CamundaQueryParam(value = "processDefinitionKeyIn", converter = StringArrayConverter.class)
public void setProcessDefinitionKeyIn(String[] processDefinitionKeyIn) {
this.processDefinitionKeyIn = processDefinitionKeyIn;
}

@CamundaQueryParam("processInstanceId")
public void setProcessInstanceId(String processInstanceId) {
this.processInstanceId = processInstanceId;
Expand Down Expand Up @@ -201,6 +208,9 @@ protected void applyFilters(HistoricIncidentQuery query) {
if (processDefinitionId != null) {
query.processDefinitionId(processDefinitionId);
}
if (processDefinitionKeyIn != null && processDefinitionKeyIn.length > 0) {
query.processDefinitionKeyIn(processDefinitionKeyIn);
}
if (processInstanceId != null) {
query.processInstanceId(processInstanceId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.rest.dto.AbstractQueryDto;
import org.camunda.bpm.engine.rest.dto.CamundaQueryParam;
import org.camunda.bpm.engine.rest.dto.converter.StringArrayConverter;
import org.camunda.bpm.engine.rest.dto.converter.StringListConverter;
import org.camunda.bpm.engine.runtime.IncidentQuery;

Expand Down Expand Up @@ -70,6 +71,7 @@ public class IncidentQueryDto extends AbstractQueryDto<IncidentQuery>{
protected String incidentType;
protected String incidentMessage;
protected String processDefinitionId;
protected String[] processDefinitionKeyIn;
protected String processInstanceId;
protected String executionId;
protected String activityId;
Expand Down Expand Up @@ -105,6 +107,11 @@ public void setProcessDefinitionId(String processDefinitionId) {
this.processDefinitionId = processDefinitionId;
}

@CamundaQueryParam(value = "processDefinitionKeyIn", converter = StringArrayConverter.class)
public void setProcessDefinitionKeyIn(String[] processDefinitionKeyIn) {
this.processDefinitionKeyIn = processDefinitionKeyIn;
}

@CamundaQueryParam("processInstanceId")
public void setProcessInstanceId(String processInstanceId) {
this.processInstanceId = processInstanceId;
Expand Down Expand Up @@ -170,6 +177,9 @@ protected void applyFilters(IncidentQuery query) {
if (processDefinitionId != null) {
query.processDefinitionId(processDefinitionId);
}
if (processDefinitionKeyIn != null && processDefinitionKeyIn.length > 0) {
query.processDefinitionKeyIn(processDefinitionKeyIn);
}
if (processInstanceId != null) {
query.processInstanceId(processInstanceId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,22 @@ public void testQueryByProcessDefinitionId() {
verify(mockedQuery).processDefinitionId(processDefinitionId);
}

@Test
public void testQueryByProcessDefinitionKey() {
String key1 = "foo";
String key2 = "bar";

given()
.queryParam("processDefinitionKeyIn", key1 + "," + key2)
.then().expect().statusCode(Status.OK.getStatusCode())
.when().get(INCIDENT_QUERY_URL);

InOrder inOrder = inOrder(mockedQuery);

inOrder.verify(mockedQuery).processDefinitionKeyIn("foo", "bar");
inOrder.verify(mockedQuery).list();
}

@Test
public void testQueryByProcessInstanceId() {
String processInstanceId = MockProvider.EXAMPLE_INCIDENT_PROC_INST_ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,22 @@ public void testQueryByProcessDefinitionId() {
verify(mockedQuery).processDefinitionId(processDefinitionId);
}

@Test
public void testQueryByProcessDefinitionKey() {
String key1 = "foo";
String key2 = "bar";

given()
.queryParam("processDefinitionKeyIn", key1 + "," + key2)
.then().expect().statusCode(Status.OK.getStatusCode())
.when().get(HISTORY_INCIDENT_QUERY_URL);

InOrder inOrder = inOrder(mockedQuery);

inOrder.verify(mockedQuery).processDefinitionKeyIn("foo", "bar");
inOrder.verify(mockedQuery).list();
}

@Test
public void testQueryByProcessInstanceId() {
String processInstanceId = MockProvider.EXAMPLE_HIST_INCIDENT_PROC_INST_ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public interface HistoricIncidentQuery extends Query<HistoricIncidentQuery, Hist
/** Only select historic incidents which have the given process definition id. **/
HistoricIncidentQuery processDefinitionId(String processDefinitionId);

/** Only select historic incidents which have one of the given process definition keys. **/
HistoricIncidentQuery processDefinitionKeyIn(String... processDefinitionKeys);

/** Only select historic incidents which have the given process instance id. **/
HistoricIncidentQuery processInstanceId(String processInstanceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.camunda.bpm.engine.history.IncidentState;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
import org.camunda.bpm.engine.impl.interceptor.CommandExecutor;
import org.camunda.bpm.engine.runtime.IncidentQuery;

/**
* @author Roman Smirnov
Expand All @@ -42,6 +43,7 @@ public class HistoricIncidentQueryImpl extends AbstractVariableQueryImpl<Histori
protected String activityId;
protected String processInstanceId;
protected String processDefinitionId;
protected String[] processDefinitionKeys;
protected String causeIncidentId;
protected String rootCauseIncidentId;
protected String configuration;
Expand Down Expand Up @@ -99,6 +101,13 @@ public HistoricIncidentQuery processDefinitionId(String processDefinitionId) {
return this;
}

public HistoricIncidentQuery processDefinitionKeyIn(String... processDefinitionKeys) {
ensureNotNull("processDefinitionKeys", (Object[]) processDefinitionKeys);
this.processDefinitionKeys = processDefinitionKeys;
return this;
}


public HistoricIncidentQuery causeIncidentId(String causeIncidentId) {
ensureNotNull("causeIncidentId", causeIncidentId);
this.causeIncidentId = causeIncidentId;
Expand Down Expand Up @@ -284,6 +293,10 @@ public String getProcessDefinitionId() {
return processDefinitionId;
}

public String[] getProcessDefinitionKeys() {
return processDefinitionKeys;
}

public String getCauseIncidentId() {
return causeIncidentId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.camunda.bpm.engine.impl.interceptor.CommandExecutor;
import org.camunda.bpm.engine.runtime.Incident;
import org.camunda.bpm.engine.runtime.IncidentQuery;
import org.camunda.bpm.engine.runtime.ProcessInstanceQuery;

/**
* @author roman.smirnov
Expand All @@ -40,6 +41,7 @@ public class IncidentQueryImpl extends AbstractQuery<IncidentQuery, Incident> im
protected String activityId;
protected String processInstanceId;
protected String processDefinitionId;
protected String[] processDefinitionKeys;
protected String causeIncidentId;
protected String rootCauseIncidentId;
protected String configuration;
Expand Down Expand Up @@ -88,6 +90,12 @@ public IncidentQuery processDefinitionId(String processDefinitionId) {
return this;
}

public IncidentQuery processDefinitionKeyIn(String... processDefinitionKeys) {
ensureNotNull("processDefinitionKeys", (Object[]) processDefinitionKeys);
this.processDefinitionKeys = processDefinitionKeys;
return this;
}

public IncidentQuery causeIncidentId(String causeIncidentId) {
this.causeIncidentId = causeIncidentId;
return this;
Expand Down Expand Up @@ -194,4 +202,8 @@ public List<Incident> executeList(CommandContext commandContext, Page page) {
.findIncidentByQueryCriteria(this, page);
}

public String[] getProcessDefinitionKeys() {
return processDefinitionKeys;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public interface IncidentQuery extends Query<IncidentQuery, Incident> {
/** Only select incidents which have the given process definition id. **/
IncidentQuery processDefinitionId(String processDefinitionId);

/** Only select incidents which have one of the given process definition keys. **/
IncidentQuery processDefinitionKeyIn(String... processDefinitionKeys);

/** Only select incidents which have the given process instance id. **/
IncidentQuery processInstanceId(String processInstanceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@
<if test="processDefinitionId != null">
and RES.PROC_DEF_ID_ = #{processDefinitionId}
</if>
<if test="processDefinitionKeys != null &amp;&amp; processDefinitionKeys.length > 0">
and RES.PROC_DEF_KEY_ in
<foreach item="item" index="index" collection="processDefinitionKeys"
open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="causeIncidentId != null">
and RES.CAUSE_INCIDENT_ID_ = #{causeIncidentId}
</if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@
<sql id="selectIncidentByQueryCriteriaSql">
from ${prefix}ACT_RU_INCIDENT RES

<if test="authCheck.shouldPerformAuthorizatioCheck &amp;&amp; authCheck.authUserId != null">
<if test="(processDefinitionKeys != null &amp;&amp; processDefinitionKeys.length > 0)
|| (authCheck.shouldPerformAuthorizatioCheck &amp;&amp; authCheck.authUserId != null)">
left join ${prefix}ACT_RE_PROCDEF PROCDEF
on RES.PROC_DEF_ID_ = PROCDEF.ID_
</if>
Expand All @@ -154,6 +155,13 @@
<if test="processDefinitionId != null">
and RES.PROC_DEF_ID_ = #{processDefinitionId}
</if>
<if test="processDefinitionKeys != null &amp;&amp; processDefinitionKeys.length > 0">
and PROCDEF.KEY_ in
<foreach item="item" index="index" collection="processDefinitionKeys"
open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="causeIncidentId != null">
and RES.CAUSE_INCIDENT_ID_ = #{causeIncidentId}
</if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.RuleChain;

/**
Expand All @@ -70,6 +71,9 @@ public class IncidentQueryTest {
@Rule
public RuleChain chain = RuleChain.outerRule(engineRule).around(testHelper);

@Rule
public ExpectedException exception = ExpectedException.none();

private List<String> processInstanceIds;

protected RuntimeService runtimeService;
Expand Down Expand Up @@ -156,6 +160,7 @@ public void testQueryByInvalidIncidentMessage() {
assertNull(incident);
}

@Test
public void testQueryByProcessDefinitionId() {
String processDefinitionId = engineRule.getRepositoryService().createProcessDefinitionQuery().singleResult().getId();

Expand All @@ -180,6 +185,58 @@ public void testQueryByInvalidProcessDefinitionId() {
assertNull(incident);
}

@Test
public void testQueryByProcessDefinitionKeys() {
// given
// 4 failed instances of "process"

// one incident of each of the following processes
testHelper.deploy(Bpmn.createExecutableProcess("proc1").startEvent().userTask().endEvent().done());
ProcessInstance instance5 = runtimeService.startProcessInstanceByKey("proc1");
Incident incident5 = runtimeService.createIncident("foo", instance5.getId(), "a");

testHelper.deploy(Bpmn.createExecutableProcess("proc2").startEvent().userTask().endEvent().done());
ProcessInstance instance6 = runtimeService.startProcessInstanceByKey("proc2");
Incident incident6 = runtimeService.createIncident("foo", instance6.getId(), "b");

// when
List<Incident> incidents = runtimeService.createIncidentQuery()
.processDefinitionKeyIn("proc1", "proc2")
.orderByConfiguration()
.asc()
.list();

// then
assertThat(incidents).hasSize(2);
assertThat(incidents.get(0).getId()).isEqualTo(incident5.getId());
assertThat(incidents.get(1).getId()).isEqualTo(incident6.getId());
}

@Test
public void testQueryByInvalidProcessDefinitionKeys() {
// given
IncidentQuery incidentQuery = runtimeService.createIncidentQuery();

// then
exception.expect(ProcessEngineException.class);

// when
incidentQuery.processDefinitionKeyIn((String[]) null);
}

@Test
public void testQueryByOneInvalidProcessDefinitionKey() {
// given
IncidentQuery incidentQuery = runtimeService.createIncidentQuery();

// then
exception.expect(ProcessEngineException.class);

// when
incidentQuery.processDefinitionKeyIn((String) null);
}

@Test
public void testQueryByProcessInstanceId() {
IncidentQuery query = runtimeService.createIncidentQuery().processInstanceId(processInstanceIds.get(0));

Expand Down Expand Up @@ -233,6 +290,7 @@ public void testQueryByInvalidIncidentId() {
assertNull(incident);
}

@Test
public void testQueryByExecutionId() {
Execution execution = runtimeService.createExecutionQuery().processInstanceId(processInstanceIds.get(0)).singleResult();
assertNotNull(execution);
Expand All @@ -259,8 +317,9 @@ public void testQueryByInvalidExecutionId() {
assertNull(incident);
}

@Test
public void testQueryByActivityId() {
IncidentQuery query = runtimeService.createIncidentQuery().activityId("theServiceTask");
IncidentQuery query = runtimeService.createIncidentQuery().activityId("task");
assertEquals(4, query.count());

List<Incident> incidents = query.list();
Expand Down
Loading

0 comments on commit 9458be7

Please sign in to comment.