StartWorkflowWorkflowOperationHandler extends AbstractWorkflowOperationHandler { @Override public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException { final WorkflowOperationInstance operation = workflowInstance.getCurrentOperation(); final String configuredMediaPackageID = trimToEmpty(operation.getConfiguration(MEDIA_PACKAGE_ID)); final String configuredWorkflowDefinition = trimToEmpty(operation.getConfiguration(WORKFLOW_DEFINITION)); Opt<MediaPackage> mpOpt = assetManager.getMediaPackage(configuredMediaPackageID); if (mpOpt.isNone()) { throw new WorkflowOperationException(format("Media package %s not found", configuredMediaPackageID)); } final MediaPackage mp = mpOpt.get(); final Map<String, String> properties = new HashMap<>(); for (String key : operation.getConfigurationKeys()) { if (MEDIA_PACKAGE_ID.equals(key) || WORKFLOW_DEFINITION.equals(key)) { continue; } properties.put(key, operation.getConfiguration(key)); } try { final WorkflowDefinition workflowDefinition = workflowService.getWorkflowDefinitionById( configuredWorkflowDefinition); logger.info("Starting '{}' workflow for media package '{}'", configuredWorkflowDefinition, configuredMediaPackageID); workflowService.start(workflowDefinition, mp, properties); } catch (NotFoundException e) { throw new WorkflowOperationException(format("Workflow Definition '%s' not found", configuredWorkflowDefinition)); } catch (Exception e) { throw new WorkflowOperationException(e); } return createResult(WorkflowOperationResult.Action.CONTINUE); }  void setAssetManager(AssetManager assetManager); void setWorkflowService(WorkflowService workflowService); @Override WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context); static final String MEDIA_PACKAGE_ID; static final String WORKFLOW_DEFINITION; }
@Test(expected = WorkflowOperationException.class) public void testNoMediaPackage() throws Exception { operationHandler.start(workflowInstance, null); }
@Test(expected = WorkflowOperationException.class) public void testNoWorkflowDefinition() throws Exception { reset(workflowService); expect(workflowService.getWorkflowDefinitionById(WD_ID)).andThrow(new NotFoundException()); replay(workflowService); operationHandler.start(workflowInstance, null); }
@Test public void testStartWorkflow() throws Exception { MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew(); mp.setIdentifier(new IdImpl(MP_ID)); reset(assetManager); expect(assetManager.getMediaPackage(anyString())).andReturn(Opt.some(mp)); WorkflowDefinition wd = new WorkflowDefinitionImpl(); wd.setId(WD_ID); Capture<Map<String, String>> wProperties = newCapture(); reset(workflowService); expect(workflowService.getWorkflowDefinitionById(WD_ID)).andReturn(wd); expect(workflowService.start(eq(wd), eq(mp), capture(wProperties))).andReturn(null); replay(assetManager, workflowService); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); verify(assetManager, workflowService); assertEquals(WorkflowOperationResult.Action.CONTINUE, result.getAction()); assertEquals(2, wProperties.getValue().size()); assertEquals("true", wProperties.getValue().get("workflowConfigurations")); assertEquals("value", wProperties.getValue().get("key")); }