|
@@ -21,48 +21,9 @@ import org.elasticsearch.test.AbstractSerializingTestCase;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.UncheckedIOException;
|
|
|
-import java.util.Collections;
|
|
|
|
|
|
public class ScriptMetadataTests extends AbstractSerializingTestCase<ScriptMetadata> {
|
|
|
|
|
|
- public void testFromXContentLoading() throws Exception {
|
|
|
- // failure to load to old namespace scripts with the same id but different langs
|
|
|
- XContentBuilder builder = XContentFactory.jsonBuilder();
|
|
|
- builder.startObject().field("lang0#id0", "script0").field("lang1#id0", "script1").endObject();
|
|
|
- XContentParser parser0 = XContentType.JSON.xContent()
|
|
|
- .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
|
|
|
- BytesReference.bytes(builder).streamInput());
|
|
|
- expectThrows(IllegalArgumentException.class, () -> ScriptMetadata.fromXContent(parser0));
|
|
|
-
|
|
|
- // failure to load a new namespace script and old namespace script with the same id but different langs
|
|
|
- builder = XContentFactory.jsonBuilder();
|
|
|
- builder.startObject().field("lang0#id0", "script0")
|
|
|
- .startObject("id0").field("lang", "lang1").field("source", "script1").endObject().endObject();
|
|
|
- XContentParser parser1 = XContentType.JSON.xContent()
|
|
|
- .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
|
|
|
- BytesReference.bytes(builder).streamInput());
|
|
|
- expectThrows(IllegalArgumentException.class, () -> ScriptMetadata.fromXContent(parser1));
|
|
|
-
|
|
|
- // failure to load a new namespace script and old namespace script with the same id but different langs with additional scripts
|
|
|
- builder = XContentFactory.jsonBuilder();
|
|
|
- builder.startObject().field("lang0#id0", "script0").field("lang0#id1", "script1")
|
|
|
- .startObject("id1").field("lang", "lang0").field("source", "script0").endObject()
|
|
|
- .startObject("id0").field("lang", "lang1").field("source", "script1").endObject().endObject();
|
|
|
- XContentParser parser2 = XContentType.JSON.xContent()
|
|
|
- .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
|
|
|
- BytesReference.bytes(builder).streamInput());
|
|
|
- expectThrows(IllegalArgumentException.class, () -> ScriptMetadata.fromXContent(parser2));
|
|
|
-
|
|
|
- // okay to load the same script from the new and old namespace if the lang is the same
|
|
|
- builder = XContentFactory.jsonBuilder();
|
|
|
- builder.startObject().field("lang0#id0", "script0")
|
|
|
- .startObject("id0").field("lang", "lang0").field("source", "script1").endObject().endObject();
|
|
|
- XContentParser parser3 = XContentType.JSON.xContent()
|
|
|
- .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
|
|
|
- BytesReference.bytes(builder).streamInput());
|
|
|
- ScriptMetadata.fromXContent(parser3);
|
|
|
- }
|
|
|
-
|
|
|
public void testGetScript() throws Exception {
|
|
|
ScriptMetadata.Builder builder = new ScriptMetadata.Builder(null);
|
|
|
|
|
@@ -126,72 +87,18 @@ public class ScriptMetadataTests extends AbstractSerializingTestCase<ScriptMetad
|
|
|
|
|
|
public void testLoadEmptyScripts() throws IOException {
|
|
|
XContentBuilder builder = XContentFactory.jsonBuilder();
|
|
|
- builder.startObject().field("mustache#empty", "").endObject();
|
|
|
- XContentParser parser = XContentType.JSON.xContent()
|
|
|
- .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
|
|
|
- BytesReference.bytes(builder).streamInput());
|
|
|
- ScriptMetadata.fromXContent(parser);
|
|
|
- assertWarnings("empty templates should no longer be used");
|
|
|
-
|
|
|
- builder = XContentFactory.jsonBuilder();
|
|
|
- builder.startObject().field("lang#empty", "").endObject();
|
|
|
- parser = XContentType.JSON.xContent()
|
|
|
- .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
|
|
|
- BytesReference.bytes(builder).streamInput());
|
|
|
- ScriptMetadata.fromXContent(parser);
|
|
|
- assertWarnings("empty scripts should no longer be used");
|
|
|
-
|
|
|
- builder = XContentFactory.jsonBuilder();
|
|
|
builder.startObject().startObject("script").field("lang", "lang").field("source", "").endObject().endObject();
|
|
|
- parser = XContentType.JSON.xContent()
|
|
|
+ XContentParser parser = XContentType.JSON.xContent()
|
|
|
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
|
|
|
BytesReference.bytes(builder).streamInput());
|
|
|
- ScriptMetadata.fromXContent(parser);
|
|
|
- assertWarnings("empty scripts should no longer be used");
|
|
|
+ assertTrue(ScriptMetadata.fromXContent(parser).getStoredScripts().isEmpty());
|
|
|
|
|
|
builder = XContentFactory.jsonBuilder();
|
|
|
builder.startObject().startObject("script").field("lang", "mustache").field("source", "").endObject().endObject();
|
|
|
parser = XContentType.JSON.xContent()
|
|
|
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
|
|
|
BytesReference.bytes(builder).streamInput());
|
|
|
- ScriptMetadata.fromXContent(parser);
|
|
|
- assertWarnings("empty templates should no longer be used");
|
|
|
- }
|
|
|
-
|
|
|
- public void testOldStyleDropped() throws IOException {
|
|
|
- XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
|
|
|
-
|
|
|
- builder.startObject();
|
|
|
- {
|
|
|
- builder.startObject("painless#test");
|
|
|
- {
|
|
|
- builder.field("lang", "painless");
|
|
|
- builder.field("source", "code");
|
|
|
- }
|
|
|
- builder.endObject();
|
|
|
- builder.startObject("lang#test");
|
|
|
- {
|
|
|
- builder.field("lang", "test");
|
|
|
- builder.field("source", "code");
|
|
|
- }
|
|
|
- builder.endObject();
|
|
|
- builder.startObject("test");
|
|
|
- {
|
|
|
- builder.field("lang", "painless");
|
|
|
- builder.field("source", "code");
|
|
|
- }
|
|
|
- builder.endObject();
|
|
|
- }
|
|
|
- builder.endObject();
|
|
|
-
|
|
|
- XContentParser parser = XContentType.JSON.xContent()
|
|
|
- .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
|
|
|
- BytesReference.bytes(builder).streamInput());
|
|
|
- ScriptMetadata smd = ScriptMetadata.fromXContent(parser);
|
|
|
- assertNull(smd.getStoredScript("painless#test"));
|
|
|
- assertNull(smd.getStoredScript("lang#test"));
|
|
|
- assertEquals(new StoredScriptSource("painless", "code", Collections.emptyMap()), smd.getStoredScript("test"));
|
|
|
- assertEquals(1, smd.getStoredScripts().size());
|
|
|
+ assertTrue(ScriptMetadata.fromXContent(parser).getStoredScripts().isEmpty());
|
|
|
}
|
|
|
|
|
|
@Override
|