Skip to content

Commit a1e84bf

Browse files
authored
fix connector tool IT (opensearch-project#4233)
* fix connector tool IT Signed-off-by: Yaliang Wu <[email protected]> * add more checking Signed-off-by: Yaliang Wu <[email protected]> --------- Signed-off-by: Yaliang Wu <[email protected]>
1 parent 8ae83bb commit a1e84bf

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

plugin/src/test/java/org/opensearch/ml/rest/RestConnectorToolIT.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@
66
package org.opensearch.ml.rest;
77

88
import static org.hamcrest.Matchers.containsString;
9+
import static org.opensearch.ml.common.output.model.ModelTensor.DATA_AS_MAP_FIELD;
910
import static org.opensearch.ml.rest.RestMLRemoteInferenceIT.disableClusterConnectorAccessControl;
1011

1112
import java.io.IOException;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.Optional;
1216

1317
import org.apache.hc.core5.http.ParseException;
1418
import org.hamcrest.MatcherAssert;
1519
import org.junit.After;
1620
import org.junit.Before;
21+
import org.opensearch.client.Response;
1722
import org.opensearch.client.ResponseException;
23+
import org.opensearch.core.rest.RestStatus;
24+
import org.opensearch.ml.common.output.model.ModelTensorOutput;
25+
import org.opensearch.ml.common.output.model.ModelTensors;
26+
import org.opensearch.ml.utils.TestHelper;
1827

1928
public class RestConnectorToolIT extends RestBaseAgentToolsIT {
2029
private static final String AWS_ACCESS_KEY_ID = System.getenv("AWS_ACCESS_KEY_ID");
@@ -143,8 +152,25 @@ public void testConnectorToolInFlowAgent() throws IOException, ParseException {
143152
+ "}";
144153
String agentId = createAgent(registerAgentRequestBody);
145154
String agentInput = "{\n" + " \"parameters\": {\n" + " \"messages\": \"hello\"\n" + " }\n" + "}";
146-
String result = executeAgent(agentId, agentInput);
155+
Response response = TestHelper
156+
.makeRequest(client(), "POST", "/_plugins/_ml/agents/" + agentId + "/_execute", null, agentInput, null);
157+
String result = parseResponseFromResponse(response);
158+
assertEquals(RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode()));
147159
assertNotNull(result);
148160
}
149161

162+
private String parseResponseFromResponse(Response response) throws IOException, ParseException {
163+
Map<String, Object> responseInMap = parseResponseToMap(response);
164+
return Optional
165+
.ofNullable(responseInMap)
166+
.map(m -> (List<Object>) m.get(ModelTensorOutput.INFERENCE_RESULT_FIELD))
167+
.filter(l -> !l.isEmpty())
168+
.map(l -> (Map<String, Object>) l.get(0))
169+
.map(m -> (List<Object>) m.get(ModelTensors.OUTPUT_FIELD))
170+
.filter(l -> !l.isEmpty())
171+
.map(l -> (Map<String, Object>) l.get(0))
172+
.map(m -> (Map<String, Object>) m.get(DATA_AS_MAP_FIELD))
173+
.map(m -> (String) m.get("response"))
174+
.orElseThrow(() -> new AssertionError("Unable to parse response from agent execution"));
175+
}
150176
}

0 commit comments

Comments
 (0)