|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.action.bulk; |
| 21 | + |
| 22 | +import org.elasticsearch.ElasticsearchException; |
| 23 | +import org.elasticsearch.action.DocWriteRequest; |
| 24 | +import org.elasticsearch.action.DocWriteResponse; |
| 25 | +import org.elasticsearch.action.delete.DeleteResponseTests; |
| 26 | +import org.elasticsearch.action.index.IndexResponseTests; |
| 27 | +import org.elasticsearch.action.update.UpdateResponseTests; |
| 28 | +import org.elasticsearch.common.bytes.BytesReference; |
| 29 | +import org.elasticsearch.common.collect.Tuple; |
| 30 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 31 | +import org.elasticsearch.common.xcontent.XContentType; |
| 32 | +import org.elasticsearch.test.ESTestCase; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | + |
| 36 | +import static org.elasticsearch.ElasticsearchExceptionTests.randomExceptions; |
| 37 | +import static org.elasticsearch.action.bulk.BulkItemResponseTests.assertBulkItemResponse; |
| 38 | +import static org.elasticsearch.action.bulk.BulkResponse.NO_INGEST_TOOK; |
| 39 | +import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; |
| 40 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; |
| 41 | + |
| 42 | +public class BulkResponseTests extends ESTestCase { |
| 43 | + |
| 44 | + public void testToAndFromXContent() throws IOException { |
| 45 | + XContentType xContentType = randomFrom(XContentType.values()); |
| 46 | + boolean humanReadable = randomBoolean(); |
| 47 | + |
| 48 | + long took = randomFrom(randomNonNegativeLong(), -1L); |
| 49 | + long ingestTook = randomFrom(randomNonNegativeLong(), NO_INGEST_TOOK); |
| 50 | + int nbBulkItems = randomIntBetween(1, 10); |
| 51 | + |
| 52 | + BulkItemResponse[] bulkItems = new BulkItemResponse[nbBulkItems]; |
| 53 | + BulkItemResponse[] expectedBulkItems = new BulkItemResponse[nbBulkItems]; |
| 54 | + |
| 55 | + for (int i = 0; i < nbBulkItems; i++) { |
| 56 | + DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values()); |
| 57 | + |
| 58 | + if (frequently()) { |
| 59 | + Tuple<? extends DocWriteResponse, ? extends DocWriteResponse> randomDocWriteResponses = null; |
| 60 | + if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) { |
| 61 | + randomDocWriteResponses = IndexResponseTests.randomIndexResponse(); |
| 62 | + } else if (opType == DocWriteRequest.OpType.DELETE) { |
| 63 | + randomDocWriteResponses = DeleteResponseTests.randomDeleteResponse(); |
| 64 | + } else if (opType == DocWriteRequest.OpType.UPDATE) { |
| 65 | + randomDocWriteResponses = UpdateResponseTests.randomUpdateResponse(xContentType); |
| 66 | + } else { |
| 67 | + fail("Test does not support opType [" + opType + "]"); |
| 68 | + } |
| 69 | + |
| 70 | + bulkItems[i] = new BulkItemResponse(i, opType, randomDocWriteResponses.v1()); |
| 71 | + expectedBulkItems[i] = new BulkItemResponse(i, opType, randomDocWriteResponses.v2()); |
| 72 | + } else { |
| 73 | + String index = randomAsciiOfLength(5); |
| 74 | + String type = randomAsciiOfLength(5); |
| 75 | + String id = randomAsciiOfLength(5); |
| 76 | + |
| 77 | + Tuple<Throwable, ElasticsearchException> failures = randomExceptions(); |
| 78 | + bulkItems[i] = new BulkItemResponse(i, opType, new BulkItemResponse.Failure(index, type, id, (Exception) failures.v1())); |
| 79 | + expectedBulkItems[i] = new BulkItemResponse(i, opType, new BulkItemResponse.Failure(index, type, id, failures.v2())); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + BulkResponse bulkResponse = new BulkResponse(bulkItems, took, ingestTook); |
| 84 | + BytesReference originalBytes = toXContent(bulkResponse, xContentType, humanReadable); |
| 85 | + |
| 86 | + if (randomBoolean()) { |
| 87 | + try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { |
| 88 | + originalBytes = shuffleXContent(parser, randomBoolean()).bytes(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + BulkResponse parsedBulkResponse; |
| 93 | + try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { |
| 94 | + parsedBulkResponse = BulkResponse.fromXContent(parser); |
| 95 | + assertNull(parser.nextToken()); |
| 96 | + } |
| 97 | + |
| 98 | + assertEquals(took, parsedBulkResponse.getTookInMillis()); |
| 99 | + assertEquals(ingestTook, parsedBulkResponse.getIngestTookInMillis()); |
| 100 | + assertEquals(expectedBulkItems.length, parsedBulkResponse.getItems().length); |
| 101 | + |
| 102 | + for (int i = 0; i < expectedBulkItems.length; i++) { |
| 103 | + assertBulkItemResponse(expectedBulkItems[i], parsedBulkResponse.getItems()[i]); |
| 104 | + } |
| 105 | + |
| 106 | + BytesReference finalBytes = toXContent(parsedBulkResponse, xContentType, humanReadable); |
| 107 | + BytesReference expectedFinalBytes = toXContent(parsedBulkResponse, xContentType, humanReadable); |
| 108 | + assertToXContentEquivalent(expectedFinalBytes, finalBytes, xContentType); |
| 109 | + } |
| 110 | +} |
0 commit comments