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"));
}
}
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());
}
}
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());
}
}
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"));
}
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());
}
}
Aggregations