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