Skip to content

Commit

Permalink
cleanup and move unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize committed Aug 4, 2023
1 parent 01c2d77 commit 65a19b1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,37 @@
import org.opensearch.common.util.set.Sets;
import org.opensearch.test.OpenSearchTestCase;

import static org.hamcrest.Matchers.is;

/** tests for Strings utility class */
public class StringsTests extends OpenSearchTestCase {
public void testIsAllOrWildCardString() {
assertThat(Strings.isAllOrWildcard("_all"), is(true));
assertThat(Strings.isAllOrWildcard("*"), is(true));
assertThat(Strings.isAllOrWildcard("foo"), is(false));
assertThat(Strings.isAllOrWildcard(""), is(false));
assertThat(Strings.isAllOrWildcard((String) null), is(false));
}

public void testCleanTruncate() {
assertEquals(null, Strings.cleanTruncate(null, 10));
assertEquals("foo", Strings.cleanTruncate("foo", 10));
assertEquals("foo", Strings.cleanTruncate("foo", 3));
// Throws out high surrogates
assertEquals("foo", Strings.cleanTruncate("foo\uD83D\uDEAB", 4));
// But will keep the whole character
assertEquals("foo\uD83D\uDEAB", Strings.cleanTruncate("foo\uD83D\uDEAB", 5));
/*
* Doesn't take care around combining marks. This example has its
* meaning changed because that last codepoint is supposed to combine
* backwards into the find "o" and be represented as the "o" with a
* circle around it with a slash through it. As in "no 'o's allowed
* here.
*/
assertEquals("o", org.opensearch.core.common.Strings.cleanTruncate("o\uD83D\uDEAB", 1));
assertEquals("", org.opensearch.core.common.Strings.cleanTruncate("foo", 0));
}

public void testSplitStringToSet() {
assertEquals(Strings.tokenizeByCommaToSet(null), Sets.newHashSet());
assertEquals(Strings.tokenizeByCommaToSet(""), Sets.newHashSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ public void setupSuiteScopeCluster() throws Exception {
client().prepareIndex("high_card_idx")
.setSource(
jsonBuilder().startObject()
.field(SINGLE_VALUED_FIELD_NAME, "val" + org.opensearch.core.common.Strings.padStart(i + "", 3, '0'))
.field(SINGLE_VALUED_FIELD_NAME, "val" + Strings.padStart(i + "", 3, '0'))
.startArray(MULTI_VALUED_FIELD_NAME)
.value("val" + org.opensearch.core.common.Strings.padStart(i + "", 3, '0'))
.value("val" + Strings.padStart(i + "", 3, '0'))
.value("val" + Strings.padStart((i + 1) + "", 3, '0'))
.endArray()
.endObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private static Map<String, Object> prepareMap(
}
try {
String source = XContentHelper.convertToJson(doc.source(), reformat, doc.getMediaType());
String trim = org.opensearch.core.common.Strings.cleanTruncate(source, maxSourceCharsToLog).trim();
String trim = Strings.cleanTruncate(source, maxSourceCharsToLog).trim();
StringBuilder sb = new StringBuilder(trim);
StringBuilders.escapeJson(sb, 0);
map.put("source", sb.toString());
Expand Down
11 changes: 5 additions & 6 deletions server/src/main/java/org/opensearch/rest/RestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@
import org.opensearch.common.CheckedConsumer;
import org.opensearch.common.Nullable;
import org.opensearch.common.SetOnce;
import org.opensearch.common.Strings;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.collect.Tuple;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.http.HttpChannel;
import org.opensearch.http.HttpRequest;

Expand Down Expand Up @@ -337,7 +336,7 @@ public final long getRequestId() {
}

/**
* The {@link XContentType} that was parsed from the {@code Content-Type} header. This value will be {@code null} in the case of
* The {@link MediaType} that was parsed from the {@code Content-Type} header. This value will be {@code null} in the case of
* a request without a valid {@code Content-Type} header, a request without content ({@link #hasContent()}, or a plain text request
*/
@Nullable
Expand Down
36 changes: 4 additions & 32 deletions server/src/test/java/org/opensearch/common/StringsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,17 @@

package org.opensearch.common;

import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.test.OpenSearchTestCase;

import java.util.Collections;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

public class StringsTests extends OpenSearchTestCase {

public void testIsAllOrWildCardString() {
assertThat(org.opensearch.core.common.Strings.isAllOrWildcard("_all"), is(true));
assertThat(org.opensearch.core.common.Strings.isAllOrWildcard("*"), is(true));
assertThat(org.opensearch.core.common.Strings.isAllOrWildcard("foo"), is(false));
assertThat(org.opensearch.core.common.Strings.isAllOrWildcard(""), is(false));
assertThat(org.opensearch.core.common.Strings.isAllOrWildcard((String) null), is(false));
}

public void testSubstring() {
assertEquals(null, Strings.substring(null, 0, 1000));
assertEquals("foo", Strings.substring("foo", 0, 1000));
Expand All @@ -61,25 +52,6 @@ public void testSubstring() {
assertEquals("f", Strings.substring("foo", 0, 1));
}

public void testCleanTruncate() {
assertEquals(null, org.opensearch.core.common.Strings.cleanTruncate(null, 10));
assertEquals("foo", org.opensearch.core.common.Strings.cleanTruncate("foo", 10));
assertEquals("foo", org.opensearch.core.common.Strings.cleanTruncate("foo", 3));
// Throws out high surrogates
assertEquals("foo", org.opensearch.core.common.Strings.cleanTruncate("foo\uD83D\uDEAB", 4));
// But will keep the whole character
assertEquals("foo\uD83D\uDEAB", org.opensearch.core.common.Strings.cleanTruncate("foo\uD83D\uDEAB", 5));
/*
* Doesn't take care around combining marks. This example has its
* meaning changed because that last codepoint is supposed to combine
* backwards into the find "o" and be represented as the "o" with a
* circle around it with a slash through it. As in "no 'o's allowed
* here.
*/
assertEquals("o", org.opensearch.core.common.Strings.cleanTruncate("o\uD83D\uDEAB", 1));
assertEquals("", org.opensearch.core.common.Strings.cleanTruncate("foo", 0));
}

public void testToStringToXContent() {
final ToXContent toXContent;
final boolean error;
Expand All @@ -104,7 +76,7 @@ public void testToStringToXContent() {
}
}

String toString = Strings.toString(XContentType.JSON, toXContent);
String toString = Strings.toString(MediaTypeRegistry.JSON, toXContent);
if (error) {
assertThat(toString, containsString("\"error\":\"error building toString out of XContent:"));
assertThat(toString, containsString("\"stack_trace\":"));
Expand All @@ -117,10 +89,10 @@ public void testToStringToXContent() {
public void testToStringToXContentWithOrWithoutParams() {
ToXContent toXContent = (builder, params) -> builder.field("color_from_param", params.param("color", "red"));
// Rely on the default value of "color" param when params are not passed
assertThat(Strings.toString(XContentType.JSON, toXContent), containsString("\"color_from_param\":\"red\""));
assertThat(Strings.toString(MediaTypeRegistry.JSON, toXContent), containsString("\"color_from_param\":\"red\""));
// Pass "color" param explicitly
assertThat(
Strings.toString(XContentType.JSON, toXContent, new ToXContent.MapParams(Collections.singletonMap("color", "blue"))),
Strings.toString(MediaTypeRegistry.JSON, toXContent, new ToXContent.MapParams(Collections.singletonMap("color", "blue"))),
containsString("\"color_from_param\":\"blue\"")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private static String visualizeEntry(Entry entry, Map<Tuple<EventType, Integer>,
int beginIndex = eventToPosition.get(Tuple.tuple(EventType.INVOCATION, id));
int endIndex = eventToPosition.get(Tuple.tuple(EventType.RESPONSE, id));
input = input.substring(0, Math.min(beginIndex + 25, input.length()));
return org.opensearch.core.common.Strings.padStart(input, beginIndex + 25, ' ')
return Strings.padStart(input, beginIndex + 25, ' ')
+ " "
+ Strings.padStart("", endIndex - beginIndex, 'X')
+ " "
Expand Down

0 comments on commit 65a19b1

Please sign in to comment.