[TEST] rename REST tests Stash methods to distinguish between retrieving a value and replacing values within a map

Stash#unstashMap -> replaceStashedValues
Stash#unstashValue -> getValue
This commit is contained in:
javanna 2016-06-24 13:08:21 +02:00 committed by Luca Cavanna
parent 62462f5d9b
commit 76199ce497
4 changed files with 20 additions and 18 deletions

View File

@ -67,7 +67,7 @@ public class RestTestExecutionContext implements Closeable {
HashMap<String, String> requestParams = new HashMap<>(params); HashMap<String, String> requestParams = new HashMap<>(params);
for (Map.Entry<String, String> entry : requestParams.entrySet()) { for (Map.Entry<String, String> entry : requestParams.entrySet()) {
if (stash.isStashedValue(entry.getValue())) { if (stash.isStashedValue(entry.getValue())) {
entry.setValue(stash.unstashValue(entry.getValue()).toString()); entry.setValue(stash.getValue(entry.getValue()).toString());
} }
} }
@ -90,12 +90,12 @@ public class RestTestExecutionContext implements Closeable {
} }
if (bodies.size() == 1) { if (bodies.size() == 1) {
return bodyAsString(stash.unstashMap(bodies.get(0))); return bodyAsString(stash.replaceStashedValues(bodies.get(0)));
} }
StringBuilder bodyBuilder = new StringBuilder(); StringBuilder bodyBuilder = new StringBuilder();
for (Map<String, Object> body : bodies) { for (Map<String, Object> body : bodies) {
bodyBuilder.append(bodyAsString(stash.unstashMap(body))).append("\n"); bodyBuilder.append(bodyAsString(stash.replaceStashedValues(body))).append("\n");
} }
return bodyBuilder.toString(); return bodyBuilder.toString();
} }

View File

@ -69,7 +69,8 @@ public class Stash implements ToXContent {
} }
/** /**
* Tells whether a particular value needs to be looked up in the stash * Tells whether a particular key needs to be looked up in the stash based on its name.
* Returns true if the string representation of the key starts with "$", false otherwise
* The stash contains fields eventually extracted from previous responses that can be reused * The stash contains fields eventually extracted from previous responses that can be reused
* as arguments for following requests (e.g. scroll_id) * as arguments for following requests (e.g. scroll_id)
*/ */
@ -82,28 +83,29 @@ public class Stash implements ToXContent {
} }
/** /**
* Extracts a value from the current stash * Retrieves a value from the current stash.
* The stash contains fields eventually extracted from previous responses that can be reused * The stash contains fields eventually extracted from previous responses that can be reused
* as arguments for following requests (e.g. scroll_id) * as arguments for following requests (e.g. scroll_id)
*/ */
public Object unstashValue(String value) throws IOException { public Object getValue(String key) throws IOException {
if (value.startsWith("$body.")) { if (key.startsWith("$body.")) {
if (response == null) { if (response == null) {
return null; return null;
} }
return response.evaluate(value.substring("$body".length()), this); return response.evaluate(key.substring("$body".length()), this);
} }
Object stashedValue = stash.get(value.substring(1)); Object stashedValue = stash.get(key.substring(1));
if (stashedValue == null) { if (stashedValue == null) {
throw new IllegalArgumentException("stashed value not found for key [" + value + "]"); throw new IllegalArgumentException("stashed value not found for key [" + key + "]");
} }
return stashedValue; return stashedValue;
} }
/** /**
* Recursively unstashes map values if needed * Goes recursively against each map entry and replaces any string value starting with "$" with its
* corresponding value retrieved from the stash
*/ */
public Map<String, Object> unstashMap(Map<String, Object> map) throws IOException { public Map<String, Object> replaceStashedValues(Map<String, Object> map) throws IOException {
Map<String, Object> copy = new HashMap<>(map); Map<String, Object> copy = new HashMap<>(map);
unstashObject(copy); unstashObject(copy);
return copy; return copy;
@ -116,7 +118,7 @@ public class Stash implements ToXContent {
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
Object o = list.get(i); Object o = list.get(i);
if (isStashedValue(o)) { if (isStashedValue(o)) {
list.set(i, unstashValue(o.toString())); list.set(i, getValue(o.toString()));
} else { } else {
unstashObject(o); unstashObject(o);
} }
@ -126,7 +128,7 @@ public class Stash implements ToXContent {
Map<String, Object> map = (Map) obj; Map<String, Object> map = (Map) obj;
for (Map.Entry<String, Object> entry : map.entrySet()) { for (Map.Entry<String, Object> entry : map.entrySet()) {
if (isStashedValue(entry.getValue())) { if (isStashedValue(entry.getValue())) {
entry.setValue(unstashValue(entry.getValue().toString())); entry.setValue(getValue(entry.getValue().toString()));
} else { } else {
unstashObject(entry.getValue()); unstashObject(entry.getValue());
} }

View File

@ -71,7 +71,7 @@ public class JsonPath {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Object evaluate(String key, Object object, Stash stash) throws IOException { private Object evaluate(String key, Object object, Stash stash) throws IOException {
if (stash.isStashedValue(key)) { if (stash.isStashedValue(key)) {
key = stash.unstashValue(key).toString(); key = stash.getValue(key).toString();
} }
if (object instanceof Map) { if (object instanceof Map) {

View File

@ -48,18 +48,18 @@ public abstract class Assertion implements ExecutableSection {
if (expectedValue instanceof Map) { if (expectedValue instanceof Map) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) expectedValue; Map<String, Object> map = (Map<String, Object>) expectedValue;
return executionContext.stash().unstashMap(map); return executionContext.stash().replaceStashedValues(map);
} }
if (executionContext.stash().isStashedValue(expectedValue)) { if (executionContext.stash().isStashedValue(expectedValue)) {
return executionContext.stash().unstashValue(expectedValue.toString()); return executionContext.stash().getValue(expectedValue.toString());
} }
return expectedValue; return expectedValue;
} }
protected final Object getActualValue(RestTestExecutionContext executionContext) throws IOException { protected final Object getActualValue(RestTestExecutionContext executionContext) throws IOException {
if (executionContext.stash().isStashedValue(field)) { if (executionContext.stash().isStashedValue(field)) {
return executionContext.stash().unstashValue(field); return executionContext.stash().getValue(field);
} }
return executionContext.response(field); return executionContext.response(field);
} }