Skip to content

Commit

Permalink
Switch from XContentType to MediaType to fix compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
junqiu-lei committed Jul 26, 2023
1 parent e5e3066 commit f586df1
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -203,7 +203,7 @@ public void validateModelCreated(String modelId) throws Exception {
String responseBody = EntityUtils.toString(getResponse.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();
assertEquals(modelId, responseMap.get(MODEL_ID));
assertTrainingSucceeds(modelId, NUM_OF_ATTEMPTS, DELAY_MILLI_SEC);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +62,7 @@ public void testDeleteModelExists() throws Exception {
String responseBody = EntityUtils.toString(getModelResponse.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();

assertEquals(modelId, responseMap.get(MODEL_ID));

Expand Down Expand Up @@ -99,7 +99,7 @@ public void testDeleteTrainingModel() throws Exception {
String responseBody = EntityUtils.toString(getModelResponse.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();

assertEquals(modelId, responseMap.get(MODEL_ID));

Expand Down Expand Up @@ -205,7 +205,7 @@ private void trainModel(String modelId, String trainingIndexName, String trainin
String responseBody = EntityUtils.toString(getResponse.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();

assertEquals(modelId, responseMap.get(MODEL_ID));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,7 +74,7 @@ public void testGetModelExists() throws Exception {
String responseBody = EntityUtils.toString(response.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> 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));
Expand Down Expand Up @@ -106,7 +106,7 @@ public void testGetModelExistsWithFilter() throws Exception {
String responseBody = EntityUtils.toString(response.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();

assertTrue(responseMap.size() == filteredPath.size());
assertEquals(modelId, responseMap.get(MODEL_ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> statsMap = createParser(XContentType.JSON.xContent(), responseBody).map();
final Map<String, Object> 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()));
Expand All @@ -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<String, Object> statsMap = createParser(XContentType.JSON.xContent(), responseBody).map();
final Map<String, Object> statsMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();

// Check that model health status is not null
assertNotNull(statsMap.get(modelIndexStatusName));
Expand Down Expand Up @@ -452,7 +452,7 @@ public void validateModelCreated(String modelId) throws Exception {
String responseBody = EntityUtils.toString(getResponse.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();
assertEquals(modelId, responseMap.get(MODEL_ID));
assertTrainingSucceeds(modelId, NUM_OF_ATTEMPTS, DELAY_MILLI_SEC);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -96,7 +96,8 @@ public void testTrainModel_fail_notEnoughData() throws Exception {
String trainResponseBody = EntityUtils.toString(trainResponse.getEntity());
assertNotNull(trainResponseBody);

Map<String, Object> trainResponseMap = createParser(XContentType.JSON.xContent(), trainResponseBody).map();
Map<String, Object> trainResponseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), trainResponseBody)
.map();
String modelId = (String) trainResponseMap.get(MODEL_ID);
assertNotNull(modelId);

Expand All @@ -105,7 +106,7 @@ public void testTrainModel_fail_notEnoughData() throws Exception {
String responseBody = EntityUtils.toString(getResponse.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();

assertEquals(modelId, responseMap.get(MODEL_ID));

Expand Down Expand Up @@ -176,7 +177,8 @@ public void testTrainModel_fail_tooMuchData() throws Exception {
String trainResponseBody = EntityUtils.toString(trainResponse.getEntity());
assertNotNull(trainResponseBody);

Map<String, Object> trainResponseMap = createParser(XContentType.JSON.xContent(), trainResponseBody).map();
Map<String, Object> trainResponseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), trainResponseBody)
.map();
String modelId = (String) trainResponseMap.get(MODEL_ID);
assertNotNull(modelId);

Expand All @@ -185,7 +187,7 @@ public void testTrainModel_fail_tooMuchData() throws Exception {
String responseBody = EntityUtils.toString(getResponse.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();

assertEquals(modelId, responseMap.get(MODEL_ID));

Expand Down Expand Up @@ -256,7 +258,7 @@ public void testTrainModel_success_withId() throws Exception {
String responseBody = EntityUtils.toString(getResponse.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();

assertEquals(modelId, responseMap.get(MODEL_ID));

Expand Down Expand Up @@ -326,7 +328,8 @@ public void testTrainModel_success_noId() throws Exception {
String trainResponseBody = EntityUtils.toString(trainResponse.getEntity());
assertNotNull(trainResponseBody);

Map<String, Object> trainResponseMap = createParser(XContentType.JSON.xContent(), trainResponseBody).map();
Map<String, Object> trainResponseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), trainResponseBody)
.map();
String modelId = (String) trainResponseMap.get(MODEL_ID);
assertNotNull(modelId);

Expand All @@ -335,7 +338,7 @@ public void testTrainModel_success_noId() throws Exception {
String responseBody = EntityUtils.toString(getResponse.getEntity());
assertNotNull(responseBody);

Map<String, Object> responseMap = createParser(XContentType.JSON.xContent(), responseBody).map();
Map<String, Object> responseMap = createParser(MediaTypeParserRegistry.getDefaultMediaType().xContent(), responseBody).map();

assertEquals(modelId, responseMap.get(MODEL_ID));

Expand Down
Loading

0 comments on commit f586df1

Please sign in to comment.