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