Back to Repositories

Testing Administrative API Operations in XXL-Job

This test suite validates the core administrative business operations of XXL-Job, focusing on executor registration, callback handling, and registry management. It ensures reliable communication between job executors and the admin interface through comprehensive API testing.

Test Coverage Overview

The test suite provides thorough coverage of XXL-Job’s administrative API functionality.

Key areas tested include:
  • Executor callback processing
  • Executor registration workflow
  • Registry removal operations
  • Success code validation
Integration points focus on HTTP communication between executor and admin components, with specific attention to response handling and status codes.

Implementation Analysis

The testing approach utilizes JUnit Jupiter for structured unit testing of the AdminBiz interface implementation. The tests employ the AdminBizClient to simulate real-world executor-admin interactions.

Notable patterns include:
  • Consistent client configuration setup
  • Standardized assertion checking
  • Isolated test cases for each API operation

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • AdminBizClient for API communication
  • Local test environment (127.0.0.1:8080)
  • Configurable timeout and access token settings
  • HandleCallbackParam and RegistryParam for request modeling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming and organization
  • Proper exception handling
  • Consistent assertion patterns
  • Isolated test scenarios
  • Comprehensive API operation coverage
  • Reusable client configuration

xuxueli/xxl-job

xxl-job-admin/src/test/java/com/xxl/job/adminbiz/AdminBizTest.java

            
package com.xxl.job.adminbiz;

import com.xxl.job.core.biz.AdminBiz;
import com.xxl.job.core.biz.client.AdminBizClient;
import com.xxl.job.core.biz.model.HandleCallbackParam;
import com.xxl.job.core.biz.model.RegistryParam;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.enums.RegistryConfig;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * admin api test
 *
 * @author xuxueli 2017-07-28 22:14:52
 */
public class AdminBizTest {

    // admin-client
    private static String addressUrl = "http://127.0.0.1:8080/xxl-job-admin/";
    private static String accessToken = null;
    private static int timeoutSecond = 3;


    @Test
    public void callback() throws Exception {
        AdminBiz adminBiz = new AdminBizClient(addressUrl, accessToken, timeoutSecond);

        HandleCallbackParam param = new HandleCallbackParam();
        param.setLogId(1);
        param.setHandleCode(XxlJobContext.HANDLE_CODE_SUCCESS);

        List<HandleCallbackParam> callbackParamList = Arrays.asList(param);

        ReturnT<String> returnT = adminBiz.callback(callbackParamList);

        assertTrue(returnT.getCode() == ReturnT.SUCCESS_CODE);
    }

    /**
     * registry executor
     *
     * @throws Exception
     */
    @Test
    public void registry() throws Exception {
        AdminBiz adminBiz = new AdminBizClient(addressUrl, accessToken, timeoutSecond);

        RegistryParam registryParam = new RegistryParam(RegistryConfig.RegistType.EXECUTOR.name(), "xxl-job-executor-example", "127.0.0.1:9999");
        ReturnT<String> returnT = adminBiz.registry(registryParam);

        assertTrue(returnT.getCode() == ReturnT.SUCCESS_CODE);
    }

    /**
     * registry executor remove
     *
     * @throws Exception
     */
    @Test
    public void registryRemove() throws Exception {
        AdminBiz adminBiz = new AdminBizClient(addressUrl, accessToken, timeoutSecond);

        RegistryParam registryParam = new RegistryParam(RegistryConfig.RegistType.EXECUTOR.name(), "xxl-job-executor-example", "127.0.0.1:9999");
        ReturnT<String> returnT = adminBiz.registryRemove(registryParam);

        assertTrue(returnT.getCode() == ReturnT.SUCCESS_CODE);

    }

}