-
Notifications
You must be signed in to change notification settings - Fork 229
[app-builder] fix: avoid NullPointerException for unknown HTTP status codes (#595) #596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
loveTsong
merged 2 commits into
ModelEngine-Group:1.3.x
from
loveTsong:bugfix/data-mate-knowledge-nullpointer-fix
Feb 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...c/test/java/modelengine/fit/jade/datamate/knowledge/DataMateKnowledgeBaseManagerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fit.jade.datamate.knowledge; | ||
|
|
||
| import modelengine.fit.http.client.HttpClassicClientResponse; | ||
| import modelengine.fitframework.annotation.Fit; | ||
| import modelengine.fitframework.test.annotation.MvcTest; | ||
| import modelengine.fitframework.test.domain.mvc.MockMvc; | ||
| import modelengine.fit.jade.datamate.knowledge.dto.DataMateKnowledgeListQueryParam; | ||
| import modelengine.fit.jade.datamate.knowledge.dto.DataMateRetrievalParam; | ||
| import modelengine.fit.jade.datamate.knowledge.entity.DataMateKnowledgeEntity; | ||
| import modelengine.fit.jade.datamate.knowledge.entity.DataMateKnowledgeListEntity; | ||
| import modelengine.fit.jade.datamate.knowledge.entity.DataMateRetrievalChunksEntity; | ||
| import modelengine.fit.jade.datamate.knowledge.entity.DataMateRetrievalResult; | ||
| import modelengine.fit.jade.datamate.knowledge.external.DataMateKnowledgeBaseManager; | ||
| import modelengine.jade.knowledge.exception.KnowledgeException; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.reflect.Field; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * 表示 {@link DataMateKnowledgeBaseManager} 的测试集。 | ||
| * | ||
| * @author songyongtan | ||
| * @since 2026-02-11 | ||
| */ | ||
| @MvcTest(classes = {MockedDataMateKnowledgeBaseInnerController.class, DataMateKnowledgeBaseManager.class}) | ||
| public class DataMateKnowledgeBaseManagerTest { | ||
| private String apiKey = "123"; | ||
|
|
||
| @Fit | ||
| DataMateKnowledgeBaseManager manager; | ||
|
|
||
| @Fit | ||
| private MockMvc mockMvc; | ||
|
|
||
| private HttpClassicClientResponse<?> response; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| Field dataMateUrls = manager.getClass().getDeclaredField("dataMateUrls"); | ||
| dataMateUrls.setAccessible(true); | ||
| Map<String, String> urls = new HashMap<>(); | ||
| urls.put("list", "http://localhost:" + mockMvc.getPort() + "/v2/knowledgeBase"); | ||
| urls.put("retrieve", "http://localhost:" + mockMvc.getPort() + "/v2/knowledgebases/query"); | ||
| dataMateUrls.set(manager, urls); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void teardown() throws IOException { | ||
| if (this.response != null) { | ||
| this.response.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("查询知识库列表成功") | ||
| public void shouldOkWhenListRepo() { | ||
| DataMateKnowledgeListQueryParam param = DataMateKnowledgeListQueryParam.builder().name("ok").build(); | ||
| DataMateKnowledgeListEntity entity = this.manager.listRepos(apiKey, param); | ||
| assertThat(entity.getContent().size()).isEqualTo(2); | ||
| assertThat(entity.getContent().get(0)).extracting(DataMateKnowledgeEntity::getId, | ||
| DataMateKnowledgeEntity::getName, | ||
| DataMateKnowledgeEntity::getDescription).containsExactly("1", "test1", "test1知识库"); | ||
| assertThat(entity.getContent().get(1)).extracting(DataMateKnowledgeEntity::getId, | ||
| DataMateKnowledgeEntity::getName, | ||
| DataMateKnowledgeEntity::getDescription).containsExactly("2", "test2", "test2知识库"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("查询知识库列表失败,抛出异常") | ||
| public void shouldFailWhenListRepoThrowException() { | ||
| DataMateKnowledgeListQueryParam param = DataMateKnowledgeListQueryParam.builder().name("error").build(); | ||
| assertThatThrownBy(() -> this.manager.listRepos(apiKey, param)).isInstanceOf(KnowledgeException.class) | ||
| .extracting("code") | ||
| .isEqualTo(130703005); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("检索知识库成功") | ||
| public void shouldOkWhenRetrieve() { | ||
| DataMateRetrievalParam param = DataMateRetrievalParam.builder().query("ok").build(); | ||
| DataMateRetrievalResult result = this.manager.retrieve(apiKey, param); | ||
| assertThat(result.getData().size()).isEqualTo(3); | ||
| assertThat(result.getData().get(0).chunkId()).isEqualTo("chunk1"); | ||
| assertThat(result.getData().get(0).content()).isEqualTo("content1"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("检索知识库失败,抛出异常") | ||
| public void shouldFailWhenRetrieveThrowException() { | ||
| DataMateRetrievalParam param = DataMateRetrievalParam.builder().query("error").build(); | ||
| assertThatThrownBy(() -> this.manager.retrieve(apiKey, param)).isInstanceOf(KnowledgeException.class) | ||
| .extracting("code") | ||
| .isEqualTo(130703005); | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
...a/modelengine/fit/jade/datamate/knowledge/MockedDataMateKnowledgeBaseInnerController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package modelengine.fit.jade.datamate.knowledge; | ||
|
|
||
| import static modelengine.fitframework.util.IoUtils.content; | ||
|
|
||
| import modelengine.fit.http.annotation.PostMapping; | ||
| import modelengine.fit.http.annotation.RequestBody; | ||
| import modelengine.fit.http.annotation.RequestMapping; | ||
| import modelengine.fit.http.client.HttpClientException; | ||
| import modelengine.fitframework.annotation.Component; | ||
| import modelengine.fitframework.serialization.ObjectSerializer; | ||
| import modelengine.fit.jade.datamate.knowledge.entity.DataMateResponse; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * 表示 DataMate 内部接口的打桩实现。 | ||
| * | ||
| * @author songyongtan | ||
| * @since 2026-02-11 | ||
| */ | ||
| @Component | ||
| @RequestMapping(path = "/v2", group = "DataMate知识库内部接口打桩") | ||
| public class MockedDataMateKnowledgeBaseInnerController { | ||
| private final ObjectSerializer serializer; | ||
|
|
||
| public MockedDataMateKnowledgeBaseInnerController(ObjectSerializer serializer) { | ||
| this.serializer = serializer; | ||
| } | ||
|
|
||
| @PostMapping(path = "/knowledgeBase") | ||
| public Map<String, Object> listRepos(@RequestBody MockedDataMateKnowledgeListQueryParam param) throws IOException { | ||
| if (param.getName().equals("error")) { | ||
| throw new HttpClientException("error"); | ||
| } | ||
| String resourceName = "/listRepoResult.json"; | ||
| String jsonContent = content(DataMateResponse.class, resourceName); | ||
| return serializer.deserialize(jsonContent, Map.class); | ||
| } | ||
|
|
||
| @PostMapping(path = "/knowledgebases/query") | ||
| public Map<String, Object> retrieve(@RequestBody MockedDataMateRetrievalParam param) throws IOException { | ||
| if (param.getQuery().equals("error")) { | ||
| throw new HttpClientException("error"); | ||
| } | ||
| String resourceName = "/retrieveResult.json"; | ||
| String jsonContent = content(DataMateResponse.class, resourceName); | ||
| return serializer.deserialize(jsonContent, Map.class); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
...t/java/modelengine/fit/jade/datamate/knowledge/MockedDataMateKnowledgeListQueryParam.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| */ | ||
|
|
||
| package modelengine.fit.jade.datamate.knowledge; | ||
|
|
||
| import lombok.Data; | ||
| import modelengine.fitframework.serialization.annotation.SerializeStrategy; | ||
|
|
||
| /** | ||
| * 表示 {@link modelengine.fit.jade.datamate.knowledge.dto.DataMateKnowledgeListQueryParam} 类的测试类实现。 | ||
| * | ||
| * @author songyongtan | ||
| * @since 2026-02-11 | ||
| */ | ||
| @Data | ||
| @SerializeStrategy(include = SerializeStrategy.Include.NON_NULL) | ||
| public class MockedDataMateKnowledgeListQueryParam { | ||
| /** | ||
| * 页码,从0开始。 | ||
| */ | ||
| private Integer page; | ||
|
|
||
| /** | ||
| * 每页大小。 | ||
| */ | ||
| private Integer size; | ||
|
|
||
| /** | ||
| * 知识库名称过滤。 | ||
| */ | ||
| private String name; | ||
|
|
||
| /** | ||
| * 知识库描述过滤。 | ||
| */ | ||
| private String description; | ||
| } |
41 changes: 41 additions & 0 deletions
41
...e/src/test/java/modelengine/fit/jade/datamate/knowledge/MockedDataMateRetrievalParam.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. | ||
| * This file is a part of the ModelEngine Project. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| */ | ||
|
|
||
| package modelengine.fit.jade.datamate.knowledge; | ||
|
|
||
| import lombok.Data; | ||
| import modelengine.fitframework.annotation.Property; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * 表示 {@link modelengine.fit.jade.datamate.knowledge.dto.DataMateRetrievalParam} 类的测试类实现。 | ||
| * | ||
| * @author songyongtan | ||
| * @since 2026-02-11 | ||
| */ | ||
| @Data | ||
| public class MockedDataMateRetrievalParam { | ||
| /** | ||
| * 检索 query。 | ||
| */ | ||
| private String query; | ||
| /** | ||
| * 返回前多少的条目。 | ||
| */ | ||
| @Property(description = "topK", name = "topK") | ||
| private Integer topK; | ||
| /** | ||
| * 相关性阈值。 | ||
| */ | ||
| @Property(description = "threshold", name = "threshold") | ||
| private Double threshold; | ||
| /** | ||
| * 指定知识库的id集合。 | ||
| */ | ||
| @Property(description = "knowledgeBaseIds", name = "knowledgeBaseIds") | ||
| private List<String> knowledgeBaseIds; | ||
| } |
24 changes: 24 additions & 0 deletions
24
app-builder/plugins/data-mate-knowledge/src/test/resources/listRepoResult.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "data": { | ||
| "page": 0, | ||
| "size": 2, | ||
| "totalElements": 2, | ||
| "totalPages": 1, | ||
| "content": [ | ||
| { | ||
| "id": "1", | ||
| "name": "test1", | ||
| "description": "test1知识库", | ||
| "createdAt": "2025-04-22T14:27:17Z" | ||
| }, | ||
| { | ||
| "id": "2", | ||
| "name": "test2", | ||
| "description": "test2知识库", | ||
| "createdAt": "2023-10-25T08:06:19Z" | ||
| } | ||
| ] | ||
| }, | ||
| "code": "0", | ||
| "message": "success" | ||
| } |
33 changes: 33 additions & 0 deletions
33
app-builder/plugins/data-mate-knowledge/src/test/resources/retrieveResult.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "data": [ | ||
| { | ||
| "entity": { | ||
| "id": "chunk1", | ||
| "text": "content1", | ||
| "metadata": "{}" | ||
| }, | ||
| "score": 0.9, | ||
| "primaryKey": "id" | ||
| }, | ||
| { | ||
| "entity": { | ||
| "id": "chunk2", | ||
| "text": "content2", | ||
| "metadata": "{}" | ||
| }, | ||
| "score": 0.8, | ||
| "primaryKey": "id" | ||
| }, | ||
| { | ||
| "entity": { | ||
| "id": "chunk3", | ||
| "text": "content3", | ||
| "metadata": "{}" | ||
| }, | ||
| "score": 0.7, | ||
| "primaryKey": "id" | ||
| } | ||
| ], | ||
| "code": "0", | ||
| "message": "success" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,8 +119,9 @@ public QianfanRetrievalResult retrieve(String apiKey, QianfanRetrievalParam para | |
|
|
||
| private KnowledgeException handleException(HttpClientResponseException ex) { | ||
| int statusCode = ex.statusCode(); | ||
| log.error(this.exceptionMap.get(statusCode).getMsg(), ex); | ||
| return new KnowledgeException(this.exceptionMap.get(statusCode), ex.getSimpleMessage()); | ||
| KnowledgeManagerRetCode retCode = this.exceptionMap.getOrDefault(statusCode, INTERNAL_SERVICE_ERROR); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 缺少单元测试覆盖 |
||
| log.error(retCode.getMsg(), ex); | ||
| return new KnowledgeException(retCode, ex.getSimpleMessage()); | ||
| } | ||
|
|
||
| private HttpClassicClient getHttpClient() { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
缺少单元测试覆盖