Skip to content

Commit f253013

Browse files
committed
[app-builder] test: add unit tests for DataMateKnowledgeBaseManager
1 parent d5ea9b3 commit f253013

6 files changed

Lines changed: 301 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
7+
package modelengine.fit.jade.datamate.knowledge;
8+
9+
import modelengine.fit.http.client.HttpClassicClientResponse;
10+
import modelengine.fitframework.annotation.Fit;
11+
import modelengine.fitframework.test.annotation.MvcTest;
12+
import modelengine.fitframework.test.domain.mvc.MockMvc;
13+
import modelengine.fit.jade.datamate.knowledge.dto.DataMateKnowledgeListQueryParam;
14+
import modelengine.fit.jade.datamate.knowledge.dto.DataMateRetrievalParam;
15+
import modelengine.fit.jade.datamate.knowledge.entity.DataMateKnowledgeEntity;
16+
import modelengine.fit.jade.datamate.knowledge.entity.DataMateKnowledgeListEntity;
17+
import modelengine.fit.jade.datamate.knowledge.entity.DataMateRetrievalChunksEntity;
18+
import modelengine.fit.jade.datamate.knowledge.entity.DataMateRetrievalResult;
19+
import modelengine.fit.jade.datamate.knowledge.external.DataMateKnowledgeBaseManager;
20+
import modelengine.jade.knowledge.exception.KnowledgeException;
21+
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.DisplayName;
25+
import org.junit.jupiter.api.Test;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
29+
30+
import java.io.IOException;
31+
import java.lang.reflect.Field;
32+
import java.util.HashMap;
33+
import java.util.Map;
34+
35+
/**
36+
* 表示 {@link DataMateKnowledgeBaseManager} 的测试集。
37+
*
38+
* @since 2026-02-11
39+
*/
40+
@MvcTest(classes = {MockedDataMateKnowledgeBaseInnerController.class, DataMateKnowledgeBaseManager.class})
41+
public class DataMateKnowledgeBaseManagerTest {
42+
private String apiKey = "123";
43+
44+
@Fit
45+
DataMateKnowledgeBaseManager manager;
46+
47+
@Fit
48+
private MockMvc mockMvc;
49+
50+
private HttpClassicClientResponse<?> response;
51+
52+
@BeforeEach
53+
void setUp() throws Exception {
54+
Field dataMateUrls = manager.getClass().getDeclaredField("dataMateUrls");
55+
dataMateUrls.setAccessible(true);
56+
Map<String, String> urls = new HashMap<>();
57+
urls.put("list", "http://localhost:" + mockMvc.getPort() + "/v2/knowledgeBase");
58+
urls.put("retrieve", "http://localhost:" + mockMvc.getPort() + "/v2/knowledgebases/query");
59+
dataMateUrls.set(manager, urls);
60+
}
61+
62+
@AfterEach
63+
void teardown() throws IOException {
64+
if (this.response != null) {
65+
this.response.close();
66+
}
67+
}
68+
69+
@Test
70+
@DisplayName("查询知识库列表成功")
71+
public void shouldOkWhenListRepo() {
72+
DataMateKnowledgeListQueryParam param = DataMateKnowledgeListQueryParam.builder().name("ok").build();
73+
DataMateKnowledgeListEntity entity = this.manager.listRepos(apiKey, param);
74+
assertThat(entity.getContent().size()).isEqualTo(2);
75+
assertThat(entity.getContent().get(0)).extracting(DataMateKnowledgeEntity::getId,
76+
DataMateKnowledgeEntity::getName,
77+
DataMateKnowledgeEntity::getDescription).containsExactly("1", "test1", "test1知识库");
78+
assertThat(entity.getContent().get(1)).extracting(DataMateKnowledgeEntity::getId,
79+
DataMateKnowledgeEntity::getName,
80+
DataMateKnowledgeEntity::getDescription).containsExactly("2", "test2", "test2知识库");
81+
}
82+
83+
@Test
84+
@DisplayName("查询知识库列表失败,抛出异常")
85+
public void shouldFailWhenListRepoThrowException() {
86+
DataMateKnowledgeListQueryParam param = DataMateKnowledgeListQueryParam.builder().name("error").build();
87+
assertThatThrownBy(() -> this.manager.listRepos(apiKey, param)).isInstanceOf(KnowledgeException.class)
88+
.extracting("code")
89+
.isEqualTo(130703005);
90+
}
91+
92+
@Test
93+
@DisplayName("检索知识库成功")
94+
public void shouldOkWhenRetrieve() {
95+
DataMateRetrievalParam param = DataMateRetrievalParam.builder().query("ok").build();
96+
DataMateRetrievalResult result = this.manager.retrieve(apiKey, param);
97+
assertThat(result.getData().size()).isEqualTo(3);
98+
assertThat(result.getData().get(0).chunkId()).isEqualTo("chunk1");
99+
assertThat(result.getData().get(0).content()).isEqualTo("content1");
100+
}
101+
102+
@Test
103+
@DisplayName("检索知识库失败,抛出异常")
104+
public void shouldFailWhenRetrieveThrowException() {
105+
DataMateRetrievalParam param = DataMateRetrievalParam.builder().query("error").build();
106+
assertThatThrownBy(() -> this.manager.retrieve(apiKey, param)).isInstanceOf(KnowledgeException.class)
107+
.extracting("code")
108+
.isEqualTo(130703005);
109+
}
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
7+
package modelengine.fit.jade.datamate.knowledge;
8+
9+
import static modelengine.fitframework.util.IoUtils.content;
10+
11+
import modelengine.fit.http.annotation.PostMapping;
12+
import modelengine.fit.http.annotation.RequestBody;
13+
import modelengine.fit.http.annotation.RequestMapping;
14+
import modelengine.fit.http.client.HttpClientException;
15+
import modelengine.fitframework.annotation.Component;
16+
import modelengine.fitframework.serialization.ObjectSerializer;
17+
import modelengine.fit.jade.datamate.knowledge.entity.DataMateResponse;
18+
19+
import java.io.IOException;
20+
import java.util.Map;
21+
22+
/**
23+
* 表示 DataMate 内部接口的打桩实现。
24+
*
25+
* @since 2026-02-11
26+
*/
27+
@Component
28+
@RequestMapping(path = "/v2", group = "DataMate知识库内部接口打桩")
29+
public class MockedDataMateKnowledgeBaseInnerController {
30+
private final ObjectSerializer serializer;
31+
32+
public MockedDataMateKnowledgeBaseInnerController(ObjectSerializer serializer) {
33+
this.serializer = serializer;
34+
}
35+
36+
@PostMapping(path = "/knowledgeBase")
37+
public Map<String, Object> listRepos(@RequestBody MockedDataMateKnowledgeListQueryParam param) throws IOException {
38+
if (param.getName().equals("error")) {
39+
throw new HttpClientException("error");
40+
}
41+
String resourceName = "/listRepoResult.json";
42+
String jsonContent = content(DataMateResponse.class, resourceName);
43+
return serializer.deserialize(jsonContent, Map.class);
44+
}
45+
46+
@PostMapping(path = "/knowledgebases/query")
47+
public Map<String, Object> retrieve(@RequestBody MockedDataMateRetrievalParam param) throws IOException {
48+
if (param.getQuery().equals("error")) {
49+
throw new HttpClientException("error");
50+
}
51+
String resourceName = "/retrieveResult.json";
52+
String jsonContent = content(DataMateResponse.class, resourceName);
53+
return serializer.deserialize(jsonContent, Map.class);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
6+
7+
package modelengine.fit.jade.datamate.knowledge;
8+
9+
import lombok.Data;
10+
import modelengine.fitframework.serialization.annotation.SerializeStrategy;
11+
12+
/**
13+
* 表示 {@link modelengine.fit.jade.datamate.knowledge.dto.DataMateKnowledgeListQueryParam} 类的测试类实现。
14+
*
15+
* @since 2026-02-11
16+
*/
17+
@Data
18+
@SerializeStrategy(include = SerializeStrategy.Include.NON_NULL)
19+
public class MockedDataMateKnowledgeListQueryParam {
20+
/**
21+
* 页码,从0开始。
22+
*/
23+
private Integer page;
24+
25+
/**
26+
* 每页大小。
27+
*/
28+
private Integer size;
29+
30+
/**
31+
* 知识库名称过滤。
32+
*/
33+
private String name;
34+
35+
/**
36+
* 知识库描述过滤。
37+
*/
38+
private String description;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
6+
7+
package modelengine.fit.jade.datamate.knowledge;
8+
9+
import lombok.Data;
10+
import modelengine.fitframework.annotation.Property;
11+
12+
import java.util.List;
13+
14+
/**
15+
* 表示 {@link modelengine.fit.jade.datamate.knowledge.dto.DataMateRetrievalParam} 类的测试类实现。
16+
*
17+
* @since 2026-02-11
18+
*/
19+
@Data
20+
public class MockedDataMateRetrievalParam {
21+
/**
22+
* 检索 query。
23+
*/
24+
private String query;
25+
/**
26+
* 返回前多少的条目。
27+
*/
28+
@Property(description = "topK", name = "topK")
29+
private Integer topK;
30+
/**
31+
* 相关性阈值。
32+
*/
33+
@Property(description = "threshold", name = "threshold")
34+
private Double threshold;
35+
/**
36+
* 指定知识库的id集合。
37+
*/
38+
@Property(description = "knowledgeBaseIds", name = "knowledgeBaseIds")
39+
private List<String> knowledgeBaseIds;
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"data": {
3+
"page": 0,
4+
"size": 2,
5+
"totalElements": 2,
6+
"totalPages": 1,
7+
"content": [
8+
{
9+
"id": "1",
10+
"name": "test1",
11+
"description": "test1知识库",
12+
"createdAt": "2025-04-22T14:27:17Z"
13+
},
14+
{
15+
"id": "2",
16+
"name": "test2",
17+
"description": "test2知识库",
18+
"createdAt": "2023-10-25T08:06:19Z"
19+
}
20+
]
21+
},
22+
"code": "0",
23+
"message": "success"
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"data": [
3+
{
4+
"entity": {
5+
"id": "chunk1",
6+
"text": "content1",
7+
"metadata": "{}"
8+
},
9+
"score": 0.9,
10+
"primaryKey": "id"
11+
},
12+
{
13+
"entity": {
14+
"id": "chunk2",
15+
"text": "content2",
16+
"metadata": "{}"
17+
},
18+
"score": 0.8,
19+
"primaryKey": "id"
20+
},
21+
{
22+
"entity": {
23+
"id": "chunk3",
24+
"text": "content3",
25+
"metadata": "{}"
26+
},
27+
"score": 0.7,
28+
"primaryKey": "id"
29+
}
30+
],
31+
"code": "0",
32+
"message": "success"
33+
}

0 commit comments

Comments
 (0)