diff --git a/src/test/java/org/opensearch/security/dlic/rest/api/RoleBasedAccessTest.java b/src/test/java/org/opensearch/security/dlic/rest/api/RoleBasedAccessTest.java index fc5c59d36f..9d8badba6f 100644 --- a/src/test/java/org/opensearch/security/dlic/rest/api/RoleBasedAccessTest.java +++ b/src/test/java/org/opensearch/security/dlic/rest/api/RoleBasedAccessTest.java @@ -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; @@ -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 diff --git a/src/test/java/org/opensearch/security/test/helper/rest/RestHelper.java b/src/test/java/org/opensearch/security/test/helper/rest/RestHelper.java index 730a22f18f..87ffa06da7 100644 --- a/src/test/java/org/opensearch/security/test/helper/rest/RestHelper.java +++ b/src/test/java/org/opensearch/security/test/helper/rest/RestHelper.java @@ -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; @@ -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 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 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); @@ -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; } }