Skip to content

Commit 55ee4c9

Browse files
committed
apply spotless
Signed-off-by: Jiaping Zeng <[email protected]>
1 parent 8821c44 commit 55ee4c9

File tree

1 file changed

+62
-58
lines changed

1 file changed

+62
-58
lines changed

ml-algorithms/src/test/java/org/opensearch/ml/engine/algorithms/agent/AgentUtilsTest.java

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,10 +2005,14 @@ private void mockMcpStreamableHttpConnector(MockedStatic<Connector> connectorSta
20052005
@Test
20062006
public void testExtractRequestHeaders_WithValidHeaders() {
20072007
Map<String, String> parameters = new HashMap<>();
2008-
parameters.put(org.opensearch.ml.common.CommonValue.MCP_REQUEST_HEADERS, "{\"Authorization\":\"Bearer token123\",\"Content-Type\":\"application/json\"}");
2009-
2008+
parameters
2009+
.put(
2010+
org.opensearch.ml.common.CommonValue.MCP_REQUEST_HEADERS,
2011+
"{\"Authorization\":\"Bearer token123\",\"Content-Type\":\"application/json\"}"
2012+
);
2013+
20102014
Map<String, String> result = AgentUtils.extractRequestHeaders(parameters);
2011-
2015+
20122016
assertEquals(2, result.size());
20132017
assertEquals("Bearer token123", result.get("Authorization"));
20142018
assertEquals("application/json", result.get("Content-Type"));
@@ -2017,7 +2021,7 @@ public void testExtractRequestHeaders_WithValidHeaders() {
20172021
@Test
20182022
public void testExtractRequestHeaders_WithNullParameters() {
20192023
Map<String, String> result = AgentUtils.extractRequestHeaders(null);
2020-
2024+
20212025
assertEquals(0, result.size());
20222026
assertEquals(Collections.emptyMap(), result);
20232027
}
@@ -2026,9 +2030,9 @@ public void testExtractRequestHeaders_WithNullParameters() {
20262030
public void testExtractRequestHeaders_WithEmptyHeadersJson() {
20272031
Map<String, String> parameters = new HashMap<>();
20282032
parameters.put(org.opensearch.ml.common.CommonValue.MCP_REQUEST_HEADERS, "");
2029-
2033+
20302034
Map<String, String> result = AgentUtils.extractRequestHeaders(parameters);
2031-
2035+
20322036
assertEquals(0, result.size());
20332037
assertEquals(Collections.emptyMap(), result);
20342038
}
@@ -2037,9 +2041,9 @@ public void testExtractRequestHeaders_WithEmptyHeadersJson() {
20372041
public void testExtractRequestHeaders_WithNullHeadersJson() {
20382042
Map<String, String> parameters = new HashMap<>();
20392043
parameters.put(org.opensearch.ml.common.CommonValue.MCP_REQUEST_HEADERS, null);
2040-
2044+
20412045
Map<String, String> result = AgentUtils.extractRequestHeaders(parameters);
2042-
2046+
20432047
assertEquals(0, result.size());
20442048
assertEquals(Collections.emptyMap(), result);
20452049
}
@@ -2048,9 +2052,9 @@ public void testExtractRequestHeaders_WithNullHeadersJson() {
20482052
public void testExtractRequestHeaders_WithWhitespaceHeadersJson() {
20492053
Map<String, String> parameters = new HashMap<>();
20502054
parameters.put(org.opensearch.ml.common.CommonValue.MCP_REQUEST_HEADERS, " ");
2051-
2055+
20522056
Map<String, String> result = AgentUtils.extractRequestHeaders(parameters);
2053-
2057+
20542058
assertEquals(0, result.size());
20552059
assertEquals(Collections.emptyMap(), result);
20562060
}
@@ -2059,9 +2063,9 @@ public void testExtractRequestHeaders_WithWhitespaceHeadersJson() {
20592063
public void testExtractRequestHeaders_WithInvalidJson() {
20602064
Map<String, String> parameters = new HashMap<>();
20612065
parameters.put(org.opensearch.ml.common.CommonValue.MCP_REQUEST_HEADERS, "{invalid json}");
2062-
2066+
20632067
Map<String, String> result = AgentUtils.extractRequestHeaders(parameters);
2064-
2068+
20652069
assertEquals(0, result.size());
20662070
assertEquals(Collections.emptyMap(), result);
20672071
}
@@ -2070,9 +2074,9 @@ public void testExtractRequestHeaders_WithInvalidJson() {
20702074
public void testExtractRequestHeaders_WithEmptyJsonObject() {
20712075
Map<String, String> parameters = new HashMap<>();
20722076
parameters.put(org.opensearch.ml.common.CommonValue.MCP_REQUEST_HEADERS, "{}");
2073-
2077+
20742078
Map<String, String> result = AgentUtils.extractRequestHeaders(parameters);
2075-
2079+
20762080
assertEquals(0, result.size());
20772081
}
20782082

@@ -2081,13 +2085,13 @@ public void testMergeHeaders_BothNonEmpty() {
20812085
Map<String, String> staticHeaders = new HashMap<>();
20822086
staticHeaders.put("X-Static-Header", "static-value");
20832087
staticHeaders.put("Authorization", "Bearer static-token");
2084-
2088+
20852089
Map<String, String> requestHeaders = new HashMap<>();
20862090
requestHeaders.put("X-Request-Header", "request-value");
20872091
requestHeaders.put("Authorization", "Bearer request-token");
2088-
2092+
20892093
Map<String, String> result = AgentUtils.mergeHeaders(staticHeaders, requestHeaders);
2090-
2094+
20912095
assertEquals(3, result.size());
20922096
assertEquals("static-value", result.get("X-Static-Header"));
20932097
assertEquals("request-value", result.get("X-Request-Header"));
@@ -2098,9 +2102,9 @@ public void testMergeHeaders_BothNonEmpty() {
20982102
public void testMergeHeaders_OnlyStaticHeaders() {
20992103
Map<String, String> staticHeaders = new HashMap<>();
21002104
staticHeaders.put("X-Static-Header", "static-value");
2101-
2105+
21022106
Map<String, String> result = AgentUtils.mergeHeaders(staticHeaders, null);
2103-
2107+
21042108
assertEquals(1, result.size());
21052109
assertEquals("static-value", result.get("X-Static-Header"));
21062110
}
@@ -2109,34 +2113,34 @@ public void testMergeHeaders_OnlyStaticHeaders() {
21092113
public void testMergeHeaders_OnlyRequestHeaders() {
21102114
Map<String, String> requestHeaders = new HashMap<>();
21112115
requestHeaders.put("X-Request-Header", "request-value");
2112-
2116+
21132117
Map<String, String> result = AgentUtils.mergeHeaders(null, requestHeaders);
2114-
2118+
21152119
assertEquals(1, result.size());
21162120
assertEquals("request-value", result.get("X-Request-Header"));
21172121
}
21182122

21192123
@Test
21202124
public void testMergeHeaders_BothNull() {
21212125
Map<String, String> result = AgentUtils.mergeHeaders(null, null);
2122-
2126+
21232127
assertEquals(0, result.size());
21242128
}
21252129

21262130
@Test
21272131
public void testMergeHeaders_BothEmpty() {
21282132
Map<String, String> result = AgentUtils.mergeHeaders(new HashMap<>(), new HashMap<>());
2129-
2133+
21302134
assertEquals(0, result.size());
21312135
}
21322136

21332137
@Test
21342138
public void testMergeHeaders_EmptyStaticNonEmptyRequest() {
21352139
Map<String, String> requestHeaders = new HashMap<>();
21362140
requestHeaders.put("X-Request-Header", "request-value");
2137-
2141+
21382142
Map<String, String> result = AgentUtils.mergeHeaders(new HashMap<>(), requestHeaders);
2139-
2143+
21402144
assertEquals(1, result.size());
21412145
assertEquals("request-value", result.get("X-Request-Header"));
21422146
}
@@ -2150,7 +2154,7 @@ public void testCleanupMcpClients_WithNullTools() {
21502154
@Test
21512155
public void testCleanupMcpClients_WithEmptyTools() {
21522156
Map<String, Tool> tools = new HashMap<>();
2153-
2157+
21542158
// Should not throw exception
21552159
AgentUtils.cleanupMcpClients(tools);
21562160
}
@@ -2161,7 +2165,7 @@ public void testCleanupMcpClients_WithToolsWithoutMcpClient() {
21612165
Tool tool = mock(Tool.class);
21622166
when(tool.getAttributes()).thenReturn(new HashMap<>());
21632167
tools.put("tool1", tool);
2164-
2168+
21652169
// Should not throw exception
21662170
AgentUtils.cleanupMcpClients(tools);
21672171
}
@@ -2172,7 +2176,7 @@ public void testCleanupMcpClients_WithToolsWithNullAttributes() {
21722176
Tool tool = mock(Tool.class);
21732177
when(tool.getAttributes()).thenReturn(null);
21742178
tools.put("tool1", tool);
2175-
2179+
21762180
// Should not throw exception
21772181
AgentUtils.cleanupMcpClients(tools);
21782182
}
@@ -2182,41 +2186,41 @@ public void testCleanupMcpClients_WithMcpClient() {
21822186
Map<String, Tool> tools = new HashMap<>();
21832187
Tool tool = mock(Tool.class);
21842188
io.modelcontextprotocol.client.McpSyncClient mcpClient = mock(io.modelcontextprotocol.client.McpSyncClient.class);
2185-
2189+
21862190
Map<String, Object> attributes = new HashMap<>();
21872191
attributes.put(org.opensearch.ml.common.CommonValue.MCP_SYNC_CLIENT, mcpClient);
21882192
when(tool.getAttributes()).thenReturn(attributes);
21892193
when(tool.getName()).thenReturn("test-tool");
21902194
tools.put("tool1", tool);
2191-
2195+
21922196
AgentUtils.cleanupMcpClients(tools);
2193-
2197+
21942198
verify(mcpClient).closeGracefully();
21952199
}
21962200

21972201
@Test
21982202
public void testCleanupMcpClients_WithMultipleToolsSameMcpClient() {
21992203
Map<String, Tool> tools = new HashMap<>();
22002204
io.modelcontextprotocol.client.McpSyncClient mcpClient = mock(io.modelcontextprotocol.client.McpSyncClient.class);
2201-
2205+
22022206
// Create two tools sharing the same MCP client
22032207
Tool tool1 = mock(Tool.class);
22042208
Map<String, Object> attributes1 = new HashMap<>();
22052209
attributes1.put(org.opensearch.ml.common.CommonValue.MCP_SYNC_CLIENT, mcpClient);
22062210
when(tool1.getAttributes()).thenReturn(attributes1);
22072211
when(tool1.getName()).thenReturn("test-tool-1");
2208-
2212+
22092213
Tool tool2 = mock(Tool.class);
22102214
Map<String, Object> attributes2 = new HashMap<>();
22112215
attributes2.put(org.opensearch.ml.common.CommonValue.MCP_SYNC_CLIENT, mcpClient);
22122216
when(tool2.getAttributes()).thenReturn(attributes2);
22132217
when(tool2.getName()).thenReturn("test-tool-2");
2214-
2218+
22152219
tools.put("tool1", tool1);
22162220
tools.put("tool2", tool2);
2217-
2221+
22182222
AgentUtils.cleanupMcpClients(tools);
2219-
2223+
22202224
// Should only close once even though two tools share the same client
22212225
verify(mcpClient).closeGracefully();
22222226
}
@@ -2226,24 +2230,24 @@ public void testCleanupMcpClients_WithMultipleToolsDifferentMcpClients() {
22262230
Map<String, Tool> tools = new HashMap<>();
22272231
io.modelcontextprotocol.client.McpSyncClient mcpClient1 = mock(io.modelcontextprotocol.client.McpSyncClient.class);
22282232
io.modelcontextprotocol.client.McpSyncClient mcpClient2 = mock(io.modelcontextprotocol.client.McpSyncClient.class);
2229-
2233+
22302234
Tool tool1 = mock(Tool.class);
22312235
Map<String, Object> attributes1 = new HashMap<>();
22322236
attributes1.put(org.opensearch.ml.common.CommonValue.MCP_SYNC_CLIENT, mcpClient1);
22332237
when(tool1.getAttributes()).thenReturn(attributes1);
22342238
when(tool1.getName()).thenReturn("test-tool-1");
2235-
2239+
22362240
Tool tool2 = mock(Tool.class);
22372241
Map<String, Object> attributes2 = new HashMap<>();
22382242
attributes2.put(org.opensearch.ml.common.CommonValue.MCP_SYNC_CLIENT, mcpClient2);
22392243
when(tool2.getAttributes()).thenReturn(attributes2);
22402244
when(tool2.getName()).thenReturn("test-tool-2");
2241-
2245+
22422246
tools.put("tool1", tool1);
22432247
tools.put("tool2", tool2);
2244-
2248+
22452249
AgentUtils.cleanupMcpClients(tools);
2246-
2250+
22472251
verify(mcpClient1).closeGracefully();
22482252
verify(mcpClient2).closeGracefully();
22492253
}
@@ -2253,19 +2257,19 @@ public void testCleanupMcpClients_WithExceptionDuringClose() {
22532257
Map<String, Tool> tools = new HashMap<>();
22542258
Tool tool = mock(Tool.class);
22552259
io.modelcontextprotocol.client.McpSyncClient mcpClient = mock(io.modelcontextprotocol.client.McpSyncClient.class);
2256-
2260+
22572261
Map<String, Object> attributes = new HashMap<>();
22582262
attributes.put(org.opensearch.ml.common.CommonValue.MCP_SYNC_CLIENT, mcpClient);
22592263
when(tool.getAttributes()).thenReturn(attributes);
22602264
when(tool.getName()).thenReturn("test-tool");
22612265
tools.put("tool1", tool);
2262-
2266+
22632267
// Make closeGracefully throw an exception
22642268
doThrow(new RuntimeException("Close failed")).when(mcpClient).closeGracefully();
2265-
2269+
22662270
// Should not throw exception, just log warning
22672271
AgentUtils.cleanupMcpClients(tools);
2268-
2272+
22692273
verify(mcpClient).closeGracefully();
22702274
}
22712275

@@ -2274,18 +2278,18 @@ public void testWrapListenerWithMcpCleanup_OnResponse() {
22742278
Map<String, Tool> tools = new HashMap<>();
22752279
Tool tool = mock(Tool.class);
22762280
io.modelcontextprotocol.client.McpSyncClient mcpClient = mock(io.modelcontextprotocol.client.McpSyncClient.class);
2277-
2281+
22782282
Map<String, Object> attributes = new HashMap<>();
22792283
attributes.put(org.opensearch.ml.common.CommonValue.MCP_SYNC_CLIENT, mcpClient);
22802284
when(tool.getAttributes()).thenReturn(attributes);
22812285
when(tool.getName()).thenReturn("test-tool");
22822286
tools.put("tool1", tool);
2283-
2287+
22842288
ActionListener<String> delegate = mock(ActionListener.class);
22852289
ActionListener<String> wrapped = AgentUtils.wrapListenerWithMcpCleanup(delegate, tools);
2286-
2290+
22872291
wrapped.onResponse("test-response");
2288-
2292+
22892293
verify(mcpClient).closeGracefully();
22902294
verify(delegate).onResponse("test-response");
22912295
}
@@ -2295,19 +2299,19 @@ public void testWrapListenerWithMcpCleanup_OnFailure() {
22952299
Map<String, Tool> tools = new HashMap<>();
22962300
Tool tool = mock(Tool.class);
22972301
io.modelcontextprotocol.client.McpSyncClient mcpClient = mock(io.modelcontextprotocol.client.McpSyncClient.class);
2298-
2302+
22992303
Map<String, Object> attributes = new HashMap<>();
23002304
attributes.put(org.opensearch.ml.common.CommonValue.MCP_SYNC_CLIENT, mcpClient);
23012305
when(tool.getAttributes()).thenReturn(attributes);
23022306
when(tool.getName()).thenReturn("test-tool");
23032307
tools.put("tool1", tool);
2304-
2308+
23052309
ActionListener<String> delegate = mock(ActionListener.class);
23062310
ActionListener<String> wrapped = AgentUtils.wrapListenerWithMcpCleanup(delegate, tools);
2307-
2311+
23082312
Exception testException = new RuntimeException("test exception");
23092313
wrapped.onFailure(testException);
2310-
2314+
23112315
verify(mcpClient).closeGracefully();
23122316
verify(delegate).onFailure(testException);
23132317
}
@@ -2317,21 +2321,21 @@ public void testWrapListenerWithMcpCleanup_CleanupFailureDoesNotPreventDelegateC
23172321
Map<String, Tool> tools = new HashMap<>();
23182322
Tool tool = mock(Tool.class);
23192323
io.modelcontextprotocol.client.McpSyncClient mcpClient = mock(io.modelcontextprotocol.client.McpSyncClient.class);
2320-
2324+
23212325
Map<String, Object> attributes = new HashMap<>();
23222326
attributes.put(org.opensearch.ml.common.CommonValue.MCP_SYNC_CLIENT, mcpClient);
23232327
when(tool.getAttributes()).thenReturn(attributes);
23242328
when(tool.getName()).thenReturn("test-tool");
23252329
tools.put("tool1", tool);
2326-
2330+
23272331
// Make cleanup fail
23282332
doThrow(new RuntimeException("Cleanup failed")).when(mcpClient).closeGracefully();
2329-
2333+
23302334
ActionListener<String> delegate = mock(ActionListener.class);
23312335
ActionListener<String> wrapped = AgentUtils.wrapListenerWithMcpCleanup(delegate, tools);
2332-
2336+
23332337
wrapped.onResponse("test-response");
2334-
2338+
23352339
// Delegate should still be called even if cleanup fails
23362340
verify(delegate).onResponse("test-response");
23372341
}

0 commit comments

Comments
 (0)