|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.action.search; |
| 11 | + |
| 12 | +import org.elasticsearch.common.bytes.BytesArray; |
| 13 | +import org.elasticsearch.common.bytes.BytesReference; |
| 14 | +import org.elasticsearch.test.ESTestCase; |
| 15 | +import org.elasticsearch.xcontent.ToXContent; |
| 16 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 17 | +import org.elasticsearch.xcontent.XContentType; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Base64; |
| 21 | +import java.util.Locale; |
| 22 | + |
| 23 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; |
| 24 | + |
| 25 | +public class OpenPointInTimeResponseTests extends ESTestCase { |
| 26 | + |
| 27 | + public void testIdCantBeNull() { |
| 28 | + BytesReference pointInTimeId = null; |
| 29 | + expectThrows(NullPointerException.class, () -> { new OpenPointInTimeResponse(pointInTimeId, 11, 8, 2, 1); }); |
| 30 | + } |
| 31 | + |
| 32 | + public void testToXContent() throws IOException { |
| 33 | + String id = "test-id"; |
| 34 | + BytesReference pointInTimeId = new BytesArray(id); |
| 35 | + |
| 36 | + BytesReference actual; |
| 37 | + try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { |
| 38 | + OpenPointInTimeResponse response = new OpenPointInTimeResponse(pointInTimeId, 11, 8, 2, 1); |
| 39 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 40 | + actual = BytesReference.bytes(builder); |
| 41 | + } |
| 42 | + |
| 43 | + String encodedId = Base64.getUrlEncoder().encodeToString(BytesReference.toBytes(pointInTimeId)); |
| 44 | + BytesReference expected = new BytesArray(String.format(Locale.ROOT, """ |
| 45 | + { |
| 46 | + "id": "%s", |
| 47 | + "_shards": { |
| 48 | + "total": 11, |
| 49 | + "successful": 8, |
| 50 | + "failed": 2, |
| 51 | + "skipped": 1 |
| 52 | + } |
| 53 | + } |
| 54 | + """, encodedId)); |
| 55 | + assertToXContentEquivalent(expected, actual, XContentType.JSON); |
| 56 | + } |
| 57 | +} |
0 commit comments