Search in sources :

Example 6 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class CategoryContextMappingTests method testQueryContextParsingMixedTypeValuesArrayHavingNULL.

public void testQueryContextParsingMixedTypeValuesArrayHavingNULL() throws Exception {
    XContentBuilder builder = jsonBuilder().startArray().value("context1").value("context2").value(true).value(10).nullValue().endArray();
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder))) {
        CategoryContextMapping mapping = ContextBuilder.category("cat").build();
        XContentParseException e = expectThrows(XContentParseException.class, () -> mapping.parseQueryContext(parser));
        assertThat(e.getMessage(), containsString("category context must be an object, string, number or boolean"));
    }
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 7 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class ScriptSortBuilderTests method testParseBadFieldNameExceptions.

public void testParseBadFieldNameExceptions() throws IOException {
    String scriptSort = "{\"_script\" : {" + "\"bad_field\" : \"number\"" + "} }";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort)) {
        parser.nextToken();
        parser.nextToken();
        parser.nextToken();
        XContentParseException e = expectThrows(XContentParseException.class, () -> ScriptSortBuilder.fromXContent(parser, null));
        assertEquals("[1:15] [_script] unknown field [bad_field]", e.getMessage());
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 8 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class ScriptSortBuilderTests method testParseBadFieldNameExceptionsOnStartObject.

public void testParseBadFieldNameExceptionsOnStartObject() throws IOException {
    String scriptSort = "{\"_script\" : {" + "\"bad_field\" : { \"order\" : \"asc\" } } }";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort)) {
        parser.nextToken();
        parser.nextToken();
        parser.nextToken();
        XContentParseException e = expectThrows(XContentParseException.class, () -> ScriptSortBuilder.fromXContent(parser, null));
        assertEquals("[1:15] [_script] unknown field [bad_field]", e.getMessage());
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 9 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class HighlightBuilderTests method testTwoFieldsInObjectInFieldsArray.

public void testTwoFieldsInObjectInFieldsArray() throws IOException {
    XContentParseException e = expectParseThrows(XContentParseException.class, "{\n" + "  \"fields\" : [ {\n" + "     \"body\" : {},\n" + "     \"nope\" : {}\n" + "   }] \n" + "}\n");
    assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]"));
    assertThat(e.getCause().getMessage(), containsString("[fields] can be a single object with any number of fields " + "or an array where each entry is an object with a single field"));
}
Also used : XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 10 with XContentParseException

use of org.opensearch.common.xcontent.XContentParseException in project OpenSearch by opensearch-project.

the class HighlightBuilderTests method testParsingTagsSchema.

/**
 * `tags_schema` is not produced by toXContent in the builder but should be parseable, so this
 * adds a simple json test for this.
 */
public void testParsingTagsSchema() throws IOException {
    String highlightElement = "{\n" + "    \"tags_schema\" : \"styled\"\n" + "}\n";
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement)) {
        HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(parser);
        assertArrayEquals("setting tags_schema 'styled' should alter pre_tags", HighlightBuilder.DEFAULT_STYLED_PRE_TAG, highlightBuilder.preTags());
        assertArrayEquals("setting tags_schema 'styled' should alter post_tags", HighlightBuilder.DEFAULT_STYLED_POST_TAGS, highlightBuilder.postTags());
        highlightElement = "{\n" + "    \"tags_schema\" : \"default\"\n" + "}\n";
    }
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement)) {
        HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(parser);
        assertArrayEquals("setting tags_schema 'default' should alter pre_tags", HighlightBuilder.DEFAULT_PRE_TAGS, highlightBuilder.preTags());
        assertArrayEquals("setting tags_schema 'default' should alter post_tags", HighlightBuilder.DEFAULT_POST_TAGS, highlightBuilder.postTags());
        XContentParseException e = expectParseThrows(XContentParseException.class, "{\n" + "    \"tags_schema\" : \"somthing_else\"\n" + "}\n");
        assertThat(e.getMessage(), containsString("[highlight] failed to parse field [tags_schema]"));
        assertEquals("Unknown tag schema [somthing_else]", e.getCause().getMessage());
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Aggregations

XContentParseException (org.opensearch.common.xcontent.XContentParseException)41 XContentParser (org.opensearch.common.xcontent.XContentParser)32 Matchers.containsString (org.hamcrest.Matchers.containsString)11 XContentType (org.opensearch.common.xcontent.XContentType)8 BytesReference (org.opensearch.common.bytes.BytesReference)7 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 ParsingException (org.opensearch.common.ParsingException)4 CategoryContextMapping (org.opensearch.search.suggest.completion.context.CategoryContextMapping)4 IOException (java.io.IOException)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 HasAttributeNodeSelector (org.opensearch.client.HasAttributeNodeSelector)2 Node (org.opensearch.client.Node)2 NodeSelector (org.opensearch.client.NodeSelector)2 InputStream (java.io.InputStream)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ContentTooLongException (org.apache.http.ContentTooLongException)1 HttpEntity (org.apache.http.HttpEntity)1 LegacyESVersion (org.opensearch.LegacyESVersion)1