Back to Repositories

Testing ZhiPuAiImageModel Integration in ruoyi-vue-pro

This test suite validates the integration of ZhiPuAiImageModel with Spring AI for image generation capabilities. It focuses on testing the image generation API functionality using the ZhiPu AI service with specific model configurations and prompts.

Test Coverage Overview

The test suite covers the core image generation functionality of ZhiPuAiImageModel.

Key areas tested include:
  • API authentication and initialization
  • Image prompt processing
  • Model configuration with CogView-3
  • Response handling from ZhiPu AI service

Implementation Analysis

The testing approach utilizes JUnit 5 for unit testing with a focus on direct API integration verification. The implementation demonstrates a straightforward setup using Spring AI’s ImagePrompt and ImageResponse interfaces, with ZhiPuAiImageOptions for model configuration.

Notable patterns include:
  • Builder pattern for options configuration
  • Dependency injection of API client
  • Disabled test annotation for controlled execution

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • Spring AI framework integration
  • ZhiPuAiImageApi client configuration
  • CogView-3 model specification
  • API key authentication

Best Practices Demonstrated

The test implementation showcases several testing best practices for AI service integration.

Notable practices include:
  • Clear test method organization
  • Explicit parameter preparation
  • Proper API client initialization
  • Use of builder pattern for configuration
  • Test isolation with @Disabled annotation

yunaiv/ruoyi-vue-pro

yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/ZhiPuAiImageModelTests.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.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.zhipuai.ZhiPuAiImageModel;
import org.springframework.ai.zhipuai.ZhiPuAiImageOptions;
import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi;

/**
 * {@link ZhiPuAiImageModel} 集成测试
 */
public class ZhiPuAiImageModelTests {

    private final ZhiPuAiImageApi imageApi = new ZhiPuAiImageApi(
            "78d3228c1d9e5e342a3e1ab349e2dd7b.VXLoq5vrwK2ofboy");
    private final ZhiPuAiImageModel imageModel = new ZhiPuAiImageModel(imageApi);

    @Test
    @Disabled
    public void testCall() {
        // 准备参数
        ZhiPuAiImageOptions imageOptions = ZhiPuAiImageOptions.builder()
                .withModel(ZhiPuAiImageApi.ImageModel.CogView_3.getValue())
                .build();
        ImagePrompt prompt = new ImagePrompt("万里长城", imageOptions);

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

}