@Test
  public void testEncodeDecodeCommonStats() throws IOException {
    CommonStatsFlags flags = new CommonStatsFlags();
    Flag[] values = CommonStatsFlags.Flag.values();
    assertThat(flags.anySet(), equalTo(true));

    for (Flag flag : values) {
      flags.set(flag, false);
    }
    assertThat(flags.anySet(), equalTo(false));
    for (Flag flag : values) {
      flags.set(flag, true);
    }
    assertThat(flags.anySet(), equalTo(true));
    Random random = getRandom();
    flags.set(values[random.nextInt(values.length)], false);
    assertThat(flags.anySet(), equalTo(true));

    {
      BytesStreamOutput out = new BytesStreamOutput();
      flags.writeTo(out);
      out.close();
      BytesReference bytes = out.bytes();
      CommonStatsFlags readStats =
          CommonStatsFlags.readCommonStatsFlags(new BytesStreamInput(bytes));
      for (Flag flag : values) {
        assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag)));
      }
    }

    {
      for (Flag flag : values) {
        flags.set(flag, random.nextBoolean());
      }
      BytesStreamOutput out = new BytesStreamOutput();
      flags.writeTo(out);
      out.close();
      BytesReference bytes = out.bytes();
      CommonStatsFlags readStats =
          CommonStatsFlags.readCommonStatsFlags(new BytesStreamInput(bytes));
      for (Flag flag : values) {
        assertThat(flags.isSet(flag), equalTo(readStats.isSet(flag)));
      }
    }
  }
  @Test
  public void testAllFlags() throws Exception {
    // rely on 1 replica for this tests
    createIndex("test1");
    createIndex("test2");

    ClusterHealthResponse clusterHealthResponse =
        client()
            .admin()
            .cluster()
            .prepareHealth()
            .setWaitForEvents(Priority.LANGUID)
            .setWaitForGreenStatus()
            .execute()
            .actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));

    client()
        .prepareIndex("test1", "type1", Integer.toString(1))
        .setSource("field", "value")
        .execute()
        .actionGet();
    client()
        .prepareIndex("test1", "type2", Integer.toString(1))
        .setSource("field", "value")
        .execute()
        .actionGet();
    client()
        .prepareIndex("test2", "type", Integer.toString(1))
        .setSource("field", "value")
        .execute()
        .actionGet();

    client().admin().indices().prepareRefresh().execute().actionGet();
    IndicesStatsRequestBuilder builder = client().admin().indices().prepareStats();
    Flag[] values = CommonStatsFlags.Flag.values();
    for (Flag flag : values) {
      set(flag, builder, false);
    }

    IndicesStatsResponse stats = builder.execute().actionGet();
    for (Flag flag : values) {
      assertThat(isSet(flag, stats.getPrimaries()), equalTo(false));
      assertThat(isSet(flag, stats.getTotal()), equalTo(false));
    }

    for (Flag flag : values) {
      set(flag, builder, true);
    }
    stats = builder.execute().actionGet();
    for (Flag flag : values) {
      assertThat(isSet(flag, stats.getPrimaries()), equalTo(true));
      assertThat(isSet(flag, stats.getTotal()), equalTo(true));
    }
    Random random = getRandom();
    EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
    for (Flag flag : values) {
      if (random.nextBoolean()) {
        flags.add(flag);
      }
    }

    for (Flag flag : values) {
      set(flag, builder, false); // clear all
    }

    for (Flag flag : flags) { // set the flags
      set(flag, builder, true);
    }
    stats = builder.execute().actionGet();
    for (Flag flag : flags) { // check the flags
      assertThat(isSet(flag, stats.getPrimaries()), equalTo(true));
      assertThat(isSet(flag, stats.getTotal()), equalTo(true));
    }

    for (Flag flag : EnumSet.complementOf(flags)) { // check the complement
      assertThat(isSet(flag, stats.getPrimaries()), equalTo(false));
      assertThat(isSet(flag, stats.getTotal()), equalTo(false));
    }
  }