use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class PipelineConfigurationTests method testParser.
public void testParser() throws IOException {
ContextParser<Void, PipelineConfiguration> parser = PipelineConfiguration.getParser();
XContentType xContentType = randomFrom(XContentType.values());
final BytesReference bytes;
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
new PipelineConfiguration("1", new BytesArray("{}".getBytes(StandardCharsets.UTF_8)), XContentType.JSON).toXContent(builder, ToXContent.EMPTY_PARAMS);
bytes = BytesReference.bytes(builder);
}
XContentParser xContentParser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput());
PipelineConfiguration parsed = parser.parse(xContentParser, null);
assertEquals(xContentType, parsed.getXContentType());
assertEquals("{}", XContentHelper.convertToJson(parsed.getConfig(), false, parsed.getXContentType()));
assertEquals("1", parsed.getId());
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class BytesRestResponseTests method testNoErrorFromXContent.
public void testNoErrorFromXContent() throws IOException {
IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
try (XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.values()).xContent())) {
builder.startObject();
builder.field("status", randomFrom(RestStatus.values()).getStatus());
builder.endObject();
try (XContentParser parser = createParser(builder.contentType().xContent(), BytesReference.bytes(builder))) {
BytesRestResponse.errorFromXContent(parser);
}
}
});
assertEquals("Failed to parse opensearch status exception: no exception was found", e.getMessage());
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class RestAnalyzeActionTests method testParseXContentForAnalyzeRequestWithCustomFilters.
public void testParseXContentForAnalyzeRequestWithCustomFilters() throws Exception {
try (XContentParser content = createParser(XContentFactory.jsonBuilder().startObject().field("text", "THIS IS A TEST").field("tokenizer", "keyword").startArray("filter").value("lowercase").startObject().field("type", "stop").array("stopwords", "foo", "buzz").endObject().endArray().startArray("char_filter").startObject().field("type", "mapping").array("mappings", "ph => f", "qu => q").endObject().endArray().field("normalizer", "normalizer").endObject())) {
AnalyzeAction.Request analyzeRequest = AnalyzeAction.Request.fromXContent(content, "for test");
assertThat(analyzeRequest.text().length, equalTo(1));
assertThat(analyzeRequest.text(), equalTo(new String[] { "THIS IS A TEST" }));
assertThat(analyzeRequest.tokenizer().name, equalTo("keyword"));
assertThat(analyzeRequest.tokenFilters().size(), equalTo(2));
assertThat(analyzeRequest.tokenFilters().get(0).name, equalTo("lowercase"));
assertThat(analyzeRequest.tokenFilters().get(1).definition, notNullValue());
assertThat(analyzeRequest.charFilters().size(), equalTo(1));
assertThat(analyzeRequest.charFilters().get(0).definition, notNullValue());
assertThat(analyzeRequest.normalizer(), equalTo("normalizer"));
}
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class RestAnalyzeActionTests method testParseXContentForAnalyzeRequestWithInvalidNormalizerThrowsException.
public void testParseXContentForAnalyzeRequestWithInvalidNormalizerThrowsException() throws Exception {
try (XContentParser invalidExplain = createParser(XContentFactory.jsonBuilder().startObject().field("normalizer", true).endObject())) {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> AnalyzeAction.Request.fromXContent(invalidExplain, "for test"));
assertThat(e.getMessage(), containsString("normalizer doesn't support values of type: VALUE_BOOLEAN"));
}
}
use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.
the class RestAnalyzeActionTests method testParseXContentForAnalyzeRequestWithUnknownParamThrowsException.
public void testParseXContentForAnalyzeRequestWithUnknownParamThrowsException() throws Exception {
try (XContentParser invalidContent = createParser(XContentFactory.jsonBuilder().startObject().field("text", "THIS IS A TEST").field("unknown", "keyword").endObject())) {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> AnalyzeAction.Request.fromXContent(invalidContent, "for test"));
assertThat(e.getMessage(), containsString("unknown field [unknown]"));
}
}
Aggregations