use of com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow in project conductor by Netflix.
the class TestSimpleEventProcessor method testEventHandlerWithCondition.
@Test
public void testEventHandlerWithCondition() {
EventHandler eventHandler = new EventHandler();
eventHandler.setName("cms_intermediate_video_ingest_handler");
eventHandler.setActive(true);
eventHandler.setEvent("sqs:dev_cms_asset_ingest_queue");
eventHandler.setCondition("$.Message.testKey1 == 'level1' && $.Message.metadata.testKey2 == 123456");
Map<String, Object> startWorkflowInput = new LinkedHashMap<>();
startWorkflowInput.put("param1", "${Message.metadata.testKey2}");
startWorkflowInput.put("param2", "SQS-${MessageId}");
Action startWorkflowAction = new Action();
startWorkflowAction.setAction(Type.start_workflow);
startWorkflowAction.setStart_workflow(new StartWorkflow());
startWorkflowAction.getStart_workflow().setName("cms_artwork_automation");
startWorkflowAction.getStart_workflow().setVersion(1);
startWorkflowAction.getStart_workflow().setInput(startWorkflowInput);
startWorkflowAction.setExpandInlineJSON(true);
eventHandler.getActions().add(startWorkflowAction);
eventHandler.setEvent(event);
when(metadataService.getAllEventHandlers()).thenReturn(Collections.singletonList(eventHandler));
when(metadataService.getEventHandlersForEvent(event, true)).thenReturn(Collections.singletonList(eventHandler));
when(executionService.addEventExecution(any())).thenReturn(true);
when(queue.rePublishIfNoAck()).thenReturn(false);
String id = UUID.randomUUID().toString();
AtomicBoolean started = new AtomicBoolean(false);
doAnswer((Answer<String>) invocation -> {
started.set(true);
return id;
}).when(workflowExecutor).startWorkflow(eq(startWorkflowAction.getStart_workflow().getName()), eq(startWorkflowAction.getStart_workflow().getVersion()), eq(startWorkflowAction.getStart_workflow().getCorrelationId()), anyMap(), eq(null), eq(event), eq(null));
WorkflowDef workflowDef = new WorkflowDef();
workflowDef.setName(startWorkflowAction.getStart_workflow().getName());
when(metadataService.getWorkflowDef(any(), any())).thenReturn(workflowDef);
SimpleActionProcessor actionProcessor = new SimpleActionProcessor(workflowExecutor, parametersUtils, jsonUtils);
SimpleEventProcessor eventProcessor = new SimpleEventProcessor(executionService, metadataService, actionProcessor, eventQueues, jsonUtils, new TestConfiguration(), objectMapper);
assertNotNull(eventProcessor.getQueues());
assertEquals(1, eventProcessor.getQueues().size());
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
assertTrue(started.get());
}
use of com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow in project conductor by Netflix.
the class SimpleActionProcessor method startWorkflow.
private Map<String, Object> startWorkflow(Action action, Object payload, String event, String messageId) {
StartWorkflow params = action.getStart_workflow();
Map<String, Object> output = new HashMap<>();
try {
Map<String, Object> inputParams = params.getInput();
Map<String, Object> workflowInput = parametersUtils.replace(inputParams, payload);
Map<String, Object> paramsMap = new HashMap<>();
Optional.ofNullable(params.getCorrelationId()).ifPresent(value -> paramsMap.put("correlationId", value));
Map<String, Object> replaced = parametersUtils.replace(paramsMap, payload);
workflowInput.put("conductor.event.messageId", messageId);
workflowInput.put("conductor.event.name", event);
String workflowId = executor.startWorkflow(params.getName(), params.getVersion(), Optional.ofNullable(replaced.get("correlationId")).map(Object::toString).orElse(params.getCorrelationId()), workflowInput, null, event, params.getTaskToDomain());
output.put("workflowId", workflowId);
logger.debug("Started workflow: {}/{}/{} for event: {} for message:{}", params.getName(), params.getVersion(), workflowId, event, messageId);
} catch (RuntimeException e) {
Monitors.recordEventActionError(action.getAction().name(), params.getName(), event);
logger.error("Error starting workflow: {}, version: {}, for event: {} for message: {}", params.getName(), params.getVersion(), event, messageId, e);
output.put("error", e.getMessage());
throw e;
}
return output;
}
use of com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow in project conductor by Netflix.
the class RedisEventHandlerDAOTest method testEventHandlers.
@Test
public void testEventHandlers() {
String event1 = "SQS::arn:account090:sqstest1";
String event2 = "SQS::arn:account090:sqstest2";
EventHandler eventHandler = new EventHandler();
eventHandler.setName(UUID.randomUUID().toString());
eventHandler.setActive(false);
Action action = new Action();
action.setAction(Type.start_workflow);
action.setStart_workflow(new StartWorkflow());
action.getStart_workflow().setName("test_workflow");
eventHandler.getActions().add(action);
eventHandler.setEvent(event1);
redisEventHandlerDAO.addEventHandler(eventHandler);
List<EventHandler> allEventHandlers = redisEventHandlerDAO.getAllEventHandlers();
assertNotNull(allEventHandlers);
assertEquals(1, allEventHandlers.size());
assertEquals(eventHandler.getName(), allEventHandlers.get(0).getName());
assertEquals(eventHandler.getEvent(), allEventHandlers.get(0).getEvent());
List<EventHandler> byEvents = redisEventHandlerDAO.getEventHandlersForEvent(event1, true);
assertNotNull(byEvents);
// event is marked as in-active
assertEquals(0, byEvents.size());
eventHandler.setActive(true);
eventHandler.setEvent(event2);
redisEventHandlerDAO.updateEventHandler(eventHandler);
allEventHandlers = redisEventHandlerDAO.getAllEventHandlers();
assertNotNull(allEventHandlers);
assertEquals(1, allEventHandlers.size());
byEvents = redisEventHandlerDAO.getEventHandlersForEvent(event1, true);
assertNotNull(byEvents);
assertEquals(0, byEvents.size());
byEvents = redisEventHandlerDAO.getEventHandlersForEvent(event2, true);
assertNotNull(byEvents);
assertEquals(1, byEvents.size());
}
use of com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow in project conductor by Netflix.
the class TestSimpleActionProcessor method testStartWorkflow_correlationId.
@SuppressWarnings("unchecked")
@Test
public void testStartWorkflow_correlationId() throws Exception {
StartWorkflow startWorkflow = new StartWorkflow();
startWorkflow.setName("testWorkflow");
startWorkflow.getInput().put("testInput", "${testId}");
startWorkflow.setCorrelationId("${correlationId}");
Map<String, String> taskToDomain = new HashMap<>();
taskToDomain.put("*", "dev");
startWorkflow.setTaskToDomain(taskToDomain);
Action action = new Action();
action.setAction(Type.start_workflow);
action.setStart_workflow(startWorkflow);
Object payload = objectMapper.readValue("{\"correlationId\":\"test-id\", \"testId\":\"test_1\"}", Object.class);
WorkflowDef workflowDef = new WorkflowDef();
workflowDef.setName("testWorkflow");
workflowDef.setVersion(1);
when(workflowExecutor.startWorkflow(eq("testWorkflow"), eq(null), any(), any(), any(), eq("testEvent"), anyMap())).thenReturn("workflow_1");
Map<String, Object> output = actionProcessor.execute(action, payload, "testEvent", "testMessage");
assertNotNull(output);
assertEquals("workflow_1", output.get("workflowId"));
ArgumentCaptor<String> correlationIdCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Map> inputParamCaptor = ArgumentCaptor.forClass(Map.class);
ArgumentCaptor<Map> taskToDomainCaptor = ArgumentCaptor.forClass(Map.class);
verify(workflowExecutor).startWorkflow(eq("testWorkflow"), eq(null), correlationIdCaptor.capture(), inputParamCaptor.capture(), any(), eq("testEvent"), taskToDomainCaptor.capture());
assertEquals("test_1", inputParamCaptor.getValue().get("testInput"));
assertEquals("test-id", correlationIdCaptor.getValue());
assertEquals("testMessage", inputParamCaptor.getValue().get("conductor.event.messageId"));
assertEquals("testEvent", inputParamCaptor.getValue().get("conductor.event.name"));
assertEquals(taskToDomain, taskToDomainCaptor.getValue());
}
use of com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow in project conductor by Netflix.
the class TestSimpleActionProcessor method testStartWorkflow.
@SuppressWarnings("unchecked")
@Test
public void testStartWorkflow() throws Exception {
StartWorkflow startWorkflow = new StartWorkflow();
startWorkflow.setName("testWorkflow");
startWorkflow.getInput().put("testInput", "${testId}");
Map<String, String> taskToDomain = new HashMap<>();
taskToDomain.put("*", "dev");
startWorkflow.setTaskToDomain(taskToDomain);
Action action = new Action();
action.setAction(Type.start_workflow);
action.setStart_workflow(startWorkflow);
Object payload = objectMapper.readValue("{\"testId\":\"test_1\"}", Object.class);
WorkflowDef workflowDef = new WorkflowDef();
workflowDef.setName("testWorkflow");
workflowDef.setVersion(1);
when(workflowExecutor.startWorkflow(eq("testWorkflow"), eq(null), any(), any(), any(), eq("testEvent"), anyMap())).thenReturn("workflow_1");
Map<String, Object> output = actionProcessor.execute(action, payload, "testEvent", "testMessage");
assertNotNull(output);
assertEquals("workflow_1", output.get("workflowId"));
ArgumentCaptor<String> correlationIdCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Map> inputParamCaptor = ArgumentCaptor.forClass(Map.class);
ArgumentCaptor<Map> taskToDomainCaptor = ArgumentCaptor.forClass(Map.class);
verify(workflowExecutor).startWorkflow(eq("testWorkflow"), eq(null), correlationIdCaptor.capture(), inputParamCaptor.capture(), any(), eq("testEvent"), taskToDomainCaptor.capture());
assertEquals("test_1", inputParamCaptor.getValue().get("testInput"));
assertNull(correlationIdCaptor.getValue());
assertEquals("testMessage", inputParamCaptor.getValue().get("conductor.event.messageId"));
assertEquals("testEvent", inputParamCaptor.getValue().get("conductor.event.name"));
assertEquals(taskToDomain, taskToDomainCaptor.getValue());
}
Aggregations