Skip to content

Commit

Permalink
Adds NamedWriteable implementations
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Nov 27, 2024
1 parent 774a4a1 commit 37cacf0
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

package org.opensearch.accesscontrol.resources;

import org.opensearch.core.common.io.stream.NamedWriteable;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;

Expand All @@ -19,23 +23,16 @@
*
* @opensearch.experimental
*/
public class CreatedBy implements ToXContentFragment {
public class CreatedBy implements ToXContentFragment, NamedWriteable {

private String user;

private String backendRole;

public CreatedBy(String user, String backendRole) {
public CreatedBy(String user) {
this.user = user;
this.backendRole = backendRole;
}

public String getBackendRole() {
return backendRole;
}

Check warning on line 32 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L30-L32

Added lines #L30 - L32 were not covered by tests

public void setBackendRole(String backendRole) {
this.backendRole = backendRole;
public CreatedBy(StreamInput in) throws IOException {
this(in.readString());
}

Check warning on line 36 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L35-L36

Added lines #L35 - L36 were not covered by tests

public String getUser() {
Expand All @@ -48,11 +45,44 @@ public void setUser(String user) {

@Override
public String toString() {
return "CreatedBy {" + "user='" + user + '\'' + ", backendRole='" + backendRole + '\'' + '}';
return "CreatedBy {" + "user='" + user + '\'' + '}';

Check warning on line 48 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L48

Added line #L48 was not covered by tests
}

@Override
public String getWriteableName() {
return "created_by";

Check warning on line 53 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L53

Added line #L53 was not covered by tests
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(user);
}

Check warning on line 59 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L58-L59

Added lines #L58 - L59 were not covered by tests

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject().field("user", user).field("backend_role", backendRole).endObject();
return builder.startObject().field("user", user).endObject();

Check warning on line 63 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L63

Added line #L63 was not covered by tests
}

public static CreatedBy fromXContent(XContentParser parser) throws IOException {
String user = null;
String currentFieldName = null;

Check warning on line 68 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L67-L68

Added lines #L67 - L68 were not covered by tests
XContentParser.Token token;

while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();

Check warning on line 73 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L73

Added line #L73 was not covered by tests
} else if (token == XContentParser.Token.VALUE_STRING) {
if ("user".equals(currentFieldName)) {
user = parser.text();

Check warning on line 76 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L76

Added line #L76 was not covered by tests
}
}
}

if (user == null) {
throw new IllegalArgumentException("user field is required");

Check warning on line 82 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L82

Added line #L82 was not covered by tests
}

return new CreatedBy(user);

Check warning on line 85 in server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/CreatedBy.java#L85

Added line #L85 was not covered by tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@
*/
public enum EntityType {

Check warning on line 16 in server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java#L16

Added line #L16 was not covered by tests

USERS,
USERS("users"),
ROLES("roles"),
BACKEND_ROLES("backend_roles");

Check warning on line 20 in server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java#L18-L20

Added lines #L18 - L20 were not covered by tests

ROLES,
private final String value;

BACKEND_ROLES,
EntityType(String value) {
this.value = value;
}

Check warning on line 26 in server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java#L24-L26

Added lines #L24 - L26 were not covered by tests

@Override
public String toString() {
return value;

Check warning on line 30 in server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java#L30

Added line #L30 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

package org.opensearch.accesscontrol.resources;

import org.opensearch.core.common.io.stream.NamedWriteable;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;
Expand All @@ -22,7 +25,7 @@
*
* @opensearch.experimental
*/
public class ResourceSharing implements ToXContentFragment {
public class ResourceSharing implements ToXContentFragment, NamedWriteable {

private String sourceIdx;

Expand Down Expand Up @@ -103,6 +106,24 @@ public String toString() {
+ '}';
}

@Override
public String getWriteableName() {
return "resource_sharing";

Check warning on line 111 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L111

Added line #L111 was not covered by tests
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(sourceIdx);
out.writeString(resourceId);
createdBy.writeTo(out);

Check warning on line 118 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L116-L118

Added lines #L116 - L118 were not covered by tests
if (shareWith != null) {
out.writeBoolean(true);
shareWith.writeTo(out);

Check warning on line 121 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L120-L121

Added lines #L120 - L121 were not covered by tests
} else {
out.writeBoolean(false);

Check warning on line 123 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L123

Added line #L123 was not covered by tests
}
}

Check warning on line 125 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L125

Added line #L125 was not covered by tests

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject().field("source_idx", sourceIdx).field("resource_id", resourceId).field("created_by");
Expand All @@ -113,4 +134,52 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
return builder.endObject();

Check warning on line 135 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L135

Added line #L135 was not covered by tests
}

public static ResourceSharing fromXContent(XContentParser parser) throws IOException {
String sourceIdx = null;
String resourceId = null;
CreatedBy createdBy = null;
ShareWith shareWith = null;

Check warning on line 142 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L139-L142

Added lines #L139 - L142 were not covered by tests

String currentFieldName = null;

Check warning on line 144 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L144

Added line #L144 was not covered by tests
XContentParser.Token token;

while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();

Check warning on line 149 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L149

Added line #L149 was not covered by tests
} else {
switch (Objects.requireNonNull(currentFieldName)) {
case "source_idx":
sourceIdx = parser.text();
break;

Check warning on line 154 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L153-L154

Added lines #L153 - L154 were not covered by tests
case "resource_id":
resourceId = parser.text();
break;

Check warning on line 157 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L156-L157

Added lines #L156 - L157 were not covered by tests
case "created_by":
createdBy = CreatedBy.fromXContent(parser);
break;

Check warning on line 160 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L159-L160

Added lines #L159 - L160 were not covered by tests
case "share_with":
shareWith = ShareWith.fromXContent(parser);
break;

Check warning on line 163 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L162-L163

Added lines #L162 - L163 were not covered by tests
default:
parser.skipChildren();

Check warning on line 165 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L165

Added line #L165 was not covered by tests
break;
}

Check warning on line 167 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L167

Added line #L167 was not covered by tests
}
}

// Validate required fields
if (sourceIdx == null) {
throw new IllegalArgumentException("source_idx is required");

Check warning on line 173 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L173

Added line #L173 was not covered by tests
}
if (resourceId == null) {
throw new IllegalArgumentException("resource_id is required");

Check warning on line 176 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L176

Added line #L176 was not covered by tests
}
if (createdBy == null) {
throw new IllegalArgumentException("created_by is required");

Check warning on line 179 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L179

Added line #L179 was not covered by tests
}

return new ResourceSharing(sourceIdx, resourceId, createdBy, shareWith);

Check warning on line 182 in server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ResourceSharing.java#L182

Added line #L182 was not covered by tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
package org.opensearch.accesscontrol.resources;

import org.opensearch.core.common.io.stream.NamedWriteable;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -42,6 +45,10 @@ public ShareWith(List<SharedWithScope> sharedWithScopes) {
this.sharedWithScopes = sharedWithScopes;
}

Check warning on line 46 in server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java#L44-L46

Added lines #L44 - L46 were not covered by tests

public ShareWith(StreamInput in) throws IOException {
this.sharedWithScopes = in.readList(SharedWithScope::new);
}

Check warning on line 50 in server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java#L48-L50

Added lines #L48 - L50 were not covered by tests

public List<SharedWithScope> getSharedWithScopes() {
return sharedWithScopes;

Check warning on line 53 in server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java#L53

Added line #L53 was not covered by tests
}
Expand All @@ -57,6 +64,26 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder.endObject();

Check warning on line 64 in server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java#L64

Added line #L64 was not covered by tests
}

public static ShareWith fromXContent(XContentParser parser) throws IOException {
List<SharedWithScope> sharedWithScopes = new ArrayList<>();

Check warning on line 68 in server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java#L68

Added line #L68 was not covered by tests

// Ensure we're at the start of the object
if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
parser.nextToken();

Check warning on line 72 in server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java#L72

Added line #L72 was not covered by tests
}

XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
// Each field in the object represents a SharedWithScope
if (token == XContentParser.Token.FIELD_NAME) {
SharedWithScope scope = SharedWithScope.fromXContent(parser);
sharedWithScopes.add(scope);
}

Check warning on line 81 in server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java#L79-L81

Added lines #L79 - L81 were not covered by tests
}

return new ShareWith(sharedWithScopes);

Check warning on line 84 in server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java#L84

Added line #L84 was not covered by tests
}

@Override
public String getWriteableName() {
return "share_with";

Check warning on line 89 in server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/ShareWith.java#L89

Added line #L89 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
package org.opensearch.accesscontrol.resources;

import org.opensearch.core.common.io.stream.NamedWriteable;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -32,6 +36,11 @@ public SharedWithScope(String scope, SharedWithPerScope sharedWithPerScope) {
this.sharedWithPerScope = sharedWithPerScope;
}

Check warning on line 37 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L34-L37

Added lines #L34 - L37 were not covered by tests

public SharedWithScope(StreamInput in) throws IOException {
this.scope = in.readString();
this.sharedWithPerScope = new SharedWithPerScope(in);
}

Check warning on line 42 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L39-L42

Added lines #L39 - L42 were not covered by tests

public String getScope() {
return scope;

Check warning on line 45 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L45

Added line #L45 was not covered by tests
}
Expand All @@ -50,6 +59,16 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder.endObject();

Check warning on line 59 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L59

Added line #L59 was not covered by tests
}

public static SharedWithScope fromXContent(XContentParser parser) throws IOException {
String scope = parser.currentName();

Check warning on line 63 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L63

Added line #L63 was not covered by tests

parser.nextToken();

Check warning on line 65 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L65

Added line #L65 was not covered by tests

SharedWithPerScope sharedWithPerScope = SharedWithPerScope.fromXContent(parser);

Check warning on line 67 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L67

Added line #L67 was not covered by tests

return new SharedWithScope(scope, sharedWithPerScope);

Check warning on line 69 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L69

Added line #L69 was not covered by tests
}

@Override
public String toString() {
return "{" + scope + ": " + sharedWithPerScope + '}';

Check warning on line 74 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L74

Added line #L74 was not covered by tests
Expand All @@ -72,6 +91,10 @@ public void writeTo(StreamOutput out) throws IOException {
* @opensearch.experimental
*/
public static class SharedWithPerScope implements ToXContentFragment, NamedWriteable {
private static final String USERS_FIELD = EntityType.USERS.toString();
private static final String ROLES_FIELD = EntityType.ROLES.toString();
private static final String BACKEND_ROLES_FIELD = EntityType.BACKEND_ROLES.toString();

Check warning on line 96 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L94-L96

Added lines #L94 - L96 were not covered by tests

private List<String> users;

private List<String> roles;
Expand All @@ -84,6 +107,12 @@ public SharedWithPerScope(List<String> users, List<String> roles, List<String> b
this.backendRoles = backendRoles;
}

Check warning on line 108 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L104-L108

Added lines #L104 - L108 were not covered by tests

public SharedWithPerScope(StreamInput in) throws IOException {
this.users = Arrays.asList(in.readStringArray());
this.roles = Arrays.asList(in.readStringArray());
this.backendRoles = Arrays.asList(in.readStringArray());
}

Check warning on line 114 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L110-L114

Added lines #L110 - L114 were not covered by tests

public List<String> getUsers() {
return users;

Check warning on line 117 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L117

Added line #L117 was not covered by tests
}
Expand All @@ -110,17 +139,61 @@ public void setBackendRoles(List<String> backendRoles) {

@Override
public String toString() {
return "{" + "users=" + users + ", roles=" + roles + ", backendRoles=" + backendRoles + '}';
return "{"

Check warning on line 142 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L142

Added line #L142 was not covered by tests
+ USERS_FIELD
+ "="
+ users
+ ", "
+ ROLES_FIELD
+ "="
+ roles
+ ", "
+ BACKEND_ROLES_FIELD
+ "="
+ backendRoles
+ '}';
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
writeFieldOrEmptyArray(builder, "users", users);
writeFieldOrEmptyArray(builder, "roles", roles);
writeFieldOrEmptyArray(builder, "backend_roles", backendRoles);
writeFieldOrEmptyArray(builder, USERS_FIELD, users);
writeFieldOrEmptyArray(builder, ROLES_FIELD, roles);
writeFieldOrEmptyArray(builder, BACKEND_ROLES_FIELD, backendRoles);
return builder;

Check warning on line 162 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L159-L162

Added lines #L159 - L162 were not covered by tests
}

public static SharedWithPerScope fromXContent(XContentParser parser) throws IOException {
List<String> users = new ArrayList<>();
List<String> roles = new ArrayList<>();
List<String> backendRoles = new ArrayList<>();

Check warning on line 168 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L166-L168

Added lines #L166 - L168 were not covered by tests

XContentParser.Token token;
String currentFieldName = null;

Check warning on line 171 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L171

Added line #L171 was not covered by tests
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();

Check warning on line 174 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L174

Added line #L174 was not covered by tests
} else if (token == XContentParser.Token.START_ARRAY) {
if (USERS_FIELD.equals(currentFieldName)) {
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
users.add(parser.text());

Check warning on line 178 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L178

Added line #L178 was not covered by tests
}
} else if (ROLES_FIELD.equals(currentFieldName)) {
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
roles.add(parser.text());

Check warning on line 182 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L182

Added line #L182 was not covered by tests
}
} else if (BACKEND_ROLES_FIELD.equals(currentFieldName)) {
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
backendRoles.add(parser.text());

Check warning on line 186 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L186

Added line #L186 was not covered by tests
}
} else {
parser.skipChildren();

Check warning on line 189 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L189

Added line #L189 was not covered by tests
}
}
}

return new SharedWithPerScope(users, roles, backendRoles);

Check warning on line 194 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L194

Added line #L194 was not covered by tests
}

private void writeFieldOrEmptyArray(XContentBuilder builder, String fieldName, List<String> values) throws IOException {
if (values != null) {
builder.field(fieldName, values);

Check warning on line 199 in server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/accesscontrol/resources/SharedWithScope.java#L199

Added line #L199 was not covered by tests
Expand Down
Loading

0 comments on commit 37cacf0

Please sign in to comment.