|
| 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 | + * @author songyongtan |
| 39 | + * @since 2026-02-11 |
| 40 | + */ |
| 41 | +@MvcTest(classes = {MockedDataMateKnowledgeBaseInnerController.class, DataMateKnowledgeBaseManager.class}) |
| 42 | +public class DataMateKnowledgeBaseManagerTest { |
| 43 | + private String apiKey = "123"; |
| 44 | + |
| 45 | + @Fit |
| 46 | + DataMateKnowledgeBaseManager manager; |
| 47 | + |
| 48 | + @Fit |
| 49 | + private MockMvc mockMvc; |
| 50 | + |
| 51 | + private HttpClassicClientResponse<?> response; |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + void setUp() throws Exception { |
| 55 | + Field dataMateUrls = manager.getClass().getDeclaredField("dataMateUrls"); |
| 56 | + dataMateUrls.setAccessible(true); |
| 57 | + Map<String, String> urls = new HashMap<>(); |
| 58 | + urls.put("list", "http://localhost:" + mockMvc.getPort() + "/v2/knowledgeBase"); |
| 59 | + urls.put("retrieve", "http://localhost:" + mockMvc.getPort() + "/v2/knowledgebases/query"); |
| 60 | + dataMateUrls.set(manager, urls); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterEach |
| 64 | + void teardown() throws IOException { |
| 65 | + if (this.response != null) { |
| 66 | + this.response.close(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + @DisplayName("查询知识库列表成功") |
| 72 | + public void shouldOkWhenListRepo() { |
| 73 | + DataMateKnowledgeListQueryParam param = DataMateKnowledgeListQueryParam.builder().name("ok").build(); |
| 74 | + DataMateKnowledgeListEntity entity = this.manager.listRepos(apiKey, param); |
| 75 | + assertThat(entity.getContent().size()).isEqualTo(2); |
| 76 | + assertThat(entity.getContent().get(0)).extracting(DataMateKnowledgeEntity::getId, |
| 77 | + DataMateKnowledgeEntity::getName, |
| 78 | + DataMateKnowledgeEntity::getDescription).containsExactly("1", "test1", "test1知识库"); |
| 79 | + assertThat(entity.getContent().get(1)).extracting(DataMateKnowledgeEntity::getId, |
| 80 | + DataMateKnowledgeEntity::getName, |
| 81 | + DataMateKnowledgeEntity::getDescription).containsExactly("2", "test2", "test2知识库"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @DisplayName("查询知识库列表失败,抛出异常") |
| 86 | + public void shouldFailWhenListRepoThrowException() { |
| 87 | + DataMateKnowledgeListQueryParam param = DataMateKnowledgeListQueryParam.builder().name("error").build(); |
| 88 | + assertThatThrownBy(() -> this.manager.listRepos(apiKey, param)).isInstanceOf(KnowledgeException.class) |
| 89 | + .extracting("code") |
| 90 | + .isEqualTo(130703005); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + @DisplayName("检索知识库成功") |
| 95 | + public void shouldOkWhenRetrieve() { |
| 96 | + DataMateRetrievalParam param = DataMateRetrievalParam.builder().query("ok").build(); |
| 97 | + DataMateRetrievalResult result = this.manager.retrieve(apiKey, param); |
| 98 | + assertThat(result.getData().size()).isEqualTo(3); |
| 99 | + assertThat(result.getData().get(0).chunkId()).isEqualTo("chunk1"); |
| 100 | + assertThat(result.getData().get(0).content()).isEqualTo("content1"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("检索知识库失败,抛出异常") |
| 105 | + public void shouldFailWhenRetrieveThrowException() { |
| 106 | + DataMateRetrievalParam param = DataMateRetrievalParam.builder().query("error").build(); |
| 107 | + assertThatThrownBy(() -> this.manager.retrieve(apiKey, param)).isInstanceOf(KnowledgeException.class) |
| 108 | + .extracting("code") |
| 109 | + .isEqualTo(130703005); |
| 110 | + } |
| 111 | +} |
0 commit comments