Skip to content

Commit

Permalink
Add UT
Browse files Browse the repository at this point in the history
Signed-off-by: Shivansh Arora <[email protected]>
  • Loading branch information
shiv0408 committed Jun 5, 2024
1 parent 5a0cf23 commit d02aed3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.opensearch.core.xcontent.XContentParser.Token;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;

Expand Down Expand Up @@ -178,4 +180,14 @@ public static <T> void parseTypedKeysObject(XContentParser parser, String delimi
throw new ParsingException(parser.getTokenLocation(), "Failed to parse object: empty key");
}
}

public static List<String> parseStringList(XContentParser parser) throws IOException {
List<String> valueList = new ArrayList<>();
ensureExpectedToken(Token.START_ARRAY, parser.currentToken(), parser);
while (parser.nextToken() != Token.END_ARRAY) {
ensureExpectedToken(Token.VALUE_STRING, parser.currentToken(), parser);
valueList.add(parser.text());
}
return valueList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

package org.opensearch.cluster;

import org.elasticsearch.snapshots.SnapshotId;
import org.opensearch.Version;
import org.opensearch.cluster.ClusterState.Custom;
import org.opensearch.cluster.metadata.Metadata;
Expand Down
3 changes: 3 additions & 0 deletions server/src/main/java/org/opensearch/snapshots/SnapshotId.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}

public static SnapshotId fromXContent(XContentParser parser) throws IOException {
if (parser.currentToken() == null) { // fresh parser? move to next token
parser.nextToken();
}
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
String name = null;
String uuid = null;
Expand Down
81 changes: 81 additions & 0 deletions server/src/test/java/org/opensearch/snapshots/SnapshotIdTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.snapshots;

import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.XContentTestUtils;

import java.io.IOException;
import java.util.Collections;

public class SnapshotIdTests extends OpenSearchTestCase {
public void testToXContent() throws IOException {
SnapshotId snapshotId = new SnapshotId("repo", "snapshot");
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
snapshotId.toXContent(builder, null);
assertEquals(
"{\n" +
" \"name\" : \"repo\",\n" +
" \"uuid\" : \"snapshot\"\n" +
"}", builder.toString());
}

public void testFromXContent() throws IOException {
doFromXContentTestWithRandomFields(false);
}

public void testFromXContentWithRandomField() throws IOException {
doFromXContentTestWithRandomFields(true);
}

private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException {
SnapshotId snapshotId = new SnapshotId(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10));
boolean humanReadable = randomBoolean();
final MediaType mediaType = MediaTypeRegistry.JSON;
BytesReference originalBytes = toShuffledXContent(
snapshotId,
mediaType,
new ToXContent.MapParams(Collections.emptyMap()),
humanReadable
);

if (addRandomFields) {
String unsupportedField = "unsupported_field";
BytesReference mutated = BytesReference.bytes(
XContentTestUtils.insertIntoXContent(
mediaType.xContent(),
originalBytes,
Collections.singletonList(""),
() -> unsupportedField,
() -> randomAlphaOfLengthBetween(3, 10)
)
);
IllegalArgumentException iae = expectThrows(
IllegalArgumentException.class,
() -> SnapshotId.fromXContent(createParser(mediaType.xContent(), mutated))
);
assertEquals(
"unknown field [" + unsupportedField + "]",
iae.getMessage()
);
} else {
try (XContentParser parser = createParser(mediaType.xContent(), originalBytes)) {
SnapshotId parsedSnapshotId = SnapshotId.fromXContent(parser);
assertEquals(snapshotId, parsedSnapshotId);
}
}
}
}

0 comments on commit d02aed3

Please sign in to comment.