use of org.elasticsearch.common.xcontent.XContent in project elasticsearch by elastic.
the class ElasticsearchExceptionTests method testFromXContentWithCause.
public void testFromXContentWithCause() throws IOException {
ElasticsearchException e = new ElasticsearchException("foo", new ElasticsearchException("bar", new ElasticsearchException("baz", new RoutingMissingException("_test", "_type", "_id"))));
final XContent xContent = randomFrom(XContentType.values()).xContent();
XContentBuilder builder = XContentBuilder.builder(xContent).startObject().value(e).endObject();
ElasticsearchException parsed;
try (XContentParser parser = createParser(builder)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
parsed = ElasticsearchException.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertNull(parser.nextToken());
}
assertNotNull(parsed);
assertEquals(parsed.getMessage(), "Elasticsearch exception [type=exception, reason=foo]");
ElasticsearchException cause = (ElasticsearchException) parsed.getCause();
assertEquals(cause.getMessage(), "Elasticsearch exception [type=exception, reason=bar]");
cause = (ElasticsearchException) cause.getCause();
assertEquals(cause.getMessage(), "Elasticsearch exception [type=exception, reason=baz]");
cause = (ElasticsearchException) cause.getCause();
assertEquals(cause.getMessage(), "Elasticsearch exception [type=routing_missing_exception, reason=routing is required for [_test]/[_type]/[_id]]");
assertThat(cause.getHeaderKeys(), hasSize(0));
assertThat(cause.getMetadataKeys(), hasSize(2));
assertThat(cause.getMetadata("es.index"), hasItem("_test"));
assertThat(cause.getMetadata("es.index_uuid"), hasItem("_na_"));
}
use of org.elasticsearch.common.xcontent.XContent in project elasticsearch by elastic.
the class ElasticsearchExceptionTests method testThrowableToAndFromXContent.
public void testThrowableToAndFromXContent() throws IOException {
final XContent xContent = randomFrom(XContentType.values()).xContent();
final Tuple<Throwable, ElasticsearchException> exceptions = randomExceptions();
final Throwable throwable = exceptions.v1();
BytesReference throwableBytes = XContentHelper.toXContent((builder, params) -> {
ElasticsearchException.generateThrowableXContent(builder, params, throwable);
return builder;
}, xContent.type(), randomBoolean());
ElasticsearchException parsedException;
try (XContentParser parser = createParser(xContent, throwableBytes)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
parsedException = ElasticsearchException.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertNull(parser.nextToken());
}
assertDeepEquals(exceptions.v2(), parsedException);
}
use of org.elasticsearch.common.xcontent.XContent in project elasticsearch by elastic.
the class AbstractFilteringJsonGeneratorTestCase method assertBinary.
protected void assertBinary(XContentBuilder expected, XContentBuilder builder) {
assertNotNull(builder);
assertNotNull(expected);
try {
XContent xContent = XContentFactory.xContent(builder.contentType());
XContentParser jsonParser = createParser(xContent, expected.bytes());
XContentParser testParser = createParser(xContent, builder.bytes());
while (true) {
XContentParser.Token token1 = jsonParser.nextToken();
XContentParser.Token token2 = testParser.nextToken();
if (token1 == null) {
assertThat(token2, nullValue());
return;
}
assertThat(token1, equalTo(token2));
switch(token1) {
case FIELD_NAME:
assertThat(jsonParser.currentName(), equalTo(testParser.currentName()));
break;
case VALUE_STRING:
assertThat(jsonParser.text(), equalTo(testParser.text()));
break;
case VALUE_NUMBER:
assertThat(jsonParser.numberType(), equalTo(testParser.numberType()));
assertThat(jsonParser.numberValue(), equalTo(testParser.numberValue()));
break;
}
}
} catch (Exception e) {
fail("Fail to verify the result of the XContentBuilder: " + e.getMessage());
}
}
Aggregations