From d02aed39b7da595a0a04776738f83cc3f7576c29 Mon Sep 17 00:00:00 2001 From: Shivansh Arora Date: Wed, 5 Jun 2024 19:12:52 +0530 Subject: [PATCH] Add UT Signed-off-by: Shivansh Arora --- .../core/xcontent/XContentParserUtils.java | 12 +++ .../opensearch/cluster/RestoreInProgress.java | 1 - .../org/opensearch/snapshots/SnapshotId.java | 3 + .../opensearch/snapshots/SnapshotIdTests.java | 81 +++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 server/src/test/java/org/opensearch/snapshots/SnapshotIdTests.java diff --git a/libs/core/src/main/java/org/opensearch/core/xcontent/XContentParserUtils.java b/libs/core/src/main/java/org/opensearch/core/xcontent/XContentParserUtils.java index b10be393f9adb..a543556baa262 100644 --- a/libs/core/src/main/java/org/opensearch/core/xcontent/XContentParserUtils.java +++ b/libs/core/src/main/java/org/opensearch/core/xcontent/XContentParserUtils.java @@ -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; @@ -178,4 +180,14 @@ public static void parseTypedKeysObject(XContentParser parser, String delimi throw new ParsingException(parser.getTokenLocation(), "Failed to parse object: empty key"); } } + + public static List parseStringList(XContentParser parser) throws IOException { + List 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; + } } diff --git a/server/src/main/java/org/opensearch/cluster/RestoreInProgress.java b/server/src/main/java/org/opensearch/cluster/RestoreInProgress.java index 627a04bd8277b..a1fcbab3cbb21 100644 --- a/server/src/main/java/org/opensearch/cluster/RestoreInProgress.java +++ b/server/src/main/java/org/opensearch/cluster/RestoreInProgress.java @@ -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; diff --git a/server/src/main/java/org/opensearch/snapshots/SnapshotId.java b/server/src/main/java/org/opensearch/snapshots/SnapshotId.java index 31b18f14c854b..d74895de4caf7 100644 --- a/server/src/main/java/org/opensearch/snapshots/SnapshotId.java +++ b/server/src/main/java/org/opensearch/snapshots/SnapshotId.java @@ -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; diff --git a/server/src/test/java/org/opensearch/snapshots/SnapshotIdTests.java b/server/src/test/java/org/opensearch/snapshots/SnapshotIdTests.java new file mode 100644 index 0000000000000..3e86033fb9876 --- /dev/null +++ b/server/src/test/java/org/opensearch/snapshots/SnapshotIdTests.java @@ -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); + } + } + } +}