use of org.graylog.shaded.elasticsearch7.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);
}
use of org.graylog.shaded.elasticsearch7.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);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class SuggestionTests method testFromXContentFailsWithoutTypeParam.
/**
* test that we throw error if RestSearchAction.TYPED_KEYS_PARAM isn't set while rendering xContent
*/
public void testFromXContentFailsWithoutTypeParam() throws IOException {
XContentType xContentType = randomFrom(XContentType.values());
BytesReference originalBytes = toXContent(createTestItem(), xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
ParsingException e = expectThrows(ParsingException.class, () -> Suggestion.fromXContent(parser));
assertEquals("Cannot parse suggestion response without type information. " + "Set [typed_keys] parameter on the request to ensure the type information " + "is added to the response output", e.getMessage());
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class JsonXContentGenerator method writeRawField.
@Override
public final void writeRawField(String name, BytesReference content) throws IOException {
XContentType contentType = XContentFactory.xContentType(content);
if (contentType == null) {
throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
}
writeRawField(name, content, contentType);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class JsonXContentGenerator method writeRawValue.
@Override
public final void writeRawValue(BytesReference content) throws IOException {
XContentType contentType = XContentFactory.xContentType(content);
if (contentType == null) {
throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
}
writeRawValue(content, contentType);
}
Aggregations