Skip to content

Commit

Permalink
Use builder.toString() to get a builder string value and format code
Browse files Browse the repository at this point in the history
Signed-off-by: zane-neo <[email protected]>
  • Loading branch information
zane-neo committed Aug 8, 2023
1 parent b1cf15a commit 56c63fa
Show file tree
Hide file tree
Showing 51 changed files with 56 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.opensearch.ml.common.AccessMode;
import org.opensearch.ml.common.MLCommonsClassLoader;
import org.opensearch.ml.common.output.model.ModelTensor;
import org.opensearch.ml.common.utils.StringUtils;

/**
* Connector defines how to connect to a remote service.
Expand Down Expand Up @@ -93,7 +92,7 @@ static Connector fromStream(StreamInput in) throws IOException {
}

static Connector createConnector(XContentBuilder builder, String connectorProtocol) throws IOException {
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
return createConnector(jsonStr, connectorProtocol);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.XContentBuilder;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -84,8 +82,4 @@ public static Map<String, String> getParameterMap(Map<String, ?> parameterObjs)
}
return parameters;
}

public static String xContentBuilderToString(XContentBuilder builder) {
return BytesReference.bytes(builder).utf8ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.ml.common.utils.StringUtils;

import java.io.IOException;
import java.util.function.Function;
Expand All @@ -34,7 +33,7 @@ public static <T> void testParse(ToXContentObject obj, Function<XContentParser,
if (wrapWithObject) {
builder.endObject();
}
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
testParseFromString(obj, jsonStr, function);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentBuilder;

Expand All @@ -32,7 +31,7 @@ public void testToXContent() throws IOException {
value.toXContent(builder);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"BOOLEAN\",\"value\":true}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import org.junit.Before;
import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -54,7 +53,7 @@ public void testToXContent() throws IOException {
columnMeta.toXContent(builder);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"name\":\"columnMetaName\",\"column_type\":\"STRING\"}", jsonStr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.ml.common.TestHelper;
import org.opensearch.ml.common.utils.StringUtils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -232,7 +231,7 @@ public void testToXContent() throws IOException {
builder.endObject();

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_metas\":[" +
"{\"name\":\"c1\",\"column_type\":\"STRING\"}," +
"{\"name\":\"c2\",\"column_type\":\"INTEGER\"}," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.ml.common.utils.StringUtils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -50,7 +49,7 @@ public void testToXContent() throws IOException {
doubleValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"DOUBLE\",\"value\":5.0}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentBuilder;

Expand All @@ -33,7 +32,7 @@ public void testToXContent() throws IOException {
floatValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"FLOAT\",\"value\":2.1}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.junit.Test;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.ml.common.utils.StringUtils;

import java.io.IOException;

Expand All @@ -32,7 +31,7 @@ public void testToXContent() throws IOException {
intValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"INTEGER\",\"value\":2}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentBuilder;

Expand All @@ -32,7 +31,7 @@ public void testToXContent() throws IOException {
longValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"LONG\",\"value\":2}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
Expand All @@ -33,7 +32,7 @@ public void testToXContent() throws IOException {
value.toXContent(builder, ToXContent.EMPTY_PARAMS);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"NULL\"}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -136,7 +135,7 @@ public void testToXContent() throws IOException {
row.toXContent(builder);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();

assertEquals("{\"values\":[{\"column_type\":\"INTEGER\",\"value\":0}]}", jsonStr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.XContentBuilder;

Expand All @@ -32,7 +31,7 @@ public void testToXContent() throws IOException {
shortValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"SHORT\",\"value\":2}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -37,7 +36,7 @@ public void testToXContent() throws IOException {
value.toXContent(builder, ToXContent.EMPTY_PARAMS);

assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"STRING\",\"value\":\"str\"}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -153,7 +152,7 @@ private void testParse(FunctionName algorithm, MLInputDataset inputDataset, Stri
XContentBuilder builder = XContentFactory.jsonBuilder();
input.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertNotNull(builder);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals(expectedInputStr, jsonStr);

XContentParser parser = XContentType.JSON.xContent().createParser(new NamedXContentRegistry(new SearchModule(Settings.EMPTY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Optional;

import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.io.stream.NamedWriteableAwareStreamInput;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
Expand All @@ -36,7 +35,7 @@ public void testXContentFullObject() throws Exception {
Optional.of(QueryBuilders.matchAllQuery()));
XContentBuilder builder = XContentFactory.jsonBuilder();
builder = input.toXContent(builder, null);
String json = StringUtils.xContentBuilderToString(builder);
String json = builder.toString();

XContentParser parser = XContentType.JSON.xContent().createParser(new NamedXContentRegistry(new SearchModule(Settings.EMPTY,
Collections.emptyList()).getNamedXContents()), null, json);
Expand All @@ -52,7 +51,7 @@ public void testXContentMissingAnomalyStartFilter() throws Exception {
"@timestamp", 0L, 10L, 1L, 2, Optional.empty(), Optional.empty());
XContentBuilder builder = XContentFactory.jsonBuilder();
builder = input.toXContent(builder, null);
String json = StringUtils.xContentBuilderToString(builder);
String json = builder.toString();

XContentParser parser = XContentType.JSON.xContent().createParser(new NamedXContentRegistry(new SearchModule(Settings.EMPTY,
Collections.emptyList()).getNamedXContents()), null, json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -47,7 +46,7 @@ public void setUp() throws Exception {
public void parseTextDocsMLInput() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
input.toXContent(builder, ToXContent.EMPTY_PARAMS);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
parseMLInput(jsonStr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import org.junit.Before;
import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.common.xcontent.XContentFactory;
Expand Down Expand Up @@ -47,7 +46,7 @@ public void setUp() {
public void toXContent() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
output.toXContent(builder, ToXContent.EMPTY_PARAMS);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"task_id\":\"test_task_id\",\"status\":\"test_status\",\"prediction_result\":" +
"{\"column_metas\":[{\"name\":\"test\",\"column_type\":\"INTEGER\"}],\"rows\":[{\"values\":" +
"[{\"column_type\":\"INTEGER\",\"value\":1}]},{\"values\":[{\"column_type\":\"INTEGER\"," +
Expand All @@ -59,7 +58,7 @@ public void toXContent_EmptyOutput() throws IOException {
MLPredictionOutput output = MLPredictionOutput.builder().build();
XContentBuilder builder = XContentFactory.jsonBuilder();
output.toXContent(builder, ToXContent.EMPTY_PARAMS);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{}", jsonStr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.opensearch.ml.common.output;

import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
Expand All @@ -22,7 +21,7 @@ public void parse_MLTrain() throws IOException {
.modelId("test_modelId").status("test_status").build();
XContentBuilder builder = XContentFactory.jsonBuilder();
output.toXContent(builder, ToXContent.EMPTY_PARAMS);
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"model_id\":\"test_modelId\",\"status\":\"test_status\"}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import org.junit.Before;
import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -51,7 +50,7 @@ public void testXContent() throws Exception {
builder.startObject();
builder = output.toXContent(builder, null);
builder.endObject();
String json = StringUtils.xContentBuilderToString(builder);
String json = builder.toString();
XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, null, json);
AnomalyLocalizationOutput newOutput = AnomalyLocalizationOutput.parse(parser);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import org.junit.Before;
import org.junit.Test;
import org.opensearch.ml.common.utils.StringUtils;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.common.xcontent.XContentFactory;
Expand All @@ -34,7 +33,7 @@ public void toXContent() throws IOException {
builder.startObject();
output.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{\"result\":1.0}", jsonStr);
}

Expand All @@ -45,7 +44,7 @@ public void toXContent_EmptyOutput() throws IOException {
builder.startObject();
output.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
String jsonStr = StringUtils.xContentBuilderToString(builder);
String jsonStr = builder.toString();
assertEquals("{}", jsonStr);
}

Expand Down
Loading

0 comments on commit 56c63fa

Please sign in to comment.