Back to Repositories

Testing User-Defined Task Mapping Implementation in Conductor OSS

This test suite validates the UserDefinedTaskMapper functionality in Conductor’s core execution module. It ensures proper mapping of user-defined tasks within workflows and handles task definition validation and error scenarios.

Test Coverage Overview

The test suite covers critical functionality of the UserDefinedTaskMapper class, focusing on task mapping and error handling.

  • Tests successful mapping of user-defined tasks with valid task definitions
  • Verifies exception handling for invalid task configurations
  • Validates task type assignment and workflow context integration

Implementation Analysis

The testing approach uses JUnit 4 framework with mock objects to isolate the mapper functionality.

Key implementation patterns include:
  • Mock integration with ParametersUtils and MetadataDAO
  • TaskMapperContext builder pattern usage
  • ID generation for task tracking

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • Mockito for dependency mocking
  • ExpectedException rule for exception testing
  • IDGenerator utility for task identification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test setup with @Before annotation
  • Isolated test cases with specific assertions
  • Proper exception testing methodology
  • Comprehensive task context configuration

conductor-oss/conductor

core/src/test/java/com/netflix/conductor/core/execution/mapper/UserDefinedTaskMapperTest.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.exception.TerminateWorkflowException;
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 UserDefinedTaskMapperTest {

    private IDGenerator idGenerator;

    private UserDefinedTaskMapper userDefinedTaskMapper;

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

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

    @Test
    public void getMappedTasks() {
        // Given
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("user_task");
        workflowTask.setType(TaskType.USER_DEFINED.name());
        workflowTask.setTaskDefinition(new TaskDef("user_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 = userDefinedTaskMapper.getMappedTasks(taskMapperContext);

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

    @Test
    public void getMappedTasksException() {
        // Given
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("user_task");
        workflowTask.setType(TaskType.USER_DEFINED.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)
                        .withWorkflowTask(workflowTask)
                        .withTaskInput(new HashMap<>())
                        .withRetryCount(0)
                        .withRetryTaskId(retriedTaskId)
                        .withTaskId(taskId)
                        .build();

        // then
        expectedException.expect(TerminateWorkflowException.class);
        expectedException.expectMessage(
                String.format(
                        "Invalid task specified. Cannot find task by name %s in the task definitions",
                        workflowTask.getName()));
        // when
        userDefinedTaskMapper.getMappedTasks(taskMapperContext);
    }
}