@Before public void setUpRuntimeData() { runtimeServiceMock = mock(RuntimeService.class); when(processEngine.getRuntimeService()).thenReturn(runtimeServiceMock); historyServiceMock = mock(HistoryService.class); when(processEngine.getHistoryService()).thenReturn(historyServiceMock); builderMock = mock(RestartProcessInstanceBuilder.class); when(builderMock.startAfterActivity(anyString())).thenReturn(builderMock); when(builderMock.startBeforeActivity(anyString())).thenReturn(builderMock); when(builderMock.startTransition(anyString())).thenReturn(builderMock); when(builderMock.processInstanceIds(anyListOf(String.class))).thenReturn(builderMock); when(builderMock.historicProcessInstanceQuery(any(HistoricProcessInstanceQuery.class))).thenReturn(builderMock); when(builderMock.skipCustomListeners()).thenReturn(builderMock); when(builderMock.skipIoMappings()).thenReturn(builderMock); when(builderMock.initialSetOfVariables()).thenReturn(builderMock); when(builderMock.withoutBusinessKey()).thenReturn(builderMock); Batch batchMock = createMockBatch(); when(builderMock.executeAsync()).thenReturn(batchMock); when(runtimeServiceMock.restartProcessInstances(anyString())).thenReturn(builderMock); }
@Test public void testRestartProcessInstanceWithoutProcessInstanceIdsAsync() { when(historyServiceMock.createHistoricProcessInstanceQuery()).thenReturn(new HistoricProcessInstanceQueryImpl()); Map<String, Object> json = new HashMap<String, Object>(); List<Map<String, Object>> instructions = new ArrayList<Map<String, Object>>(); HistoricProcessInstanceQueryDto query = new HistoricProcessInstanceQueryDto(); query.setFinished(true); json.put("historicProcessInstanceQuery", query); instructions.add(ModificationInstructionBuilder.startBefore().activityId("activityId").getJson()); json.put("instructions", instructions); given() .pathParam("id", MockProvider.EXAMPLE_PROCESS_DEFINITION_ID) .contentType(ContentType.JSON) .body(json) .then() .expect() .statusCode(Status.OK.getStatusCode()) .when() .post(RESTART_PROCESS_INSTANCE_ASYNC_URL); verify(builderMock).startBeforeActivity("activityId"); verify(builderMock).historicProcessInstanceQuery(query.toQuery(processEngine)); verify(builderMock).executeAsync(); verifyNoMoreInteractions(builderMock); } }
@Test public void testRestartProcessInstanceWithoutProcessInstanceIdsSync() { when(historyServiceMock.createHistoricProcessInstanceQuery()).thenReturn(new HistoricProcessInstanceQueryImpl()); Map<String, Object> json = new HashMap<String, Object>(); List<Map<String, Object>> instructions = new ArrayList<Map<String, Object>>(); HistoricProcessInstanceQueryDto query = new HistoricProcessInstanceQueryDto(); query.setFinished(true); json.put("historicProcessInstanceQuery", query); instructions.add(ModificationInstructionBuilder.startBefore().activityId("activityId").getJson()); json.put("instructions", instructions); given() .pathParam("id", MockProvider.EXAMPLE_PROCESS_DEFINITION_ID) .contentType(ContentType.JSON) .body(json) .then() .expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .post(RESTART_PROCESS_INSTANCE_URL); verify(builderMock).startBeforeActivity("activityId"); verify(builderMock).historicProcessInstanceQuery(query.toQuery(processEngine)); verify(builderMock).execute(); verifyNoMoreInteractions(builderMock); }
@Test public void testRestartProcessInstanceWithHistoricProcessInstanceQuerySync() { when(historyServiceMock.createHistoricProcessInstanceQuery()).thenReturn(new HistoricProcessInstanceQueryImpl()); HashMap<String, Object> json = new HashMap<String, Object>(); List<Map<String, Object>> instructions = new ArrayList<Map<String, Object>>(); instructions.add(ModificationInstructionBuilder.startBefore().activityId("activityId").getJson()); json.put("instructions", instructions); HistoricProcessInstanceQueryDto query = new HistoricProcessInstanceQueryDto(); query.setProcessInstanceBusinessKey("businessKey"); json.put("historicProcessInstanceQuery", query); given() .pathParam("id", MockProvider.EXAMPLE_PROCESS_DEFINITION_ID) .contentType(ContentType.JSON) .body(json) .then().expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .post(RESTART_PROCESS_INSTANCE_URL); verify(runtimeServiceMock).restartProcessInstances(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID); verify(builderMock).startBeforeActivity("activityId"); verify(builderMock).historicProcessInstanceQuery(query.toQuery(processEngine)); verify(builderMock).execute(); }
@Test public void testRestartProcessInstanceWithHistoricProcessInstanceQueryAsync() { when(historyServiceMock.createHistoricProcessInstanceQuery()).thenReturn(new HistoricProcessInstanceQueryImpl()); HashMap<String, Object> json = new HashMap<String, Object>(); List<Map<String, Object>> instructions = new ArrayList<Map<String, Object>>(); instructions.add(ModificationInstructionBuilder.startBefore().activityId("activityId").getJson()); json.put("instructions", instructions); HistoricProcessInstanceQueryDto query = new HistoricProcessInstanceQueryDto(); query.setProcessInstanceBusinessKey("businessKey"); json.put("historicProcessInstanceQuery", query); given() .pathParam("id", MockProvider.EXAMPLE_PROCESS_DEFINITION_ID) .contentType(ContentType.JSON) .body(json) .then().expect() .statusCode(Status.OK.getStatusCode()) .when() .post(RESTART_PROCESS_INSTANCE_ASYNC_URL); verify(runtimeServiceMock).restartProcessInstances(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID); verify(builderMock).startBeforeActivity("activityId"); verify(builderMock).historicProcessInstanceQuery(query.toQuery(processEngine)); verify(builderMock).executeAsync(); }
@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_FULL) public void testFailToRestartProcessInstanceAsyncWithOtherTenantIdByHistoricProcessInstanceQuery() { // given ProcessInstance processInstance = startAndDeleteProcessInstance(TENANT_ONE, PROCESS); HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processDefinitionId(processInstance.getProcessDefinitionId()); identityService.setAuthentication("user", null, Collections.singletonList(TENANT_TWO)); try { // when runtimeService.restartProcessInstances(processInstance.getProcessDefinitionId()) .startBeforeActivity("userTask") .historicProcessInstanceQuery(query) .executeAsync(); } catch (ProcessEngineException e) { assertThat(e.getMessage(), containsString("processInstanceIds is empty")); } }
@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_FULL) public void testFailToRestartProcessInstanceSyncWithOtherTenantIdByHistoricProcessInstanceQuery() { // given ProcessInstance processInstance = startAndDeleteProcessInstance(TENANT_ONE, PROCESS); HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processDefinitionId(processInstance.getProcessDefinitionId()); identityService.setAuthentication("user", null, Collections.singletonList(TENANT_TWO)); try { // when runtimeService.restartProcessInstances(processInstance.getProcessDefinitionId()) .startBeforeActivity("userTask") .historicProcessInstanceQuery(query) .execute(); fail("expected exception"); } catch (BadUserRequestException e) { // then assertThat(e.getMessage(), containsString("processInstanceIds is empty")); } }
private RestartProcessInstanceBuilder createRestartProcessInstanceBuilder(RestartProcessInstanceDto restartProcessInstanceDto) { RuntimeService runtimeService = engine.getRuntimeService(); RestartProcessInstanceBuilder builder = runtimeService .restartProcessInstances(processDefinitionId); if (restartProcessInstanceDto.getProcessInstanceIds() != null) { builder.processInstanceIds(restartProcessInstanceDto.getProcessInstanceIds()); } if (restartProcessInstanceDto.getHistoricProcessInstanceQuery() != null) { builder.historicProcessInstanceQuery(restartProcessInstanceDto.getHistoricProcessInstanceQuery().toQuery(engine)); } if (restartProcessInstanceDto.isInitialVariables()) { builder.initialSetOfVariables(); } if (restartProcessInstanceDto.isWithoutBusinessKey()) { builder.withoutBusinessKey(); } if (restartProcessInstanceDto.isSkipCustomListeners()) { builder.skipCustomListeners(); } if (restartProcessInstanceDto.isSkipIoMappings()) { builder.skipIoMappings(); } restartProcessInstanceDto.applyTo(builder, engine, objectMapper); return builder; }
private RestartProcessInstanceBuilder createRestartProcessInstanceBuilder(RestartProcessInstanceDto restartProcessInstanceDto) { RuntimeService runtimeService = engine.getRuntimeService(); RestartProcessInstanceBuilder builder = runtimeService .restartProcessInstances(processDefinitionId); if (restartProcessInstanceDto.getProcessInstanceIds() != null) { builder.processInstanceIds(restartProcessInstanceDto.getProcessInstanceIds()); } if (restartProcessInstanceDto.getHistoricProcessInstanceQuery() != null) { builder.historicProcessInstanceQuery(restartProcessInstanceDto.getHistoricProcessInstanceQuery().toQuery(engine)); } if (restartProcessInstanceDto.isInitialVariables()) { builder.initialSetOfVariables(); } if (restartProcessInstanceDto.isWithoutBusinessKey()) { builder.withoutBusinessKey(); } if (restartProcessInstanceDto.isSkipCustomListeners()) { builder.skipCustomListeners(); } if (restartProcessInstanceDto.isSkipIoMappings()) { builder.skipIoMappings(); } restartProcessInstanceDto.applyTo(builder, engine, objectMapper); return builder; }
@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_FULL) public void testRestartProcessInstanceSyncWithTenantIdByHistoricProcessInstanceQuery() { // given ProcessInstance processInstance = startAndDeleteProcessInstance(TENANT_ONE, PROCESS); HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processDefinitionId(processInstance.getProcessDefinitionId()); identityService.setAuthentication("user", null, Collections.singletonList(TENANT_ONE)); // when runtimeService.restartProcessInstances(processInstance.getProcessDefinitionId()) .startBeforeActivity("userTask") .historicProcessInstanceQuery(query) .execute(); // then ProcessInstance restartedInstance = runtimeService.createProcessInstanceQuery().active() .processDefinitionId(processInstance.getProcessDefinitionId()).singleResult(); assertNotNull(restartedInstance); assertEquals(restartedInstance.getTenantId(), TENANT_ONE); }
@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_FULL) public void testRestartProcessInstanceAsyncWithTenantIdByHistoricProcessInstanceQuery() { // given ProcessInstance processInstance = startAndDeleteProcessInstance(TENANT_ONE, PROCESS); HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processDefinitionId(processInstance.getProcessDefinitionId()); identityService.setAuthentication("user", null, Collections.singletonList(TENANT_ONE)); // when Batch batch = runtimeService.restartProcessInstances(processInstance.getProcessDefinitionId()) .startBeforeActivity("userTask") .historicProcessInstanceQuery(query) .executeAsync(); batchHelper.completeBatch(batch); // then ProcessInstance restartedInstance = runtimeService.createProcessInstanceQuery().active() .processDefinitionId(processInstance.getProcessDefinitionId()).singleResult(); assertNotNull(restartedInstance); assertEquals(restartedInstance.getTenantId(), TENANT_ONE); }
@Test public void testBatchCreationWithOverlappingProcessInstanceIdsAndQuery() { // given int processInstanceCount = 2; ProcessDefinition processDefinition = testRule.deployAndGetDefinition(ProcessModels.TWO_TASKS_PROCESS); ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("Process"); ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("Process"); runtimeService.deleteProcessInstance(processInstance1.getId(), "test"); runtimeService.deleteProcessInstance(processInstance2.getId(), "test"); // when HistoricProcessInstanceQuery processInstanceQuery = engineRule.getHistoryService() .createHistoricProcessInstanceQuery() .processDefinitionId(processDefinition.getId()); Batch batch = runtimeService .restartProcessInstances(processDefinition.getId()) .startTransition("flow1") .processInstanceIds(processInstance1.getId(), processInstance2.getId()) .historicProcessInstanceQuery(processInstanceQuery) .executeAsync(); helper.completeBatch(batch); // then List<ProcessInstance> restartedProcessInstances = engineRule.getRuntimeService().createProcessInstanceQuery().processDefinitionId(processDefinition.getId()).list(); assertEquals(restartedProcessInstances.size(), processInstanceCount); }
@Test public void shouldRestartProcessInstanceUsingHistoricProcessInstanceQuery() { // given ProcessDefinition processDefinition = testRule.deployAndGetDefinition(ProcessModels.TWO_TASKS_PROCESS); ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("Process"); Task task1 = taskService.createTaskQuery().processInstanceId(processInstance1.getId()).active().singleResult(); ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("Process"); Task task2 = taskService.createTaskQuery().processInstanceId(processInstance2.getId()).active().singleResult(); runtimeService.deleteProcessInstance(processInstance1.getId(), "test"); runtimeService.deleteProcessInstance(processInstance2.getId(), "test"); // when HistoricProcessInstanceQuery historicProcessInstanceQuery = engineRule.getHistoryService().createHistoricProcessInstanceQuery().processDefinitionId(processDefinition.getId()); Batch batch = runtimeService.restartProcessInstances(processDefinition.getId()) .startBeforeActivity("userTask1") .historicProcessInstanceQuery(historicProcessInstanceQuery) .executeAsync(); helper.completeBatch(batch); // then List<ProcessInstance> restartedProcessInstances = runtimeService.createProcessInstanceQuery().active().list(); ProcessInstance restartedProcessInstance = restartedProcessInstances.get(0); Task restartedTask = taskService.createTaskQuery().processInstanceId(restartedProcessInstance.getId()).active().singleResult(); Assert.assertEquals(task1.getTaskDefinitionKey(), restartedTask.getTaskDefinitionKey()); restartedProcessInstance = restartedProcessInstances.get(1); restartedTask = taskService.createTaskQuery().processInstanceId(restartedProcessInstance.getId()).active().singleResult(); Assert.assertEquals(task2.getTaskDefinitionKey(), restartedTask.getTaskDefinitionKey()); }
@Test public void shouldRestartProcessInstanceUsingHistoricProcessInstanceQuery() { // given ProcessDefinition processDefinition = testRule.deployAndGetDefinition(ProcessModels.TWO_TASKS_PROCESS); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Process"); runtimeService.deleteProcessInstance(processInstance.getId(), "test"); // when HistoricProcessInstanceQuery historicProcessInstanceQuery = engineRule.getHistoryService() .createHistoricProcessInstanceQuery() .processDefinitionId(processDefinition.getId()); runtimeService.restartProcessInstances(processDefinition.getId()) .startBeforeActivity("userTask1") .historicProcessInstanceQuery(historicProcessInstanceQuery) .execute(); // then ProcessInstance restartedProcessInstance = runtimeService.createProcessInstanceQuery().active().singleResult(); ActivityInstance updatedTree = runtimeService.getActivityInstance(restartedProcessInstance.getId()); assertNotNull(updatedTree); assertEquals(restartedProcessInstance.getId(), updatedTree.getProcessInstanceId()); assertThat(updatedTree).hasStructure( describeActivityInstanceTree( processDefinition.getId()) .activity("userTask1") .done()); }
@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_FULL) public void testFailToRestartProcessInstanceAsyncWithOtherTenantIdByHistoricProcessInstanceQuery() { // given ProcessInstance processInstance = startAndDeleteProcessInstance(TENANT_ONE, PROCESS); HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processDefinitionId(processInstance.getProcessDefinitionId()); identityService.setAuthentication("user", null, Collections.singletonList(TENANT_TWO)); try { // when runtimeService.restartProcessInstances(processInstance.getProcessDefinitionId()) .startBeforeActivity("userTask") .historicProcessInstanceQuery(query) .executeAsync(); } catch (ProcessEngineException e) { assertThat(e.getMessage(), containsString("processInstanceIds is empty")); } }
@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_FULL) public void testFailToRestartProcessInstanceSyncWithOtherTenantIdByHistoricProcessInstanceQuery() { // given ProcessInstance processInstance = startAndDeleteProcessInstance(TENANT_ONE, PROCESS); HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processDefinitionId(processInstance.getProcessDefinitionId()); identityService.setAuthentication("user", null, Collections.singletonList(TENANT_TWO)); try { // when runtimeService.restartProcessInstances(processInstance.getProcessDefinitionId()) .startBeforeActivity("userTask") .historicProcessInstanceQuery(query) .execute(); fail("expected exception"); } catch (BadUserRequestException e) { // then assertThat(e.getMessage(), containsString("processInstanceIds is empty")); } }
@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_FULL) public void testRestartProcessInstanceSyncWithTenantIdByHistoricProcessInstanceQuery() { // given ProcessInstance processInstance = startAndDeleteProcessInstance(TENANT_ONE, PROCESS); HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processDefinitionId(processInstance.getProcessDefinitionId()); identityService.setAuthentication("user", null, Collections.singletonList(TENANT_ONE)); // when runtimeService.restartProcessInstances(processInstance.getProcessDefinitionId()) .startBeforeActivity("userTask") .historicProcessInstanceQuery(query) .execute(); // then ProcessInstance restartedInstance = runtimeService.createProcessInstanceQuery().active() .processDefinitionId(processInstance.getProcessDefinitionId()).singleResult(); assertNotNull(restartedInstance); assertEquals(restartedInstance.getTenantId(), TENANT_ONE); }
@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_FULL) public void testRestartProcessInstanceAsyncWithTenantIdByHistoricProcessInstanceQuery() { // given ProcessInstance processInstance = startAndDeleteProcessInstance(TENANT_ONE, PROCESS); HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().processDefinitionId(processInstance.getProcessDefinitionId()); identityService.setAuthentication("user", null, Collections.singletonList(TENANT_ONE)); // when Batch batch = runtimeService.restartProcessInstances(processInstance.getProcessDefinitionId()) .startBeforeActivity("userTask") .historicProcessInstanceQuery(query) .executeAsync(); batchHelper.completeBatch(batch); // then ProcessInstance restartedInstance = runtimeService.createProcessInstanceQuery().active() .processDefinitionId(processInstance.getProcessDefinitionId()).singleResult(); assertNotNull(restartedInstance); assertEquals(restartedInstance.getTenantId(), TENANT_ONE); }
@Test public void testBatchCreationWithOverlappingProcessInstanceIdsAndQuery() { // given int processInstanceCount = 2; ProcessDefinition processDefinition = testRule.deployAndGetDefinition(ProcessModels.TWO_TASKS_PROCESS); ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("Process"); ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("Process"); runtimeService.deleteProcessInstance(processInstance1.getId(), "test"); runtimeService.deleteProcessInstance(processInstance2.getId(), "test"); // when HistoricProcessInstanceQuery processInstanceQuery = engineRule.getHistoryService() .createHistoricProcessInstanceQuery() .processDefinitionId(processDefinition.getId()); Batch batch = runtimeService .restartProcessInstances(processDefinition.getId()) .startTransition("flow1") .processInstanceIds(processInstance1.getId(), processInstance2.getId()) .historicProcessInstanceQuery(processInstanceQuery) .executeAsync(); helper.completeBatch(batch); // then List<ProcessInstance> restartedProcessInstances = engineRule.getRuntimeService().createProcessInstanceQuery().processDefinitionId(processDefinition.getId()).list(); assertEquals(restartedProcessInstances.size(), processInstanceCount); }
@Test public void shouldRestartProcessInstanceUsingHistoricProcessInstanceQuery() { // given ProcessDefinition processDefinition = testRule.deployAndGetDefinition(ProcessModels.TWO_TASKS_PROCESS); ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("Process"); Task task1 = taskService.createTaskQuery().processInstanceId(processInstance1.getId()).active().singleResult(); ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("Process"); Task task2 = taskService.createTaskQuery().processInstanceId(processInstance2.getId()).active().singleResult(); runtimeService.deleteProcessInstance(processInstance1.getId(), "test"); runtimeService.deleteProcessInstance(processInstance2.getId(), "test"); // when HistoricProcessInstanceQuery historicProcessInstanceQuery = engineRule.getHistoryService().createHistoricProcessInstanceQuery().processDefinitionId(processDefinition.getId()); Batch batch = runtimeService.restartProcessInstances(processDefinition.getId()) .startBeforeActivity("userTask1") .historicProcessInstanceQuery(historicProcessInstanceQuery) .executeAsync(); helper.completeBatch(batch); // then List<ProcessInstance> restartedProcessInstances = runtimeService.createProcessInstanceQuery().active().list(); ProcessInstance restartedProcessInstance = restartedProcessInstances.get(0); Task restartedTask = taskService.createTaskQuery().processInstanceId(restartedProcessInstance.getId()).active().singleResult(); Assert.assertEquals(task1.getTaskDefinitionKey(), restartedTask.getTaskDefinitionKey()); restartedProcessInstance = restartedProcessInstances.get(1); restartedTask = taskService.createTaskQuery().processInstanceId(restartedProcessInstance.getId()).active().singleResult(); Assert.assertEquals(task2.getTaskDefinitionKey(), restartedTask.getTaskDefinitionKey()); }