15
15
16
16
package software .amazon .awssdk .services .cloudwatch ;
17
17
18
+ import static java .util .stream .Collectors .toList ;
19
+ import static org .hamcrest .CoreMatchers .is ;
18
20
import static org .hamcrest .MatcherAssert .assertThat ;
21
+ import static org .hamcrest .Matchers .equalTo ;
22
+ import static org .hamcrest .Matchers .greaterThan ;
23
+ import static org .hamcrest .Matchers .hasSize ;
24
+ import static org .hamcrest .Matchers .isIn ;
19
25
import static org .junit .Assert .assertEquals ;
20
- import static org .junit .Assert .assertFalse ;
21
26
import static org .junit .Assert .assertNotNull ;
22
27
import static org .junit .Assert .assertTrue ;
23
28
import static org .junit .Assert .fail ;
26
31
import java .io .IOException ;
27
32
import java .time .Duration ;
28
33
import java .time .Instant ;
34
+ import java .util .ArrayList ;
29
35
import java .util .Collection ;
30
36
import java .util .LinkedList ;
37
+ import java .util .List ;
31
38
import org .junit .AfterClass ;
32
39
import org .junit .BeforeClass ;
33
40
import org .junit .Test ;
37
44
import software .amazon .awssdk .core .regions .Region ;
38
45
import software .amazon .awssdk .services .cloudwatch .model .Datapoint ;
39
46
import software .amazon .awssdk .services .cloudwatch .model .DeleteAlarmsRequest ;
40
- import software .amazon .awssdk .services .cloudwatch .model .DescribeAlarmHistoryRequest ;
41
- import software .amazon .awssdk .services .cloudwatch .model .DescribeAlarmHistoryResponse ;
42
- import software .amazon .awssdk .services .cloudwatch .model .DescribeAlarmsForMetricRequest ;
43
47
import software .amazon .awssdk .services .cloudwatch .model .DescribeAlarmsForMetricResponse ;
44
48
import software .amazon .awssdk .services .cloudwatch .model .DescribeAlarmsRequest ;
45
49
import software .amazon .awssdk .services .cloudwatch .model .DescribeAlarmsResponse ;
46
50
import software .amazon .awssdk .services .cloudwatch .model .Dimension ;
47
- import software .amazon .awssdk .services .cloudwatch .model .DisableAlarmActionsRequest ;
48
- import software .amazon .awssdk .services .cloudwatch .model .EnableAlarmActionsRequest ;
49
51
import software .amazon .awssdk .services .cloudwatch .model .GetMetricStatisticsRequest ;
50
52
import software .amazon .awssdk .services .cloudwatch .model .GetMetricStatisticsResponse ;
51
53
import software .amazon .awssdk .services .cloudwatch .model .HistoryItemType ;
56
58
import software .amazon .awssdk .services .cloudwatch .model .MetricDatum ;
57
59
import software .amazon .awssdk .services .cloudwatch .model .PutMetricAlarmRequest ;
58
60
import software .amazon .awssdk .services .cloudwatch .model .PutMetricDataRequest ;
59
- import software .amazon .awssdk .services .cloudwatch .model .SetAlarmStateRequest ;
61
+ import software .amazon .awssdk .services .cloudwatch .model .StateValue ;
60
62
import software .amazon .awssdk .testutils .Waiter ;
61
63
import software .amazon .awssdk .testutils .service .AwsIntegrationTestBase ;
62
- import software .amazon .awssdk .testutils .SdkAsserts ;
63
64
64
65
/**
65
66
* Integration tests for the AWS CloudWatch service.
@@ -90,7 +91,7 @@ public static void setUp() throws IOException {
90
91
public static void cleanupAlarms () {
91
92
if (cloudwatch != null ) {
92
93
DescribeAlarmsResponse describeResult = cloudwatch .describeAlarms (DescribeAlarmsRequest .builder ().build ());
93
- Collection <String > toDelete = new LinkedList <String >();
94
+ Collection <String > toDelete = new LinkedList <>();
94
95
for (MetricAlarm alarm : describeResult .metricAlarms ()) {
95
96
if (alarm .metricName ().startsWith (CloudWatchIntegrationTest .class .getName ())) {
96
97
toDelete .add (alarm .alarmName ());
@@ -176,7 +177,7 @@ public void put_metric_large_data_throws_request_entity_large_exception()
176
177
long now = System .currentTimeMillis ();
177
178
double value = 42.0 ;
178
179
179
- Collection <MetricDatum > data = new LinkedList <MetricDatum >();
180
+ Collection <MetricDatum > data = new LinkedList <>();
180
181
for (int i = ONE_WEEK_IN_MILLISECONDS ; i >= 0 ; i -= ONE_HOUR_IN_MILLISECONDS ) {
181
182
long time = now - i ;
182
183
MetricDatum datum = MetricDatum .builder ().dimensions (
@@ -200,112 +201,88 @@ public void put_metric_large_data_throws_request_entity_large_exception()
200
201
*/
201
202
@ Test
202
203
public void describe_alarms_returns_values_set () {
203
- String metricName = this .getClass ().getName ()
204
- + System .currentTimeMillis ();
204
+ String metricName = this .getClass ().getName () + System .currentTimeMillis ();
205
205
206
- PutMetricAlarmRequest [] rqs = createTwoNewAlarms (metricName );
206
+ List < PutMetricAlarmRequest > rqs = createTwoNewAlarms (metricName );
207
207
208
- PutMetricAlarmRequest rq1 = rqs [0 ];
209
- PutMetricAlarmRequest rq2 = rqs [1 ];
208
+ rqs .forEach (rq -> cloudwatch .setAlarmState (r -> r .alarmName (rq .alarmName ()).stateValue ("ALARM" ).stateReason ("manual" )));
210
209
211
- /*
212
- * Set the state
213
- */
214
- SetAlarmStateRequest setAlarmStateRequest = SetAlarmStateRequest .builder ()
215
- .alarmName (rq1 .alarmName ()).stateValue ("ALARM" )
216
- .stateReason ("manual" ).build ();
217
- cloudwatch .setAlarmState (setAlarmStateRequest );
218
- setAlarmStateRequest = SetAlarmStateRequest .builder ().alarmName (
219
- rq2 .alarmName ()).stateValue ("ALARM" ).stateReason (
220
- "manual" ).build ();
221
- cloudwatch .setAlarmState (setAlarmStateRequest );
222
-
223
- DescribeAlarmsForMetricResponse describeResult = cloudwatch
224
- .describeAlarmsForMetric (DescribeAlarmsForMetricRequest .builder ()
225
- .dimensions (rq1 .dimensions ()).metricName (
226
- metricName ).namespace (rq1 .namespace ()).build ());
227
- assertEquals (2 , describeResult .metricAlarms ().size ());
228
- for (MetricAlarm alarm : describeResult .metricAlarms ()) {
229
- assertTrue (rq1 .alarmName ().equals (alarm .alarmName ())
230
- || rq2 .alarmName ().equals (alarm .alarmName ()));
231
- assertEquals (setAlarmStateRequest .stateValue (), alarm
232
- .stateValue ());
233
- assertEquals (setAlarmStateRequest .stateReason (), alarm
234
- .stateReason ());
235
- }
210
+ DescribeAlarmsForMetricResponse describeResult = describeAlarmsForMetric (rqs );
236
211
237
- /*
238
- * Get the history
239
- */
240
- DescribeAlarmHistoryRequest alarmHistoryRequest = DescribeAlarmHistoryRequest .builder ()
241
- .alarmName (rq1 .alarmName ()).historyItemType (HistoryItemType .STATE_UPDATE )
242
- .build ();
243
- DescribeAlarmHistoryResponse historyResult = cloudwatch
244
- .describeAlarmHistory (alarmHistoryRequest );
245
- assertEquals (1 , historyResult .alarmHistoryItems ().size ());
212
+ assertThat (describeResult .metricAlarms (), hasSize (2 ));
213
+
214
+ //check the state is correct
215
+ describeResult .metricAlarms ().forEach (alarm -> {
216
+ assertThat (alarm .alarmName (), isIn (rqs .stream ().map (PutMetricAlarmRequest ::alarmName ).collect (toList ())));
217
+ assertThat (alarm .stateValue (), equalTo (StateValue .ALARM ));
218
+ assertThat (alarm .stateReason (), equalTo ("manual" ));
219
+ });
220
+
221
+ //check the state history has been recorded
222
+ rqs .stream ().map (alarm -> cloudwatch .describeAlarmHistory (r -> r .alarmName (alarm .alarmName ())
223
+ .historyItemType (HistoryItemType .STATE_UPDATE )))
224
+ .forEach (history -> assertThat (history .alarmHistoryItems (), hasSize (greaterThan (0 ))));
246
225
}
247
226
248
227
/**
249
228
* Tests disabling and enabling alarm actions
250
229
*/
251
230
@ Test
252
231
public void disable_enable_alarms_returns_success () {
253
- String metricName = this .getClass ().getName ()
254
- + System .currentTimeMillis ();
232
+ String metricName = this .getClass ().getName () + System .currentTimeMillis ();
255
233
256
- PutMetricAlarmRequest [] rqs = createTwoNewAlarms (metricName );
234
+ List <PutMetricAlarmRequest > rqs = createTwoNewAlarms (metricName );
235
+ List <String > alarmNames = rqs .stream ().map (PutMetricAlarmRequest ::alarmName ).collect (toList ());
257
236
258
- PutMetricAlarmRequest rq1 = rqs [ 0 ] ;
259
- PutMetricAlarmRequest rq2 = rqs [ 1 ] ;
237
+ PutMetricAlarmRequest rq1 = rqs . get ( 0 ) ;
238
+ PutMetricAlarmRequest rq2 = rqs . get ( 1 ) ;
260
239
261
240
/*
262
241
* Disable
263
242
*/
264
- DisableAlarmActionsRequest disable = DisableAlarmActionsRequest .builder ()
265
- .alarmNames (rq1 .alarmName (), rq2 .alarmName ()).build ();
266
- cloudwatch .disableAlarmActions (disable );
267
-
268
- DescribeAlarmsForMetricResponse describeResult = cloudwatch
269
- .describeAlarmsForMetric (DescribeAlarmsForMetricRequest .builder ()
270
- .dimensions (rq1 .dimensions ()).metricName (
271
- metricName ).namespace (rq1 .namespace ()).build ());
272
- assertEquals (2 , describeResult .metricAlarms ().size ());
273
- for (MetricAlarm alarm : describeResult .metricAlarms ()) {
274
- assertTrue (rq1 .alarmName ().equals (alarm .alarmName ())
275
- || rq2 .alarmName ().equals (alarm .alarmName ()));
276
- assertFalse (alarm .actionsEnabled ());
277
- }
243
+ cloudwatch .disableAlarmActions (r -> r .alarmNames (alarmNames ));
244
+
245
+ DescribeAlarmsForMetricResponse describeDisabledResult = describeAlarmsForMetric (rqs );
246
+
247
+ assertThat (describeDisabledResult .metricAlarms (), hasSize (2 ));
248
+
249
+ describeDisabledResult .metricAlarms ().forEach (alarm -> {
250
+ assertThat (alarm .alarmName (), isIn (alarmNames ));
251
+ assertThat (alarm .actionsEnabled (), is (false ));
252
+ });
278
253
279
254
/*
280
255
* Enable
281
256
*/
282
- EnableAlarmActionsRequest enable = EnableAlarmActionsRequest .builder ()
283
- .alarmNames (rq1 .alarmName (), rq2 .alarmName ()).build ();
284
- cloudwatch .enableAlarmActions (enable );
285
-
286
- describeResult = cloudwatch
287
- .describeAlarmsForMetric (DescribeAlarmsForMetricRequest .builder ()
288
- .dimensions (rq1 .dimensions ()).metricName (
289
- metricName ).namespace (rq1 .namespace ()).build ());
290
- assertEquals (2 , describeResult .metricAlarms ().size ());
291
- for (MetricAlarm alarm : describeResult .metricAlarms ()) {
292
- assertTrue (rq1 .alarmName ().equals (alarm .alarmName ())
293
- || rq2 .alarmName ().equals (alarm .alarmName ()));
294
- assertTrue (alarm .actionsEnabled ());
295
- }
257
+ cloudwatch .enableAlarmActions (r -> r .alarmNames (alarmNames ));
258
+
259
+ DescribeAlarmsForMetricResponse describeEnabledResult = describeAlarmsForMetric (rqs );
260
+
261
+ assertThat (describeEnabledResult .metricAlarms (), hasSize (2 ));
262
+ describeEnabledResult .metricAlarms ().forEach (alarm -> {
263
+ assertThat (alarm .alarmName (), isIn (alarmNames ));
264
+ assertThat (alarm .actionsEnabled (), is (true ));
265
+ });
266
+ }
267
+
268
+ private DescribeAlarmsForMetricResponse describeAlarmsForMetric (List <PutMetricAlarmRequest > rqs ) {
269
+ return cloudwatch .describeAlarmsForMetric (r -> r .dimensions (rqs .get (0 ).dimensions ())
270
+ .metricName (rqs .get (0 ).metricName ())
271
+ .namespace (rqs .get (0 ).namespace ()));
296
272
}
297
273
298
274
/**
299
275
* Creates two alarms on the metric name given and returns the two requests
300
276
* as an array.
301
277
*/
302
- private PutMetricAlarmRequest [] createTwoNewAlarms (String metricName ) {
303
- PutMetricAlarmRequest [] rqs = new PutMetricAlarmRequest [2 ];
278
+ private List <PutMetricAlarmRequest > createTwoNewAlarms (String metricName ) {
279
+
280
+ List <PutMetricAlarmRequest > rqs = new ArrayList <>();
304
281
305
282
/*
306
- * Put two metric alarms
283
+ * Create & put two metric alarms
307
284
*/
308
- rqs [ 0 ] = PutMetricAlarmRequest .builder ().actionsEnabled (true )
285
+ rqs . add ( PutMetricAlarmRequest .builder ().actionsEnabled (true )
309
286
.alarmDescription ("Some alarm description" ).alarmName (
310
287
"An Alarm Name" + metricName ).comparisonOperator (
311
288
"GreaterThanThreshold" ).dimensions (
@@ -314,11 +291,10 @@ private PutMetricAlarmRequest[] createTwoNewAlarms(String metricName) {
314
291
.metricName (metricName ).namespace ("AWS/EC2" )
315
292
.period (60 ).statistic ("Average" ).threshold (1.0 )
316
293
.unit ("Count" )
317
- .build ();
294
+ .build ()) ;
318
295
319
- cloudwatch .putMetricAlarm (rqs [0 ]);
320
296
321
- rqs [ 1 ] = PutMetricAlarmRequest .builder ().actionsEnabled (true )
297
+ rqs . add ( PutMetricAlarmRequest .builder ().actionsEnabled (true )
322
298
.alarmDescription ("Some alarm description 2" )
323
299
.alarmName ("An Alarm Name 2" + metricName )
324
300
.comparisonOperator ("GreaterThanThreshold" ).dimensions (
@@ -327,8 +303,9 @@ private PutMetricAlarmRequest[] createTwoNewAlarms(String metricName) {
327
303
.metricName (metricName ).namespace ("AWS/EC2" )
328
304
.period (60 ).statistic ("Average" ).threshold (2.0 )
329
305
.unit ("Count" )
330
- .build ();
331
- cloudwatch .putMetricAlarm (rqs [1 ]);
306
+ .build ());
307
+
308
+ rqs .forEach (cloudwatch ::putMetricAlarm );
332
309
return rqs ;
333
310
}
334
311
0 commit comments