Remove most usages of 1-arg Script ctor (#24325)
The one argument ctor for `Script` creates a script with the default language but most usages of are for testing and either don't care about the language or are for use with `MockScriptEngine`. This replaces most usages of the one argument ctor on `Script` with calls to `ESTestCase#mockScript` to make it clear that the tests don't need the default scripting language. I've also factored out some copy and pasted script generation code into a single place. I would have had to change that code to use `mockScript` anyway, so it was easier to perform the refactor. Relates to #16314
This commit is contained in:
parent
149629fec6
commit
bc45d10e82
|
@ -39,7 +39,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
|||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.VersionType;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.test.RandomObjects;
|
||||
|
@ -287,7 +286,7 @@ public class RequestTests extends ESTestCase {
|
|||
expectedParams.put("doc_as_upsert", "true");
|
||||
}
|
||||
} else {
|
||||
updateRequest.script(new Script("_value + 1"));
|
||||
updateRequest.script(mockScript("_value + 1"));
|
||||
updateRequest.scriptedUpsert(randomBoolean());
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
|
@ -520,7 +519,7 @@ public class RequestTests extends ESTestCase {
|
|||
{
|
||||
BulkRequest bulkRequest = new BulkRequest();
|
||||
bulkRequest.add(new DeleteRequest("index", "type", "0"));
|
||||
bulkRequest.add(new UpdateRequest("index", "type", "1").script(new Script("test")));
|
||||
bulkRequest.add(new UpdateRequest("index", "type", "1").script(mockScript("test")));
|
||||
bulkRequest.add(new DeleteRequest("index", "type", "2"));
|
||||
|
||||
Request request = Request.bulk(bulkRequest);
|
||||
|
|
|
@ -20,6 +20,9 @@
|
|||
package org.elasticsearch.index.query.functionscore;
|
||||
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptType;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
|
||||
/**
|
||||
* Static method aliases for constructors of known {@link ScoreFunctionBuilder}s.
|
||||
|
@ -69,7 +72,7 @@ public class ScoreFunctionBuilders {
|
|||
}
|
||||
|
||||
public static ScriptScoreFunctionBuilder scriptFunction(String script) {
|
||||
return (new ScriptScoreFunctionBuilder(new Script(script)));
|
||||
return (new ScriptScoreFunctionBuilder(new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, script, emptyMap())));
|
||||
}
|
||||
|
||||
public static RandomScoreFunctionBuilder randomFunction(int seed) {
|
||||
|
|
|
@ -21,9 +21,8 @@ package org.elasticsearch.common.lucene.search.function;
|
|||
|
||||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.elasticsearch.script.AbstractDoubleSearchScript;
|
||||
import org.elasticsearch.script.LeafSearchScript;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.GeneralScriptException;
|
||||
import org.elasticsearch.script.LeafSearchScript;
|
||||
import org.elasticsearch.script.SearchScript;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
|
@ -35,7 +34,7 @@ public class ScriptScoreFunctionTests extends ESTestCase {
|
|||
*/
|
||||
public void testScriptScoresReturnsNaN() throws IOException {
|
||||
// script that always returns NaN
|
||||
ScoreFunction scoreFunction = new ScriptScoreFunction(new Script("Double.NaN"), new SearchScript() {
|
||||
ScoreFunction scoreFunction = new ScriptScoreFunction(mockScript("Double.NaN"), new SearchScript() {
|
||||
@Override
|
||||
public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
|
||||
return new AbstractDoubleSearchScript() {
|
||||
|
|
|
@ -34,6 +34,7 @@ import org.elasticsearch.env.Environment;
|
|||
import org.elasticsearch.index.query.QueryParseContext;
|
||||
import org.elasticsearch.indices.IndicesModule;
|
||||
import org.elasticsearch.search.SearchModule;
|
||||
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
|
||||
import org.elasticsearch.test.AbstractQueryTestCase;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
|
@ -176,4 +177,22 @@ public abstract class BaseAggregationTestCase<AB extends AbstractAggregationBuil
|
|||
return INT_FIELD_NAME;
|
||||
}
|
||||
}
|
||||
|
||||
protected void randomFieldOrScript(ValuesSourceAggregationBuilder<?, ?> factory, String field) {
|
||||
int choice = randomInt(2);
|
||||
switch (choice) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(mockScript("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(mockScript("doc[" + field + "] + 1"));
|
||||
break;
|
||||
default:
|
||||
throw new AssertionError("Unknow random operation [" + choice + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,6 @@ package org.elasticsearch.search.aggregations.bucket;
|
|||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.automaton.RegExp;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.ChiSquare;
|
||||
|
@ -34,6 +33,7 @@ import org.elasticsearch.search.aggregations.bucket.significant.heuristics.Scrip
|
|||
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristic;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregatorFactory.ExecutionMode;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude;
|
||||
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
|
@ -54,21 +54,8 @@ public class SignificantTermsTests extends BaseAggregationTestCase<SignificantTe
|
|||
String name = randomAlphaOfLengthBetween(3, 20);
|
||||
SignificantTermsAggregationBuilder factory = new SignificantTermsAggregationBuilder(name, null);
|
||||
String field = randomAlphaOfLengthBetween(3, 20);
|
||||
int randomFieldBranch = randomInt(2);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
default:
|
||||
fail();
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
|
||||
if (randomBoolean()) {
|
||||
factory.missing("MISSING");
|
||||
}
|
||||
|
@ -179,7 +166,7 @@ public class SignificantTermsTests extends BaseAggregationTestCase<SignificantTe
|
|||
significanceHeuristic = new MutualInformation(randomBoolean(), randomBoolean());
|
||||
break;
|
||||
case 4:
|
||||
significanceHeuristic = new ScriptHeuristic(new Script("foo"));
|
||||
significanceHeuristic = new ScriptHeuristic(mockScript("foo"));
|
||||
break;
|
||||
case 5:
|
||||
significanceHeuristic = new JLHScore();
|
||||
|
|
|
@ -21,13 +21,13 @@ package org.elasticsearch.search.aggregations.bucket;
|
|||
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.automaton.RegExp;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
|
||||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregatorFactory.ExecutionMode;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.SortedSet;
|
||||
|
@ -50,21 +50,7 @@ public class TermsTests extends BaseAggregationTestCase<TermsAggregationBuilder>
|
|||
String name = randomAlphaOfLengthBetween(3, 20);
|
||||
TermsAggregationBuilder factory = new TermsAggregationBuilder(name, null);
|
||||
String field = randomAlphaOfLengthBetween(3, 20);
|
||||
int randomFieldBranch = randomInt(2);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
default:
|
||||
fail();
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
if (randomBoolean()) {
|
||||
factory.missing("MISSING");
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.search.aggregations.bucket.sampler;
|
||||
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregator.ExecutionMode;
|
||||
|
||||
|
@ -29,19 +28,7 @@ public class DiversifiedAggregationBuilderTests extends BaseAggregationTestCase<
|
|||
protected final DiversifiedAggregationBuilder createTestAggregatorBuilder() {
|
||||
DiversifiedAggregationBuilder factory = new DiversifiedAggregationBuilder("foo");
|
||||
String field = randomNumericField();
|
||||
int randomFieldBranch = randomInt(3);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
if (randomBoolean()) {
|
||||
factory.missing("MISSING");
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.support.ValuesSource;
|
||||
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
|
||||
|
@ -31,19 +30,7 @@ public abstract class AbstractNumericMetricTestCase<AF extends ValuesSourceAggre
|
|||
protected final AF createTestAggregatorBuilder() {
|
||||
AF factory = doCreateTestAggregatorFactory();
|
||||
String field = randomNumericField();
|
||||
int randomFieldBranch = randomInt(3);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
if (randomBoolean()) {
|
||||
factory.missing("MISSING");
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroidAggregationBuilder;
|
||||
|
||||
|
@ -29,19 +28,7 @@ public class GeoCentroidTests extends BaseAggregationTestCase<GeoCentroidAggrega
|
|||
protected GeoCentroidAggregationBuilder createTestAggregatorBuilder() {
|
||||
GeoCentroidAggregationBuilder factory = new GeoCentroidAggregationBuilder(randomAlphaOfLengthBetween(1, 20));
|
||||
String field = randomNumericField();
|
||||
int randomFieldBranch = randomInt(3);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
if (randomBoolean()) {
|
||||
factory.missing("0,0");
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.missing.MissingAggregationBuilder;
|
||||
|
||||
|
@ -29,19 +28,7 @@ public class MissingTests extends BaseAggregationTestCase<MissingAggregationBuil
|
|||
protected final MissingAggregationBuilder createTestAggregatorBuilder() {
|
||||
MissingAggregationBuilder factory = new MissingAggregationBuilder("foo", null);
|
||||
String field = randomNumericField();
|
||||
int randomFieldBranch = randomInt(3);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
return factory;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,19 +44,7 @@ public class PercentileRanksTests extends BaseAggregationTestCase<PercentileRank
|
|||
factory.compression(randomIntBetween(1, 50000));
|
||||
}
|
||||
String field = randomNumericField();
|
||||
int randomFieldBranch = randomInt(3);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
if (randomBoolean()) {
|
||||
factory.missing("MISSING");
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesAggregationBuilder;
|
||||
|
||||
|
@ -46,19 +45,7 @@ public class PercentilesTests extends BaseAggregationTestCase<PercentilesAggrega
|
|||
factory.compression(randomIntBetween(1, 50000));
|
||||
}
|
||||
String field = randomNumericField();
|
||||
int randomFieldBranch = randomInt(3);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
if (randomBoolean()) {
|
||||
factory.missing("MISSING");
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ public class ScriptedMetricTests extends BaseAggregationTestCase<ScriptedMetricA
|
|||
|
||||
private Script randomScript(String script) {
|
||||
if (randomBoolean()) {
|
||||
return new Script(script);
|
||||
return mockScript(script);
|
||||
} else {
|
||||
ScriptType type = randomFrom(ScriptType.values());
|
||||
return new Script(
|
||||
|
|
|
@ -90,9 +90,9 @@ public class TopHitsTests extends BaseAggregationTestCase<TopHitsAggregationBuil
|
|||
int scriptFieldsSize = randomInt(25);
|
||||
for (int i = 0; i < scriptFieldsSize; i++) {
|
||||
if (randomBoolean()) {
|
||||
factory.scriptField(randomAlphaOfLengthBetween(5, 50), new Script("foo"), randomBoolean());
|
||||
factory.scriptField(randomAlphaOfLengthBetween(5, 50), mockScript("foo"), randomBoolean());
|
||||
} else {
|
||||
factory.scriptField(randomAlphaOfLengthBetween(5, 50), new Script("foo"));
|
||||
factory.scriptField(randomAlphaOfLengthBetween(5, 50), mockScript("foo"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ public class TopHitsTests extends BaseAggregationTestCase<TopHitsAggregationBuil
|
|||
factory.sort(SortBuilders.scoreSort().order(randomFrom(SortOrder.values())));
|
||||
break;
|
||||
case 3:
|
||||
factory.sort(SortBuilders.scriptSort(new Script("foo"), ScriptSortType.NUMBER).order(randomFrom(SortOrder.values())));
|
||||
factory.sort(SortBuilders.scriptSort(mockScript("foo"), ScriptSortType.NUMBER).order(randomFrom(SortOrder.values())));
|
||||
break;
|
||||
case 4:
|
||||
factory.sort(randomAlphaOfLengthBetween(5, 20));
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
|
||||
|
||||
|
@ -29,19 +28,7 @@ public class ValueCountTests extends BaseAggregationTestCase<ValueCountAggregati
|
|||
protected final ValueCountAggregationBuilder createTestAggregatorBuilder() {
|
||||
ValueCountAggregationBuilder factory = new ValueCountAggregationBuilder("foo", null);
|
||||
String field = randomNumericField();
|
||||
int randomFieldBranch = randomInt(3);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
if (randomBoolean()) {
|
||||
factory.missing("MISSING");
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
package org.elasticsearch.search.aggregations.metrics.cardinality;
|
||||
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
|
||||
public class CardinalityTests extends BaseAggregationTestCase<CardinalityAggregationBuilder> {
|
||||
|
@ -28,19 +27,7 @@ public class CardinalityTests extends BaseAggregationTestCase<CardinalityAggrega
|
|||
protected final CardinalityAggregationBuilder createTestAggregatorBuilder() {
|
||||
CardinalityAggregationBuilder factory = new CardinalityAggregationBuilder("foo", null);
|
||||
String field = randomNumericField();
|
||||
int randomFieldBranch = randomInt(3);
|
||||
switch (randomFieldBranch) {
|
||||
case 0:
|
||||
factory.field(field);
|
||||
break;
|
||||
case 1:
|
||||
factory.field(field);
|
||||
factory.script(new Script("_value + 1"));
|
||||
break;
|
||||
case 2:
|
||||
factory.script(new Script("doc[" + field + "] + 1"));
|
||||
break;
|
||||
}
|
||||
randomFieldOrScript(factory, field);
|
||||
if (randomBoolean()) {
|
||||
factory.missing("MISSING");
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class BucketScriptTests extends BasePipelineAggregationTestCase<BucketScr
|
|||
}
|
||||
Script script;
|
||||
if (randomBoolean()) {
|
||||
script = new Script("script");
|
||||
script = mockScript("script");
|
||||
} else {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
if (randomBoolean()) {
|
||||
|
|
|
@ -40,7 +40,7 @@ public class BucketSelectorTests extends BasePipelineAggregationTestCase<BucketS
|
|||
}
|
||||
Script script;
|
||||
if (randomBoolean()) {
|
||||
script = new Script("script");
|
||||
script = mockScript("script");
|
||||
} else {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
if (randomBoolean()) {
|
||||
|
|
|
@ -43,7 +43,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
|||
|
||||
public static ScriptSortBuilder randomScriptSortBuilder() {
|
||||
ScriptSortType type = randomBoolean() ? ScriptSortType.NUMBER : ScriptSortType.STRING;
|
||||
ScriptSortBuilder builder = new ScriptSortBuilder(new Script(randomAlphaOfLengthBetween(5, 10)),
|
||||
ScriptSortBuilder builder = new ScriptSortBuilder(mockScript(randomAlphaOfLengthBetween(5, 10)),
|
||||
type);
|
||||
if (randomBoolean()) {
|
||||
builder.order(randomFrom(SortOrder.values()));
|
||||
|
@ -76,7 +76,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
|||
Script script = original.script();
|
||||
ScriptSortType type = original.type();
|
||||
if (randomBoolean()) {
|
||||
result = new ScriptSortBuilder(new Script(script.getIdOrCode() + "_suffix"), type);
|
||||
result = new ScriptSortBuilder(mockScript(script.getIdOrCode() + "_suffix"), type);
|
||||
} else {
|
||||
result = new ScriptSortBuilder(script, type.equals(ScriptSortType.NUMBER) ? ScriptSortType.STRING : ScriptSortType.NUMBER);
|
||||
}
|
||||
|
@ -251,7 +251,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
|
|||
* script sort of type {@link ScriptSortType} does not work with {@link SortMode#AVG}, {@link SortMode#MEDIAN} or {@link SortMode#SUM}
|
||||
*/
|
||||
public void testBadSortMode() throws IOException {
|
||||
ScriptSortBuilder builder = new ScriptSortBuilder(new Script("something"), ScriptSortType.STRING);
|
||||
ScriptSortBuilder builder = new ScriptSortBuilder(mockScript("something"), ScriptSortType.STRING);
|
||||
String sortMode = randomFrom(new String[] { "avg", "median", "sum" });
|
||||
Exception e = expectThrows(IllegalArgumentException.class, () -> builder.sortMode(SortMode.fromString(sortMode)));
|
||||
assertEquals("script sort of type [string] doesn't support mode [" + sortMode + "]", e.getMessage());
|
||||
|
|
|
@ -45,7 +45,7 @@ public class ScriptProcessorTests extends ESTestCase {
|
|||
int randomBytesTotal = randomBytesIn + randomBytesOut;
|
||||
|
||||
ScriptService scriptService = mock(ScriptService.class);
|
||||
Script script = new Script("_script");
|
||||
Script script = mockScript("_script");
|
||||
ExecutableScript executableScript = mock(ExecutableScript.class);
|
||||
when(scriptService.executable(any(CompiledScript.class), any())).thenReturn(executableScript);
|
||||
|
||||
|
|
|
@ -30,7 +30,6 @@ import org.elasticsearch.action.delete.DeleteRequest;
|
|||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.script.CompiledScript;
|
||||
import org.elasticsearch.script.ExecutableScript;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.junit.Before;
|
||||
import org.mockito.Matchers;
|
||||
|
@ -49,8 +48,6 @@ public abstract class AbstractAsyncBulkByScrollActionScriptTestCase<
|
|||
Response extends BulkByScrollResponse>
|
||||
extends AbstractAsyncBulkByScrollActionTestCase<Request, Response> {
|
||||
|
||||
private static final Script EMPTY_SCRIPT = new Script("");
|
||||
|
||||
protected ScriptService scriptService;
|
||||
|
||||
@Before
|
||||
|
@ -66,7 +63,7 @@ public abstract class AbstractAsyncBulkByScrollActionScriptTestCase<
|
|||
|
||||
when(scriptService.executable(any(CompiledScript.class), Matchers.<Map<String, Object>>any()))
|
||||
.thenReturn(executableScript);
|
||||
AbstractAsyncBulkByScrollAction<Request> action = action(scriptService, request().setScript(EMPTY_SCRIPT));
|
||||
AbstractAsyncBulkByScrollAction<Request> action = action(scriptService, request().setScript(mockScript("")));
|
||||
RequestWrapper<?> result = action.buildScriptApplier().apply(AbstractAsyncBulkByScrollAction.wrap(index), doc);
|
||||
return (result != null) ? (T) result.self() : null;
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ import org.elasticsearch.action.index.IndexRequest;
|
|||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.index.reindex.remote.RemoteInfo;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.slice.SliceBuilder;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
|
@ -71,7 +70,7 @@ public class ReindexRequestTests extends AbstractBulkByScrollRequestTestCase<Rei
|
|||
@Override
|
||||
protected void extraRandomizationForSlice(ReindexRequest original) {
|
||||
if (randomBoolean()) {
|
||||
original.setScript(new Script(randomAlphaOfLength(5)));
|
||||
original.setScript(mockScript(randomAlphaOfLength(5)));
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
original.setRemoteInfo(new RemoteInfo(randomAlphaOfLength(5), randomAlphaOfLength(5), between(1, 10000),
|
||||
|
|
|
@ -22,7 +22,6 @@ package org.elasticsearch.index.reindex;
|
|||
import org.elasticsearch.action.bulk.byscroll.AbstractBulkByScrollRequestTestCase;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.script.Script;
|
||||
|
||||
import static org.apache.lucene.util.TestUtil.randomSimpleString;
|
||||
|
||||
|
@ -68,7 +67,7 @@ public class UpdateByQueryRequestTests extends AbstractBulkByScrollRequestTestCa
|
|||
@Override
|
||||
protected void extraRandomizationForSlice(UpdateByQueryRequest original) {
|
||||
if (randomBoolean()) {
|
||||
original.setScript(new Script(randomAlphaOfLength(5)));
|
||||
original.setScript(mockScript(randomAlphaOfLength(5)));
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
original.setPipeline(randomAlphaOfLength(5));
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
|
|||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptType;
|
||||
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.search.collapse.CollapseBuilder;
|
||||
|
@ -50,8 +51,10 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static org.elasticsearch.test.ESTestCase.between;
|
||||
import static org.elasticsearch.test.ESTestCase.generateRandomStringArray;
|
||||
import static org.elasticsearch.test.ESTestCase.mockScript;
|
||||
import static org.elasticsearch.test.ESTestCase.randomAlphaOfLengthBetween;
|
||||
import static org.elasticsearch.test.ESTestCase.randomBoolean;
|
||||
import static org.elasticsearch.test.ESTestCase.randomByte;
|
||||
|
@ -164,9 +167,9 @@ public class RandomSearchRequestGenerator {
|
|||
int scriptFieldsSize = randomInt(25);
|
||||
for (int i = 0; i < scriptFieldsSize; i++) {
|
||||
if (randomBoolean()) {
|
||||
builder.scriptField(randomAlphaOfLengthBetween(5, 50), new Script("foo"), randomBoolean());
|
||||
builder.scriptField(randomAlphaOfLengthBetween(5, 50), mockScript("foo"), randomBoolean());
|
||||
} else {
|
||||
builder.scriptField(randomAlphaOfLengthBetween(5, 50), new Script("foo"));
|
||||
builder.scriptField(randomAlphaOfLengthBetween(5, 50), mockScript("foo"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -242,8 +245,11 @@ public class RandomSearchRequestGenerator {
|
|||
builder.sort(SortBuilders.scoreSort().order(randomFrom(SortOrder.values())));
|
||||
break;
|
||||
case 3:
|
||||
builder.sort(SortBuilders.scriptSort(new Script("foo"),
|
||||
ScriptSortBuilder.ScriptSortType.NUMBER).order(randomFrom(SortOrder.values())));
|
||||
builder.sort(SortBuilders
|
||||
.scriptSort(
|
||||
new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, "foo", emptyMap()),
|
||||
ScriptSortBuilder.ScriptSortType.NUMBER)
|
||||
.order(randomFrom(SortOrder.values())));
|
||||
break;
|
||||
case 4:
|
||||
builder.sort(randomAlphaOfLengthBetween(5, 20));
|
||||
|
|
|
@ -29,6 +29,7 @@ import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
|
|||
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
|
||||
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
|
||||
import com.carrotsearch.randomizedtesting.rules.TestRuleAdapter;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
@ -90,8 +91,10 @@ import org.elasticsearch.indices.analysis.AnalysisModule;
|
|||
import org.elasticsearch.plugins.AnalysisPlugin;
|
||||
import org.elasticsearch.plugins.MapperPlugin;
|
||||
import org.elasticsearch.script.MockScriptEngine;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptModule;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.script.ScriptType;
|
||||
import org.elasticsearch.search.MockSearchService;
|
||||
import org.elasticsearch.test.junit.listeners.LoggingListener;
|
||||
import org.elasticsearch.test.junit.listeners.ReproduceInfoPrinter;
|
||||
|
@ -133,6 +136,7 @@ import java.util.stream.Collectors;
|
|||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
|
@ -1083,6 +1087,14 @@ public abstract class ESTestCase extends LuceneTestCase {
|
|||
return new NamedXContentRegistry(ClusterModule.getNamedXWriteables());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a "mock" script for use either with {@link MockScriptEngine} or anywhere where you need a script but don't really care about
|
||||
* its contents.
|
||||
*/
|
||||
public static final Script mockScript(String id) {
|
||||
return new Script(ScriptType.INLINE, MockScriptEngine.NAME, id, emptyMap());
|
||||
}
|
||||
|
||||
/** Returns the suite failure marker: internal use only! */
|
||||
public static TestRuleMarkFailure getSuiteFailureMarker() {
|
||||
return suiteFailureMarker;
|
||||
|
|
Loading…
Reference in New Issue