use of org.opensearch.script.ScriptContextInfo.ScriptMethodInfo in project OpenSearch by opensearch-project.
the class ScriptContextInfoTests method testGetterConditional.
public void testGetterConditional() {
Set<ScriptMethodInfo> getters = new ScriptContextInfo("getter_conditional", GetterConditional.class).getters;
assertEquals(2, getters.size());
HashMap<String, String> methods = new HashMap<String, String>() {
{
put("getNonDefault1", "boolean");
put("getNonDefault2", "float");
}
};
for (ScriptContextInfo.ScriptMethodInfo method : getters) {
String returnType = methods.remove(method.name);
assertNotNull(returnType);
assertEquals(returnType, method.returnType);
}
assertEquals(0, methods.size());
}
use of org.opensearch.script.ScriptContextInfo.ScriptMethodInfo in project OpenSearch by opensearch-project.
the class ScriptMethodInfoSerializingTests method randomGetterInstances.
static Set<ScriptMethodInfo> randomGetterInstances() {
Set<String> suffixes = new HashSet<>();
int numGetters = randomIntBetween(0, MAX_LENGTH);
Set<ScriptMethodInfo> getters = new HashSet<>(numGetters);
for (int i = 0; i < numGetters; i++) {
String suffix = randomValueOtherThanMany(suffixes::contains, () -> randomAlphaOfLengthBetween(MIN_LENGTH, MAX_LENGTH));
suffixes.add(suffix);
getters.add(new ScriptMethodInfo(GET_PREFIX + suffix, randomAlphaOfLengthBetween(MIN_LENGTH, MAX_LENGTH), Collections.unmodifiableList(new ArrayList<>())));
}
return Collections.unmodifiableSet(getters);
}
use of org.opensearch.script.ScriptContextInfo.ScriptMethodInfo in project OpenSearch by opensearch-project.
the class ScriptContextInfoTests method testScriptContextInfoParser.
public void testScriptContextInfoParser() throws IOException {
String json = "{" + " \"name\": \"similarity\"," + " \"methods\": [" + " {" + " \"name\": \"execute\"," + " \"return_type\": \"double\"," + " \"params\": [" + " {" + " \"type\": \"double\"," + " \"name\": \"weight\"" + " }," + " {" + " \"type\": \"org.opensearch.index.similarity.ScriptedSimilarity$Query\"," + " \"name\": \"query\"" + " }," + " {" + " \"type\": \"org.opensearch.index.similarity.ScriptedSimilarity$Field\"," + " \"name\": \"field\"" + " }," + " {" + " \"type\": \"org.opensearch.index.similarity.ScriptedSimilarity$Term\"," + " \"name\": \"term\"" + " }," + " {" + " \"type\": \"org.opensearch.index.similarity.ScriptedSimilarity$Doc\"," + " \"name\": \"doc\"" + " }" + " ]" + " }," + " {" + " \"name\": \"getParams\"," + " \"return_type\": \"java.util.Map\"," + " \"params\": []" + " }," + " {" + " \"name\": \"getDoc\"," + " \"return_type\": \"java.util.Map\"," + " \"params\": []" + " }," + " {" + " \"name\": \"get_score\"," + " \"return_type\": \"double\"," + " \"params\": []" + " }" + " ]" + "}";
XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(json).streamInput());
ScriptContextInfo parsed = ScriptContextInfo.fromXContent(parser);
ScriptContextInfo expected = new ScriptContextInfo("similarity", new ScriptMethodInfo("execute", "double", Collections.unmodifiableList(Arrays.asList(new ParameterInfo("double", "weight"), new ParameterInfo("org.opensearch.index.similarity.ScriptedSimilarity$Query", "query"), new ParameterInfo("org.opensearch.index.similarity.ScriptedSimilarity$Field", "field"), new ParameterInfo("org.opensearch.index.similarity.ScriptedSimilarity$Term", "term"), new ParameterInfo("org.opensearch.index.similarity.ScriptedSimilarity$Doc", "doc")))), Collections.unmodifiableSet(new HashSet<>(Arrays.asList(new ScriptMethodInfo("getParams", "java.util.Map", new ArrayList<>()), new ScriptMethodInfo("getDoc", "java.util.Map", new ArrayList<>()), new ScriptMethodInfo("get_score", "double", new ArrayList<>())))));
assertEquals(expected, parsed);
}
use of org.opensearch.script.ScriptContextInfo.ScriptMethodInfo in project OpenSearch by opensearch-project.
the class ScriptContextInfoTests method testIgnoreOtherMethodsInListConstructor.
public void testIgnoreOtherMethodsInListConstructor() {
ScriptContextInfo constructed = new ScriptContextInfo("otherNames", Collections.unmodifiableList(Arrays.asList(new ScriptMethodInfo("execute", "double", Collections.emptyList()), new ScriptMethodInfo("otherName", "bool", Collections.emptyList()))));
ScriptContextInfo expected = new ScriptContextInfo("otherNames", new ScriptMethodInfo("execute", "double", Collections.emptyList()), Collections.emptySet());
assertEquals(expected, constructed);
}
use of org.opensearch.script.ScriptContextInfo.ScriptMethodInfo in project OpenSearch by opensearch-project.
the class ScriptContextInfoTests method testMultipleExecuteInListConstructor.
public void testMultipleExecuteInListConstructor() {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new ScriptContextInfo("multiexecute", Collections.unmodifiableList(Arrays.asList(new ScriptMethodInfo("execute", "double", Collections.emptyList()), new ScriptMethodInfo("execute", "double", Collections.unmodifiableList(Arrays.asList(new ParameterInfo("double", "weight"))))))));
assertEquals("Cannot have multiple [execute] methods in [multiexecute], found [2]", e.getMessage());
}
Aggregations