Search in sources :

Example 11 with XContent

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_"));
}
Also used : XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 12 with XContent

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 13 with XContent

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());
    }
}
Also used : XContent(org.elasticsearch.common.xcontent.XContent) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IOException(java.io.IOException)

Aggregations

XContent (org.elasticsearch.common.xcontent.XContent)13 XContentParser (org.elasticsearch.common.xcontent.XContentParser)13 ToXContent (org.elasticsearch.common.xcontent.ToXContent)10 BytesReference (org.elasticsearch.common.bytes.BytesReference)7 IOException (java.io.IOException)4 RoutingMissingException (org.elasticsearch.action.RoutingMissingException)4 ParsingException (org.elasticsearch.common.ParsingException)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 EOFException (java.io.EOFException)2 FileNotFoundException (java.io.FileNotFoundException)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 NoShardAvailableActionException (org.elasticsearch.action.NoShardAvailableActionException)2 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)2 BroadcastShardOperationFailedException (org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException)2 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)2 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)2 CircuitBreakingException (org.elasticsearch.common.breaker.CircuitBreakingException)2 Index (org.elasticsearch.index.Index)2 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)2 QueryShardException (org.elasticsearch.index.query.QueryShardException)2