Search in sources :

Example 6 with Action

use of com.netflix.conductor.common.metadata.events.EventHandler.Action in project conductor by Netflix.

the class TestSimpleActionProcessor method testCompleteTask.

@Test
public void testCompleteTask() throws Exception {
    TaskDetails taskDetails = new TaskDetails();
    taskDetails.setWorkflowId("${workflowId}");
    taskDetails.setTaskRefName("testTask");
    taskDetails.getOutput().put("someNEKey", "${Message.someNEKey}");
    taskDetails.getOutput().put("someKey", "${Message.someKey}");
    taskDetails.getOutput().put("someNullKey", "${Message.someNullKey}");
    Action action = new Action();
    action.setAction(Type.complete_task);
    action.setComplete_task(taskDetails);
    String payloadJson = "{\"workflowId\":\"workflow_1\",\"Message\":{\"someKey\":\"someData\",\"someNullKey\":null}}";
    Object payload = objectMapper.readValue(payloadJson, Object.class);
    Task task = new Task();
    task.setReferenceTaskName("testTask");
    Workflow workflow = new Workflow();
    workflow.getTasks().add(task);
    when(workflowExecutor.getWorkflow(eq("workflow_1"), anyBoolean())).thenReturn(workflow);
    actionProcessor.execute(action, payload, "testEvent", "testMessage");
    ArgumentCaptor<TaskResult> argumentCaptor = ArgumentCaptor.forClass(TaskResult.class);
    verify(workflowExecutor).updateTask(argumentCaptor.capture());
    assertEquals(Status.COMPLETED, argumentCaptor.getValue().getStatus());
    assertEquals("testMessage", argumentCaptor.getValue().getOutputData().get("conductor.event.messageId"));
    assertEquals("testEvent", argumentCaptor.getValue().getOutputData().get("conductor.event.name"));
    assertEquals("workflow_1", argumentCaptor.getValue().getOutputData().get("workflowId"));
    assertEquals("testTask", argumentCaptor.getValue().getOutputData().get("taskRefName"));
    assertEquals("someData", argumentCaptor.getValue().getOutputData().get("someKey"));
    // Assert values not in message are evaluated to null
    assertTrue("testTask", argumentCaptor.getValue().getOutputData().containsKey("someNEKey"));
    // Assert null values from message are kept
    assertTrue("testTask", argumentCaptor.getValue().getOutputData().containsKey("someNullKey"));
    assertNull("testTask", argumentCaptor.getValue().getOutputData().get("someNullKey"));
}
Also used : Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) Task(com.netflix.conductor.common.metadata.tasks.Task) TaskDetails(com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) Test(org.junit.Test)

Example 7 with Action

use of com.netflix.conductor.common.metadata.events.EventHandler.Action 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());
}
Also used : Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Map(java.util.Map) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Test(org.junit.Test)

Example 8 with Action

use of com.netflix.conductor.common.metadata.events.EventHandler.Action in project conductor by Netflix.

the class TestSimpleEventProcessor method testEventProcessor.

@Test
public void testEventProcessor() {
    // setup event handler
    EventHandler eventHandler = new EventHandler();
    eventHandler.setName(UUID.randomUUID().toString());
    eventHandler.setActive(true);
    Map<String, String> taskToDomain = new HashMap<>();
    taskToDomain.put("*", "dev");
    Action startWorkflowAction = new Action();
    startWorkflowAction.setAction(Type.start_workflow);
    startWorkflowAction.setStart_workflow(new StartWorkflow());
    startWorkflowAction.getStart_workflow().setName("workflow_x");
    startWorkflowAction.getStart_workflow().setVersion(1);
    startWorkflowAction.getStart_workflow().setTaskToDomain(taskToDomain);
    eventHandler.getActions().add(startWorkflowAction);
    Action completeTaskAction = new Action();
    completeTaskAction.setAction(Type.complete_task);
    completeTaskAction.setComplete_task(new TaskDetails());
    completeTaskAction.getComplete_task().setTaskRefName("task_x");
    completeTaskAction.getComplete_task().setWorkflowId(UUID.randomUUID().toString());
    completeTaskAction.getComplete_task().setOutput(new HashMap<>());
    eventHandler.getActions().add(completeTaskAction);
    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), anyMap());
    AtomicBoolean completed = new AtomicBoolean(false);
    doAnswer((Answer<String>) invocation -> {
        completed.set(true);
        return null;
    }).when(workflowExecutor).updateTask(any());
    Task task = new Task();
    task.setReferenceTaskName(completeTaskAction.getComplete_task().getTaskRefName());
    Workflow workflow = new Workflow();
    workflow.setTasks(Collections.singletonList(task));
    when(workflowExecutor.getWorkflow(completeTaskAction.getComplete_task().getWorkflowId(), true)).thenReturn(workflow);
    WorkflowDef workflowDef = new WorkflowDef();
    workflowDef.setVersion(startWorkflowAction.getStart_workflow().getVersion());
    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());
    String queueEvent = eventProcessor.getQueues().keySet().iterator().next();
    assertEquals(eventHandler.getEvent(), queueEvent);
    String eventProcessorQueue = eventProcessor.getQueues().values().iterator().next();
    assertEquals(queueURI, eventProcessorQueue);
    Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue(started.get());
    assertTrue(completed.get());
    verify(queue, atMost(1)).ack(any());
    verify(queue, never()).publish(any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MetadataService(com.netflix.conductor.service.MetadataService) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) Task(com.netflix.conductor.common.metadata.tasks.Task) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Observable(rx.Observable) ParametersUtils(com.netflix.conductor.core.execution.ParametersUtils) JsonUtils(com.netflix.conductor.core.utils.JsonUtils) LinkedHashMap(java.util.LinkedHashMap) Answer(org.mockito.stubbing.Answer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Workflow(com.netflix.conductor.common.run.Workflow) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) WorkflowExecutor(com.netflix.conductor.core.execution.WorkflowExecutor) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) Before(org.junit.Before) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) Message(com.netflix.conductor.core.events.queue.Message) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) UUID(java.util.UUID) Type(com.netflix.conductor.common.metadata.events.EventHandler.Action.Type) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) Mockito.atMost(org.mockito.Mockito.atMost) TaskDetails(com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails) ExecutionService(com.netflix.conductor.service.ExecutionService) JsonMapperProvider(com.netflix.conductor.common.utils.JsonMapperProvider) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TaskDetails(com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails) Test(org.junit.Test)

Example 9 with Action

use of com.netflix.conductor.common.metadata.events.EventHandler.Action in project conductor by Netflix.

the class TestSimpleEventProcessor method testExecuteRetriableApplicationException.

@Test
public void testExecuteRetriableApplicationException() {
    AtomicInteger executeInvoked = new AtomicInteger(0);
    doAnswer((Answer<Map<String, Object>>) invocation -> {
        executeInvoked.incrementAndGet();
        throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, "some retriable error");
    }).when(actionProcessor).execute(any(), any(), any(), any());
    SimpleEventProcessor eventProcessor = new SimpleEventProcessor(executionService, metadataService, actionProcessor, eventQueues, jsonUtils, new TestConfiguration(), objectMapper);
    EventExecution eventExecution = new EventExecution("id", "messageId");
    eventExecution.setStatus(EventExecution.Status.IN_PROGRESS);
    eventExecution.setEvent("event");
    Action action = new Action();
    action.setAction(Type.start_workflow);
    eventProcessor.execute(eventExecution, action, "payload");
    assertEquals(3, executeInvoked.get());
    assertNull(eventExecution.getOutput().get("exception"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MetadataService(com.netflix.conductor.service.MetadataService) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) Task(com.netflix.conductor.common.metadata.tasks.Task) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Observable(rx.Observable) ParametersUtils(com.netflix.conductor.core.execution.ParametersUtils) JsonUtils(com.netflix.conductor.core.utils.JsonUtils) LinkedHashMap(java.util.LinkedHashMap) Answer(org.mockito.stubbing.Answer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Workflow(com.netflix.conductor.common.run.Workflow) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) WorkflowExecutor(com.netflix.conductor.core.execution.WorkflowExecutor) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) Before(org.junit.Before) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) Message(com.netflix.conductor.core.events.queue.Message) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) UUID(java.util.UUID) Type(com.netflix.conductor.common.metadata.events.EventHandler.Action.Type) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) Mockito.atMost(org.mockito.Mockito.atMost) TaskDetails(com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails) ExecutionService(com.netflix.conductor.service.ExecutionService) JsonMapperProvider(com.netflix.conductor.common.utils.JsonMapperProvider) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 10 with Action

use of com.netflix.conductor.common.metadata.events.EventHandler.Action in project conductor by Netflix.

the class TestSimpleEventProcessor method testExecuteInvalidAction.

@SuppressWarnings("unchecked")
@Test
public void testExecuteInvalidAction() {
    AtomicInteger executeInvoked = new AtomicInteger(0);
    doAnswer((Answer<Map<String, Object>>) invocation -> {
        executeInvoked.incrementAndGet();
        throw new UnsupportedOperationException("error");
    }).when(actionProcessor).execute(any(), any(), any(), any());
    SimpleEventProcessor eventProcessor = new SimpleEventProcessor(executionService, metadataService, actionProcessor, eventQueues, jsonUtils, new TestConfiguration(), objectMapper);
    EventExecution eventExecution = new EventExecution("id", "messageId");
    eventExecution.setName("handler");
    eventExecution.setStatus(EventExecution.Status.IN_PROGRESS);
    eventExecution.setEvent("event");
    Action action = new Action();
    eventExecution.setAction(Type.start_workflow);
    eventProcessor.execute(eventExecution, action, "payload");
    assertEquals(1, executeInvoked.get());
    assertEquals(EventExecution.Status.FAILED, eventExecution.getStatus());
    assertNotNull(eventExecution.getOutput().get("exception"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MetadataService(com.netflix.conductor.service.MetadataService) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) Task(com.netflix.conductor.common.metadata.tasks.Task) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Observable(rx.Observable) ParametersUtils(com.netflix.conductor.core.execution.ParametersUtils) JsonUtils(com.netflix.conductor.core.utils.JsonUtils) LinkedHashMap(java.util.LinkedHashMap) Answer(org.mockito.stubbing.Answer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Workflow(com.netflix.conductor.common.run.Workflow) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) WorkflowExecutor(com.netflix.conductor.core.execution.WorkflowExecutor) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) Before(org.junit.Before) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) Message(com.netflix.conductor.core.events.queue.Message) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) UUID(java.util.UUID) Type(com.netflix.conductor.common.metadata.events.EventHandler.Action.Type) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) Mockito.atMost(org.mockito.Mockito.atMost) TaskDetails(com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails) ExecutionService(com.netflix.conductor.service.ExecutionService) JsonMapperProvider(com.netflix.conductor.common.utils.JsonMapperProvider) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Action (com.netflix.conductor.common.metadata.events.EventHandler.Action)13 Test (org.junit.Test)12 StartWorkflow (com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow)9 TaskDetails (com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails)9 EventHandler (com.netflix.conductor.common.metadata.events.EventHandler)8 Task (com.netflix.conductor.common.metadata.tasks.Task)7 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)7 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)7 TestConfiguration (com.netflix.conductor.core.execution.TestConfiguration)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ArgumentMatchers.anyMap (org.mockito.ArgumentMatchers.anyMap)7 EventExecution (com.netflix.conductor.common.metadata.events.EventExecution)6 Workflow (com.netflix.conductor.common.run.Workflow)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Uninterruptibles (com.google.common.util.concurrent.Uninterruptibles)5 Type (com.netflix.conductor.common.metadata.events.EventHandler.Action.Type)5 JsonMapperProvider (com.netflix.conductor.common.utils.JsonMapperProvider)5 Message (com.netflix.conductor.core.events.queue.Message)5 ObservableQueue (com.netflix.conductor.core.events.queue.ObservableQueue)5