Back to Repositories

Testing JSON JQ Transform Task Mapping Implementation in Conductor

This test suite validates the JsonJQTransformTaskMapper functionality in Conductor’s core execution engine, focusing on the mapping of JSON transformation tasks using the JQ query language. The tests ensure proper task creation and parameter handling for JSON transformation operations.

Test Coverage Overview

The test suite provides comprehensive coverage of the JsonJQTransformTaskMapper class, focusing on task mapping scenarios with and without TaskDef configurations.

  • Tests task creation with complete TaskDef configuration
  • Validates mapping behavior without TaskDef
  • Verifies correct handling of input parameters and JQ query expressions
  • Ensures proper task type assignment

Implementation Analysis

The testing approach utilizes JUnit framework with Mockito for dependency mocking. The implementation follows a structured pattern of setting up test fixtures, executing the mapper, and validating the resulting TaskModel objects.

  • Uses mock objects for ParametersUtils and MetadataDAO
  • Implements Before setup for test initialization
  • Employs builder pattern for TaskMapperContext creation

Technical Details

Testing infrastructure leverages:

  • JUnit 4 testing framework
  • Mockito mocking framework
  • IDGenerator for unique task identification
  • TaskMapperContext builder for test context creation
  • HashMap for input parameter simulation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation through @Before setup
  • Clear test method naming conventions
  • Comprehensive assertion checking
  • Effective use of mocking for external dependencies
  • Separate test cases for different scenarios

conductor-oss/conductor

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

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.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 JsonJQTransformTaskMapperTest {

    private IDGenerator idGenerator;
    private ParametersUtils parametersUtils;
    private MetadataDAO metadataDAO;

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

    @Test
    public void getMappedTasks() {

        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("json_jq_transform_task");
        workflowTask.setType(TaskType.JSON_JQ_TRANSFORM.name());
        workflowTask.setTaskDefinition(new TaskDef("json_jq_transform_task"));

        Map<String, Object> taskInput = new HashMap<>();
        taskInput.put("in1", new String[] {"a", "b"});
        taskInput.put("in2", new String[] {"c", "d"});
        taskInput.put("queryExpression", "{ out: (.in1 + .in2) }");
        workflowTask.setInputParameters(taskInput);

        String taskId = 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(taskInput)
                        .withRetryCount(0)
                        .withTaskId(taskId)
                        .build();

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

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

    @Test
    public void getMappedTasks_WithoutTaskDef() {
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("json_jq_transform_task");
        workflowTask.setType(TaskType.JSON_JQ_TRANSFORM.name());

        Map<String, Object> taskInput = new HashMap<>();
        taskInput.put("in1", new String[] {"a", "b"});
        taskInput.put("in2", new String[] {"c", "d"});
        taskInput.put("queryExpression", "{ out: (.in1 + .in2) }");
        workflowTask.setInputParameters(taskInput);

        String taskId = idGenerator.generate();

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

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

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

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