use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class HighlightFieldTests method testFromXContent.
public void testFromXContent() throws IOException {
HighlightField highlightField = createTestItem();
XContentType xcontentType = randomFrom(XContentType.values());
XContentBuilder builder = XContentFactory.contentBuilder(xcontentType);
if (randomBoolean()) {
builder.prettyPrint();
}
// we need to wrap xContent output in proper object to create a parser for it
builder.startObject();
builder = highlightField.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
XContentParser parser = createParser(builder);
// skip to the opening object token, fromXContent advances from here and starts with the field name
parser.nextToken();
parser.nextToken();
HighlightField parsedField = HighlightField.fromXContent(parser);
assertEquals(highlightField, parsedField);
if (highlightField.fragments() != null) {
assertEquals(XContentParser.Token.END_ARRAY, parser.currentToken());
}
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class SearchProfileShardResultsTests method testFromXContent.
public void testFromXContent() throws IOException {
SearchProfileShardResults shardResult = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(shardResult, xContentType, humanReadable);
SearchProfileShardResults parsed = null;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser::getTokenLocation);
ensureFieldName(parser, parser.nextToken(), SearchProfileShardResults.PROFILE_FIELD);
ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser::getTokenLocation);
parsed = SearchProfileShardResults.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class AggregationProfileShardResultTests method testFromXContent.
public void testFromXContent() throws IOException {
AggregationProfileShardResult profileResult = createTestItem(2);
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(profileResult, xContentType, humanReadable);
AggregationProfileShardResult parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
XContentParserUtils.ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser::getTokenLocation);
XContentParserUtils.ensureFieldName(parser, parser.nextToken(), AggregationProfileShardResult.AGGREGATIONS);
XContentParserUtils.ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_ARRAY, parser::getTokenLocation);
parsed = AggregationProfileShardResult.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class CollectorResultTests method testFromXContent.
public void testFromXContent() throws IOException {
CollectorResult collectorResult = createTestItem(1);
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(collectorResult, xContentType, humanReadable);
CollectorResult parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
parsed = CollectorResult.fromXContent(parser);
assertNull(parser.nextToken());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
use of org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class SuggestTests method testFromXContent.
public void testFromXContent() throws IOException {
ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
Suggest suggest = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(suggest, xContentType, params, humanReadable);
Suggest parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
ensureFieldName(parser, parser.nextToken(), Suggest.NAME);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
parsed = Suggest.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertEquals(suggest.size(), parsed.size());
for (Suggestion suggestion : suggest) {
Suggestion<? extends Entry<? extends Option>> parsedSuggestion = parsed.getSuggestion(suggestion.getName());
assertNotNull(parsedSuggestion);
assertEquals(suggestion.getClass(), parsedSuggestion.getClass());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, params, humanReadable), xContentType);
}
Aggregations