CleanupOldIndexJob implements Job { void cleanup(LocalDate now) { ClusterStateResponse state = elasticSearch.state(); for (var entry : state.metadata.indices.entrySet()) { String index = entry.getKey(); indexService.createdDate(index).ifPresent(date -> { long days = ChronoUnit.DAYS.between(date, now); if (days >= 30) { deleteIndex(index); } else if (days >= 7 && entry.getValue().state == ClusterStateResponse.IndexState.OPEN) { closeIndex(index); } }); } }  @Override void execute(JobContext context);  }
@Test void cleanup() { var state = new ClusterStateResponse(); state.metadata = new ClusterStateResponse.Metadata(); state.metadata.indices = Map.of("action-2017.10.01", index(ClusterStateResponse.IndexState.OPEN), "action-2017.10.30", index(ClusterStateResponse.IndexState.CLOSE), "action-2017.11.01", index(ClusterStateResponse.IndexState.OPEN)); when(elasticSearch.state()).thenReturn(state); job.cleanup(LocalDate.of(2017, 11, 8)); verify(elasticSearch).deleteIndex("action-2017.10.01"); verify(elasticSearch).closeIndex("action-2017.11.01"); }