|
25 | 25 | import modelengine.jade.store.service.PluginToolService; |
26 | 26 |
|
27 | 27 | import org.jetbrains.annotations.NotNull; |
| 28 | +import org.junit.jupiter.api.Assertions; |
28 | 29 | import org.junit.jupiter.api.BeforeEach; |
29 | 30 | import org.junit.jupiter.api.DisplayName; |
30 | 31 | import org.junit.jupiter.api.Test; |
@@ -68,7 +69,7 @@ void beforeAll() { |
68 | 69 | this.aippModelCenter, |
69 | 70 | this.toolService, |
70 | 71 | this.localeService, |
71 | | - this.appRepository); |
| 72 | + this.appRepository, "system"); |
72 | 73 | } |
73 | 74 |
|
74 | 75 | @Test |
@@ -135,6 +136,81 @@ void shouldOkWhenSelectTools() { |
135 | 136 | "UNIQUENAME3"); |
136 | 137 | } |
137 | 138 |
|
| 139 | + @Test |
| 140 | + void testMerge_tool1IsNull_returnsTools2() { |
| 141 | + ListResult<PluginToolData> tools2 = new ListResult<>(List.of(new PluginToolData()), 1); |
| 142 | + ListResult<PluginToolData> result = this.invokeMerge(null, tools2); |
| 143 | + Assertions.assertSame(tools2, result); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * 测试当 tool1 的 count 为 0 时是否正确返回 tools2 |
| 148 | + */ |
| 149 | + @Test |
| 150 | + void testMerge_tool1IsEmpty_returnsTools2() { |
| 151 | + ListResult<PluginToolData> tool1 = new ListResult<>(new ArrayList<>(), 0); |
| 152 | + ListResult<PluginToolData> tools2 = new ListResult<>(List.of(new PluginToolData()), 1); |
| 153 | + ListResult<PluginToolData> result = this.invokeMerge(tool1, tools2); |
| 154 | + Assertions.assertSame(tools2, result); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * 测试当 tools2 为 null 时是否正确返回 tool1 |
| 159 | + */ |
| 160 | + @Test |
| 161 | + void testMerge_tools2IsNull_returnsTool1() { |
| 162 | + ListResult<PluginToolData> tool1 = new ListResult<>(List.of(new PluginToolData()), 1); |
| 163 | + ListResult<PluginToolData> result = invokeMerge(tool1, null); |
| 164 | + Assertions.assertSame(tool1, result); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * 测试当 tools2 的 count 为 0 时是否正确返回 tool1 |
| 169 | + */ |
| 170 | + @Test |
| 171 | + void testMerge_tools2IsEmpty_returnsTool1() { |
| 172 | + ListResult<PluginToolData> tool1 = new ListResult<>(List.of(new PluginToolData()), 1); |
| 173 | + ListResult<PluginToolData> tools2 = new ListResult<>(new ArrayList<>(), 0); |
| 174 | + ListResult<PluginToolData> result = invokeMerge(tool1, tools2); |
| 175 | + Assertions.assertSame(tool1, result); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * 测试正常情况下两个列表能否成功合并 |
| 180 | + */ |
| 181 | + @Test |
| 182 | + void testMerge_bothValid_mergeCorrectly() { |
| 183 | + PluginToolData data1 = new PluginToolData(); |
| 184 | + PluginToolData data2 = new PluginToolData(); |
| 185 | + ListResult<PluginToolData> tool1 = new ListResult<>(List.of(data1), 1); |
| 186 | + ListResult<PluginToolData> tools2 = new ListResult<>(List.of(data2), 1); |
| 187 | + |
| 188 | + ListResult<PluginToolData> result = invokeMerge(tool1, tools2); |
| 189 | + |
| 190 | + Assertions.assertNotNull(result); |
| 191 | + Assertions.assertEquals(2, result.getCount()); |
| 192 | + Assertions.assertTrue(result.getData().contains(data1)); |
| 193 | + Assertions.assertTrue(result.getData().contains(data2)); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * 利用反射机制调用私有方法 merge |
| 198 | + * |
| 199 | + * @param tool1 第一个 ListResult 参数 |
| 200 | + * @param tools2 第二个 ListResult 参数 |
| 201 | + * @return 合并后的 ListResult 结果 |
| 202 | + */ |
| 203 | + private ListResult<PluginToolData> invokeMerge(ListResult<PluginToolData> tool1, ListResult<PluginToolData> tools2) { |
| 204 | + try { |
| 205 | + var method = AgentInfoGenerateServiceImpl.class.getDeclaredMethod("merge", |
| 206 | + ListResult.class, ListResult.class); |
| 207 | + method.setAccessible(true); |
| 208 | + return (ListResult<PluginToolData>) method.invoke(this.agentInfoGenerateService, tool1, tools2); |
| 209 | + } catch (Exception e) { |
| 210 | + throw new RuntimeException("Failed to invoke private method 'merge'", e); |
| 211 | + } |
| 212 | + } |
| 213 | + |
138 | 214 | @NotNull |
139 | 215 | private ModelListDto getModelListDto() { |
140 | 216 | List<ModelAccessInfo> modelAccessInfos = new ArrayList<>(); |
|
0 commit comments