From 579baa2bca9c9ddcd0d199934d7f49e6b8161719 Mon Sep 17 00:00:00 2001 From: javanna Date: Fri, 24 Jun 2016 13:29:15 +0200 Subject: [PATCH 1/2] [TEST] make JsonPath independent of data format, rename to ObjectPath The internal representation of the object that JsonPath gives access to is a map. That is independent of the initial input format, which is json but could also be yaml etc. This commit renames JsonPath to ObjectPath and adds a static method to create an ObjectPath from an XContent Original commit: elastic/x-pack-elasticsearch@bc84c68161dbb1310eb798448cc2be72888082ba --- .../rest/action/RestAuthenticateActionTests.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java b/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java index ad979915726..1d9e5a7eabb 100644 --- a/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java +++ b/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java @@ -16,7 +16,7 @@ import org.elasticsearch.xpack.security.authz.InternalAuthorizationService; import org.elasticsearch.xpack.security.user.AnonymousUser; import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSource; -import org.elasticsearch.test.rest.json.JsonPath; +import org.elasticsearch.test.rest.ObjectPath; import org.junit.BeforeClass; import java.util.Collections; @@ -56,10 +56,10 @@ public class RestAuthenticateActionTests extends SecurityIntegTestCase { new BasicHeader("Authorization", basicAuthHeaderValue(SecuritySettingsSource.DEFAULT_USER_NAME, new SecuredString(SecuritySettingsSource.DEFAULT_PASSWORD.toCharArray()))))) { assertThat(response.getStatusLine().getStatusCode(), is(200)); - JsonPath jsonPath = new JsonPath(EntityUtils.toString(response.getEntity())); - assertThat(jsonPath.evaluate("username").toString(), equalTo(SecuritySettingsSource.DEFAULT_USER_NAME)); + ObjectPath objectPath = ObjectPath.createFromXContent(EntityUtils.toString(response.getEntity())); + assertThat(objectPath.evaluate("username").toString(), equalTo(SecuritySettingsSource.DEFAULT_USER_NAME)); @SuppressWarnings("unchecked") - List roles = (List) jsonPath.evaluate("roles"); + List roles = (List) objectPath.evaluate("roles"); assertThat(roles.size(), is(1)); assertThat(roles, contains(SecuritySettingsSource.DEFAULT_ROLE)); } @@ -70,10 +70,10 @@ public class RestAuthenticateActionTests extends SecurityIntegTestCase { Collections.emptyMap(), null)) { if (anonymousEnabled) { assertThat(response.getStatusLine().getStatusCode(), is(200)); - JsonPath jsonPath = new JsonPath(EntityUtils.toString(response.getEntity())); - assertThat(jsonPath.evaluate("username").toString(), equalTo("anon")); + ObjectPath objectPath = ObjectPath.createFromXContent(EntityUtils.toString(response.getEntity())); + assertThat(objectPath.evaluate("username").toString(), equalTo("anon")); @SuppressWarnings("unchecked") - List roles = (List) jsonPath.evaluate("roles"); + List roles = (List) objectPath.evaluate("roles"); assertThat(roles.size(), is(2)); assertThat(roles, contains(SecuritySettingsSource.DEFAULT_ROLE, "foo")); } else { From 4eb21f4c01affdf24284850f2c171db7530916f6 Mon Sep 17 00:00:00 2001 From: javanna Date: Sat, 25 Jun 2016 15:41:10 +0200 Subject: [PATCH 2/2] [TEST] eagerly parse response body at ObjectPath initialization and read content type from response headers We are going to parse the body anyways whenever it's in json format as it is going to be stashed. It is not useful to lazily parse it anymore. Also this allows us to not rely on automatic detection of the xcontent type based on the content of the response, but rather read the content type from the response headers. Original commit: elastic/x-pack-elasticsearch@11be4684ae37f649a506d1ff0ffe0adbbfe552ee --- .../security/rest/action/RestAuthenticateActionTests.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java b/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java index 1d9e5a7eabb..dc95118b79f 100644 --- a/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java +++ b/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java @@ -11,6 +11,8 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.xpack.security.authc.support.SecuredString; import org.elasticsearch.xpack.security.authz.InternalAuthorizationService; import org.elasticsearch.xpack.security.user.AnonymousUser; @@ -56,7 +58,8 @@ public class RestAuthenticateActionTests extends SecurityIntegTestCase { new BasicHeader("Authorization", basicAuthHeaderValue(SecuritySettingsSource.DEFAULT_USER_NAME, new SecuredString(SecuritySettingsSource.DEFAULT_PASSWORD.toCharArray()))))) { assertThat(response.getStatusLine().getStatusCode(), is(200)); - ObjectPath objectPath = ObjectPath.createFromXContent(EntityUtils.toString(response.getEntity())); + ObjectPath objectPath = ObjectPath.createFromXContent(XContentFactory.xContent(XContentType.JSON), + EntityUtils.toString(response.getEntity())); assertThat(objectPath.evaluate("username").toString(), equalTo(SecuritySettingsSource.DEFAULT_USER_NAME)); @SuppressWarnings("unchecked") List roles = (List) objectPath.evaluate("roles"); @@ -70,7 +73,8 @@ public class RestAuthenticateActionTests extends SecurityIntegTestCase { Collections.emptyMap(), null)) { if (anonymousEnabled) { assertThat(response.getStatusLine().getStatusCode(), is(200)); - ObjectPath objectPath = ObjectPath.createFromXContent(EntityUtils.toString(response.getEntity())); + ObjectPath objectPath = ObjectPath.createFromXContent(XContentFactory.xContent(XContentType.JSON), + EntityUtils.toString(response.getEntity())); assertThat(objectPath.evaluate("username").toString(), equalTo("anon")); @SuppressWarnings("unchecked") List roles = (List) objectPath.evaluate("roles");