@Test
  @UsingDataSet(
      locations = "EsIndexRangeServiceTest.json",
      loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
  public void savePersistsIndexRangeInReadOnlyIndex() throws Exception {
    final String indexName = "graylog_read_only";
    final DateTime begin = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC);
    final DateTime end = new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC);
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final IndexRange indexRange = IndexRange.create(indexName, begin, end, now, 42);

    try {
      indices.create(indexName);
      indices.setReadOnly(indexName);
      assumeTrue(indices.isReadOnly(indexName));
      indexRangeService.save(indexRange);

      verify(clusterEventBus, times(1)).post(IndexRangeUpdatedEvent.create(indexName));
      assertThat(indices.isReadOnly(indexName)).isTrue();

      final IndexRange result = indexRangeService.get(indexName);
      assertThat(result.indexName()).isEqualTo(indexName);
      assertThat(result.begin()).isEqualTo(begin);
      assertThat(result.end()).isEqualTo(end);
      assertThat(result.calculatedAt()).isEqualTo(now);
      assertThat(result.calculationDuration()).isEqualTo(42);
    } finally {
      indices.delete(indexName);
    }
  }
  @Test
  @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
  public void saveSkipsUpToDateExistingIndexRange() throws Exception {
    final String indexName = "graylog_1";
    final DateTime begin = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC);
    final DateTime end = new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC);
    final DateTime calculatedAt = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC);
    final IndexRange indexRange = IndexRange.create(indexName, begin, end, calculatedAt, 23);

    indexRangeService.save(indexRange);
    verify(clusterEventBus, never()).post(IndexRangeUpdatedEvent.create(indexName));
  }
  @Test
  @UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
  public void savePersistsIndexRange() throws Exception {
    final String indexName = "graylog";
    final DateTime begin = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC);
    final DateTime end = new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC);
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final IndexRange indexRange = IndexRange.create(indexName, begin, end, now, 42);

    indexRangeService.save(indexRange);

    final IndexRange result = indexRangeService.get(indexName);
    verify(clusterEventBus, times(1)).post(IndexRangeUpdatedEvent.create(indexName));
    assertThat(result.indexName()).isEqualTo(indexName);
    assertThat(result.begin()).isEqualTo(begin);
    assertThat(result.end()).isEqualTo(end);
    assertThat(result.calculatedAt()).isEqualTo(now);
    assertThat(result.calculationDuration()).isEqualTo(42);
  }
  @Test
  @UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
  public void saveOverwritesExistingIndexRange() throws Exception {
    final String indexName = "graylog";
    final DateTime begin = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC);
    final DateTime end = new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC);
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final IndexRange indexRangeBefore = IndexRange.create(indexName, begin, end, now, 1);
    final IndexRange indexRangeAfter = IndexRange.create(indexName, begin, end, now, 2);

    indexRangeService.save(indexRangeBefore);

    final IndexRange before = indexRangeService.get(indexName);
    assertThat(before.calculationDuration()).isEqualTo(1);

    indexRangeService.save(indexRangeAfter);

    final IndexRange after = indexRangeService.get(indexName);
    assertThat(after.calculationDuration()).isEqualTo(2);

    verify(clusterEventBus, times(2)).post(IndexRangeUpdatedEvent.create(indexName));
  }