Back to Repositories

Testing SimpleTaskMapper Workflow Task Handling in conductor-oss

This test suite validates the SimpleTaskMapper functionality in the Conductor OSS framework, focusing on task mapping and error handling for simple workflow tasks. It ensures proper task definition mapping and exception handling for invalid task configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of SimpleTaskMapper functionality.

Key areas tested include:
  • Successful task mapping with valid task definitions
  • Error handling for missing task definitions
  • Task ID generation and context building
  • Workflow task configuration validation

Implementation Analysis

The testing approach uses JUnit 4 framework with mock objects for isolated unit testing. The implementation follows AAA (Arrange-Act-Assert) pattern and leverages MockitoFramework for dependency isolation.

Key implementation patterns include:
  • Setup method for test initialization
  • ExpectedException rule for exception testing
  • Builder pattern for TaskMapperContext creation

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • Mockito for mocking dependencies
  • IDGenerator for unique task identification
  • TaskMapperContext builder for test context creation
  • ExpectedException rule for exception validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java unit testing.

Notable practices include:
  • Proper test isolation through mocking
  • Clear test method naming conventions
  • Robust exception testing
  • Comprehensive assertion validation
  • Clean setup and initialization patterns

conductor-oss/conductor

core/src/test/java/com/netflix/conductor/core/execution/mapper/SimpleTaskMapperTest.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.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.model.TaskModel;
import com.netflix.conductor.model.WorkflowModel;

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

public class SimpleTaskMapperTest {

    private SimpleTaskMapper simpleTaskMapper;

    private IDGenerator idGenerator = new IDGenerator();

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

    @Before
    public void setUp() {
        ParametersUtils parametersUtils = mock(ParametersUtils.class);
        simpleTaskMapper = new SimpleTaskMapper(parametersUtils);
    }

    @Test
    public void getMappedTasks() {

        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("simple_task");
        workflowTask.setTaskDefinition(new TaskDef("simple_task"));

        String taskId = idGenerator.generate();
        String retriedTaskId = idGenerator.generate();

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

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

        List<TaskModel> mappedTasks = simpleTaskMapper.getMappedTasks(taskMapperContext);
        assertNotNull(mappedTasks);
        assertEquals(1, mappedTasks.size());
    }

    @Test
    public void getMappedTasksException() {

        // Given
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("simple_task");
        String taskId = idGenerator.generate();
        String retriedTaskId = idGenerator.generate();

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

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

        // then
        expectedException.expect(TerminateWorkflowException.class);
        expectedException.expectMessage(
                String.format(
                        "Invalid task. Task %s does not have a definition",
                        workflowTask.getName()));

        // when
        simpleTaskMapper.getMappedTasks(taskMapperContext);
    }
}