Skip to content

Commit

Permalink
Add UT
Browse files Browse the repository at this point in the history
Signed-off-by: Shivansh Arora <hishiv@amazon.com>
  • Loading branch information
shiv0408 committed Jun 5, 2024
1 parent 5a0cf23 commit 5a2cc20
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 39 deletions.
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 @@ -133,7 +133,8 @@ public static RepositoryCleanupInProgress fromXContent(XContentParser parser) th
if ("repository".equals(currentFieldName)) {
repository = parser.text();

Check warning on line 134 in server/src/main/java/org/opensearch/cluster/RepositoryCleanupInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RepositoryCleanupInProgress.java#L134

Added line #L134 was not covered by tests
} else if ("repository_state_id".equals(currentFieldName)) {
// only XContent parsed with {@link Metadata.CONTEXT_MODE_GATEWAY} will have the repository state id and can be deserialized
// only XContent parsed with {@link Metadata.CONTEXT_MODE_GATEWAY} will have the repository state id and can be
// deserialized
repositoryStateId = parser.longValue();

Check warning on line 138 in server/src/main/java/org/opensearch/cluster/RepositoryCleanupInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RepositoryCleanupInProgress.java#L138

Added line #L138 was not covered by tests
} else {
throw new IllegalArgumentException("unknown field [" + currentFieldName + "]");

Check warning on line 140 in server/src/main/java/org/opensearch/cluster/RepositoryCleanupInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RepositoryCleanupInProgress.java#L140

Added line #L140 was not covered by tests
Expand Down
38 changes: 11 additions & 27 deletions server/src/main/java/org/opensearch/cluster/RestoreInProgress.java
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 Expand Up @@ -365,14 +364,23 @@ public static Entry fromXContent(XContentParser parser) throws IOException {
throw new IllegalArgumentException("unknown field [" + currentFieldName + "]");

Check warning on line 364 in server/src/main/java/org/opensearch/cluster/RestoreInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RestoreInProgress.java#L364

Added line #L364 was not covered by tests
}
}
shards.put(new ShardId(indexName, indexUUID, shardId), new ShardRestoreStatus(nodeId, State.fromValue((byte) restoreState), reason));
shards.put(

Check warning on line 367 in server/src/main/java/org/opensearch/cluster/RestoreInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RestoreInProgress.java#L366-L367

Added lines #L366 - L367 were not covered by tests
new ShardId(indexName, indexUUID, shardId),
new ShardRestoreStatus(nodeId, State.fromValue((byte) restoreState), reason)

Check warning on line 369 in server/src/main/java/org/opensearch/cluster/RestoreInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RestoreInProgress.java#L369

Added line #L369 was not covered by tests
);
}

Check warning on line 371 in server/src/main/java/org/opensearch/cluster/RestoreInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RestoreInProgress.java#L371

Added line #L371 was not covered by tests
break;
default:
throw new IllegalArgumentException("unknown field [" + currentFieldName + "]");

Check warning on line 374 in server/src/main/java/org/opensearch/cluster/RestoreInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RestoreInProgress.java#L374

Added line #L374 was not covered by tests
}
}
return new Entry(uuid, new Snapshot(snapshotRepository, new SnapshotId(snapshotName, snapshotUUID)), State.fromValue((byte) state), indices, shards);
return new Entry(

Check warning on line 377 in server/src/main/java/org/opensearch/cluster/RestoreInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RestoreInProgress.java#L376-L377

Added lines #L376 - L377 were not covered by tests
uuid,
new Snapshot(snapshotRepository, new SnapshotId(snapshotName, snapshotUUID)),
State.fromValue((byte) state),

Check warning on line 380 in server/src/main/java/org/opensearch/cluster/RestoreInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RestoreInProgress.java#L380

Added line #L380 was not covered by tests
indices,
shards
);
}
}

Expand Down Expand Up @@ -663,27 +671,3 @@ public void toXContent(Entry entry, XContentBuilder builder, ToXContent.Params p
entry.toXContent(builder, params);

Check warning on line 671 in server/src/main/java/org/opensearch/cluster/RestoreInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/RestoreInProgress.java#L671

Added line #L671 was not covered by tests
}
}
























Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ public static SnapshotDeletionsInProgress fromXContent(XContentParser parser) th
return SnapshotDeletionsInProgress.of(entries);
}


@Override
public String toString() {
StringBuilder builder = new StringBuilder("SnapshotDeletionsInProgress[");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,9 +870,12 @@ public static Entry fromXContent(XContentParser parser) throws IOException {
throw new IllegalArgumentException("unknown field [" + currentShardField + "]");

Check warning on line 870 in server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java#L870

Added line #L870 was not covered by tests
}
}
shards.put(new ShardId(index, shardId),
reason != null ? new ShardSnapshotStatus(nodeId, shardState, reason, generation) :
new ShardSnapshotStatus(nodeId, shardState, generation));
shards.put(
new ShardId(index, shardId),
reason != null
? new ShardSnapshotStatus(nodeId, shardState, reason, generation)
: new ShardSnapshotStatus(nodeId, shardState, generation)
);
}
break;
case DATA_STREAMS:
Expand Down Expand Up @@ -927,9 +930,12 @@ public static Entry fromXContent(XContentParser parser) throws IOException {
throw new IllegalArgumentException("unknown field [" + currentFieldName + "]");
}

Check warning on line 931 in server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java#L930-L931

Added lines #L930 - L931 were not covered by tests
}
clones.put(new RepositoryShardId(new IndexId(index, indexId), shardId),
reason != null ? new ShardSnapshotStatus(nodeId, ShardState.fromValue(snapshotShardStatus), reason, generation) :
new ShardSnapshotStatus(nodeId, ShardState.fromValue(snapshotShardStatus), generation));
clones.put(

Check warning on line 933 in server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java#L933

Added line #L933 was not covered by tests
new RepositoryShardId(new IndexId(index, indexId), shardId),
reason != null
? new ShardSnapshotStatus(nodeId, ShardState.fromValue(snapshotShardStatus), reason, generation)
: new ShardSnapshotStatus(nodeId, ShardState.fromValue(snapshotShardStatus), generation)

Check warning on line 937 in server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java#L936-L937

Added lines #L936 - L937 were not covered by tests
);
}

Check warning on line 939 in server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java#L939

Added line #L939 was not covered by tests
break;
case VERSION:
Expand Down Expand Up @@ -1202,7 +1208,7 @@ public static State fromValue(byte value) {
}

public static State fromString(String value) {
switch(value) {
switch (value) {
case "INIT":
return INIT;

Check warning on line 1213 in server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/SnapshotsInProgress.java#L1213

Added line #L1213 was not covered by tests
case "STARTED":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.core.index.Index;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;
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
74 changes: 74 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,74 @@
/*
* 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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.opensearch.cluster.SnapshotsInProgress.ShardState;
import org.opensearch.cluster.SnapshotsInProgress.State;
import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.common.UUIDs;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.json.JsonXContent;
Expand Down Expand Up @@ -214,7 +213,13 @@ public void testToXContent() throws IOException {
}

public void testToXContent_deletion() throws IOException {
SnapshotDeletionsInProgress.Entry entry = new SnapshotDeletionsInProgress.Entry(List.of(new SnapshotId("name1", "uuid1")), "repo", 10000000L, 10000L, SnapshotDeletionsInProgress.State.WAITING);
SnapshotDeletionsInProgress.Entry entry = new SnapshotDeletionsInProgress.Entry(
List.of(new SnapshotId("name1", "uuid1")),
"repo",
10000000L,
10000L,
SnapshotDeletionsInProgress.State.WAITING
);
SnapshotDeletionsInProgress sdip = SnapshotDeletionsInProgress.of(List.of(entry));
boolean humanReadable = false;
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
Expand Down

0 comments on commit 5a2cc20

Please sign in to comment.