Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/git issue 2334 fetch variable process instance api enhancement #4641

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,17 @@
desc = "Match all variable names in this query case-insensitively.
If set to true variableName and variablename are treated as equal."/>

<@lib.parameter name = "withVariablesInReturn"
location = "query"
type = "boolean"
defaultValue = 'false'
desc = "Indicates if the variables associated with the process instance should be returned.
Value may only be true, as false is the default behavior."/>

<@lib.parameter name = "variableValuesIgnoreCase"
location = "query"
type = "boolean"
defaultValue = 'false'
last = last
desc = "Match all variable values in this query case-insensitively.
If set to true variableValue and variablevalue are treated as equal."/>
If set to true variableValue and variablevalue are treated as equal."/>
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
desc = "Match all variable values in this query case-insensitively.
If set to true variableValue and variablevalue are treated as equal." />

<@lib.property
name = "withVariablesInReturn"
type = "boolean"
desc = "Indicates if the variables associated with the process instance should be returned.
Value may only be true, as false is the default behavior." />

<@lib.property
name = "orQueries"
type = "array"
Expand All @@ -196,4 +202,4 @@

</@lib.dto>

</#macro>
</#macro>
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class ProcessInstanceQueryDto extends AbstractQueryDto<ProcessInstanceQue
private Boolean rootProcessInstances;
private Boolean leafProcessInstances;
private Boolean isProcessDefinitionWithoutTenantId;
private Boolean withVariablesInReturn;

protected Boolean variableNamesIgnoreCase;
protected Boolean variableValuesIgnoreCase;
Expand Down Expand Up @@ -367,6 +368,15 @@ public void setProcessDefinitionWithoutTenantId(Boolean isProcessDefinitionWitho
this.isProcessDefinitionWithoutTenantId = isProcessDefinitionWithoutTenantId;
}

public Boolean isWithVariablesInReturn() {
return withVariablesInReturn;
}

@CamundaQueryParam(value = "withVariablesInReturn", converter = BooleanConverter.class)
public void setWithVariablesInReturn(Boolean withVariablesInReturn) {
this.withVariablesInReturn = withVariablesInReturn;
}

@Override
protected boolean isValidSortByValue(String value) {
return VALID_SORT_BY_VALUES.contains(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.camunda.bpm.engine.rest.dto.runtime.batch.CorrelationMessageAsyncDto;
import org.camunda.bpm.engine.rest.dto.runtime.batch.DeleteProcessInstancesDto;
import org.camunda.bpm.engine.rest.dto.runtime.batch.SetVariablesAsyncDto;
import org.camunda.bpm.engine.rest.dto.runtime.ProcessInstanceWithVariablesDto;
import org.camunda.bpm.engine.rest.exception.InvalidRequestException;
import org.camunda.bpm.engine.rest.exception.RestException;
import org.camunda.bpm.engine.rest.sub.runtime.ProcessInstanceResource;
Expand Down Expand Up @@ -75,7 +76,7 @@ public List<ProcessInstanceDto> getProcessInstances(

@Override
public List<ProcessInstanceDto> queryProcessInstances(
ProcessInstanceQueryDto queryDto, Integer firstResult, Integer maxResults) {
ProcessInstanceQueryDto queryDto, Integer firstResult, Integer maxResults) {
ProcessEngine engine = getProcessEngine();
queryDto.setObjectMapper(getObjectMapper());
ProcessInstanceQuery query = queryDto.toQuery(engine);
Expand All @@ -84,8 +85,18 @@ public List<ProcessInstanceDto> queryProcessInstances(

List<ProcessInstanceDto> instanceResults = new ArrayList<>();
for (ProcessInstance instance : matchingInstances) {
ProcessInstanceDto resultInstance = ProcessInstanceDto.fromProcessInstance(instance);
instanceResults.add(resultInstance);
if (null!= queryDto.isWithVariablesInReturn() && queryDto.isWithVariablesInReturn()){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please make sure your code changes are formatted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @venetrius,Would you wish to provide me all the comments at once so that I can push the changes in one commit.Thanks.

RuntimeService runtimeService = engine.getRuntimeService();
VariableMap variableMap = (VariableMap) runtimeService.getVariables(instance.getProcessInstanceId());
Map<String, VariableValueDto> variableValueDtoMap= VariableValueDto.fromMap(variableMap);
ProcessInstanceWithVariablesDto resultInstanceWithVariable = new ProcessInstanceWithVariablesDto(instance);
resultInstanceWithVariable.setVariables(variableValueDtoMap);
instanceResults.add(resultInstanceWithVariable);
}
else {
ProcessInstanceDto resultInstance = ProcessInstanceDto.fromProcessInstance(instance);
instanceResults.add(resultInstance);
}
}
return instanceResults;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
import static org.mockito.Mockito.mockStatic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
Expand Down Expand Up @@ -99,6 +100,7 @@
import org.camunda.bpm.engine.rest.helper.variable.EqualsUntypedValue;
import org.camunda.bpm.engine.rest.util.JsonPathUtil;
import org.camunda.bpm.engine.rest.util.ModificationInstructionBuilder;
import org.camunda.bpm.engine.rest.util.QueryUtil;
import org.camunda.bpm.engine.rest.util.VariablesBuilder;
import org.camunda.bpm.engine.rest.util.container.TestContainerRule;
import org.camunda.bpm.engine.runtime.MessageCorrelationAsyncBuilder;
Expand All @@ -125,6 +127,7 @@
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.mockito.MockedStatic;

public class ProcessInstanceRestServiceInteractionTest extends AbstractRestServiceTest {

Expand Down Expand Up @@ -4551,4 +4554,75 @@ private void verifyTaskComments(List<Comment> mockTaskComments, Response respons
assertEquals(mockComment.getTime(), returnedTime);
assertEquals(mockComment.getFullMessage(), returnedFullMessage);
}

@Test
public void testWithVariablesInReturnSetToFalseGetProcessList() {
List<ProcessInstance> mockProcessInstanceList = new ArrayList<>();
ProcessInstance mockInstance = MockProvider.createMockInstance();
mockProcessInstanceList.add(mockInstance);

ProcessInstanceQuery sampleInstanceQuery = mock(ProcessInstanceQuery.class);

MockedStatic<QueryUtil> mockedStatic = mockStatic(QueryUtil.class);
Mockito.when(QueryUtil.list(sampleInstanceQuery, 0, 1)).thenReturn(mockProcessInstanceList);
assertEquals(1, QueryUtil.list(sampleInstanceQuery, 0, 1).size());

when(runtimeServiceMock.createProcessInstanceQuery()).thenReturn(sampleInstanceQuery);
when(sampleInstanceQuery.processInstanceId(anyString())).thenReturn(sampleInstanceQuery);
when(sampleInstanceQuery.listPage(0, 1)).thenReturn(mockProcessInstanceList);

VariableMap mockInstanceWithVariable = MockProvider.createMockSerializedVariables();
when(runtimeServiceMock.getVariables(anyString())).thenReturn(mockInstanceWithVariable);

given()
.queryParam("withVariablesInReturn", false)
.queryParam("firstResult", 0) .queryParam("maxResults", 1)
.then().expect().statusCode(Status.OK.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
.body("[0].id", Matchers.equalTo(MockProvider.EXAMPLE_PROCESS_INSTANCE_ID))
.body("[0].definitionId", Matchers.equalTo(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID))
.body("[0].businessKey", Matchers.equalTo(MockProvider.EXAMPLE_PROCESS_INSTANCE_BUSINESS_KEY))
.body("[0].suspended", Matchers.equalTo(MockProvider.EXAMPLE_PROCESS_INSTANCE_IS_SUSPENDED))
.body("[0].tenantId", Matchers.equalTo(MockProvider.EXAMPLE_TENANT_ID))
.body("[0].variables", Matchers.nullValue())
.when().get(PROCESS_INSTANCE_URL);

mockedStatic.close();

}

@Test
public void testWithVariablesInReturnSetToTrueGetProcessList() {
List<ProcessInstance> mockProcessInstanceList = new ArrayList<>();
ProcessInstance mockInstance = MockProvider.createMockInstance();
mockProcessInstanceList.add(mockInstance);

ProcessInstanceQuery sampleInstanceQuery = mock(ProcessInstanceQuery.class);

MockedStatic<QueryUtil> mockedStatic = mockStatic(QueryUtil.class);
Mockito.when(QueryUtil.list(sampleInstanceQuery, 0, 1)).thenReturn(mockProcessInstanceList);
assertEquals(1, QueryUtil.list(sampleInstanceQuery, 0, 1).size());

when(runtimeServiceMock.createProcessInstanceQuery()).thenReturn(sampleInstanceQuery);
when(sampleInstanceQuery.processInstanceId(anyString())).thenReturn(sampleInstanceQuery);
when(sampleInstanceQuery.listPage(0, 1)).thenReturn(mockProcessInstanceList);

VariableMap mockInstanceWithVariable = MockProvider.createMockSerializedVariables();
when(runtimeServiceMock.getVariables(anyString())).thenReturn(mockInstanceWithVariable);

given()
.queryParam("withVariablesInReturn", true)
.queryParam("firstResult", 0) .queryParam("maxResults", 1)
.then().expect().statusCode(Status.OK.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
.body("[0].id", Matchers.equalTo(MockProvider.EXAMPLE_PROCESS_INSTANCE_ID))
.body("[0].definitionId", Matchers.equalTo(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID))
.body("[0].businessKey", Matchers.equalTo(MockProvider.EXAMPLE_PROCESS_INSTANCE_BUSINESS_KEY))
.body("[0].suspended", Matchers.equalTo(MockProvider.EXAMPLE_PROCESS_INSTANCE_IS_SUSPENDED))
.body("[0].tenantId", Matchers.equalTo(MockProvider.EXAMPLE_TENANT_ID))
.body("[0].variables", Matchers.notNullValue())
.when().get(PROCESS_INSTANCE_URL);

mockedStatic.close();
}
}