Skip to content

Commit

Permalink
Fixing busted test, adding findArrayInJson for response bodies
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied committed Jun 23, 2023
1 parent 91d631b commit 5f0f3e8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

import static org.opensearch.security.OpenSearchSecurityPlugin.PLUGINS_PREFIX;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;

public class RoleBasedAccessTest extends AbstractRestApiUnitTest {
private final String ENDPOINT;

Expand Down Expand Up @@ -173,11 +177,9 @@ public void testActionGroupsApi() throws Exception {
// Worf, has access to roles API, get captains role
response = rh.executeGetRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet_captains", encodeBasicHeader("worf", "worf"));
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusCode());
Assert.assertEquals(
new SecurityJsonNode(DefaultObjectMapper.readTree(response.getBody())).getDotted(
"opendistro_security_role_starfleet_captains.cluster_permissions"
).get(0).asString(),
"cluster:monitor*"
assertThat(
response.findArrayInJson("opendistro_security_role_starfleet_captains.cluster_permissions"),
allOf(hasItem("*bulk*"), hasItem("cluster:monitor*"))
);

// Worf, has access to roles API, able to delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -476,9 +477,40 @@ public String toString() {
}

/**
* Given a json path with dots delimiated returns the object at the leaf
* Given a json path with dots delimiated returns the object at the leaf as a string
*/
public String findValueInJson(final String jsonDotPath) {
final JsonNode node = this.findObjectInJson(jsonDotPath);
throwIfNotValueNode(node);
return node.asText();
}

/**
* Given a json path with dots delimiated returns the array at the leaf
*/
public List<String> findArrayInJson(final String jsonDotPath) {
final JsonNode node = this.findObjectInJson(jsonDotPath);
if (!node.isArray()) {
throw new RuntimeException("Found object was not an array, object\n" + node.toPrettyString());
}
final List<String> elements = new ArrayList<>();
for (int i = 0; i < node.size(); i++) {
final JsonNode currentNode = node.get(i);
throwIfNotValueNode(currentNode);
elements.add(currentNode.asText());
}
return elements;
}

private void throwIfNotValueNode(final JsonNode node) {
if (!node.isValueNode()) {
throw new RuntimeException(
"Unexpected value note, index directly to the object to reference, object\n" + node.toPrettyString()
);
}
}

private JsonNode findObjectInJson(final String jsonDotPath) {
// Make sure its json / then parse it
if (!isJsonContentType()) {
throw new RuntimeException("Response was expected to be JSON, body was: \n" + body);
Expand Down Expand Up @@ -551,12 +583,7 @@ public String findValueInJson(final String jsonDotPath) {
}
} while (jsonPathScanner.hasNext());

if (!currentNode.isValueNode()) {
throw new RuntimeException(
"Unexpected value note, index directly to the object to reference, object\n" + currentNode.toPrettyString()
);
}
return currentNode.asText();
return currentNode;
}
}

Expand Down

0 comments on commit 5f0f3e8

Please sign in to comment.