-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shivansh Arora <[email protected]>
- Loading branch information
Showing
4 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
server/src/test/java/org/opensearch/snapshots/SnapshotIdTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |