@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(
      locations = "EsIndexRangeServiceTest-EmptyIndex.json",
      loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
  public void testCalculateRangeWithEmptyIndex() throws Exception {
    final String index = "graylog";
    final IndexRange range = indexRangeService.calculateRange(index);

    assertThat(range).isNotNull();
    assertThat(range.indexName()).isEqualTo(index);
    assertThat(range.begin()).isEqualTo(new DateTime(0L, DateTimeZone.UTC));
    assertThat(range.end()).isEqualTo(new DateTime(0L, DateTimeZone.UTC));
  }
  @Test
  @UsingDataSet(
      locations = "EsIndexRangeServiceTest.json",
      loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
  public void getReturnsExistingIndexRange() throws Exception {
    IndexRange indexRange = indexRangeService.get("graylog_1");

    assertThat(indexRange.indexName()).isEqualTo("graylog_1");
    assertThat(indexRange.begin()).isEqualTo(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC));
    assertThat(indexRange.end()).isEqualTo(new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC));
    assertThat(indexRange.calculatedAt())
        .isEqualTo(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC));
    assertThat(indexRange.calculationDuration()).isEqualTo(23);
  }
  @Test
  @UsingDataSet(
      locations = "EsIndexRangeServiceTest.json",
      loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
  public void calculateRangeReturnsIndexRange() throws Exception {
    final String index = "graylog";
    final DateTime min = new DateTime(2015, 1, 1, 1, 0, DateTimeZone.UTC);
    final DateTime max = new DateTime(2015, 1, 1, 5, 0, DateTimeZone.UTC);
    final IndexRange indexRange = indexRangeService.calculateRange(index);

    assertThat(indexRange.indexName()).isEqualTo(index);
    assertThat(indexRange.begin()).isEqualTo(min);
    assertThat(indexRange.end()).isEqualTo(max);
    Assertions.assertThat(indexRange.calculatedAt())
        .isEqualToIgnoringHours(DateTime.now(DateTimeZone.UTC));
  }
  @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);
  }