Search in sources :

Example 61 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

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 = builder.bytes();
    }
    XContentParser xContentParser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, bytes);
    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.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 62 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class QueryProfileShardResultTests method testFromXContent.

public void testFromXContent() throws IOException {
    QueryProfileShardResult profileResult = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(profileResult, xContentType, humanReadable);
    QueryProfileShardResult parsed;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        XContentParserUtils.ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser::getTokenLocation);
        parsed = QueryProfileShardResult.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 63 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class ProfileResultTests method testFromXContent.

public void testFromXContent() throws IOException {
    ProfileResult profileResult = createTestItem(2);
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(profileResult, xContentType, humanReadable);
    ProfileResult parsed;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        parsed = ProfileResult.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertEquals(profileResult.getTime(), parsed.getTime());
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 64 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class SuggestionOptionTests method testFromXContent.

public void testFromXContent() throws IOException {
    Option option = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(option, xContentType, humanReadable);
    Option parsed;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        parsed = Option.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertEquals(option.getText(), parsed.getText());
    assertEquals(option.getHighlighted(), parsed.getHighlighted());
    assertEquals(option.getScore(), parsed.getScore(), Float.MIN_VALUE);
    assertEquals(option.collateMatch(), parsed.collateMatch());
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 65 with XContentType

use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class CompletionSuggestionOptionTests method testFromXContent.

public void testFromXContent() throws IOException {
    Option option = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(option, xContentType, humanReadable);
    if (randomBoolean()) {
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
        }
    }
    Option parsed;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        parsed = Option.fromXContent(parser);
        assertNull(parser.nextToken());
    }
    assertEquals(option.getText(), parsed.getText());
    assertEquals(option.getHighlighted(), parsed.getHighlighted());
    assertEquals(option.getScore(), parsed.getScore(), Float.MIN_VALUE);
    assertEquals(option.collateMatch(), parsed.collateMatch());
    assertEquals(option.getContexts(), parsed.getContexts());
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)82 BytesReference (org.elasticsearch.common.bytes.BytesReference)48 XContentParser (org.elasticsearch.common.xcontent.XContentParser)42 Map (java.util.Map)19 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)19 HashMap (java.util.HashMap)18 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 BytesRef (org.apache.lucene.util.BytesRef)8 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 Collections.emptyMap (java.util.Collections.emptyMap)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)6 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 GetRequest (org.elasticsearch.action.get.GetRequest)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5