From 01bf7c06515d4725310f1b4d7943ada5f4d52479 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 12 Mar 2024 12:48:41 -0700 Subject: [PATCH] fix npe issue (#2145) (#2194) * fix npe issue Signed-off-by: Jing Zhang * add UT and change error message Signed-off-by: Jing Zhang * add more UT Signed-off-by: Jing Zhang --------- Signed-off-by: Jing Zhang (cherry picked from commit 0b07a99309d03ad19a53fac6c1edb51feddd3e17) Co-authored-by: Jing Zhang --- .../ml/engine/algorithms/agent/MLAgentExecutor.java | 4 ++-- .../algorithms/agent/MLAgentExecutorTest.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutor.java b/ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutor.java index cb779abf31..8b7cf10ef9 100644 --- a/ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutor.java +++ b/ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutor.java @@ -98,8 +98,8 @@ public void execute(Input input, ActionListener listener) { AgentMLInput agentMLInput = (AgentMLInput) input; String agentId = agentMLInput.getAgentId(); RemoteInferenceInputDataSet inputDataSet = (RemoteInferenceInputDataSet) agentMLInput.getInputDataset(); - if (inputDataSet.getParameters() == null) { - throw new IllegalArgumentException("wrong input"); + if (inputDataSet == null || inputDataSet.getParameters() == null) { + throw new IllegalArgumentException("Agent input data can not be empty."); } List outputs = new ArrayList<>(); diff --git a/ml-algorithms/src/test/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutorTest.java b/ml-algorithms/src/test/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutorTest.java index f2e692f986..ce89464e37 100644 --- a/ml-algorithms/src/test/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutorTest.java +++ b/ml-algorithms/src/test/java/org/opensearch/ml/engine/algorithms/agent/MLAgentExecutorTest.java @@ -183,6 +183,19 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws mlAgentExecutor.execute(input, agentActionListener); } + @Test(expected = IllegalArgumentException.class) + public void test_NonInputData_ThrowsException() { + AgentMLInput agentMLInput = new AgentMLInput("test", FunctionName.AGENT, null); + mlAgentExecutor.execute(agentMLInput, agentActionListener); + } + + @Test(expected = IllegalArgumentException.class) + public void test_NonInputParas_ThrowsException() { + RemoteInferenceInputDataSet inputDataSet = RemoteInferenceInputDataSet.builder().parameters(null).build(); + AgentMLInput agentMLInput = new AgentMLInput("test", FunctionName.AGENT, inputDataSet); + mlAgentExecutor.execute(agentMLInput, agentActionListener); + } + @Test public void test_HappyCase_ReturnsResult() { ModelTensor modelTensor = ModelTensor.builder().name("response").dataAsMap(ImmutableMap.of("test_key", "test_value")).build();