Back to Repositories

Testing Terminate Task Mapping Implementation in Conductor-OSS

This test suite validates the TerminateTaskMapper functionality in Conductor OSS, focusing on task termination handling and workflow control flow. The tests ensure proper mapping of terminate tasks and verify the core termination mechanisms within the workflow execution engine.

Test Coverage Overview

The test suite provides comprehensive coverage of the TerminateTaskMapper component, specifically focusing on task mapping and termination workflows.

  • Validates correct task type assignment for terminate tasks
  • Tests task ID generation and mapping
  • Verifies workflow context handling
  • Ensures proper task definition integration

Implementation Analysis

The testing approach utilizes JUnit framework with Mockito for dependency isolation. The implementation follows a structured setup-execute-verify pattern, with mock ParametersUtils injection and explicit task mapping verification.

  • Uses TaskMapperContext builder pattern
  • Implements proper task type verification
  • Employs mock objects for external dependencies

Technical Details

  • JUnit 4 testing framework
  • Mockito mocking framework
  • TaskMapperContext builder
  • IDGenerator for unique task identification
  • WorkflowModel and TaskModel implementations

Best Practices Demonstrated

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

  • Proper test initialization with @Before setup
  • Clear test method naming conventions
  • Effective use of assertion statements
  • Isolated test scenarios with mock objects
  • Structured test organization with clear setup and verification phases

conductor-oss/conductor

core/src/test/java/com/netflix/conductor/core/execution/mapper/TerminateTaskMapperTest.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.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

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.model.TaskModel;
import com.netflix.conductor.model.WorkflowModel;

import static org.mockito.Mockito.mock;

public class TerminateTaskMapperTest {
    private ParametersUtils parametersUtils;

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

    @Test
    public void getMappedTasks() {

        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setType(TaskType.TASK_TYPE_TERMINATE);

        String taskId = new IDGenerator().generate();

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

        TaskMapperContext taskMapperContext =
                TaskMapperContext.newBuilder()
                        .withWorkflowModel(workflow)
                        .withTaskDefinition(new TaskDef())
                        .withWorkflowTask(workflowTask)
                        .withRetryCount(0)
                        .withTaskId(taskId)
                        .build();

        List<TaskModel> mappedTasks =
                new TerminateTaskMapper(parametersUtils).getMappedTasks(taskMapperContext);

        Assert.assertNotNull(mappedTasks);
        Assert.assertEquals(1, mappedTasks.size());
        Assert.assertEquals(TaskType.TASK_TYPE_TERMINATE, mappedTasks.get(0).getTaskType());
    }
}