[TEST] support stashed values within property names in our REST tests
Closes #9533
This commit is contained in:
parent
4342237acf
commit
dfe67da013
|
@ -34,9 +34,6 @@ import org.elasticsearch.common.logging.ESLogger;
|
|||
import org.elasticsearch.common.logging.Loggers;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||
import org.elasticsearch.index.engine.Segment;
|
||||
import org.elasticsearch.node.internal.InternalNode;
|
||||
import org.elasticsearch.test.ElasticsearchBackwardsCompatIntegrationTest;
|
||||
|
@ -246,6 +243,7 @@ public class UpgradeTest extends ElasticsearchBackwardsCompatIntegrationTest {
|
|||
assertEquals(200, rsp.getStatusCode());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static List<UpgradeStatus> getUpgradeStatus(HttpRequestBuilder httpClient, String path) throws Exception {
|
||||
HttpResponse rsp = httpClient.method("GET").path(path).execute();
|
||||
Map<String,Object> data = validateAndParse(rsp);
|
||||
|
@ -258,11 +256,12 @@ public class UpgradeTest extends ElasticsearchBackwardsCompatIntegrationTest {
|
|||
assertTrue("missing key size_to_upgrade_in_bytes for index " + index, status.containsKey("size_to_upgrade_in_bytes"));
|
||||
Object toUpgradeBytes = status.get("size_to_upgrade_in_bytes");
|
||||
assertTrue("size_to_upgrade_in_bytes for index " + index + " is not an integer", toUpgradeBytes instanceof Integer);
|
||||
ret.add(new UpgradeStatus(index, ((Integer)totalBytes).intValue(), ((Integer)toUpgradeBytes).intValue()));
|
||||
ret.add(new UpgradeStatus(index, (Integer)totalBytes, (Integer)toUpgradeBytes));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static Map<String, Object> validateAndParse(HttpResponse rsp) throws Exception {
|
||||
assertNotNull(rsp);
|
||||
assertEquals(200, rsp.getStatusCode());
|
||||
|
|
|
@ -114,7 +114,7 @@ public class RestTestExecutionContext implements Closeable {
|
|||
* Extracts a specific value from the last saved response
|
||||
*/
|
||||
public Object response(String path) throws IOException {
|
||||
return response.evaluate(path);
|
||||
return response.evaluate(path, stash);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -35,6 +35,8 @@ public class Stash {
|
|||
|
||||
private static final ESLogger logger = Loggers.getLogger(Stash.class);
|
||||
|
||||
public static final Stash EMPTY = new Stash();
|
||||
|
||||
private final Map<String, Object> stash = Maps.newHashMap();
|
||||
|
||||
/**
|
||||
|
@ -93,7 +95,7 @@ public class Stash {
|
|||
@SuppressWarnings("unchecked")
|
||||
private void unstashObject(Object obj) {
|
||||
if (obj instanceof List) {
|
||||
List list = (List)obj;
|
||||
List list = (List) obj;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
Object o = list.get(i);
|
||||
if (isStashedValue(o)) {
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
package org.elasticsearch.test.rest.client;
|
||||
|
||||
import org.elasticsearch.test.rest.Stash;
|
||||
import org.elasticsearch.test.rest.client.http.HttpResponse;
|
||||
import org.elasticsearch.test.rest.json.JsonPath;
|
||||
|
||||
|
@ -70,6 +71,13 @@ public class RestResponse {
|
|||
* Parses the response body as json and extracts a specific value from it (identified by the provided path)
|
||||
*/
|
||||
public Object evaluate(String path) throws IOException {
|
||||
return evaluate(path, Stash.EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the response body as json and extracts a specific value from it (identified by the provided path)
|
||||
*/
|
||||
public Object evaluate(String path, Stash stash) throws IOException {
|
||||
|
||||
if (response == null) {
|
||||
return null;
|
||||
|
@ -87,7 +95,7 @@ public class RestResponse {
|
|||
return null;
|
||||
}
|
||||
|
||||
return jsonPath.evaluate(path);
|
||||
return jsonPath.evaluate(path, stash);
|
||||
}
|
||||
|
||||
private boolean isJson() {
|
||||
|
|
|
@ -20,6 +20,7 @@ package org.elasticsearch.test.rest.json;
|
|||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||
import org.elasticsearch.test.rest.Stash;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
@ -46,10 +47,17 @@ public class JsonPath {
|
|||
* Returns the object corresponding to the provided path if present, null otherwise
|
||||
*/
|
||||
public Object evaluate(String path) {
|
||||
return evaluate(path, Stash.EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object corresponding to the provided path if present, null otherwise
|
||||
*/
|
||||
public Object evaluate(String path, Stash stash) {
|
||||
String[] parts = parsePath(path);
|
||||
Object object = jsonMap;
|
||||
for (String part : parts) {
|
||||
object = evaluate(part, object);
|
||||
object = evaluate(part, object, stash);
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -58,7 +66,11 @@ public class JsonPath {
|
|||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object evaluate(String key, Object object) {
|
||||
private Object evaluate(String key, Object object, Stash stash) {
|
||||
if (stash.isStashedValue(key)) {
|
||||
key = stash.unstashValue(key).toString();
|
||||
}
|
||||
|
||||
if (object instanceof Map) {
|
||||
return ((Map<String, Object>) object).get(key);
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ import java.util.List;
|
|||
*/
|
||||
public final class Features {
|
||||
|
||||
private static final List<String> SUPPORTED = Lists.newArrayList("gtelte");
|
||||
private static final List<String> SUPPORTED = Lists.newArrayList("gtelte", "stash_in_path");
|
||||
|
||||
private Features() {
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
package org.elasticsearch.test.rest.test;
|
||||
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.elasticsearch.test.rest.Stash;
|
||||
import org.elasticsearch.test.rest.json.JsonPath;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -147,4 +148,22 @@ public class JsonPathTests extends ElasticsearchTestCase {
|
|||
assertThat(object, instanceOf(Map.class));
|
||||
assertThat(((Map<String, Object>)object).containsKey("field1"), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateStashInPropertyName() throws Exception {
|
||||
String json = "{ \"field1\": { \"elements\" : {\"element1\": \"value1\"}}}";
|
||||
JsonPath jsonPath = new JsonPath(json);
|
||||
try {
|
||||
jsonPath.evaluate("field1.$placeholder.element1");
|
||||
fail("evaluate should have failed due to unresolved placeholder");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), containsString("stashed value not found for key [$placeholder]"));
|
||||
}
|
||||
|
||||
Stash stash = new Stash();
|
||||
stash.stashValue("placeholder", "elements");
|
||||
Object object = jsonPath.evaluate("field1.$placeholder.element1", stash);
|
||||
assertThat(object, notNullValue());
|
||||
assertThat(object.toString(), equalTo("value1"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue