|
43 | 43 | import static java.util.Collections.singletonMap;
|
44 | 44 | import static org.elasticsearch.cluster.metadata.AliasMetaData.newAliasMetaDataBuilder;
|
45 | 45 | import static org.hamcrest.CoreMatchers.equalTo;
|
| 46 | +import static org.hamcrest.Matchers.contains; |
46 | 47 |
|
47 | 48 | public class IndexTemplateMetaDataTests extends ESTestCase {
|
48 | 49 |
|
@@ -167,4 +168,54 @@ DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateWithoutPa
|
167 | 168 | assertThat(ex.getMessage(), equalTo("Index patterns must not be null or empty; got null"));
|
168 | 169 | }
|
169 | 170 | }
|
| 171 | + |
| 172 | + public void testParseTemplateWithAliases() throws Exception { |
| 173 | + String templateInJSON = "{\"aliases\": {\"log\":{}}, \"index_patterns\": [\"pattern-1\"]}"; |
| 174 | + try (XContentParser parser = |
| 175 | + XContentHelper.createParser(NamedXContentRegistry.EMPTY, |
| 176 | + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(templateInJSON), XContentType.JSON)) { |
| 177 | + IndexTemplateMetaData template = IndexTemplateMetaData.Builder.fromXContent(parser, randomAlphaOfLengthBetween(1, 100)); |
| 178 | + assertThat(template.aliases().containsKey("log"), equalTo(true)); |
| 179 | + assertThat(template.patterns(), contains("pattern-1")); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + public void testFromToXContent() throws Exception { |
| 184 | + String templateName = randomUnicodeOfCodepointLengthBetween(1, 10); |
| 185 | + IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData.builder(templateName); |
| 186 | + templateBuilder.patterns(Arrays.asList("pattern-1")); |
| 187 | + int numAlias = between(0, 5); |
| 188 | + for (int i = 0; i < numAlias; i++) { |
| 189 | + AliasMetaData.Builder alias = AliasMetaData.builder(randomAlphaOfLengthBetween(1, 100)); |
| 190 | + if (randomBoolean()) { |
| 191 | + alias.indexRouting(randomRealisticUnicodeOfLengthBetween(1, 100)); |
| 192 | + } |
| 193 | + if (randomBoolean()) { |
| 194 | + alias.searchRouting(randomRealisticUnicodeOfLengthBetween(1, 100)); |
| 195 | + } |
| 196 | + templateBuilder.putAlias(alias); |
| 197 | + } |
| 198 | + if (randomBoolean()) { |
| 199 | + templateBuilder.settings(Settings.builder().put("index.setting-1", randomLong())); |
| 200 | + templateBuilder.settings(Settings.builder().put("index.setting-2", randomTimeValue())); |
| 201 | + } |
| 202 | + if (randomBoolean()) { |
| 203 | + templateBuilder.order(randomInt()); |
| 204 | + } |
| 205 | + if (randomBoolean()) { |
| 206 | + templateBuilder.version(between(0, 100)); |
| 207 | + } |
| 208 | + if (randomBoolean()) { |
| 209 | + templateBuilder.putMapping("doc", "{\"doc\":{\"properties\":{\"type\":\"text\"}}}"); |
| 210 | + } |
| 211 | + IndexTemplateMetaData template = templateBuilder.build(); |
| 212 | + XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.JSON.xContent())); |
| 213 | + builder.startObject(); |
| 214 | + IndexTemplateMetaData.Builder.toXContent(template, builder, ToXContent.EMPTY_PARAMS); |
| 215 | + builder.endObject(); |
| 216 | + try (XContentParser parser = createParser(shuffleXContent(builder))) { |
| 217 | + IndexTemplateMetaData parsed = IndexTemplateMetaData.Builder.fromXContent(parser, templateName); |
| 218 | + assertThat(parsed, equalTo(template)); |
| 219 | + } |
| 220 | + } |
170 | 221 | }
|
0 commit comments