This is an automated email from the ASF dual-hosted git repository.

liuhongyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 0eaec1e63a feat(ut): add ai plugin proxy unit test (#6070)
0eaec1e63a is described below

commit 0eaec1e63adef81d0bd5d8797f0e907778abcfea
Author: shown <[email protected]>
AuthorDate: Mon Jul 28 16:17:34 2025 +0800

    feat(ut): add ai plugin proxy unit test (#6070)
    
    Signed-off-by: shown.Ji <[email protected]>
    Co-authored-by: aias00 <[email protected]>
---
 .../shenyu/plugin/ai/proxy/AiProxyPluginTest.java  | 133 +++++++++++++++++++++
 .../ai/proxy/handler/AiProxyPluginHandlerTest.java | 109 +++++++++++++++++
 2 files changed, 242 insertions(+)

diff --git 
a/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-proxy/src/test/java/org/apache/shenyu/plugin/ai/proxy/AiProxyPluginTest.java
 
b/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-proxy/src/test/java/org/apache/shenyu/plugin/ai/proxy/AiProxyPluginTest.java
new file mode 100644
index 0000000000..4ccd8a9d93
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-proxy/src/test/java/org/apache/shenyu/plugin/ai/proxy/AiProxyPluginTest.java
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.apache.shenyu.plugin.ai.proxy;
+
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.dto.convert.rule.AiProxyHandle;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.common.utils.Singleton;
+import org.apache.shenyu.plugin.ai.common.config.AiCommonConfig;
+import org.apache.shenyu.plugin.ai.common.strategy.AiModel;
+import org.apache.shenyu.plugin.ai.proxy.handler.AiProxyPluginHandler;
+import org.apache.shenyu.plugin.api.ShenyuPluginChain;
+import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.http.codec.HttpMessageReader;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class AiProxyPluginTest {
+
+    private AiProxyPlugin plugin;
+
+    private List<HttpMessageReader<?>> messageReaders;
+
+    @BeforeEach
+    void setUp() {
+        messageReaders = mock(List.class);
+        plugin = new AiProxyPlugin(messageReaders);
+    }
+
+    @Test
+    void testDoExecuteWithValidConfig() {
+
+        ShenyuContext shenyuContext = new ShenyuContext();
+        ServerWebExchange exchange = mock(ServerWebExchange.class);
+        
when(exchange.getAttribute(Constants.CONTEXT)).thenReturn(shenyuContext);
+
+        AiCommonConfig config = new AiCommonConfig();
+        config.setBaseUrl("https://api.example.com";);
+        config.setProvider("OPEN_AI");
+        Singleton.INST.single(AiCommonConfig.class, config);
+
+        AiModel aiModel = mock(AiModel.class);
+        when(aiModel.invoke(any(), any(), any(), 
any())).thenReturn(Mono.empty());
+        
AiProxyPluginHandler.SELECTOR_CACHED_HANDLE.get().cachedHandle("selector-id_default_rule",
 new AiProxyHandle());
+
+        SelectorData selector = mock(SelectorData.class);
+        RuleData rule = mock(RuleData.class);
+        ShenyuPluginChain chain = mock(ShenyuPluginChain.class);
+        Mono<Void> result = plugin.doExecute(exchange, chain, selector, rule);
+
+        assertNotNull(result);
+        verify(aiModel, times(0)).invoke(any(), any(), any(), any());
+    }
+
+    @Test
+    void testDoExecuteWithValidContext() {
+
+        ShenyuContext shenyuContext = new ShenyuContext();
+        ServerWebExchange exchange = mock(ServerWebExchange.class);
+        
when(exchange.getAttribute(Constants.CONTEXT)).thenReturn(shenyuContext);
+
+        AiCommonConfig config = new AiCommonConfig();
+        config.setBaseUrl("https://api.example.com";);
+        Singleton.INST.single(AiCommonConfig.class, config);
+
+        AiModel aiModel = mock(AiModel.class);
+        when(aiModel.invoke(any(), any(), any(), 
any())).thenReturn(Mono.empty());
+        
AiProxyPluginHandler.SELECTOR_CACHED_HANDLE.get().cachedHandle("selector-id_default_rule",
 new AiProxyHandle());
+
+        ShenyuPluginChain chain = mock(ShenyuPluginChain.class);
+        SelectorData selector = mock(SelectorData.class);
+        RuleData rule = mock(RuleData.class);
+
+        Mono<Void> result = plugin.doExecute(exchange, chain, selector, rule);
+
+        assertNotNull(result);
+        verify(chain, times(0)).execute(exchange);
+    }
+
+    @Test
+    void testDoExecuteWithInvalidProvider() {
+        ServerWebExchange exchange = mock(ServerWebExchange.class);
+        ShenyuPluginChain chain = mock(ShenyuPluginChain.class);
+        SelectorData selector = mock(SelectorData.class);
+        RuleData rule = mock(RuleData.class);
+
+        AiCommonConfig config = new AiCommonConfig();
+        config.setProvider("INVALID_PROVIDER");
+        Singleton.INST.single(AiCommonConfig.class, config);
+
+        assertThrows(NullPointerException.class, () -> 
plugin.doExecute(exchange, chain, selector, rule));
+    }
+
+    @Test
+    void testGetOrder() {
+        assertEquals(PluginEnum.AI_PROXY.getCode(), plugin.getOrder());
+    }
+
+    @Test
+    void testNamed() {
+        assertEquals(PluginEnum.AI_PROXY.getName(), plugin.named());
+    }
+}
diff --git 
a/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-proxy/src/test/java/org/apache/shenyu/plugin/ai/proxy/handler/AiProxyPluginHandlerTest.java
 
b/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-proxy/src/test/java/org/apache/shenyu/plugin/ai/proxy/handler/AiProxyPluginHandlerTest.java
new file mode 100644
index 0000000000..4e8fdeea68
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-proxy/src/test/java/org/apache/shenyu/plugin/ai/proxy/handler/AiProxyPluginHandlerTest.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.apache.shenyu.plugin.ai.proxy.handler;
+
+import org.apache.shenyu.common.dto.PluginData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.dto.convert.rule.AiProxyHandle;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.common.utils.Singleton;
+import org.apache.shenyu.plugin.ai.common.config.AiCommonConfig;
+import org.apache.shenyu.plugin.base.cache.CommonHandleCache;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class AiProxyPluginHandlerTest {
+
+    private AiProxyPluginHandler handler;
+
+    @BeforeEach
+    void setUp() {
+        handler = new AiProxyPluginHandler();
+    }
+
+    @Test
+    void testHandlerPluginWithValidData() {
+        PluginData pluginData = mock(PluginData.class);
+        when(pluginData.getEnabled()).thenReturn(true);
+        
when(pluginData.getConfig()).thenReturn("{\"baseUrl\":\"https://api.example.com\",\"apiKey\":\"test-key\"}";);
+
+        handler.handlerPlugin(pluginData);
+
+        AiCommonConfig config = Singleton.INST.get(AiCommonConfig.class);
+        assertNotNull(config);
+        assertEquals("https://api.example.com";, config.getBaseUrl());
+        assertEquals("test-key", config.getApiKey());
+    }
+
+    @Test
+    void testHandlerPluginWithNullData() {
+        handler.handlerPlugin(null);
+        AiCommonConfig config = Singleton.INST.get(AiCommonConfig.class);
+        assertNull(config);
+    }
+
+    @Test
+    void testHandlerSelectorWithValidData() {
+        SelectorData selectorData = mock(SelectorData.class);
+        when(selectorData.getId()).thenReturn("selector-id");
+        
when(selectorData.getHandle()).thenReturn("{\"provider\":\"OPEN_AI\",\"baseUrl\":\"https://api.example.com\"}";);
+
+        handler.handlerSelector(selectorData);
+
+        CommonHandleCache<String, AiProxyHandle> cache = 
AiProxyPluginHandler.SELECTOR_CACHED_HANDLE.get();
+        AiProxyHandle handle = cache.obtainHandle("selector-id_default_rule");
+        assertNotNull(handle);
+        assertEquals("OPEN_AI", handle.getProvider());
+        assertEquals("https://api.example.com";, handle.getBaseUrl());
+    }
+
+    @Test
+    void testHandlerSelectorWithNullHandle() {
+        SelectorData selectorData = mock(SelectorData.class);
+        when(selectorData.getHandle()).thenReturn(null);
+
+        handler.handlerSelector(selectorData);
+
+        CommonHandleCache<String, AiProxyHandle> cache = 
AiProxyPluginHandler.SELECTOR_CACHED_HANDLE.get();
+        assertNull(cache.obtainHandle("selector-id_default_rule"));
+    }
+
+    @Test
+    void testRemoveSelector() {
+        SelectorData selectorData = mock(SelectorData.class);
+        when(selectorData.getId()).thenReturn("selector-id");
+        
when(selectorData.getHandle()).thenReturn("{\"provider\":\"OPEN_AI\",\"baseUrl\":\"https://api.example.com\"}";);
+
+        handler.handlerSelector(selectorData);
+        handler.removeSelector(selectorData);
+
+        CommonHandleCache<String, AiProxyHandle> cache = 
AiProxyPluginHandler.SELECTOR_CACHED_HANDLE.get();
+        assertNull(cache.obtainHandle("selector-id_default_rule"));
+    }
+
+    @Test
+    void testPluginNamed() {
+        assertEquals(PluginEnum.AI_PROXY.getName(), handler.pluginNamed());
+    }
+}

Reply via email to