Search in sources :

Example 1 with ScriptMethodInfo

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());
}
Also used : HashMap(java.util.HashMap) ScriptMethodInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo) ScriptMethodInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo)

Example 2 with ScriptMethodInfo

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);
}
Also used : HashSet(java.util.HashSet) ScriptMethodInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo)

Example 3 with ScriptMethodInfo

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);
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) ParameterInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo.ParameterInfo) XContentParser(org.opensearch.common.xcontent.XContentParser) ScriptMethodInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo) HashSet(java.util.HashSet)

Example 4 with ScriptMethodInfo

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);
}
Also used : ScriptMethodInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo)

Example 5 with ScriptMethodInfo

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());
}
Also used : ParameterInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo.ParameterInfo) ScriptMethodInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo)

Aggregations

ScriptMethodInfo (org.opensearch.script.ScriptContextInfo.ScriptMethodInfo)5 HashSet (java.util.HashSet)2 ParameterInfo (org.opensearch.script.ScriptContextInfo.ScriptMethodInfo.ParameterInfo)2 HashMap (java.util.HashMap)1 BytesArray (org.opensearch.common.bytes.BytesArray)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1