Search in sources :

Example 31 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method testFromXContentWithIgnoredMetadataAndHeaders.

/**
     * Test that some values like arrays of numbers are ignored when parsing back
     * an exception.
     */
public void testFromXContentWithIgnoredMetadataAndHeaders() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    // The exception content to parse is built using a XContentBuilder
    // because the current Java API does not allow to add metadata/headers
    // of other types than list of strings.
    BytesReference originalBytes;
    try (XContentBuilder builder = XContentBuilder.builder(xContent)) {
        builder.startObject().field("metadata_int", 1).array("metadata_array_of_ints", new int[] { 8, 13, 21 }).field("reason", "Custom reason").array("metadata_array_of_boolean", new boolean[] { false, false }).startArray("metadata_array_of_objects").startObject().field("object_array_one", "value_one").endObject().startObject().field("object_array_two", "value_two").endObject().endArray().field("type", "custom_exception").field("metadata_long", 1L).array("metadata_array_of_longs", new long[] { 2L, 3L, 5L }).field("metadata_other", "some metadata").startObject("header").field("header_string", "some header").array("header_array_of_strings", new String[] { "foo", "bar", "baz" }).endObject().startObject("metadata_object").field("object_field", "value").endObject().endObject();
        originalBytes = builder.bytes();
    }
    ElasticsearchException parsedException;
    try (XContentParser parser = createParser(xContent, originalBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        parsedException = ElasticsearchException.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertNotNull(parsedException);
    assertEquals("Elasticsearch exception [type=custom_exception, reason=Custom reason]", parsedException.getMessage());
    assertEquals(2, parsedException.getHeaderKeys().size());
    assertThat(parsedException.getHeader("header_string"), hasItem("some header"));
    assertThat(parsedException.getHeader("header_array_of_strings"), hasItems("foo", "bar", "baz"));
    assertEquals(1, parsedException.getMetadataKeys().size());
    assertThat(parsedException.getMetadata("es.metadata_other"), hasItem("some metadata"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 32 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class BulkRequestTests method testSmileIsSupported.

public void testSmileIsSupported() throws IOException {
    XContentType xContentType = XContentType.SMILE;
    BytesReference data;
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
            builder.startObject();
            builder.startObject("index");
            builder.field("_index", "index");
            builder.field("_type", "type");
            builder.field("_id", "test");
            builder.endObject();
            builder.endObject();
        }
        out.write(xContentType.xContent().streamSeparator());
        try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, out)) {
            builder.startObject();
            builder.field("field", "value");
            builder.endObject();
        }
        out.write(xContentType.xContent().streamSeparator());
        data = out.bytes();
    }
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(data, null, null, xContentType);
    assertEquals(1, bulkRequest.requests().size());
    DocWriteRequest docWriteRequest = bulkRequest.requests().get(0);
    assertEquals(DocWriteRequest.OpType.INDEX, docWriteRequest.opType());
    assertEquals("index", docWriteRequest.index());
    assertEquals("type", docWriteRequest.type());
    assertEquals("test", docWriteRequest.id());
    assertThat(docWriteRequest, instanceOf(IndexRequest.class));
    IndexRequest request = (IndexRequest) docWriteRequest;
    assertEquals(1, request.sourceAsMap().size());
    assertEquals("value", request.sourceAsMap().get("field"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 33 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class BulkItemResponseTests method testToAndFromXContent.

public void testToAndFromXContent() throws IOException {
    final XContentType xContentType = randomFrom(XContentType.values());
    for (DocWriteRequest.OpType opType : DocWriteRequest.OpType.values()) {
        int bulkItemId = randomIntBetween(0, 100);
        boolean humanReadable = randomBoolean();
        Tuple<? extends DocWriteResponse, ? extends DocWriteResponse> randomDocWriteResponses = null;
        if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
            randomDocWriteResponses = IndexResponseTests.randomIndexResponse();
        } else if (opType == DocWriteRequest.OpType.DELETE) {
            randomDocWriteResponses = DeleteResponseTests.randomDeleteResponse();
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            randomDocWriteResponses = UpdateResponseTests.randomUpdateResponse(xContentType);
        } else {
            fail("Test does not support opType [" + opType + "]");
        }
        BulkItemResponse bulkItemResponse = new BulkItemResponse(bulkItemId, opType, randomDocWriteResponses.v1());
        BulkItemResponse expectedBulkItemResponse = new BulkItemResponse(bulkItemId, opType, randomDocWriteResponses.v2());
        BytesReference originalBytes = toXContent(bulkItemResponse, xContentType, humanReadable);
        // Shuffle the XContent fields
        if (randomBoolean()) {
            try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
                originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
            }
        }
        BulkItemResponse parsedBulkItemResponse;
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
            parsedBulkItemResponse = BulkItemResponse.fromXContent(parser, bulkItemId);
            assertNull(parser.nextToken());
        }
        assertBulkItemResponse(expectedBulkItemResponse, parsedBulkItemResponse);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 34 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference 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);
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) BytesReference(org.elasticsearch.common.bytes.BytesReference) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Suggestion(org.elasticsearch.search.suggest.Suggest.Suggestion) PhraseSuggestion(org.elasticsearch.search.suggest.phrase.PhraseSuggestion) TermSuggestion(org.elasticsearch.search.suggest.term.TermSuggestion) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 35 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class SuggestionEntryTests method testFromXContent.

@SuppressWarnings("unchecked")
public void testFromXContent() throws IOException {
    for (Class<? extends Entry> entryType : ENTRY_PARSERS.keySet()) {
        Entry<Option> entry = createTestItem(entryType);
        XContentType xContentType = randomFrom(XContentType.values());
        boolean humanReadable = randomBoolean();
        BytesReference originalBytes = toXContent(entry, xContentType, humanReadable);
        Entry<Option> parsed;
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
            parsed = ENTRY_PARSERS.get(entry.getClass()).apply(parser);
            assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
            assertNull(parser.nextToken());
        }
        assertEquals(entry.getClass(), parsed.getClass());
        assertEquals(entry.getText(), parsed.getText());
        assertEquals(entry.getLength(), parsed.getLength());
        assertEquals(entry.getOffset(), parsed.getOffset());
        assertEquals(entry.getOptions().size(), parsed.getOptions().size());
        for (int i = 0; i < entry.getOptions().size(); i++) {
            assertEquals(entry.getOptions().get(i).getClass(), parsed.getOptions().get(i).getClass());
        }
        assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

BytesReference (org.elasticsearch.common.bytes.BytesReference)318 Matchers.containsString (org.hamcrest.Matchers.containsString)72 XContentParser (org.elasticsearch.common.xcontent.XContentParser)63 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)61 IOException (java.io.IOException)58 XContentType (org.elasticsearch.common.xcontent.XContentType)50 BytesArray (org.elasticsearch.common.bytes.BytesArray)47 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)37 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)30 Map (java.util.Map)26 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)26 Test (org.junit.Test)25 List (java.util.List)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 Version (org.elasticsearch.Version)22 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 BytesRef (org.apache.lucene.util.BytesRef)18