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

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


The following commit(s) were added to refs/heads/master by this push:
     new ae03111a7 [Improve] add AiController unit test (#2443)
ae03111a7 is described below

commit ae03111a7554976b991d4b74eb90dd3ff34320a9
Author: YuLuo <[email protected]>
AuthorDate: Sat Aug 3 23:47:24 2024 +0800

    [Improve] add AiController unit test (#2443)
    
    Signed-off-by: yuluo-yx <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
---
 .../hertzbeat/manager/controller/AiController.java |  1 -
 .../manager/controller/AiControllerTest.java       | 87 ++++++++++++++++++++++
 2 files changed, 87 insertions(+), 1 deletion(-)

diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java
index 9c2c3fd7c..6c7408826 100644
--- 
a/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java
@@ -33,7 +33,6 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import reactor.core.publisher.Flux;
 
-
 /**
  * AI Management API
  */
diff --git 
a/manager/src/test/java/org/apache/hertzbeat/manager/controller/AiControllerTest.java
 
b/manager/src/test/java/org/apache/hertzbeat/manager/controller/AiControllerTest.java
new file mode 100644
index 000000000..7c1bfa5f2
--- /dev/null
+++ 
b/manager/src/test/java/org/apache/hertzbeat/manager/controller/AiControllerTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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.hertzbeat.manager.controller;
+
+import org.apache.hertzbeat.manager.config.AiProperties;
+import org.apache.hertzbeat.manager.service.AiService;
+import org.apache.hertzbeat.manager.service.impl.AiServiceFactoryImpl;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import reactor.core.publisher.Flux;
+
+import org.springframework.http.MediaType;
+import org.springframework.http.codec.ServerSentEvent;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.when;
+import static 
org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static 
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static 
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+
+/**
+ * test case for {@link AiController}
+ */
+
+@ExtendWith(MockitoExtension.class)
+class AiControllerTest {
+
+       private MockMvc mockMvc;
+
+       @Mock
+       private AiServiceFactoryImpl aiServiceFactory;
+
+       @Mock
+       private AiProperties aiProperties;
+
+       @Mock
+       private AiService aiService;
+
+       @InjectMocks
+       private AiController aiController;
+
+       @BeforeEach
+       public void setup() {
+
+               mockMvc = MockMvcBuilders.standaloneSetup(aiController).build();
+       }
+
+       @Test
+       public void testRequestAi() throws Exception {
+
+               String responseText = "response";
+               Flux<ServerSentEvent<String>> responseFlux = 
Flux.just(ServerSentEvent.builder(responseText).build());
+
+               
when(aiServiceFactory.getAiServiceImplBean(anyString())).thenReturn(aiService);
+               when(aiService.requestAi(anyString())).thenReturn(responseFlux);
+               when(aiProperties.getType()).thenReturn("alibabaAi");
+
+               mockMvc.perform(get("/api/ai/get")
+                                               .param("text", "Who are you")
+                                               
.accept(MediaType.TEXT_EVENT_STREAM))
+                               .andExpect(status().isOk())
+                               
.andExpect(content().contentType(MediaType.TEXT_EVENT_STREAM_VALUE))
+                               
.andExpect(content().string("data:response\n\n"));
+       }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to