From f586df121c671eccc8d1c3a4c0bff9234ef16ca9 Mon Sep 17 00:00:00 2001 From: Junqiu Lei Date: Tue, 25 Jul 2023 16:17:58 -0700 Subject: [PATCH] Switch from XContentType to MediaType to fix compilation errors --- .../java/org/opensearch/knn/bwc/ModelIT.java | 6 +-- .../KNN80Codec/KNN80DocValuesConsumer.java | 4 +- .../action/RestDeleteModelHandlerIT.java | 8 +-- .../plugin/action/RestGetModelHandlerIT.java | 6 +-- .../plugin/action/RestKNNStatsHandlerIT.java | 8 +-- .../action/RestSearchModelHandlerIT.java | 12 ++--- .../action/RestTrainModelHandlerIT.java | 19 ++++--- .../knn/plugin/script/KNNScriptScoringIT.java | 32 +++++++---- .../transport/DeleteModelResponseTests.java | 4 +- .../transport/GetModelResponseTests.java | 6 +-- .../org/opensearch/knn/KNNRestTestCase.java | 54 ++++++++++++------- .../org/opensearch/knn/ODFERestTestCase.java | 9 ++-- .../java/org/opensearch/knn/TestUtils.java | 4 +- 13 files changed, 101 insertions(+), 71 deletions(-) diff --git a/qa/restart-upgrade/src/test/java/org/opensearch/knn/bwc/ModelIT.java b/qa/restart-upgrade/src/test/java/org/opensearch/knn/bwc/ModelIT.java index 0e129f5ad..cef1b9b61 100644 --- a/qa/restart-upgrade/src/test/java/org/opensearch/knn/bwc/ModelIT.java +++ b/qa/restart-upgrade/src/test/java/org/opensearch/knn/bwc/ModelIT.java @@ -15,7 +15,7 @@ import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.core.xcontent.XContentParser; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.index.SpaceType; import org.opensearch.knn.index.util.KNNEngine; import org.opensearch.knn.indices.ModelMetadata; @@ -186,7 +186,7 @@ public void searchKNNModel(String testModelID) throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(XContentType.JSON.xContent(), responseBody); + XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); assertEquals(EXP_NUM_OF_MODELS, searchResponse.getHits().getHits().length); @@ -203,7 +203,7 @@ public void validateModelCreated(String modelId) throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); assertTrainingSucceeds(modelId, NUM_OF_ATTEMPTS, DELAY_MILLI_SEC); } diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java b/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java index 691f789e0..a3ea798f6 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java @@ -10,7 +10,7 @@ import lombok.extern.log4j.Log4j2; import org.apache.lucene.store.ChecksumIndexInput; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.core.xcontent.DeprecationHandler; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.knn.index.KNNSettings; @@ -176,7 +176,7 @@ private void createKNNIndexFromScratch(FieldInfo fieldInfo, KNNCodecUtil.Pair pa parameters.put(PARAMETERS, algoParams); } else { parameters.putAll( - XContentFactory.xContent(XContentType.JSON) + XContentFactory.xContent(MediaTypeParserRegistry.getDefaultMediaType()) .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, parametersString) .map() ); diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestDeleteModelHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestDeleteModelHandlerIT.java index 871a4df39..b64d9d9a3 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestDeleteModelHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestDeleteModelHandlerIT.java @@ -17,7 +17,7 @@ import org.opensearch.client.ResponseException; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.KNNRestTestCase; import org.opensearch.knn.plugin.KNNPlugin; import org.opensearch.core.rest.RestStatus; @@ -62,7 +62,7 @@ public void testDeleteModelExists() throws Exception { String responseBody = EntityUtils.toString(getModelResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -99,7 +99,7 @@ public void testDeleteTrainingModel() throws Exception { String responseBody = EntityUtils.toString(getModelResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -205,7 +205,7 @@ private void trainModel(String modelId, String trainingIndexName, String trainin String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestGetModelHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestGetModelHandlerIT.java index 9b42301b8..81f6a36e8 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestGetModelHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestGetModelHandlerIT.java @@ -16,7 +16,7 @@ import org.opensearch.client.Request; import org.opensearch.client.Response; import org.opensearch.client.ResponseException; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.KNNRestTestCase; import org.opensearch.knn.plugin.KNNPlugin; import org.opensearch.core.rest.RestStatus; @@ -74,7 +74,7 @@ public void testGetModelExists() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); assertEquals(modelDescription, responseMap.get(MODEL_DESCRIPTION)); assertEquals(FAISS.getName(), responseMap.get(KNN_ENGINE)); @@ -106,7 +106,7 @@ public void testGetModelExistsWithFilter() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertTrue(responseMap.size() == filteredPath.size()); assertEquals(modelId, responseMap.get(MODEL_ID)); diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestKNNStatsHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestKNNStatsHandlerIT.java index 725a0e66c..c7030647b 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestKNNStatsHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestKNNStatsHandlerIT.java @@ -19,7 +19,7 @@ import org.opensearch.common.unit.TimeValue; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.index.query.MatchAllQueryBuilder; import org.opensearch.index.query.QueryBuilder; import org.opensearch.index.query.QueryBuilders; @@ -347,7 +347,7 @@ public void testModelIndexHealthMetricsStats() throws Exception { final Response response = getKnnStats(Collections.emptyList(), Arrays.asList(modelIndexStatusName)); final String responseBody = EntityUtils.toString(response.getEntity()); - final Map statsMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + final Map statsMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); // Check that model health status is null since model index is not created to system yet assertNull(statsMap.get(StatNames.MODEL_INDEX_STATUS.getName())); @@ -358,7 +358,7 @@ public void testModelIndexHealthMetricsStats() throws Exception { Response response = getKnnStats(Collections.emptyList(), Arrays.asList(modelIndexStatusName)); final String responseBody = EntityUtils.toString(response.getEntity()); - final Map statsMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + final Map statsMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); // Check that model health status is not null assertNotNull(statsMap.get(modelIndexStatusName)); @@ -452,7 +452,7 @@ public void validateModelCreated(String modelId) throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); assertTrainingSucceeds(modelId, NUM_OF_ATTEMPTS, DELAY_MILLI_SEC); } diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestSearchModelHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestSearchModelHandlerIT.java index 9963b5ad4..17a61931d 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestSearchModelHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestSearchModelHandlerIT.java @@ -17,7 +17,7 @@ import org.opensearch.client.Response; import org.opensearch.client.ResponseException; import org.opensearch.core.xcontent.XContentParser; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.KNNRestTestCase; import org.opensearch.knn.index.SpaceType; import org.opensearch.knn.index.util.KNNEngine; @@ -73,7 +73,7 @@ public void testNoModelExists() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(XContentType.JSON.xContent(), responseBody); + XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); assertEquals(searchResponse.getHits().getHits().length, 0); @@ -133,7 +133,7 @@ public void testSearchModelExists() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(XContentType.JSON.xContent(), responseBody); + XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); @@ -177,7 +177,7 @@ public void testSearchModelWithoutSource() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(XContentType.JSON.xContent(), responseBody); + XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); @@ -225,7 +225,7 @@ public void testSearchModelWithSourceFilteringIncludes() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(XContentType.JSON.xContent(), responseBody); + XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); @@ -277,7 +277,7 @@ public void testSearchModelWithSourceFilteringExcludes() throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - XContentParser parser = createParser(XContentType.JSON.xContent(), responseBody); + XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); SearchResponse searchResponse = SearchResponse.fromXContent(parser); assertNotNull(searchResponse); diff --git a/src/test/java/org/opensearch/knn/plugin/action/RestTrainModelHandlerIT.java b/src/test/java/org/opensearch/knn/plugin/action/RestTrainModelHandlerIT.java index e5c911326..1f2742344 100644 --- a/src/test/java/org/opensearch/knn/plugin/action/RestTrainModelHandlerIT.java +++ b/src/test/java/org/opensearch/knn/plugin/action/RestTrainModelHandlerIT.java @@ -15,7 +15,7 @@ import org.opensearch.client.Response; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.KNNRestTestCase; import org.opensearch.core.rest.RestStatus; @@ -96,7 +96,8 @@ public void testTrainModel_fail_notEnoughData() throws Exception { String trainResponseBody = EntityUtils.toString(trainResponse.getEntity()); assertNotNull(trainResponseBody); - Map trainResponseMap = createParser(XContentType.JSON.xContent(), trainResponseBody).map(); + Map trainResponseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), trainResponseBody) + .map(); String modelId = (String) trainResponseMap.get(MODEL_ID); assertNotNull(modelId); @@ -105,7 +106,7 @@ public void testTrainModel_fail_notEnoughData() throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -176,7 +177,8 @@ public void testTrainModel_fail_tooMuchData() throws Exception { String trainResponseBody = EntityUtils.toString(trainResponse.getEntity()); assertNotNull(trainResponseBody); - Map trainResponseMap = createParser(XContentType.JSON.xContent(), trainResponseBody).map(); + Map trainResponseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), trainResponseBody) + .map(); String modelId = (String) trainResponseMap.get(MODEL_ID); assertNotNull(modelId); @@ -185,7 +187,7 @@ public void testTrainModel_fail_tooMuchData() throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -256,7 +258,7 @@ public void testTrainModel_success_withId() throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); @@ -326,7 +328,8 @@ public void testTrainModel_success_noId() throws Exception { String trainResponseBody = EntityUtils.toString(trainResponse.getEntity()); assertNotNull(trainResponseBody); - Map trainResponseMap = createParser(XContentType.JSON.xContent(), trainResponseBody).map(); + Map trainResponseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), trainResponseBody) + .map(); String modelId = (String) trainResponseMap.get(MODEL_ID); assertNotNull(modelId); @@ -335,7 +338,7 @@ public void testTrainModel_success_noId() throws Exception { String responseBody = EntityUtils.toString(getResponse.getEntity()); assertNotNull(responseBody); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); assertEquals(modelId, responseMap.get(MODEL_ID)); diff --git a/src/test/java/org/opensearch/knn/plugin/script/KNNScriptScoringIT.java b/src/test/java/org/opensearch/knn/plugin/script/KNNScriptScoringIT.java index e20312c6c..2a06376a2 100644 --- a/src/test/java/org/opensearch/knn/plugin/script/KNNScriptScoringIT.java +++ b/src/test/java/org/opensearch/knn/plugin/script/KNNScriptScoringIT.java @@ -17,7 +17,7 @@ import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.index.query.MatchAllQueryBuilder; import org.opensearch.index.query.QueryBuilder; import org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder; @@ -390,8 +390,10 @@ public void testKNNScoreforNonVectorDocument() throws Exception { assertEquals(request.getEndpoint() + ": failed", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); String responseBody = EntityUtils.toString(response.getEntity()); - List hits = (List) ((Map) createParser(XContentType.JSON.xContent(), responseBody).map() - .get("hits")).get("hits"); + List hits = (List) ((Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + responseBody + ).map().get("hits")).get("hits"); List docIds = hits.stream().map(hit -> { String id = ((String) ((Map) hit).get("_id")); @@ -456,8 +458,10 @@ public void testHammingScriptScore_Long() throws Exception { assertEquals(request1.getEndpoint() + ": failed", RestStatus.OK, RestStatus.fromCode(response1.getStatusLine().getStatusCode())); String responseBody1 = EntityUtils.toString(response1.getEntity()); - List hits1 = (List) ((Map) createParser(XContentType.JSON.xContent(), responseBody1).map() - .get("hits")).get("hits"); + List hits1 = (List) ((Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + responseBody1 + ).map().get("hits")).get("hits"); List docIds1 = hits1.stream().map(hit -> ((String) ((Map) hit).get("_id"))).collect(Collectors.toList()); @@ -494,8 +498,10 @@ public void testHammingScriptScore_Long() throws Exception { assertEquals(request2.getEndpoint() + ": failed", RestStatus.OK, RestStatus.fromCode(response2.getStatusLine().getStatusCode())); String responseBody2 = EntityUtils.toString(response2.getEntity()); - List hits2 = (List) ((Map) createParser(XContentType.JSON.xContent(), responseBody2).map() - .get("hits")).get("hits"); + List hits2 = (List) ((Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + responseBody2 + ).map().get("hits")).get("hits"); List docIds2 = hits2.stream().map(hit -> ((String) ((Map) hit).get("_id"))).collect(Collectors.toList()); @@ -563,8 +569,10 @@ public void testHammingScriptScore_Base64() throws Exception { assertEquals(request1.getEndpoint() + ": failed", RestStatus.OK, RestStatus.fromCode(response1.getStatusLine().getStatusCode())); String responseBody1 = EntityUtils.toString(response1.getEntity()); - List hits1 = (List) ((Map) createParser(XContentType.JSON.xContent(), responseBody1).map() - .get("hits")).get("hits"); + List hits1 = (List) ((Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + responseBody1 + ).map().get("hits")).get("hits"); List docIds1 = hits1.stream().map(hit -> ((String) ((Map) hit).get("_id"))).collect(Collectors.toList()); @@ -601,8 +609,10 @@ public void testHammingScriptScore_Base64() throws Exception { assertEquals(request2.getEndpoint() + ": failed", RestStatus.OK, RestStatus.fromCode(response2.getStatusLine().getStatusCode())); String responseBody2 = EntityUtils.toString(response2.getEntity()); - List hits2 = (List) ((Map) createParser(XContentType.JSON.xContent(), responseBody2).map() - .get("hits")).get("hits"); + List hits2 = (List) ((Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + responseBody2 + ).map().get("hits")).get("hits"); List docIds2 = hits2.stream().map(hit -> ((String) ((Map) hit).get("_id"))).collect(Collectors.toList()); diff --git a/src/test/java/org/opensearch/knn/plugin/transport/DeleteModelResponseTests.java b/src/test/java/org/opensearch/knn/plugin/transport/DeleteModelResponseTests.java index ed0245c8f..98e030b22 100644 --- a/src/test/java/org/opensearch/knn/plugin/transport/DeleteModelResponseTests.java +++ b/src/test/java/org/opensearch/knn/plugin/transport/DeleteModelResponseTests.java @@ -15,7 +15,7 @@ import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.KNNTestCase; import java.io.IOException; @@ -39,7 +39,7 @@ public void testXContentWithoutError() throws IOException { BytesStreamOutput streamOutput = new BytesStreamOutput(); deleteModelResponse.writeTo(streamOutput); String expectedResponseString = "{\"model_id\":\"test-model\",\"result\":\"deleted\"}"; - XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON); + XContentBuilder xContentBuilder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType()); deleteModelResponse.toXContent(xContentBuilder, null); assertEquals(expectedResponseString, Strings.toString(xContentBuilder)); } diff --git a/src/test/java/org/opensearch/knn/plugin/transport/GetModelResponseTests.java b/src/test/java/org/opensearch/knn/plugin/transport/GetModelResponseTests.java index ff69d4b4e..22ad4c78e 100644 --- a/src/test/java/org/opensearch/knn/plugin/transport/GetModelResponseTests.java +++ b/src/test/java/org/opensearch/knn/plugin/transport/GetModelResponseTests.java @@ -15,7 +15,7 @@ import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.KNNTestCase; import org.opensearch.knn.index.SpaceType; import org.opensearch.knn.index.util.KNNEngine; @@ -49,7 +49,7 @@ public void testXContent() throws IOException { GetModelResponse getModelResponse = new GetModelResponse(model); String expectedResponseString = "{\"model_id\":\"test-model\",\"model_blob\":\"aGVsbG8=\",\"state\":\"created\",\"timestamp\":\"2021-03-27 10:15:30 AM +05:30\",\"description\":\"test model\",\"error\":\"\",\"space_type\":\"l2\",\"dimension\":4,\"engine\":\"nmslib\"}"; - XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON); + XContentBuilder xContentBuilder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType()); getModelResponse.toXContent(xContentBuilder, null); assertEquals(expectedResponseString, Strings.toString(xContentBuilder)); } @@ -60,7 +60,7 @@ public void testXContentWithNoModelBlob() throws IOException { GetModelResponse getModelResponse = new GetModelResponse(model); String expectedResponseString = "{\"model_id\":\"test-model\",\"model_blob\":\"\",\"state\":\"failed\",\"timestamp\":\"2021-03-27 10:15:30 AM +05:30\",\"description\":\"test model\",\"error\":\"\",\"space_type\":\"l2\",\"dimension\":4,\"engine\":\"nmslib\"}"; - XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON); + XContentBuilder xContentBuilder = XContentFactory.contentBuilder(MediaTypeParserRegistry.getDefaultMediaType()); getModelResponse.toXContent(xContentBuilder, null); assertEquals(expectedResponseString, Strings.toString(xContentBuilder)); } diff --git a/src/testFixtures/java/org/opensearch/knn/KNNRestTestCase.java b/src/testFixtures/java/org/opensearch/knn/KNNRestTestCase.java index 02a3d11a4..a2ca398f6 100644 --- a/src/testFixtures/java/org/opensearch/knn/KNNRestTestCase.java +++ b/src/testFixtures/java/org/opensearch/knn/KNNRestTestCase.java @@ -33,7 +33,8 @@ import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaType; import org.opensearch.index.query.ExistsQueryBuilder; import org.opensearch.index.query.QueryBuilder; import org.opensearch.index.query.functionscore.ScriptScoreQueryBuilder; @@ -227,8 +228,10 @@ protected Response searchExists(String index, ExistsQueryBuilder existsQueryBuil */ protected List parseSearchResponse(String responseBody, String fieldName) throws IOException { @SuppressWarnings("unchecked") - List hits = (List) ((Map) createParser(XContentType.JSON.xContent(), responseBody).map() - .get("hits")).get("hits"); + List hits = (List) ((Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + responseBody + ).map().get("hits")).get("hits"); @SuppressWarnings("unchecked") List knnSearchResponses = hits.stream().map(hit -> { @@ -244,8 +247,10 @@ protected List parseSearchResponse(String responseBody, String fieldN protected List parseSearchResponseScore(String responseBody, String fieldName) throws IOException { @SuppressWarnings("unchecked") - List hits = (List) ((Map) createParser(XContentType.JSON.xContent(), responseBody).map() - .get("hits")).get("hits"); + List hits = (List) ((Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + responseBody + ).map().get("hits")).get("hits"); @SuppressWarnings("unchecked") List knnSearchResponses = hits.stream() @@ -260,8 +265,10 @@ protected List parseSearchResponseScore(String responseBody, String field */ protected Double parseAggregationResponse(String responseBody, String aggregationName) throws IOException { @SuppressWarnings("unchecked") - Map aggregations = ((Map) createParser(XContentType.JSON.xContent(), responseBody).map() - .get("aggregations")); + Map aggregations = ((Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + responseBody + ).map().get("aggregations")); final Map values = (Map) aggregations.get(aggregationName); return Double.valueOf(String.valueOf(values.get("value"))); @@ -366,7 +373,7 @@ public Map getIndexMappingAsMap(String index) throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); return (Map) ((Map) responseMap.get(index)).get("mappings"); } @@ -380,7 +387,7 @@ public int getDocCount(String indexName) throws Exception { String responseBody = EntityUtils.toString(response.getEntity()); - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); return (Integer) responseMap.get("count"); } @@ -496,8 +503,10 @@ protected Map getKnnDoc(final String index, final String docId) final Request request = new Request("GET", "/" + index + "/_doc/" + docId); final Response response = client().performRequest(request); - final Map responseMap = createParser(XContentType.JSON.xContent(), EntityUtils.toString(response.getEntity())) - .map(); + final Map responseMap = createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + EntityUtils.toString(response.getEntity()) + ).map(); assertNotNull(responseMap); assertTrue((Boolean) responseMap.get(DOCUMENT_FIELD_FOUND)); @@ -573,7 +582,7 @@ protected Response executeWarmupRequest(List indices, final String baseU * Parse KNN Cluster stats from response */ protected Map parseClusterStatsResponse(String responseBody) throws IOException { - Map responseMap = createParser(XContentType.JSON.xContent(), responseBody).map(); + Map responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map(); responseMap.remove("cluster_name"); responseMap.remove("_nodes"); responseMap.remove("nodes"); @@ -585,7 +594,10 @@ protected Map parseClusterStatsResponse(String responseBody) thr */ protected List> parseNodeStatsResponse(String responseBody) throws IOException { @SuppressWarnings("unchecked") - Map responseMap = (Map) createParser(XContentType.JSON.xContent(), responseBody).map().get("nodes"); + Map responseMap = (Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + responseBody + ).map().get("nodes"); @SuppressWarnings("unchecked") List> nodeResponses = responseMap.keySet() @@ -601,8 +613,10 @@ protected List> parseNodeStatsResponse(String responseBody) */ @SuppressWarnings("unchecked") protected int parseTotalSearchHits(String searchResponseBody) throws IOException { - Map responseMap = (Map) createParser(XContentType.JSON.xContent(), searchResponseBody).map() - .get("hits"); + Map responseMap = (Map) createParser( + MediaTypeParserRegistry.getDefaultMediaType().xContent(), + searchResponseBody + ).map().get("hits"); return (int) ((Map) responseMap.get("total")).get("value"); } @@ -1142,7 +1156,8 @@ public void assertTrainingSucceeds(String modelId, int attempts, int delayInMill response = getModel(modelId, null); - responseMap = createParser(XContentType.JSON.xContent(), EntityUtils.toString(response.getEntity())).map(); + responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), EntityUtils.toString(response.getEntity())) + .map(); modelState = ModelState.getModelState((String) responseMap.get(MODEL_STATE)); if (modelState == ModelState.CREATED) { @@ -1166,7 +1181,8 @@ public void assertTrainingFails(String modelId, int attempts, int delayInMillis) response = getModel(modelId, null); - responseMap = createParser(XContentType.JSON.xContent(), EntityUtils.toString(response.getEntity())).map(); + responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), EntityUtils.toString(response.getEntity())) + .map(); modelState = ModelState.getModelState((String) responseMap.get(MODEL_STATE)); if (modelState == ModelState.FAILED) { @@ -1274,9 +1290,9 @@ public interface IProxy { protected void refreshAllNonSystemIndices() throws Exception { Response response = adminClient().performRequest(new Request("GET", "/_cat/indices?format=json&expand_wildcards=all")); - XContentType xContentType = XContentType.fromMediaType(response.getEntity().getContentType()); + MediaType mediaType = MediaType.fromMediaType(response.getEntity().getContentType()); try ( - XContentParser parser = xContentType.xContent() + XContentParser parser = mediaType.xContent() .createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, diff --git a/src/testFixtures/java/org/opensearch/knn/ODFERestTestCase.java b/src/testFixtures/java/org/opensearch/knn/ODFERestTestCase.java index c0d3c2e98..8a68d124c 100644 --- a/src/testFixtures/java/org/opensearch/knn/ODFERestTestCase.java +++ b/src/testFixtures/java/org/opensearch/knn/ODFERestTestCase.java @@ -49,7 +49,8 @@ import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; +import org.opensearch.core.xcontent.MediaType; import org.opensearch.knn.plugin.KNNPlugin; import org.opensearch.core.rest.RestStatus; import org.opensearch.search.SearchHit; @@ -185,9 +186,9 @@ protected boolean preserveIndicesUponCompletion() { @After protected void wipeAllODFEIndices() throws Exception { Response response = adminClient().performRequest(new Request("GET", "/_cat/indices?format=json&expand_wildcards=all")); - XContentType xContentType = XContentType.fromMediaType(response.getEntity().getContentType()); + MediaType mediaType = MediaType.fromMediaType(response.getEntity().getContentType()); try ( - XContentParser parser = xContentType.xContent() + XContentParser parser = mediaType.xContent() .createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, @@ -233,7 +234,7 @@ private List getModelIds() throws IOException, ParseException { final String responseBody = EntityUtils.toString(response.getEntity()); assertNotNull(responseBody); - final XContentParser parser = createParser(XContentType.JSON.xContent(), responseBody); + final XContentParser parser = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody); final SearchResponse searchResponse = SearchResponse.fromXContent(parser); return Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId).collect(Collectors.toList()); diff --git a/src/testFixtures/java/org/opensearch/knn/TestUtils.java b/src/testFixtures/java/org/opensearch/knn/TestUtils.java index 0843176e7..99ab607a7 100644 --- a/src/testFixtures/java/org/opensearch/knn/TestUtils.java +++ b/src/testFixtures/java/org/opensearch/knn/TestUtils.java @@ -9,7 +9,7 @@ import org.opensearch.core.xcontent.DeprecationHandler; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.XContentFactory; -import org.opensearch.common.xcontent.XContentType; +import org.opensearch.core.xcontent.MediaTypeParserRegistry; import org.opensearch.knn.index.codec.util.KNNCodecUtil; import java.io.BufferedReader; import java.io.FileReader; @@ -252,7 +252,7 @@ private KNNCodecUtil.Pair readIndexData(String path) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(path)); String line = reader.readLine(); while (line != null) { - Map doc = XContentFactory.xContent(XContentType.JSON) + Map doc = XContentFactory.xContent(MediaTypeParserRegistry.getDefaultMediaType()) .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, line) .map(); idsList.add((Integer) doc.get("id"));