Back to Repositories

Testing Inline Task Mapping Implementation in conductor-oss/conductor

This test suite validates the InlineTaskMapper functionality in Conductor’s core execution framework, focusing on mapping inline JavaScript tasks within workflows. The tests ensure proper task creation, configuration, and type assignment for both scenarios with and without task definitions.

Test Coverage Overview

The test suite covers essential functionality of the InlineTaskMapper class, validating task mapping behavior.

  • Tests task mapping with defined TaskDef configurations
  • Verifies mapping behavior without TaskDef specifications
  • Validates correct TaskType assignment for inline tasks
  • Ensures proper task creation and initialization

Implementation Analysis

The testing approach utilizes JUnit framework with Mockito for dependency mocking.

Key patterns include:
  • Setup of mock objects for ParametersUtils and MetadataDAO
  • TaskMapperContext builder pattern usage
  • JavaScript expression handling verification
  • Workflow task configuration testing

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • Mockito mocking framework
  • Custom TaskMapperContext builder
  • IDGenerator for task identification
  • JavascriptEvaluator integration

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test setup and initialization using @Before
  • Clear test method naming conventions
  • Comprehensive assertion checking
  • Isolation of test scenarios
  • Effective mock object usage

conductor-oss/conductor

core/src/test/java/com/netflix/conductor/core/execution/mapper/InlineTaskMapperTest.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.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.execution.evaluators.JavascriptEvaluator;
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.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;

public class InlineTaskMapperTest {

    private ParametersUtils parametersUtils;
    private MetadataDAO metadataDAO;

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

    @Test
    public void getMappedTasks() {

        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("inline_task");
        workflowTask.setType(TaskType.INLINE.name());
        workflowTask.setTaskDefinition(new TaskDef("inline_task"));
        workflowTask.setEvaluatorType(JavascriptEvaluator.NAME);
        workflowTask.setExpression(
                "function scriptFun() {if ($.input.a==1){return {testValue: true}} else{return "
                        + "{testValue: false} }}; scriptFun();");

        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 InlineTaskMapper(parametersUtils, metadataDAO)
                        .getMappedTasks(taskMapperContext);

        assertEquals(1, mappedTasks.size());
        assertNotNull(mappedTasks);
        assertEquals(TaskType.INLINE.name(), mappedTasks.get(0).getTaskType());
    }

    @Test
    public void getMappedTasks_WithoutTaskDef() {

        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setType(TaskType.INLINE.name());
        workflowTask.setEvaluatorType(JavascriptEvaluator.NAME);
        workflowTask.setExpression(
                "function scriptFun() {if ($.input.a==1){return {testValue: true}} else{return "
                        + "{testValue: false} }}; scriptFun();");

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

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

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

        List<TaskModel> mappedTasks =
                new InlineTaskMapper(parametersUtils, metadataDAO)
                        .getMappedTasks(taskMapperContext);

        assertEquals(1, mappedTasks.size());
        assertNotNull(mappedTasks);
        assertEquals(TaskType.INLINE.name(), mappedTasks.get(0).getTaskType());
    }
}