summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/script/ScriptTests.java
diff options
context:
space:
mode:
authorjavanna <cavannaluca@gmail.com>2017-02-22 14:47:46 +0100
committerLuca Cavanna <javanna@users.noreply.github.com>2017-02-22 16:20:53 +0100
commitf2acf466aae3c6931ca00dbdc154fe26f6bdd2da (patch)
treef5c510629f3eab79e6ff2e9c8776a740221fda46 /core/src/test/java/org/elasticsearch/script/ScriptTests.java
parent9391c6ffa90f7a6fc01931c855155923d6f702e1 (diff)
Convert script/template objects to json format
Elasticsearch accepts multiple content-type formats, hence scripts can be stored/provided in json, yaml, cbor or smile. Yet the format that should be used internally is json. This is a problem mainly around search templates, as they only support json out of the four content-types, so instead of maintaining the content-type of the request we should rather convert the scripts/templates to json. Binary formats were not previously supported. If you stored a template in yaml format, you'd get back an error "No encoder found for MIME type [application/yaml]" when trying to execute it. With this commit the request content-type is independent from the template, which always gets converted to json internally. That is transparent to users and doesn't affect the content type of the response obtained when executing the template.
Diffstat (limited to 'core/src/test/java/org/elasticsearch/script/ScriptTests.java')
-rw-r--r--core/src/test/java/org/elasticsearch/script/ScriptTests.java18
1 files changed, 8 insertions, 10 deletions
diff --git a/core/src/test/java/org/elasticsearch/script/ScriptTests.java b/core/src/test/java/org/elasticsearch/script/ScriptTests.java
index fc841bd164..70c5af00f8 100644
--- a/core/src/test/java/org/elasticsearch/script/ScriptTests.java
+++ b/core/src/test/java/org/elasticsearch/script/ScriptTests.java
@@ -22,8 +22,8 @@ package org.elasticsearch.script;
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
import org.elasticsearch.common.io.stream.OutputStreamStreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
-import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;
@@ -39,9 +39,8 @@ import static org.hamcrest.Matchers.equalTo;
public class ScriptTests extends ESTestCase {
public void testScriptParsing() throws IOException {
- XContent xContent = randomFrom(XContentType.JSON, XContentType.YAML).xContent();
- Script expectedScript = createScript(xContent);
- try (XContentBuilder builder = XContentBuilder.builder(xContent)) {
+ Script expectedScript = createScript();
+ try (XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()))) {
expectedScript.toXContent(builder, ToXContent.EMPTY_PARAMS);
try (XContentParser parser = createParser(builder)) {
Script actualScript = Script.parse(parser);
@@ -51,8 +50,7 @@ public class ScriptTests extends ESTestCase {
}
public void testScriptSerialization() throws IOException {
- XContent xContent = randomFrom(XContentType.JSON, XContentType.YAML).xContent();
- Script expectedScript = createScript(xContent);
+ Script expectedScript = createScript();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
expectedScript.writeTo(new OutputStreamStreamOutput(out));
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
@@ -62,12 +60,12 @@ public class ScriptTests extends ESTestCase {
}
}
- private Script createScript(XContent xContent) throws IOException {
+ private Script createScript() throws IOException {
final Map<String, Object> params = randomBoolean() ? Collections.emptyMap() : Collections.singletonMap("key", "value");
ScriptType scriptType = randomFrom(ScriptType.values());
String script;
if (scriptType == ScriptType.INLINE) {
- try (XContentBuilder builder = XContentBuilder.builder(xContent)) {
+ try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
builder.startObject();
builder.field("field", randomAsciiOfLengthBetween(1, 5));
builder.endObject();
@@ -80,8 +78,8 @@ public class ScriptTests extends ESTestCase {
scriptType,
scriptType == ScriptType.STORED ? null : randomFrom("_lang1", "_lang2", "_lang3"),
script,
- scriptType == ScriptType.INLINE ? Collections.singletonMap(Script.CONTENT_TYPE_OPTION, xContent.type().mediaType()) : null,
- params
+ scriptType == ScriptType.INLINE ?
+ Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()) : null, params
);
}