WorkflowInstanceService { @Transactional public boolean updateWorkflowInstance(WorkflowInstance instance, WorkflowInstanceAction action) { Assert.notNull(instance, "Workflow instance can not be null"); Assert.notNull(action, "Workflow instance action can not be null"); Assert.notNull(workflowDefinitionService, "workflowDefinitionService can not be null"); try { WorkflowInstance.Builder builder = new WorkflowInstance.Builder(instance); if (instance.state == null) { builder.setStatus(null); } else { String type = workflowInstanceDao.getWorkflowInstanceType(instance.id); AbstractWorkflowDefinition<?> definition = workflowDefinitionService.getWorkflowDefinition(type); builder.setStatus(definition.getState(instance.state).getType().getStatus(instance.nextActivation)); } WorkflowInstance updatedInstance = builder.build(); boolean updated = workflowInstanceDao.updateNotRunningWorkflowInstance(updatedInstance); if (updated) { String currentState = workflowInstanceDao.getWorkflowInstanceState(updatedInstance.id); WorkflowInstanceAction updatedAction = new WorkflowInstanceAction.Builder(action).setState(currentState).build(); workflowInstanceDao.insertWorkflowInstanceAction(updatedInstance, updatedAction); } else { workflowInstanceDao.getWorkflowInstance(instance.id, emptySet(), 0L); } return updated; } catch (EmptyResultDataAccessException e) { throw new NflowNotFoundException("Workflow instance", instance.id, e); } } @Inject  WorkflowInstanceService(WorkflowInstanceDao workflowInstanceDao, WorkflowDefinitionService workflowDefinitionService,
      WorkflowInstancePreProcessor workflowInstancePreProcessor); WorkflowInstance getWorkflowInstance(long id, Set<WorkflowInstanceInclude> includes, Long maxActions); @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "getInitialState().toString() has no cast") long insertWorkflowInstance(WorkflowInstance instance); @Transactional boolean updateWorkflowInstance(WorkflowInstance instance, WorkflowInstanceAction action); @Transactional boolean wakeupWorkflowInstance(long id, List<String> expectedStates); Collection<WorkflowInstance> listWorkflowInstances(QueryWorkflowInstances query); Stream<WorkflowInstance> listWorkflowInstancesAsStream(QueryWorkflowInstances query); Optional<Integer> getSignal(long workflowInstanceId); boolean setSignal(long workflowInstanceId, Optional<Integer> signal, String reason, WorkflowActionType actionType);  }
@Test public void updateWorkflowInstanceWorks() { WorkflowInstance i = constructWorkflowInstanceBuilder().setId(42).build(); WorkflowInstanceAction a = new WorkflowInstanceAction.Builder().setType(externalChange).setWorkflowInstanceId(i.id).build(); when(workflowInstanceDao.getWorkflowInstanceState(i.id)).thenReturn("currentState"); when(workflowInstanceDao.updateNotRunningWorkflowInstance(any(WorkflowInstance.class))).thenReturn(true); when(workflowInstanceDao.getWorkflowInstanceType(42)).thenReturn(i.type); assertThat(service.updateWorkflowInstance(i, a), is(true)); verify(workflowInstanceDao).updateNotRunningWorkflowInstance(stored.capture()); assertThat(stored.getValue().status, is(inProgress)); verify(workflowInstanceDao).insertWorkflowInstanceAction(stored2.capture(), storedAction.capture()); assertThat(storedAction.getValue().state, is("currentState")); }
@Test public void updateWorkflowInstanceThrowsExceptionWhenActionIsNull() { WorkflowInstance i = constructWorkflowInstanceBuilder().setId(42).build(); WorkflowInstanceAction a = null; IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> service.updateWorkflowInstance(i, a)); assertThat(thrown.getMessage(), is("Workflow instance action can not be null")); }
@Test public void updateWorkflowInstanceWorksWhenStateIsNull() { WorkflowInstance i = constructWorkflowInstanceBuilder().setId(42).setState(null).build(); WorkflowInstanceAction a = new WorkflowInstanceAction.Builder().setType(externalChange).setWorkflowInstanceId(i.id).build(); when(workflowInstanceDao.getWorkflowInstanceState(i.id)).thenReturn("currentState"); when(workflowInstanceDao.updateNotRunningWorkflowInstance(any(WorkflowInstance.class))).thenReturn(true); assertThat(service.updateWorkflowInstance(i, a), is(true)); verify(workflowInstanceDao).updateNotRunningWorkflowInstance(stored.capture()); assertThat(stored.getValue().status, is(nullValue())); verify(workflowInstanceDao).insertWorkflowInstanceAction(stored2.capture(), storedAction.capture()); assertThat(storedAction.getValue().state, is("currentState")); }
@Test public void updateRunningWorkflowInstanceFails() { WorkflowInstance i = constructWorkflowInstanceBuilder().setId(42).build(); WorkflowInstanceAction a = new WorkflowInstanceAction.Builder().setType(externalChange).build(); when(workflowInstanceDao.updateNotRunningWorkflowInstance(i)).thenReturn(false); when(workflowInstanceDao.getWorkflowInstanceType(42)).thenReturn(i.type); assertThat(service.updateWorkflowInstance(i, a), is(false)); verify(workflowInstanceDao, never()).insertWorkflowInstanceAction(any(WorkflowInstance.class), any(WorkflowInstanceAction.class)); }