@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
  public void testRotateFailed() throws Exception {
    when(indices.getIndexStats("name")).thenReturn(null);
    when(deflector.getNewestTargetName()).thenReturn("name");
    when(clusterConfigService.get(SizeBasedRotationStrategyConfig.class))
        .thenReturn(SizeBasedRotationStrategyConfig.create(100));

    final SizeBasedRotationStrategy strategy =
        new SizeBasedRotationStrategy(
            indices, deflector, clusterConfigService, nodeId, auditEventSender);

    strategy.rotate();
    verify(deflector, never()).cycle();
    reset(deflector);
  }
  @Nullable
  @Override
  protected Result shouldRotate(final String index) {
    final SizeBasedRotationStrategyConfig config =
        clusterConfigService.get(SizeBasedRotationStrategyConfig.class);

    if (config == null) {
      LOG.warn("No rotation strategy configuration found, not running index rotation!");
      return null;
    }

    final IndexStatistics indexStats = indices.getIndexStats(index);
    if (indexStats == null) {
      return null;
    }

    final long sizeInBytes = indexStats.primaries().getStore().getSizeInBytes();

    final boolean shouldRotate = sizeInBytes > config.maxSize();

    return new Result() {
      public final MessageFormat ROTATE =
          new MessageFormat(
              "Storage size for index <{0}> is {1} bytes, exceeding the maximum of {2} bytes. Rotating index.",
              Locale.ENGLISH);
      public final MessageFormat NOT_ROTATE =
          new MessageFormat(
              "Storage size for index <{0}> is {1} bytes, below the maximum of {2} bytes. Not doing anything.",
              Locale.ENGLISH);

      @Override
      public String getDescription() {
        MessageFormat format = shouldRotate() ? ROTATE : NOT_ROTATE;
        return format.format(new Object[] {index, sizeInBytes, config.maxSize()});
      }

      @Override
      public boolean shouldRotate() {
        return shouldRotate;
      }
    };
  }
  @Test
  public void testDontRotate() throws Exception {
    final CommonStats commonStats = new CommonStats();
    commonStats.store = new StoreStats(1000, 0);
    final IndexStatistics stats =
        IndexStatistics.create(
            "name", commonStats, commonStats, Collections.<ShardRouting>emptyList());

    when(indices.getIndexStats("name")).thenReturn(stats);
    when(deflector.getNewestTargetName()).thenReturn("name");
    when(clusterConfigService.get(SizeBasedRotationStrategyConfig.class))
        .thenReturn(SizeBasedRotationStrategyConfig.create(100000L));

    final SizeBasedRotationStrategy strategy =
        new SizeBasedRotationStrategy(
            indices, deflector, clusterConfigService, nodeId, auditEventSender);

    strategy.rotate();
    verify(deflector, never()).cycle();
    reset(deflector);
  }