|
| 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.search; |
| 21 | + |
| 22 | +import org.elasticsearch.action.search.ClearScrollResponse; |
| 23 | +import org.elasticsearch.common.bytes.BytesReference; |
| 24 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 25 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 26 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 27 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 28 | +import org.elasticsearch.common.xcontent.XContentType; |
| 29 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
| 30 | +import org.elasticsearch.test.ESTestCase; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; |
| 35 | + |
| 36 | +public class ClearScrollResponseTests extends ESTestCase { |
| 37 | + |
| 38 | + public void testToXContent() throws IOException { |
| 39 | + ClearScrollResponse clearScrollResponse = new ClearScrollResponse(true, 10); |
| 40 | + try (XContentBuilder builder = JsonXContent.contentBuilder()) { |
| 41 | + clearScrollResponse.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 42 | + } |
| 43 | + assertEquals(true, clearScrollResponse.isSucceeded()); |
| 44 | + assertEquals(10, clearScrollResponse.getNumFreed()); |
| 45 | + } |
| 46 | + |
| 47 | + public void testToAndFromXContent() throws IOException { |
| 48 | + XContentType xContentType = randomFrom(XContentType.values()); |
| 49 | + ClearScrollResponse originalResponse = createTestItem(); |
| 50 | + BytesReference originalBytes = toShuffledXContent(originalResponse, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); |
| 51 | + ClearScrollResponse parsedResponse; |
| 52 | + try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { |
| 53 | + parsedResponse = ClearScrollResponse.fromXContent(parser); |
| 54 | + } |
| 55 | + assertEquals(originalResponse.isSucceeded(), parsedResponse.isSucceeded()); |
| 56 | + assertEquals(originalResponse.getNumFreed(), parsedResponse.getNumFreed()); |
| 57 | + BytesReference parsedBytes = XContentHelper.toXContent(parsedResponse, xContentType, randomBoolean()); |
| 58 | + assertToXContentEquivalent(originalBytes, parsedBytes, xContentType); |
| 59 | + } |
| 60 | + |
| 61 | + private static ClearScrollResponse createTestItem() { |
| 62 | + return new ClearScrollResponse(randomBoolean(), randomIntBetween(0, Integer.MAX_VALUE)); |
| 63 | + } |
| 64 | +} |
0 commit comments