Back to Repositories

Testing TongYi Image Synthesis Integration in RuoYi-Vue-Pro

This test suite validates the TongYi Image Model integration in the RuoYi Vue Pro framework, focusing on image synthesis capabilities using Alibaba Cloud’s AI services. It demonstrates the implementation of image generation functionality with configurable parameters and prompt-based image creation.

Test Coverage Overview

The test suite covers the core functionality of TongYiImagesModel, specifically testing image generation capabilities.

Key areas tested include:
  • Image synthesis with custom prompts
  • Configuration of image dimensions (256×256)
  • Integration with Alibaba Cloud’s ImageSynthesis API
  • Model specification using WANX_V1

Implementation Analysis

The testing approach utilizes JUnit 5 framework with a focused single test case methodology.

Notable implementation patterns include:
  • Builder pattern for ImageOptions configuration
  • Static API key initialization
  • Direct integration with Alibaba Cloud services
  • Spring AI framework integration for image processing

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • Alibaba Cloud AI SDK integration
  • Spring AI image processing libraries
  • TongYi API authentication setup
  • Custom image synthesis configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation with @Disabled annotation
  • Clear test method naming conventions
  • Structured test setup with distinct preparation, execution, and verification phases
  • Secure handling of API credentials
  • Modular test design with focused scope

yunaiv/ruoyi-vue-pro

yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/TongYiImagesModelTest.java

            
package cn.iocoder.yudao.framework.ai.image;

import com.alibaba.cloud.ai.tongyi.image.TongYiImagesModel;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.utils.Constants;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageOptions;

/**
 * {@link com.alibaba.cloud.ai.tongyi.image.TongYiImagesModel} 集成测试类
 *
 * @author fansili
 */
public class TongYiImagesModelTest {

    private final ImageSynthesis imageApi = new ImageSynthesis();
    private final TongYiImagesModel imageModel = new TongYiImagesModel(imageApi);

    static {
        Constants.apiKey = "sk-Zsd81gZYg7";
    }

    @Test
    @Disabled
    public void imageCallTest() {
        // 准备参数
        ImageOptions options = OpenAiImageOptions.builder()
                .withModel(ImageSynthesis.Models.WANX_V1)
                .withHeight(256).withWidth(256)
                .build();
        ImagePrompt prompt = new ImagePrompt("中国长城!", options);

        // 方法调用
        ImageResponse response = imageModel.call(prompt);
        // 打印结果
        System.out.println(response);
    }

}