Skip to content

Commit

Permalink
[Refactor] Strings utility methods to core library
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <[email protected]>
  • Loading branch information
heemin32 committed Aug 7, 2023
1 parent d509bfc commit 2e04baf
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Maintenance
* Change package for Strings.hasText ([#314](https://github.com/opensearch-project/geospatial/pull/314))
* Refactor LifecycleComponent package path ([#377](https://github.com/opensearch-project/geospatial/pull/377))
* [Refactor] Strings utility methods to core library ([#379](https://github.com/opensearch-project/geospatial/pull/379))
### Refactoring
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.List;

import org.opensearch.client.node.NodeClient;
import org.opensearch.common.Strings;
import org.opensearch.core.common.Strings;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.action.RestToXContentListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.opensearch.common.Strings;
import org.opensearch.core.common.Strings;

/**
* Parameter validator for IP2Geo APIs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected static Response createPipeline(String name, Optional<String> descripti
builder.endObject();

Request request = new Request("PUT", buildPipelinePath(name));
request.setJsonEntity(org.opensearch.common.Strings.toString(builder));
request.setJsonEntity(builder.toString());
return client().performRequest(request);
}

Expand All @@ -118,7 +118,7 @@ protected Response createDatasource(final String name, Map<String, Object> prope
builder.endObject();

Request request = new Request(PUT, buildDatasourcePath(name));
request.setJsonEntity(org.opensearch.common.Strings.toString(builder));
request.setJsonEntity(builder.toString());
return client().performRequest(request);
}

Expand Down Expand Up @@ -178,7 +178,7 @@ protected Response updateDatasource(final String name, Map<String, Object> prope
builder.endObject();

Request request = new Request(PUT, String.join(URL_DELIMITER, buildDatasourcePath(name), SETTINGS));
request.setJsonEntity(org.opensearch.common.Strings.toString(builder));
request.setJsonEntity(builder.toString());
return client().performRequest(request);
}

Expand All @@ -188,7 +188,7 @@ protected Map<String, Object> simulatePipeline(final String name, List<Object> d
builder.endObject();

Request request = new Request(GET, String.join(URL_DELIMITER, buildPipelinePath(name), SIMULATE));
request.setJsonEntity(org.opensearch.common.Strings.toString(builder));
request.setJsonEntity(builder.toString());
Response response = client().performRequest(request);
return createParser(XContentType.JSON.xContent(), EntityUtils.toString(response.getEntity())).map();
}
Expand All @@ -199,7 +199,7 @@ protected static void createIndex(String name, Settings settings, Map<String, St
xContentBuilder.startObject(entry.getKey()).field(FIELD_TYPE_KEY, entry.getValue()).endObject();
}
xContentBuilder.endObject().endObject();
String mapping = org.opensearch.common.Strings.toString(xContentBuilder);
String mapping = xContentBuilder.toString();
createIndex(name, settings, mapping.substring(1, mapping.length() - 1));
}

Expand Down Expand Up @@ -346,7 +346,7 @@ public String buildContentAsString(CheckedConsumer<XContentBuilder, IOException>
XContentBuilder builder = JsonXContent.contentBuilder().startObject();
build.accept(builder);
builder.endObject();
return org.opensearch.common.Strings.toString(builder);
return builder.toString();
}

public String buildSearchAggregationsBodyAsString(CheckedConsumer<XContentBuilder, IOException> aggregationsBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
package org.opensearch.geospatial.action.upload.geojson;

import org.opensearch.action.bulk.BulkResponse;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.geospatial.GeospatialTestHelper;
import org.opensearch.test.OpenSearchTestCase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.opensearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.opensearch.action.get.GetRequest;
import org.opensearch.action.get.GetResponse;
import org.opensearch.common.Strings;
import org.opensearch.common.compress.CompressedXContent;
import org.opensearch.common.geo.GeoJson;
import org.opensearch.common.geo.ShapeRelation;
Expand Down Expand Up @@ -92,12 +91,10 @@ protected void initializeAdditionalMappings(MapperService mapperService) throws
mapperService.merge(
DOC_TYPE,
new CompressedXContent(
Strings.toString(
PutMappingRequest.simpleMapping(
XY_SHAPE_FIELD_NAME,
String.format(Locale.ROOT, "%s=%s", MAPPING_FIELD_TYPE_KEY, XYShapeQueryBuilder.NAME)
)
)
PutMappingRequest.simpleMapping(
XY_SHAPE_FIELD_NAME,
String.format(Locale.ROOT, "%s=%s", MAPPING_FIELD_TYPE_KEY, XYShapeQueryBuilder.NAME)
).toString()
),
MapperService.MergeReason.MAPPING_UPDATE
);
Expand All @@ -120,7 +117,7 @@ protected GetResponse executeGet(GetRequest getRequest) {
builder.field(expectedShapePath, (contentBuilder, params) -> GeoJson.toXContent(indexedShapeToReturn, contentBuilder, params));
builder.field(randomAlphaOfLengthBetween(10, 20), randomLowerCaseString());
builder.endObject();
json = Strings.toString(builder);
json = builder.toString();
} catch (IOException ex) {
throw new OpenSearchException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.List;
import java.util.Locale;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.common.io.stream.BytesStreamInput;
Expand All @@ -35,7 +34,7 @@ public void testStreamInOut_whenValidInput_thenSucceed() throws Exception {
public void testToXContent_whenValidInput_thenSucceed() throws Exception {
List<Datasource> datasourceList = Arrays.asList(randomDatasource(), randomDatasource());
GetDatasourceResponse response = new GetDatasourceResponse(datasourceList);
String json = Strings.toString(response.toXContent(JsonXContent.contentBuilder(), null));
String json = response.toXContent(JsonXContent.contentBuilder(), null).toString();
for (Datasource datasource : datasourceList) {
assertTrue(json.contains(String.format(Locale.ROOT, "\"name\":\"%s\"", datasource.getName())));
assertTrue(json.contains(String.format(Locale.ROOT, "\"state\":\"%s\"", datasource.getState())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Map;

import org.opensearch.common.Randomness;
import org.opensearch.common.Strings;
import org.opensearch.core.common.Strings;
import org.opensearch.geospatial.GeospatialTestHelper;
import org.opensearch.geospatial.ip2geo.Ip2GeoTestCase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.opensearch.action.search.SearchResponse;
import org.opensearch.action.support.master.AcknowledgedResponse;
import org.opensearch.cluster.routing.Preference;
import org.opensearch.common.Strings;
import org.opensearch.common.SuppressForbidden;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.geospatial.GeospatialTestHelper;
Expand Down Expand Up @@ -101,7 +100,7 @@ public void testCreateDocument_whenBlankValue_thenDoNotAdd() {
String[] values = { "1.0.0.0/25", "USA", " ", "Seattle" };
assertEquals(
"{\"_cidr\":\"1.0.0.0/25\",\"_data\":{\"country\":\"USA\",\"city\":\"Seattle\"}}",
Strings.toString(noOpsGeoIpDataDao.createDocument(names, values))
noOpsGeoIpDataDao.createDocument(names, values).toString()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Collections;
import java.util.List;

import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContent;
Expand Down Expand Up @@ -47,7 +46,7 @@ public void testToXContentWithEmptyUploadStats() throws IOException {
contentBuilder.startObject();
totalUploadStats.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
contentBuilder.endObject();
String summary = Strings.toString(contentBuilder);
String summary = contentBuilder.toString();
assertNotNull(summary);
String expectedEmptyContent = "{\"total\":{}}";
assertEquals(expectedEmptyContent, summary);
Expand All @@ -62,7 +61,7 @@ public void testToXContentWithRequestAPICount() throws IOException {
contentBuilder.startObject();
totalUploadStats.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
contentBuilder.endObject();
String summary = Strings.toString(contentBuilder);
String summary = contentBuilder.toString();
assertNotNull(summary);
assertTrue(summary.contains(buildFieldNameValuePair(TotalUploadStats.FIELDS.REQUEST_COUNT.toString(), expectedSum)));
}
Expand All @@ -78,7 +77,7 @@ public void testToXContentWithUploadCount() throws IOException {
contentBuilder.startObject();
totalUploadStats.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
contentBuilder.endObject();
String summary = Strings.toString(contentBuilder);
String summary = contentBuilder.toString();
assertNotNull(summary);
assertTrue(summary.contains(buildFieldNameValuePair(TotalUploadStats.FIELDS.UPLOAD.toString(), expectedSum)));
}
Expand All @@ -94,7 +93,7 @@ public void testToXContentWithSuccessCount() throws IOException {
contentBuilder.startObject();
totalUploadStats.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
contentBuilder.endObject();
String summary = Strings.toString(contentBuilder);
String summary = contentBuilder.toString();
assertNotNull(summary);
assertTrue(summary.contains(buildFieldNameValuePair(TotalUploadStats.FIELDS.SUCCESS.toString(), expectedSum)));
}
Expand All @@ -110,7 +109,7 @@ public void testToXContentWithFailedCount() throws IOException {
contentBuilder.startObject();
totalUploadStats.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
contentBuilder.endObject();
String summary = Strings.toString(contentBuilder);
String summary = contentBuilder.toString();
assertNotNull(summary);
assertTrue(summary.contains(buildFieldNameValuePair(TotalUploadStats.FIELDS.FAILED.toString(), expectedSum)));
}
Expand All @@ -126,7 +125,7 @@ public void testToXContentWithDuration() throws IOException {
contentBuilder.startObject();
totalUploadStats.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
contentBuilder.endObject();
String summary = Strings.toString(contentBuilder);
String summary = contentBuilder.toString();
assertNotNull(summary);
assertTrue(summary.contains(buildFieldNameValuePair(TotalUploadStats.FIELDS.DURATION.toString(), expectedSum)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

import java.io.IOException;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.geospatial.GeospatialTestHelper;
import org.opensearch.test.OpenSearchTestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.stream.Collectors;

import org.opensearch.cluster.ClusterName;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.common.io.stream.StreamInput;
Expand All @@ -38,15 +37,15 @@ public void testXContentWithMetrics() throws IOException {
);
final XContentBuilder serviceContentBuilder = jsonBuilder();
uploadStatsResponse.toXContent(serviceContentBuilder, ToXContent.EMPTY_PARAMS);
String nodesResponseAsString = Strings.toString(serviceContentBuilder);
String nodesResponseAsString = serviceContentBuilder.toString();
assertNotNull(nodesResponseAsString);

final List<UploadMetric> uploadMetrics = getUploadMetrics(nodeResponse);
for (UploadMetric metric : uploadMetrics) {
XContentBuilder metricContent = XContentFactory.jsonBuilder().startObject();
metric.toXContent(metricContent, ToXContent.EMPTY_PARAMS);
metricContent.endObject();
final String metricAsString = Strings.toString(metricContent);
final String metricAsString = metricContent.toString();
assertNotNull(metricAsString);
assertTrue(nodesResponseAsString.contains(removeStartAndEndObject(metricAsString)));
}
Expand All @@ -62,14 +61,14 @@ public void testXContentWithTotalUploads() throws IOException {
);
final XContentBuilder serviceContentBuilder = jsonBuilder();
uploadStatsResponse.toXContent(serviceContentBuilder, ToXContent.EMPTY_PARAMS);
String nodesResponseAsString = Strings.toString(serviceContentBuilder);
String nodesResponseAsString = serviceContentBuilder.toString();
assertNotNull(nodesResponseAsString);

TotalUploadStats totalUploadStats = new TotalUploadStats(getUploadStats(nodeResponse));
XContentBuilder totalUploadStatsContent = XContentFactory.jsonBuilder().startObject();
totalUploadStats.toXContent(totalUploadStatsContent, ToXContent.EMPTY_PARAMS);
totalUploadStatsContent.endObject();
final String totalUploadStatsAsString = Strings.toString(totalUploadStatsContent);
final String totalUploadStatsAsString = totalUploadStatsContent.toString();
assertNotNull(totalUploadStatsAsString);
assertTrue(nodesResponseAsString.contains(removeStartAndEndObject(totalUploadStatsAsString)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.List;
import java.util.Map;

import org.opensearch.common.Strings;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.geospatial.GeospatialTestHelper;
Expand Down Expand Up @@ -43,7 +42,7 @@ public void testXContentWithNodeID() throws IOException {
UploadStatsService service = new UploadStatsService(randomMap);
final XContentBuilder serviceContentBuilder = jsonBuilder();
service.toXContent(serviceContentBuilder, ToXContent.EMPTY_PARAMS);
String content = Strings.toString(serviceContentBuilder);
String content = serviceContentBuilder.toString();
assertNotNull(content);
for (String nodeID : randomMap.keySet()) {
assertTrue(nodeID + " is missing", content.contains(buildFieldNameValuePair(UploadStatsService.NODE_ID, nodeID)));
Expand All @@ -55,7 +54,7 @@ public void testXContentWithEmptyStats() throws IOException {
final XContentBuilder contentBuilder = jsonBuilder();
service.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
String emptyContent = "{\"total\":{},\"metrics\":[]}";
assertEquals(emptyContent, Strings.toString(contentBuilder));
assertEquals(emptyContent, contentBuilder.toString());
}

public void testXContentWithTotalUploadStats() throws IOException {
Expand All @@ -68,14 +67,14 @@ public void testXContentWithTotalUploadStats() throws IOException {
UploadStatsService service = new UploadStatsService(randomMap);
final XContentBuilder serviceContentBuilder = jsonBuilder();
service.toXContent(serviceContentBuilder, ToXContent.EMPTY_PARAMS);
String content = Strings.toString(serviceContentBuilder);
String content = serviceContentBuilder.toString();
assertNotNull(content);

final XContentBuilder summary = jsonBuilder().startObject();
TotalUploadStats expectedSummary = new TotalUploadStats(uploadStats);
expectedSummary.toXContent(summary, ToXContent.EMPTY_PARAMS);
summary.endObject();
final String totalUploadStatsSummary = Strings.toString(summary);
final String totalUploadStatsSummary = summary.toString();
assertNotNull(totalUploadStatsSummary);
assertTrue(content.contains(removeStartAndEndObject(totalUploadStatsSummary)));
}
Expand All @@ -91,14 +90,14 @@ public void testXContentWithMetrics() throws IOException {
UploadStatsService service = new UploadStatsService(randomMap);
final XContentBuilder serviceContentBuilder = jsonBuilder();
service.toXContent(serviceContentBuilder, ToXContent.EMPTY_PARAMS);
String content = Strings.toString(serviceContentBuilder);
String content = serviceContentBuilder.toString();
assertNotNull(content);

for (UploadMetric metric : randomMetrics) {
XContentBuilder metricsAsContent = jsonBuilder().startObject();
metric.toXContent(metricsAsContent, ToXContent.EMPTY_PARAMS);
metricsAsContent.endObject();
final String metricsAsString = Strings.toString(metricsAsContent);
final String metricsAsString = metricsAsContent.toString();
assertNotNull(metricsAsString);
assertTrue(content.contains(removeStartAndEndObject(metricsAsString)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Set;
import java.util.stream.IntStream;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.common.io.stream.StreamInput;
Expand Down Expand Up @@ -91,7 +90,7 @@ public void testStreams() throws IOException {
public void testToXContent() throws IOException {
UploadStats stats = UploadStatsBuilder.randomUploadStats();
XContentBuilder statsContent = XContentFactory.jsonBuilder().startObject();
String statsAsString = Strings.toString(stats.toXContent(statsContent, ToXContent.EMPTY_PARAMS).endObject());
String statsAsString = stats.toXContent(statsContent, ToXContent.EMPTY_PARAMS).endObject().toString();
assertNotNull(statsAsString);
assertTrue(statsAsString.contains(buildFieldNameValuePair(UploadStats.FIELDS.REQUEST_COUNT.toString(), stats.getTotalAPICount())));
stats.getMetrics().forEach(uploadMetric -> {
Expand Down

0 comments on commit 2e04baf

Please sign in to comment.