1919package org .elasticsearch .client ;
2020
2121import org .elasticsearch .action .admin .cluster .health .ClusterHealthRequest ;
22+ import org .elasticsearch .ElasticsearchStatusException ;
2223import org .elasticsearch .action .admin .indices .refresh .RefreshRequest ;
2324import org .elasticsearch .action .admin .indices .refresh .RefreshResponse ;
2425import org .elasticsearch .action .bulk .BulkItemResponse ;
3435import org .elasticsearch .client .rollup .GetRollupJobResponse ;
3536import org .elasticsearch .client .rollup .GetRollupJobResponse .IndexerState ;
3637import org .elasticsearch .client .rollup .GetRollupJobResponse .JobWrapper ;
38+ import org .elasticsearch .client .rollup .DeleteRollupJobRequest ;
39+ import org .elasticsearch .client .rollup .DeleteRollupJobResponse ;
3740import org .elasticsearch .client .rollup .PutRollupJobRequest ;
3841import org .elasticsearch .client .rollup .PutRollupJobResponse ;
3942import org .elasticsearch .client .rollup .RollableIndexCaps ;
5356import org .elasticsearch .search .aggregations .metrics .min .MinAggregationBuilder ;
5457import org .elasticsearch .search .aggregations .metrics .sum .SumAggregationBuilder ;
5558import org .elasticsearch .search .aggregations .metrics .valuecount .ValueCountAggregationBuilder ;
59+ import org .junit .Before ;
5660
5761import java .util .Arrays ;
5862import java .util .Collections ;
6973import static org .hamcrest .Matchers .greaterThan ;
7074import static org .hamcrest .Matchers .hasKey ;
7175import static org .hamcrest .Matchers .hasSize ;
76+ import static org .hamcrest .Matchers .is ;
7277import static org .hamcrest .Matchers .lessThan ;
7378
7479public class RollupIT extends ESRestHighLevelClientTestCase {
7580
81+ double sum = 0.0d ;
82+ int max = Integer .MIN_VALUE ;
83+ int min = Integer .MAX_VALUE ;
7684 private static final List <String > SUPPORTED_METRICS = Arrays .asList (MaxAggregationBuilder .NAME , MinAggregationBuilder .NAME ,
77- SumAggregationBuilder .NAME , AvgAggregationBuilder .NAME , ValueCountAggregationBuilder .NAME );
78-
79- public void testPutStartAndGetRollupJob () throws Exception {
80- double sum = 0.0d ;
81- int max = Integer .MIN_VALUE ;
82- int min = Integer .MAX_VALUE ;
85+ SumAggregationBuilder .NAME , AvgAggregationBuilder .NAME , ValueCountAggregationBuilder .NAME );
86+
87+ private String id ;
88+ private String indexPattern ;
89+ private String rollupIndex ;
90+ private String cron ;
91+ private int pageSize ;
92+ private int numDocs ;
93+
94+ @ Before
95+ public void init () throws Exception {
96+ id = randomAlphaOfLength (10 );
97+ indexPattern = randomFrom ("docs" , "d*" , "doc*" );
98+ rollupIndex = randomFrom ("rollup" , "test" );
99+ cron = "*/1 * * * * ?" ;
100+ numDocs = indexDocs ();
101+ pageSize = randomIntBetween (numDocs , numDocs * 10 );
102+ }
83103
104+ public int indexDocs () throws Exception {
84105 final BulkRequest bulkRequest = new BulkRequest ();
85106 bulkRequest .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
86107 for (int minute = 0 ; minute < 60 ; minute ++) {
@@ -120,12 +141,33 @@ public void testPutStartAndGetRollupJob() throws Exception {
120141
121142 RefreshResponse refreshResponse = highLevelClient ().indices ().refresh (new RefreshRequest ("docs" ), RequestOptions .DEFAULT );
122143 assertEquals (0 , refreshResponse .getFailedShards ());
144+ return numDocs ;
145+ }
123146
124- final String id = randomAlphaOfLength (10 );
125- final String indexPattern = randomFrom ("docs" , "d*" , "doc*" );
126- final String rollupIndex = randomFrom ("rollup" , "test" );
127- final String cron = "*/1 * * * * ?" ;
128- final int pageSize = randomIntBetween (numDocs , numDocs * 10 );
147+
148+ public void testDeleteRollupJob () throws Exception {
149+ final GroupConfig groups = new GroupConfig (new DateHistogramGroupConfig ("date" , DateHistogramInterval .DAY ));
150+ final List <MetricConfig > metrics = Collections .singletonList (new MetricConfig ("value" , SUPPORTED_METRICS ));
151+ final TimeValue timeout = TimeValue .timeValueSeconds (randomIntBetween (30 , 600 ));
152+ PutRollupJobRequest putRollupJobRequest =
153+ new PutRollupJobRequest (new RollupJobConfig (id , indexPattern , rollupIndex , cron , pageSize , groups , metrics , timeout ));
154+ final RollupClient rollupClient = highLevelClient ().rollup ();
155+ PutRollupJobResponse response = execute (putRollupJobRequest , rollupClient ::putRollupJob , rollupClient ::putRollupJobAsync );
156+ DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest (id );
157+ DeleteRollupJobResponse deleteRollupJobResponse = highLevelClient ().rollup ()
158+ .deleteRollupJob (deleteRollupJobRequest , RequestOptions .DEFAULT );
159+ assertTrue (deleteRollupJobResponse .isAcknowledged ());
160+ }
161+
162+ public void testDeleteMissingRollupJob () {
163+ DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest (randomAlphaOfLength (10 ));
164+ ElasticsearchStatusException responseException = expectThrows (ElasticsearchStatusException .class ,() -> highLevelClient ().rollup ()
165+ .deleteRollupJob (deleteRollupJobRequest , RequestOptions .DEFAULT ));
166+ assertThat (responseException .status ().getStatus (), is (404 ));
167+ }
168+
169+ @ SuppressWarnings ("unchecked" )
170+ public void testPutAndGetRollupJob () throws Exception {
129171 // TODO expand this to also test with histogram and terms?
130172 final GroupConfig groups = new GroupConfig (new DateHistogramGroupConfig ("date" , DateHistogramInterval .DAY ));
131173 final List <MetricConfig > metrics = Collections .singletonList (new MetricConfig ("value" , SUPPORTED_METRICS ));
@@ -142,9 +184,6 @@ public void testPutStartAndGetRollupJob() throws Exception {
142184 StartRollupJobResponse startResponse = execute (startRequest , rollupClient ::startRollupJob , rollupClient ::startRollupJobAsync );
143185 assertTrue (startResponse .isAcknowledged ());
144186
145- int finalMin = min ;
146- int finalMax = max ;
147- double finalSum = sum ;
148187 assertBusy (() -> {
149188 SearchResponse searchResponse = highLevelClient ().search (new SearchRequest (rollupIndex ), RequestOptions .DEFAULT );
150189 assertEquals (0 , searchResponse .getFailedShards ());
@@ -162,13 +201,13 @@ public void testPutStartAndGetRollupJob() throws Exception {
162201 for (String name : metric .getMetrics ()) {
163202 Number value = (Number ) source .get (metric .getField () + "." + name + ".value" );
164203 if ("min" .equals (name )) {
165- assertEquals (finalMin , value .intValue ());
204+ assertEquals (min , value .intValue ());
166205 } else if ("max" .equals (name )) {
167- assertEquals (finalMax , value .intValue ());
206+ assertEquals (max , value .intValue ());
168207 } else if ("sum" .equals (name )) {
169- assertEquals (finalSum , value .doubleValue (), 0.0d );
208+ assertEquals (sum , value .doubleValue (), 0.0d );
170209 } else if ("avg" .equals (name )) {
171- assertEquals (finalSum , value .doubleValue (), 0.0d );
210+ assertEquals (sum , value .doubleValue (), 0.0d );
172211 Number avgCount = (Number ) source .get (metric .getField () + "." + name + "._count" );
173212 assertEquals (numDocs , avgCount .intValue ());
174213 } else if ("value_count" .equals (name )) {
0 commit comments