WorkflowInstanceResource extends JaxRsResource { @GET @Path("/id/{id}") @ApiOperation(value = "Fetch a workflow instance", notes = "Fetch full state and action history of a single workflow instance.") @ApiResponses({ @ApiResponse(code = 200, response = ListWorkflowInstanceResponse.class, message = "If instance was found"), @ApiResponse(code = 404, message = "If instance was not found") }) @SuppressFBWarnings(value = "LEST_LOST_EXCEPTION_STACK_TRACE", justification = "The empty result exception contains no useful information") public Response fetchWorkflowInstance(@ApiParam("Internal id for workflow instance") @PathParam("id") long id, @QueryParam("include") @ApiParam(value = INCLUDE_PARAM_DESC, allowableValues = INCLUDE_PARAM_VALUES, allowMultiple = true) String include, @QueryParam("maxActions") @ApiParam("Maximum number of actions returned for each workflow instance") Long maxActions) { return handleExceptions( () -> ok(super.fetchWorkflowInstance(id, include, maxActions, workflowInstances, listWorkflowConverter))); } @Inject  WorkflowInstanceResource(WorkflowInstanceService workflowInstances, CreateWorkflowConverter createWorkflowConverter,
      ListWorkflowInstanceConverter listWorkflowConverter, WorkflowInstanceFactory workflowInstanceFactory,
      WorkflowInstanceDao workflowInstanceDao); @OPTIONS @Path("{any: .*}") @ApiOperation("CORS preflight handling") @Consumes(WILDCARD) Response corsPreflight(); @PUT @ApiOperation("Submit new workflow instance") @ApiResponses({ @ApiResponse(code = 201, message = "Workflow was created", response = CreateWorkflowInstanceResponse.class), @ApiResponse(code = 400, message = "If instance could not be created, for example when state variable value was too long") }) Response createWorkflowInstance(
      @Valid @ApiParam(value = "Submitted workflow instance information", required = true) CreateWorkflowInstanceRequest req); @PUT @Path("/id/{id}") @ApiOperation(value = "Update workflow instance", notes = "The service is typically used in manual state transition via nFlow Explorer or a business UI.") @ApiResponses({ @ApiResponse(code = 204, message = "If update was successful"), @ApiResponse(code = 400, message = "If instance could not be updated, for example when state variable value was too long"), @ApiResponse(code = 409, message = "If workflow was executing and no update was done") }) Response updateWorkflowInstance(@ApiParam("Internal id for workflow instance") @PathParam("id") long id,
      @ApiParam("Submitted workflow instance information") UpdateWorkflowInstanceRequest req); @GET @Path("/id/{id}") @ApiOperation(value = "Fetch a workflow instance", notes = "Fetch full state and action history of a single workflow instance.") @ApiResponses({ @ApiResponse(code = 200, response = ListWorkflowInstanceResponse.class, message = "If instance was found"), @ApiResponse(code = 404, message = "If instance was not found") }) @SuppressFBWarnings(value = "LEST_LOST_EXCEPTION_STACK_TRACE", justification = "The empty result exception contains no useful information") Response fetchWorkflowInstance(@ApiParam("Internal id for workflow instance") @PathParam("id") long id,
      @QueryParam("include") @ApiParam(value = INCLUDE_PARAM_DESC, allowableValues = INCLUDE_PARAM_VALUES, allowMultiple = true) String include,
      @QueryParam("maxActions") @ApiParam("Maximum number of actions returned for each workflow instance") Long maxActions); @GET @ApiOperation(value = "List workflow instances", response = ListWorkflowInstanceResponse.class, responseContainer = "List") Response listWorkflowInstances(@QueryParam("id") @ApiParam("Internal id of workflow instance") List<Long> ids,
      @QueryParam("type") @ApiParam("Workflow definition type of workflow instance") List<String> types,
      @QueryParam("parentWorkflowId") @ApiParam("Id of parent workflow instance") Long parentWorkflowId,
      @QueryParam("parentActionId") @ApiParam("Id of parent workflow instance action") Long parentActionId,
      @QueryParam("state") @ApiParam("Current state of workflow instance") List<String> states,
      @QueryParam("status") @ApiParam("Current status of workflow instance") List<WorkflowInstanceStatus> statuses,
      @QueryParam("businessKey") @ApiParam("Business key for workflow instance") String businessKey,
      @QueryParam("externalId") @ApiParam("External id for workflow instance") String externalId,
      @QueryParam("include") @ApiParam(value = INCLUDE_PARAM_DESC, allowableValues = INCLUDE_PARAM_VALUES, allowMultiple = true) String include,
      @QueryParam("maxResults") @ApiParam("Maximum number of workflow instances to be returned") Long maxResults,
      @QueryParam("maxActions") @ApiParam("Maximum number of actions returned for each workflow instance") Long maxActions); @PUT @Path("/{id}/signal") @ApiOperation(value = "Set workflow instance signal value", response = SetSignalResponse.class, notes = "The service may be used for example to interrupt executing workflow instance.") Response setSignal(@ApiParam("Internal id for workflow instance") @PathParam("id") long id,
      @Valid @ApiParam("New signal value") SetSignalRequest req); @PUT @Path("/{id}/wakeup") @ApiOperation(value = "Wake up sleeping workflow instance", response = WakeupResponse.class, notes = "If expected states are given, only wake up if the instance is in one of the expected states.") Response wakeup(@ApiParam("Internal id for workflow instance") @PathParam("id") long id,
      @Valid @ApiParam("Expected states") WakeupRequest req);  }
@Test public void fetchingNonExistingWorkflowReturnsNotFound() { when(workflowInstances.getWorkflowInstance(42, emptySet(), null)) .thenThrow(new NflowNotFoundException("Workflow instance", 42, new Exception())); try (Response response = resource.fetchWorkflowInstance(42, null, null)) { assertThat(response.getStatus(), is(equalTo(NOT_FOUND.getStatusCode()))); assertThat(response.readEntity(ErrorResponse.class).error, is(equalTo("Workflow instance 42 not found"))); } }
@SuppressWarnings("unchecked") @Test public void fetchingExistingWorkflowWorks() { WorkflowInstance instance = mock(WorkflowInstance.class); when(workflowInstances.getWorkflowInstance(42, emptySet(), null)).thenReturn(instance); ListWorkflowInstanceResponse resp = mock(ListWorkflowInstanceResponse.class); when(listWorkflowConverter.convert(eq(instance), any(Set.class))).thenReturn(resp); ListWorkflowInstanceResponse result = resource.fetchWorkflowInstance(42, null, null).readEntity(ListWorkflowInstanceResponse.class); verify(workflowInstances).getWorkflowInstance(42, emptySet(), null); assertEquals(resp, result); }
@SuppressWarnings({ "unchecked" }) @Test public void fetchingExistingWorkflowWorksWithAllIncludes() { WorkflowInstance instance = mock(WorkflowInstance.class); EnumSet<WorkflowInstanceInclude> includes = EnumSet.allOf(WorkflowInstanceInclude.class); when(workflowInstances.getWorkflowInstance(42, includes, 10L)).thenReturn(instance); ListWorkflowInstanceResponse resp = mock(ListWorkflowInstanceResponse.class); when(listWorkflowConverter.convert(eq(instance), any(Set.class))).thenReturn(resp); ListWorkflowInstanceResponse result = resource.fetchWorkflowInstance(42, "actions,currentStateVariables,actionStateVariables,childWorkflows", 10L).readEntity(ListWorkflowInstanceResponse.class); verify(workflowInstances).getWorkflowInstance(42, includes, 10L); assertEquals(resp, result); }