Search in sources :

Example 71 with XContentParser

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());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) BytesArray(org.opensearch.common.bytes.BytesArray) XContentType(org.opensearch.common.xcontent.XContentType) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 72 with XContentParser

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());
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 73 with XContentParser

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"));
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) AnalyzeAction(org.opensearch.action.admin.indices.analyze.AnalyzeAction) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 74 with XContentParser

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"));
    }
}
Also used : XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 75 with XContentParser

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]"));
    }
}
Also used : XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

XContentParser (org.opensearch.common.xcontent.XContentParser)694 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)245 IOException (java.io.IOException)127 BytesReference (org.opensearch.common.bytes.BytesReference)117 ParsingException (org.opensearch.common.ParsingException)111 XContentType (org.opensearch.common.xcontent.XContentType)106 ArrayList (java.util.ArrayList)80 List (java.util.List)56 OpenSearchParseException (org.opensearch.OpenSearchParseException)55 Matchers.containsString (org.hamcrest.Matchers.containsString)51 HashMap (java.util.HashMap)49 Map (java.util.Map)46 ToXContent (org.opensearch.common.xcontent.ToXContent)45 XContentParseException (org.opensearch.common.xcontent.XContentParseException)40 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)35 SearchResponse (org.opensearch.action.search.SearchResponse)28 BytesArray (org.opensearch.common.bytes.BytesArray)26 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)25 GetRequest (org.opensearch.action.get.GetRequest)24 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)24