Skip to content

Commit f489f4f

Browse files
committed
fix failure UTs due to OpenSearch core jackson version upgrade
Signed-off-by: zane-neo <zaniu@amazon.com>
1 parent 3b174ad commit f489f4f

File tree

6 files changed

+55
-46
lines changed

6 files changed

+55
-46
lines changed

common/src/main/java/org/opensearch/ml/common/MLCommonsClassLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
import org.opensearch.ml.common.exception.MLException;
2727
import org.opensearch.ml.common.output.MLOutput;
2828
import org.opensearch.ml.common.output.MLOutputType;
29+
import org.opensearch.tools.jackson.core.JsonParseException;
2930
import org.reflections.Reflections;
3031

31-
import com.fasterxml.jackson.core.JsonParseException;
32-
3332
import lombok.extern.log4j.Log4j2;
3433

3534
@Log4j2

common/src/test/java/org/opensearch/ml/common/MLCommonsClassLoaderTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
import org.opensearch.ml.common.output.execute.metrics_correlation.MetricsCorrelationOutput;
4040
import org.opensearch.ml.common.output.execute.samplecalculator.LocalSampleCalculatorOutput;
4141
import org.opensearch.search.SearchModule;
42-
43-
import com.fasterxml.jackson.core.JsonParseException;
42+
import org.opensearch.tools.jackson.core.JsonParseException;
4443

4544
public class MLCommonsClassLoaderTests {
4645

common/src/test/java/org/opensearch/ml/common/model/MLDeployingSettingTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
import org.opensearch.core.xcontent.XContentBuilder;
2929
import org.opensearch.core.xcontent.XContentParser;
3030
import org.opensearch.search.SearchModule;
31-
32-
import com.fasterxml.jackson.core.JsonParseException;
31+
import org.opensearch.tools.jackson.core.JsonParseException;
3332

3433
public class MLDeployingSettingTests {
3534

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import org.opensearch.ml.common.spi.tools.Tool;
7373
import org.opensearch.ml.engine.encryptor.Encryptor;
7474
import org.opensearch.ml.engine.memory.ConversationIndexMemory;
75+
import org.opensearch.remote.metadata.client.GetDataObjectRequest;
7576
import org.opensearch.remote.metadata.client.GetDataObjectResponse;
7677
import org.opensearch.remote.metadata.client.SdkClient;
7778
import org.opensearch.threadpool.ThreadPool;
@@ -1815,6 +1816,49 @@ private void mockSdkClientWithAgent(String getResponseJson) {
18151816
});
18161817
}
18171818

1819+
/**
1820+
* Mock sdkClient to return agent JSON on first call, then fail on subsequent calls (e.g., model metadata fetch).
1821+
* This ensures deterministic test behavior for agents that require model lookups.
1822+
* Handles both single-arg and two-arg overloads of getDataObjectAsync.
1823+
*/
1824+
private void mockSdkClientWithAgentThenFail(String getResponseJson) {
1825+
when(clusterService.localNode()).thenReturn(localNode);
1826+
when(localNode.getId()).thenReturn("test-node-id");
1827+
AtomicBoolean firstCall = new AtomicBoolean(true);
1828+
1829+
// Two-arg version used by MLAgentExecutor.execute() for agent fetch
1830+
when(sdkClient.getDataObjectAsync(any(), any())).thenAnswer(inv -> {
1831+
CompletionStage<GetDataObjectResponse> stage = mock(CompletionStage.class);
1832+
when(stage.whenComplete(any())).thenAnswer(cbInv -> {
1833+
BiConsumer<GetDataObjectResponse, Throwable> cb = cbInv.getArgument(0);
1834+
if (firstCall.getAndSet(false)) {
1835+
// First call: return agent data
1836+
GetDataObjectResponse resp = mock(GetDataObjectResponse.class);
1837+
when(resp.parser())
1838+
.thenReturn(XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, null, getResponseJson));
1839+
cb.accept(resp, null);
1840+
} else {
1841+
// Subsequent calls: fail with not found
1842+
cb.accept(null, new RuntimeException("Model not found"));
1843+
}
1844+
return stage;
1845+
});
1846+
return stage;
1847+
});
1848+
1849+
// Single-arg version used by AgentUtils for model metadata lookups
1850+
when(sdkClient.getDataObjectAsync(any(GetDataObjectRequest.class))).thenAnswer(inv -> {
1851+
CompletionStage<GetDataObjectResponse> stage = mock(CompletionStage.class);
1852+
when(stage.whenComplete(any())).thenAnswer(cbInv -> {
1853+
BiConsumer<GetDataObjectResponse, Throwable> cb = cbInv.getArgument(0);
1854+
// Model metadata calls should fail deterministically
1855+
cb.accept(null, new RuntimeException("Model not found"));
1856+
return stage;
1857+
});
1858+
return stage;
1859+
});
1860+
}
1861+
18181862
private AgentMLInput buildV2AgentMLInput() {
18191863
ContentBlock textBlock = new ContentBlock();
18201864
textBlock.setType(ContentType.TEXT);
@@ -1850,7 +1894,7 @@ public void test_ExecuteAgent_NonV2Agent_DoesNotRouteToExecuteV2Agent() throws I
18501894
.lastUpdateTime(Instant.now())
18511895
.build();
18521896

1853-
mockSdkClientWithAgent(serializeAgentToGetResponseJson(convAgent));
1897+
mockSdkClientWithAgentThenFail(serializeAgentToGetResponseJson(convAgent));
18541898

18551899
Map<String, String> params = new HashMap<>();
18561900
params.put(QUESTION, "What is ML?");

search-processors/src/test/java/org/opensearch/searchpipelines/questionanswering/generative/GenerativeSearchResponseTests.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,16 @@
1717
*/
1818
package org.opensearch.searchpipelines.questionanswering.generative;
1919

20-
import static org.mockito.ArgumentMatchers.any;
21-
import static org.mockito.Mockito.mock;
22-
import static org.mockito.Mockito.when;
23-
2420
import java.io.IOException;
25-
import java.io.OutputStream;
2621

2722
import org.junit.Rule;
2823
import org.junit.rules.ExpectedException;
2924
import org.opensearch.action.search.SearchResponse;
3025
import org.opensearch.action.search.SearchResponseSections;
3126
import org.opensearch.action.search.ShardSearchFailure;
27+
import org.opensearch.common.xcontent.XContentFactory;
3228
import org.opensearch.core.xcontent.ToXContent;
33-
import org.opensearch.core.xcontent.XContent;
3429
import org.opensearch.core.xcontent.XContentBuilder;
35-
import org.opensearch.core.xcontent.XContentGenerator;
3630
import org.opensearch.search.SearchHit;
3731
import org.opensearch.search.SearchHits;
3832
import org.opensearch.test.OpenSearchTestCase;
@@ -66,11 +60,7 @@ public void testToXContent() throws IOException {
6660
SearchResponse.Clusters.EMPTY,
6761
"iid"
6862
);
69-
XContent xc = mock(XContent.class);
70-
OutputStream os = mock(OutputStream.class);
71-
XContentGenerator generator = mock(XContentGenerator.class);
72-
when(xc.createGenerator(any(), any(), any())).thenReturn(generator);
73-
XContentBuilder builder = new XContentBuilder(xc, os);
63+
XContentBuilder builder = XContentFactory.jsonBuilder();
7464
XContentBuilder actual = searchResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
7565
assertNotNull(actual);
7666
}
@@ -99,11 +89,7 @@ public void testToXContentWithError() throws IOException {
9989
SearchResponse.Clusters.EMPTY,
10090
"iid"
10191
);
102-
XContent xc = mock(XContent.class);
103-
OutputStream os = mock(OutputStream.class);
104-
XContentGenerator generator = mock(XContentGenerator.class);
105-
when(xc.createGenerator(any(), any(), any())).thenReturn(generator);
106-
XContentBuilder builder = new XContentBuilder(xc, os);
92+
XContentBuilder builder = XContentFactory.jsonBuilder();
10793
XContentBuilder actual = searchResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
10894
assertNotNull(actual);
10995
}

search-processors/src/test/java/org/opensearch/searchpipelines/questionanswering/generative/ext/GenerativeQAParametersTests.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,18 @@
1717
*/
1818
package org.opensearch.searchpipelines.questionanswering.generative.ext;
1919

20-
import static org.mockito.ArgumentMatchers.any;
21-
import static org.mockito.Mockito.*;
22-
2320
import java.io.IOException;
24-
import java.io.OutputStream;
2521
import java.util.ArrayList;
2622
import java.util.List;
2723
import java.util.Map;
2824

2925
import org.opensearch.Version;
3026
import org.opensearch.action.search.SearchRequest;
3127
import org.opensearch.common.io.stream.BytesStreamOutput;
28+
import org.opensearch.common.xcontent.XContentFactory;
3229
import org.opensearch.core.common.io.stream.StreamInput;
3330
import org.opensearch.core.common.io.stream.StreamOutput;
34-
import org.opensearch.core.xcontent.XContent;
3531
import org.opensearch.core.xcontent.XContentBuilder;
36-
import org.opensearch.core.xcontent.XContentGenerator;
3732
import org.opensearch.search.builder.SearchSourceBuilder;
3833
import org.opensearch.searchpipelines.questionanswering.generative.llm.MessageBlock;
3934
import org.opensearch.test.OpenSearchTestCase;
@@ -276,22 +271,13 @@ public void testToXConent() throws IOException {
276271
null,
277272
messageList
278273
);
279-
XContent xc = mock(XContent.class);
280-
OutputStream os = mock(OutputStream.class);
281-
XContentGenerator generator = mock(XContentGenerator.class);
282-
when(xc.createGenerator(any(), any(), any())).thenReturn(generator);
283-
XContentBuilder builder = new XContentBuilder(xc, os);
274+
XContentBuilder builder = XContentFactory.jsonBuilder();
284275
assertNotNull(parameters.toXContent(builder, null));
285276
}
286277

287278
public void testToXContentEmptyParams() throws IOException {
288279
GenerativeQAParameters parameters = new GenerativeQAParameters();
289-
XContent xc = mock(XContent.class);
290-
OutputStream os = mock(OutputStream.class);
291-
XContentGenerator generator = mock(XContentGenerator.class);
292-
when(xc.createGenerator(any(), any(), any())).thenReturn(generator);
293-
XContentBuilder builder = new XContentBuilder(xc, os);
294-
parameters.toXContent(builder, null);
280+
XContentBuilder builder = XContentFactory.jsonBuilder();
295281
assertNotNull(parameters.toXContent(builder, null));
296282
}
297283

@@ -316,11 +302,7 @@ public void testToXContentAllOptionalParameters() throws IOException {
316302
timeout,
317303
llmResponseField
318304
);
319-
XContent xc = mock(XContent.class);
320-
OutputStream os = mock(OutputStream.class);
321-
XContentGenerator generator = mock(XContentGenerator.class);
322-
when(xc.createGenerator(any(), any(), any())).thenReturn(generator);
323-
XContentBuilder builder = new XContentBuilder(xc, os);
305+
XContentBuilder builder = XContentFactory.jsonBuilder();
324306
assertNotNull(parameters.toXContent(builder, null));
325307
}
326308
}

0 commit comments

Comments
 (0)