Back to Repositories

Testing HTTP Task Mapping Implementation in Conductor-OSS

This test suite validates the HTTP Task Mapper functionality in the Conductor OSS workflow engine. It ensures proper mapping of HTTP tasks within workflow definitions and verifies task creation with various configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of HTTP task mapping scenarios in Conductor workflows.

  • Tests basic HTTP task creation with TaskDef
  • Verifies task mapping without TaskDef configuration
  • Validates task type assignment and properties
  • Ensures proper task ID generation and context handling

Implementation Analysis

The testing approach uses JUnit 4 framework with Mockito for dependency mocking.

Key implementation patterns include:
  • TaskMapperContext builder pattern for test setup
  • Mocked ParametersUtils and MetadataDAO dependencies
  • Verification of mapped task properties and configurations

Technical Details

Testing infrastructure includes:

  • JUnit 4 test framework
  • Mockito mocking framework
  • ExpectedException rule for error cases
  • IDGenerator for unique task identification
  • TaskMapperContext for test context management

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming and organization
  • Proper setup and initialization in @Before methods
  • Isolated test cases with specific assertions
  • Comprehensive verification of task properties
  • Effective use of mocking for external dependencies

conductor-oss/conductor

core/src/test/java/com/netflix/conductor/core/execution/mapper/HTTPTaskMapperTest.java

            
/*
 * Copyright 2022 Conductor Authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.netflix.conductor.core.execution.mapper;

import java.util.HashMap;
import java.util.List;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.netflix.conductor.common.metadata.tasks.TaskDef;
import com.netflix.conductor.common.metadata.tasks.TaskType;
import com.netflix.conductor.common.metadata.workflow.WorkflowDef;
import com.netflix.conductor.common.metadata.workflow.WorkflowTask;
import com.netflix.conductor.core.utils.IDGenerator;
import com.netflix.conductor.core.utils.ParametersUtils;
import com.netflix.conductor.dao.MetadataDAO;
import com.netflix.conductor.model.TaskModel;
import com.netflix.conductor.model.WorkflowModel;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

public class HTTPTaskMapperTest {

    private HTTPTaskMapper httpTaskMapper;
    private IDGenerator idGenerator;

    @Rule public ExpectedException expectedException = ExpectedException.none();

    @Before
    public void setUp() {
        ParametersUtils parametersUtils = mock(ParametersUtils.class);
        MetadataDAO metadataDAO = mock(MetadataDAO.class);
        httpTaskMapper = new HTTPTaskMapper(parametersUtils, metadataDAO);
        idGenerator = new IDGenerator();
    }

    @Test
    public void getMappedTasks() {
        // Given
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("http_task");
        workflowTask.setType(TaskType.HTTP.name());
        workflowTask.setTaskDefinition(new TaskDef("http_task"));
        String taskId = idGenerator.generate();
        String retriedTaskId = idGenerator.generate();

        WorkflowModel workflow = new WorkflowModel();
        WorkflowDef workflowDef = new WorkflowDef();
        workflow.setWorkflowDefinition(workflowDef);

        TaskMapperContext taskMapperContext =
                TaskMapperContext.newBuilder()
                        .withWorkflowModel(workflow)
                        .withTaskDefinition(new TaskDef())
                        .withWorkflowTask(workflowTask)
                        .withTaskInput(new HashMap<>())
                        .withRetryCount(0)
                        .withRetryTaskId(retriedTaskId)
                        .withTaskId(taskId)
                        .build();

        // when
        List<TaskModel> mappedTasks = httpTaskMapper.getMappedTasks(taskMapperContext);

        // Then
        assertEquals(1, mappedTasks.size());
        assertEquals(TaskType.HTTP.name(), mappedTasks.get(0).getTaskType());
    }

    @Test
    public void getMappedTasks_WithoutTaskDef() {
        // Given
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("http_task");
        workflowTask.setType(TaskType.HTTP.name());
        String taskId = idGenerator.generate();
        String retriedTaskId = idGenerator.generate();

        WorkflowModel workflow = new WorkflowModel();
        WorkflowDef workflowDef = new WorkflowDef();
        workflow.setWorkflowDefinition(workflowDef);

        TaskMapperContext taskMapperContext =
                TaskMapperContext.newBuilder()
                        .withWorkflowModel(workflow)
                        .withTaskDefinition(null)
                        .withWorkflowTask(workflowTask)
                        .withTaskInput(new HashMap<>())
                        .withRetryCount(0)
                        .withRetryTaskId(retriedTaskId)
                        .withTaskId(taskId)
                        .build();

        // when
        List<TaskModel> mappedTasks = httpTaskMapper.getMappedTasks(taskMapperContext);

        // Then
        assertEquals(1, mappedTasks.size());
        assertEquals(TaskType.HTTP.name(), mappedTasks.get(0).getTaskType());
    }
}