Back to Repositories

Validating TaskResult Status Transitions in conductor-oss

This test suite validates the TaskResult functionality in the Conductor OSS workflow engine, focusing on task status transitions and result validation. The tests ensure proper handling of different task states and their corresponding result mappings.

Test Coverage Overview

The test suite provides comprehensive coverage of TaskResult behavior across different task states.

  • Tests various task status transitions including CANCELED, COMPLETED_WITH_ERRORS, SCHEDULED, and COMPLETED
  • Validates mapping between Task and TaskResult status values
  • Verifies preservation of task properties during TaskResult creation

Implementation Analysis

The testing approach uses JUnit’s standard testing patterns with @Before setup and individual @Test methods.

Each test case follows a consistent pattern:
  • Setting up task status
  • Creating TaskResult from task
  • Validating result properties
  • Asserting expected status mappings

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • Task and TaskResult model classes from Conductor common package
  • setUp method for common test initialization
  • Helper method validateTaskResult for consistent property validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation with @Before setup
  • Single responsibility principle in test methods
  • Consistent validation through helper methods
  • Clear test naming conveying purpose
  • Comprehensive property verification

conductor-oss/conductor

common/src/test/java/com/netflix/conductor/common/tasks/TaskResultTest.java

            
/*
 * Copyright 2020 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.common.tasks;

import java.util.HashMap;

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

import com.netflix.conductor.common.metadata.tasks.Task;
import com.netflix.conductor.common.metadata.tasks.TaskResult;

import static org.junit.Assert.assertEquals;

public class TaskResultTest {

    private Task task;
    private TaskResult taskResult;

    @Before
    public void setUp() {
        task = new Task();
        task.setWorkflowInstanceId("workflow-id");
        task.setTaskId("task-id");
        task.setReasonForIncompletion("reason");
        task.setCallbackAfterSeconds(10);
        task.setWorkerId("worker-id");
        task.setOutputData(new HashMap<>());
        task.setExternalOutputPayloadStoragePath("externalOutput");
    }

    @Test
    public void testCanceledTask() {
        task.setStatus(Task.Status.CANCELED);
        taskResult = new TaskResult(task);
        validateTaskResult();
        assertEquals(TaskResult.Status.FAILED, taskResult.getStatus());
    }

    @Test
    public void testCompletedWithErrorsTask() {
        task.setStatus(Task.Status.COMPLETED_WITH_ERRORS);
        taskResult = new TaskResult(task);
        validateTaskResult();
        assertEquals(TaskResult.Status.FAILED, taskResult.getStatus());
    }

    @Test
    public void testScheduledTask() {
        task.setStatus(Task.Status.SCHEDULED);
        taskResult = new TaskResult(task);
        validateTaskResult();
        assertEquals(TaskResult.Status.IN_PROGRESS, taskResult.getStatus());
    }

    @Test
    public void testCompltetedTask() {
        task.setStatus(Task.Status.COMPLETED);
        taskResult = new TaskResult(task);
        validateTaskResult();
        assertEquals(TaskResult.Status.COMPLETED, taskResult.getStatus());
    }

    private void validateTaskResult() {
        assertEquals(task.getWorkflowInstanceId(), taskResult.getWorkflowInstanceId());
        assertEquals(task.getTaskId(), taskResult.getTaskId());
        assertEquals(task.getReasonForIncompletion(), taskResult.getReasonForIncompletion());
        assertEquals(task.getCallbackAfterSeconds(), taskResult.getCallbackAfterSeconds());
        assertEquals(task.getWorkerId(), taskResult.getWorkerId());
        assertEquals(task.getOutputData(), taskResult.getOutputData());
        assertEquals(
                task.getExternalOutputPayloadStoragePath(),
                taskResult.getExternalOutputPayloadStoragePath());
    }
}