|
| 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 | +package org.elasticsearch.cluster.metadata; |
| 20 | + |
| 21 | +import org.elasticsearch.common.bytes.BytesArray; |
| 22 | +import org.elasticsearch.common.bytes.BytesReference; |
| 23 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 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 static java.util.Collections.singletonMap; |
| 33 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 34 | + |
| 35 | +public class IndexTemplateMetaDataTests extends ESTestCase { |
| 36 | + |
| 37 | + public void testIndexTemplateMetaDataXContentRoundTrip() throws Exception { |
| 38 | + ToXContent.Params params = new ToXContent.MapParams(singletonMap("reduce_mappings", "true")); |
| 39 | + |
| 40 | + String template = "{\"template\" : [ \".test-*\" ],\"order\" : 1000," + |
| 41 | + "\"settings\" : {\"number_of_shards\" : 1,\"number_of_replicas\" : 0}," + |
| 42 | + "\"mappings\" : {\"doc\" :" + |
| 43 | + "{\"properties\":{\"" + |
| 44 | + randomAlphaOfLength(10) + "\":{\"type\":\"text\"},\"" + |
| 45 | + randomAlphaOfLength(10) + "\":{\"type\":\"keyword\"}}" + |
| 46 | + "}}}"; |
| 47 | + |
| 48 | + BytesReference templateBytes = new BytesArray(template); |
| 49 | + final IndexTemplateMetaData indexTemplateMetaData; |
| 50 | + try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, templateBytes, XContentType.JSON)) { |
| 51 | + indexTemplateMetaData = IndexTemplateMetaData.Builder.fromXContent(parser, "test"); |
| 52 | + } |
| 53 | + |
| 54 | + final BytesReference templateBytesRoundTrip; |
| 55 | + try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent)) { |
| 56 | + builder.startObject(); |
| 57 | + IndexTemplateMetaData.Builder.toXContent(indexTemplateMetaData, builder, params); |
| 58 | + builder.endObject(); |
| 59 | + templateBytesRoundTrip = builder.bytes(); |
| 60 | + } |
| 61 | + |
| 62 | + final IndexTemplateMetaData indexTemplateMetaDataRoundTrip; |
| 63 | + try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, templateBytesRoundTrip, XContentType.JSON)) { |
| 64 | + indexTemplateMetaDataRoundTrip = IndexTemplateMetaData.Builder.fromXContent(parser, "test"); |
| 65 | + } |
| 66 | + assertThat(indexTemplateMetaData, equalTo(indexTemplateMetaDataRoundTrip)); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments