|
| 1 | +/* |
| 2 | + * Copyright 2024 Conductor Authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * <p> |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * <p> |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package com.netflix.conductor.core.execution.mapper; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import com.netflix.conductor.common.metadata.tasks.TaskDef; |
| 22 | +import com.netflix.conductor.common.metadata.tasks.TaskType; |
| 23 | +import com.netflix.conductor.common.metadata.workflow.WorkflowDef; |
| 24 | +import com.netflix.conductor.common.metadata.workflow.WorkflowTask; |
| 25 | +import com.netflix.conductor.core.utils.IDGenerator; |
| 26 | +import com.netflix.conductor.core.utils.ParametersUtils; |
| 27 | +import com.netflix.conductor.model.TaskModel; |
| 28 | +import com.netflix.conductor.model.WorkflowModel; |
| 29 | + |
| 30 | +import static com.netflix.conductor.common.metadata.tasks.TaskType.TASK_TYPE_WAIT_FOR_WEBHOOK; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | +import static org.mockito.ArgumentMatchers.any; |
| 35 | +import static org.mockito.Mockito.doReturn; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | + |
| 38 | +public class WaitForWebhookTaskMapperTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void getTaskType() { |
| 42 | + ParametersUtils parametersUtils = mock(ParametersUtils.class); |
| 43 | + WaitForWebhookTaskMapper mapper = new WaitForWebhookTaskMapper(parametersUtils); |
| 44 | + assertEquals(TaskType.WAIT_FOR_WEBHOOK.name(), mapper.getTaskType()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void getMappedTasks_createsOneTask() { |
| 49 | + ParametersUtils parametersUtils = mock(ParametersUtils.class); |
| 50 | + WaitForWebhookTaskMapper mapper = new WaitForWebhookTaskMapper(parametersUtils); |
| 51 | + |
| 52 | + TaskMapperContext context = buildContext(parametersUtils, new HashMap<>()); |
| 53 | + List<TaskModel> tasks = mapper.getMappedTasks(context); |
| 54 | + |
| 55 | + assertEquals(1, tasks.size()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void getMappedTasks_taskIsInProgress() { |
| 60 | + ParametersUtils parametersUtils = mock(ParametersUtils.class); |
| 61 | + WaitForWebhookTaskMapper mapper = new WaitForWebhookTaskMapper(parametersUtils); |
| 62 | + |
| 63 | + TaskMapperContext context = buildContext(parametersUtils, new HashMap<>()); |
| 64 | + TaskModel task = mapper.getMappedTasks(context).get(0); |
| 65 | + |
| 66 | + assertEquals(TaskModel.Status.IN_PROGRESS, task.getStatus()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void getMappedTasks_taskTypeIsWaitForWebhook() { |
| 71 | + ParametersUtils parametersUtils = mock(ParametersUtils.class); |
| 72 | + WaitForWebhookTaskMapper mapper = new WaitForWebhookTaskMapper(parametersUtils); |
| 73 | + |
| 74 | + TaskMapperContext context = buildContext(parametersUtils, new HashMap<>()); |
| 75 | + TaskModel task = mapper.getMappedTasks(context).get(0); |
| 76 | + |
| 77 | + assertEquals(TASK_TYPE_WAIT_FOR_WEBHOOK, task.getTaskType()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void getMappedTasks_callbackSetToMaxValue() { |
| 82 | + ParametersUtils parametersUtils = mock(ParametersUtils.class); |
| 83 | + WaitForWebhookTaskMapper mapper = new WaitForWebhookTaskMapper(parametersUtils); |
| 84 | + |
| 85 | + TaskMapperContext context = buildContext(parametersUtils, new HashMap<>()); |
| 86 | + TaskModel task = mapper.getMappedTasks(context).get(0); |
| 87 | + |
| 88 | + assertEquals(Integer.MAX_VALUE, task.getCallbackAfterSeconds()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void getMappedTasks_resolvedInputStoredOnTask() { |
| 93 | + ParametersUtils parametersUtils = mock(ParametersUtils.class); |
| 94 | + Map<String, Object> resolvedInput = |
| 95 | + Map.of("matches", Map.of("$['event']['type']", "payment.completed")); |
| 96 | + doReturn(resolvedInput).when(parametersUtils).getTaskInputV2(any(), any(), any(), any()); |
| 97 | + |
| 98 | + WaitForWebhookTaskMapper mapper = new WaitForWebhookTaskMapper(parametersUtils); |
| 99 | + TaskMapperContext context = buildContext(parametersUtils, resolvedInput); |
| 100 | + TaskModel task = mapper.getMappedTasks(context).get(0); |
| 101 | + |
| 102 | + assertTrue(task.getInputData().containsKey("matches")); |
| 103 | + } |
| 104 | + |
| 105 | + private TaskMapperContext buildContext( |
| 106 | + ParametersUtils parametersUtils, Map<String, Object> inputParameters) { |
| 107 | + WorkflowTask workflowTask = new WorkflowTask(); |
| 108 | + workflowTask.setName("wait_for_webhook_task"); |
| 109 | + workflowTask.setType(TaskType.WAIT_FOR_WEBHOOK.name()); |
| 110 | + workflowTask.setInputParameters(inputParameters); |
| 111 | + |
| 112 | + WorkflowModel workflow = new WorkflowModel(); |
| 113 | + WorkflowDef workflowDef = new WorkflowDef(); |
| 114 | + workflow.setWorkflowDefinition(workflowDef); |
| 115 | + |
| 116 | + String taskId = new IDGenerator().generate(); |
| 117 | + |
| 118 | + return TaskMapperContext.newBuilder() |
| 119 | + .withWorkflowModel(workflow) |
| 120 | + .withTaskDefinition(new TaskDef()) |
| 121 | + .withWorkflowTask(workflowTask) |
| 122 | + .withTaskInput(inputParameters) |
| 123 | + .withRetryCount(0) |
| 124 | + .withTaskId(taskId) |
| 125 | + .build(); |
| 126 | + } |
| 127 | +} |
0 commit comments