class ClusterFacade {

  public static CompactionStats parseCompactionStats(String json) throws IOException {
    try {
      return parseJson(json, new TypeReference<CompactionStats>(){});
    } catch (IOException e) {
      // it can be that the storage had old format of compaction info, so we try to parse that
      List<Compaction> compactions = parseJson(json, new TypeReference<List<Compaction>>() {});
      return CompactionStats.builder()
          .withPendingCompactions(-1)
          .withActiveCompactions(compactions)
          .build();
    }
  }

}

class ClusterFacadeTest {

  @Test
  public void parseCompactionStatsTest() throws IOException {

    final ObjectMapper objectMapper = new ObjectMapper();
    Compaction compaction = Compaction.builder()
        .withId("foo")
        .withKeyspace("ks")
        .withTable("t")
        .withProgress(64L)
        .withTotal(128L)
        .withType("Validation")
        .withUnit("unit")
        .build();
    CompactionStats originalCompactionStats = CompactionStats.builder()
        .withActiveCompactions(ImmutableList.of(compaction))
        .withPendingCompactions(42)
        .build();
    String compactionJson = objectMapper.writeValueAsString(originalCompactionStats);
    CompactionStats compactionStats = ClusterFacade.parseCompactionStats(compactionJson);
    assertEquals(42L, compactionStats.getPendingCompactions().longValue());
  }

}