Back to Repositories

Testing OpenAI Image Generation Integration in RuoYi Vue Pro

This test suite validates the OpenAI Image Model integration in the RuoYi Vue Pro framework, focusing on image generation capabilities using DALL-E models through Spring AI’s abstraction layer.

Test Coverage Overview

The test suite covers core image generation functionality using OpenAI’s DALL-E 2 model.

Key areas tested include:
  • API endpoint configuration and authentication
  • Image generation parameter handling
  • Response processing from OpenAI’s image API
  • Integration with Spring AI’s image generation abstractions

Implementation Analysis

The testing approach utilizes JUnit 5 with Spring AI’s image generation interfaces.

Key implementation patterns include:
  • Direct OpenAI API integration via RestClient
  • Fluent builder pattern for image options configuration
  • Spring AI’s ImagePrompt and ImageResponse handling

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter for test execution
  • Spring AI OpenAiImageModel and OpenAiImageApi
  • RestClient for API communication
  • Custom API endpoint configuration
  • DALL-E 2 model specification
  • Image dimension parameters (256×256)

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Proper test isolation using @Disabled annotation
  • Clear parameter preparation and method invocation separation
  • Explicit model and dimension configurations
  • Structured API client initialization
  • Clean separation of concerns between API client and model handling

yunaiv/ruoyi-vue-pro

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

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

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.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.web.client.RestClient;

/**
 * {@link OpenAiImageModel} 集成测试类
 *
 * @author fansili
 */
public class OpenAiImageModelTests {

    private final OpenAiImageApi imageApi = new OpenAiImageApi(
            "https://api.holdai.top",
            "sk-dZEPiVaNcT3FHhef51996bAa0bC74806BeAb620dA5Da10Bf",
            RestClient.builder());
    private final OpenAiImageModel imageModel = new OpenAiImageModel(imageApi);

    @Test
    @Disabled
    public void testCall() {
        // 准备参数
        ImageOptions options = OpenAiImageOptions.builder()
                .withModel(OpenAiImageApi.ImageModel.DALL_E_2.getValue()) // 这个模型比较便宜
                .withHeight(256).withWidth(256)
                .build();
        ImagePrompt prompt = new ImagePrompt("中国长城!", options);

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

}