@Test
public class ToAndFromJsonMetaDataTests {
@Test
public void testSimpleJsonFromAndTo() throws IOException {
MetaData metaData = newMetaDataBuilder()
.put(newIndexMetaDataBuilder("test1")
.numberOfShards(1)
.numberOfReplicas(2))
.put(newIndexMetaDataBuilder("test2")
.settings(settingsBuilder().put("setting1", "value1").put("setting2", "value2"))
.numberOfShards(2)
.numberOfReplicas(3))
.put(newIndexMetaDataBuilder("test3")
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1))
.put(newIndexMetaDataBuilder("test4")
.settings(settingsBuilder().put("setting1", "value1").put("setting2", "value2"))
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2))
.put(newIndexMetaDataBuilder("test5")
.settings(settingsBuilder().put("setting1", "value1").put("setting2", "value2"))
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2)
.putAlias(newAliasMetaDataBuilder("alias1"))
.putAlias(newAliasMetaDataBuilder("alias2")))
.put(newIndexMetaDataBuilder("test6")
.settings(settingsBuilder()
.put("setting1", "value1")
.put("setting2", "value2")
.put("index.aliases.0", "alias3")
.put("index.aliases.1", "alias1"))
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2)
.putAlias(newAliasMetaDataBuilder("alias1"))
.putAlias(newAliasMetaDataBuilder("alias2")))
.put(newIndexMetaDataBuilder("test7")
.settings(settingsBuilder()
.put("setting1", "value1")
.put("setting2", "value2")
.put("index.aliases.0", "alias3")
.put("index.aliases.1", "alias1"))
.numberOfShards(1)
.numberOfReplicas(2)
.putMapping("mapping1", MAPPING_SOURCE1)
.putMapping("mapping2", MAPPING_SOURCE2)
.putAlias(newAliasMetaDataBuilder("alias1").filter(ALIAS_FILTER1))
.putAlias(newAliasMetaDataBuilder("alias2"))
.putAlias(newAliasMetaDataBuilder("alias4").filter(ALIAS_FILTER2)))
.build();
String metaDataSource = MetaData.Builder.toXContent(metaData);
System.out.println("ToJson: " + metaDataSource);
MetaData parsedMetaData = MetaData.Builder.fromXContent(XContentFactory.xContent(XContentType.JSON).createParser(metaDataSource));
IndexMetaData indexMetaData = parsedMetaData.index("test1");
assertThat(indexMetaData.numberOfShards(), equalTo(1));
assertThat(indexMetaData.numberOfReplicas(), equalTo(2));
assertThat(indexMetaData.settings().getAsMap().size(), equalTo(2));
assertThat(indexMetaData.mappings().size(), equalTo(0));
indexMetaData = parsedMetaData.index("test2");
assertThat(indexMetaData.numberOfShards(), equalTo(2));
assertThat(indexMetaData.numberOfReplicas(), equalTo(3));
assertThat(indexMetaData.settings().getAsMap().size(), equalTo(4));
assertThat(indexMetaData.settings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.settings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.mappings().size(), equalTo(0));
indexMetaData = parsedMetaData.index("test3");
assertThat(indexMetaData.numberOfShards(), equalTo(1));
assertThat(indexMetaData.numberOfReplicas(), equalTo(2));
assertThat(indexMetaData.settings().getAsMap().size(), equalTo(2));
assertThat(indexMetaData.mappings().size(), equalTo(1));
assertThat(indexMetaData.mappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
indexMetaData = parsedMetaData.index("test4");
assertThat(indexMetaData.numberOfShards(), equalTo(1));
assertThat(indexMetaData.numberOfReplicas(), equalTo(2));
assertThat(indexMetaData.settings().getAsMap().size(), equalTo(4));
assertThat(indexMetaData.settings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.settings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.mappings().size(), equalTo(2));
assertThat(indexMetaData.mappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.mappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
indexMetaData = parsedMetaData.index("test5");
assertThat(indexMetaData.numberOfShards(), equalTo(1));
assertThat(indexMetaData.numberOfReplicas(), equalTo(2));
assertThat(indexMetaData.settings().getAsMap().size(), equalTo(4));
assertThat(indexMetaData.settings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.settings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.mappings().size(), equalTo(2));
assertThat(indexMetaData.mappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.mappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
assertThat(indexMetaData.aliases().size(), equalTo(2));
assertThat(indexMetaData.aliases().get("alias1").alias(), equalTo("alias1"));
assertThat(indexMetaData.aliases().get("alias2").alias(), equalTo("alias2"));
indexMetaData = parsedMetaData.index("test6");
assertThat(indexMetaData.numberOfShards(), equalTo(1));
assertThat(indexMetaData.numberOfReplicas(), equalTo(2));
assertThat(indexMetaData.settings().getAsMap().size(), equalTo(4));
assertThat(indexMetaData.settings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.settings().get("setting2"), equalTo("value2"));
assertThat(indexMetaData.mappings().size(), equalTo(2));
assertThat(indexMetaData.mappings().get("mapping1").source().string(), equalTo(MAPPING_SOURCE1));
assertThat(indexMetaData.mappings().get("mapping2").source().string(), equalTo(MAPPING_SOURCE2));
assertThat(indexMetaData.aliases().size(), equalTo(3));
assertThat(indexMetaData.aliases().get("alias1").alias(), equalTo("alias1"));
assertThat(indexMetaData.aliases().get("alias2").alias(), equalTo("alias2"));
assertThat(indexMetaData.aliases().get("alias3").alias(), equalTo("alias3"));
indexMetaData = parsedMetaData.index("test7");
assertThat(indexMetaData.numberOfShards(), equalTo(1));
assertThat(indexMetaData.numberOfReplicas(), equalTo(2));
assertThat(indexMetaData.settings().getAsMap().size(), equalTo(4));
assertThat(indexMetaData.settings().get("setting1"), equalTo("value1"));
assertThat(indexMetaData.settings().get("setting2"), equalTo("value2"));