Skip to content

Commit 4469cd8

Browse files
committed
Added the object representation of the CloudWatch Alarm (metric and composite types)
Updated encoding properties to remove warnings
1 parent 5c53c44 commit 4469cd8

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed

aws-lambda-java-events/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
<maven.compiler.source>1.8</maven.compiler.source>
3636
<maven.compiler.target>1.8</maven.compiler.target>
3737
<lombok.version>1.18.22</lombok.version>
38+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
3840
</properties>
3941

4042
<distributionManagement>
@@ -69,6 +71,18 @@
6971
<version>2.36.1</version>
7072
<scope>test</scope>
7173
</dependency>
74+
<dependency>
75+
<groupId>org.skyscreamer</groupId>
76+
<artifactId>jsonassert</artifactId>
77+
<version>1.5.0</version>
78+
<scope>test</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>com.amazonaws</groupId>
82+
<artifactId>aws-lambda-java-serialization</artifactId>
83+
<version>1.1.5</version>
84+
<scope>test</scope>
85+
</dependency>
7286

7387
<dependency>
7488
<groupId>org.projectlombok</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.amazonaws.services.lambda.runtime.events;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
@Data
12+
@Builder(setterPrefix = "with")
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
public class CloudWatchAlarmEvent {
16+
17+
private String source;
18+
private String alarmArn;
19+
private String accountId;
20+
private String time;
21+
private String region;
22+
private AlarmData alarmData;
23+
24+
@Data
25+
@Builder(setterPrefix = "with")
26+
@NoArgsConstructor
27+
@AllArgsConstructor
28+
public static class AlarmData {
29+
private String alarmName;
30+
private State state;
31+
private State previousState;
32+
private Configuration configuration;
33+
}
34+
35+
@Data
36+
@Builder(setterPrefix = "with")
37+
@NoArgsConstructor
38+
@AllArgsConstructor
39+
private static class State {
40+
private String value;
41+
private String reason;
42+
private String reasonData;
43+
private String timestamp;
44+
private String actionsSuppressedBy;
45+
private String actionsSuppressedReason;
46+
}
47+
48+
@Data
49+
@Builder(setterPrefix = "with")
50+
@NoArgsConstructor
51+
@AllArgsConstructor
52+
private static class Configuration {
53+
private String description;
54+
private List<Metric> metrics;
55+
private Boolean returnData;
56+
57+
private String alarmRule;
58+
private String actionsSuppressor;
59+
private Integer actionsSuppressorWaitPeriod;
60+
private Integer actionsSuppressorExtensionPeriod;
61+
}
62+
63+
@Data
64+
@Builder(setterPrefix = "with")
65+
@NoArgsConstructor
66+
@AllArgsConstructor
67+
private static class Metric {
68+
private String id;
69+
private MetricStat metricStat;
70+
private Integer period;
71+
private String stat;
72+
private String unit;
73+
}
74+
75+
@Data
76+
@Builder(setterPrefix = "with")
77+
@NoArgsConstructor
78+
@AllArgsConstructor
79+
private static class MetricStat {
80+
private String namespace;
81+
private String name;
82+
private Map<String, String> dimensions;
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.amazonaws.services.lambda.runtime.events;
2+
3+
import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer;
4+
import com.amazonaws.services.lambda.runtime.serialization.factories.JacksonFactory;
5+
import org.json.JSONException;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.IOException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
15+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
16+
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;
17+
18+
public class CloudWatchAlarmEventTest {
19+
20+
JacksonFactory jacksonFactory = JacksonFactory.getInstance();
21+
22+
@Test
23+
public void serdeCloudWatchCompositeAlarmEvent() throws JSONException {
24+
String expected = readResource("cloudwatch-composite-alarm.json");
25+
String actual = deserializeSerializeJsonToString(expected, CloudWatchAlarmEvent.class);
26+
27+
assertEquals(expected, actual, STRICT);
28+
}
29+
30+
@Test
31+
public void serdeCloudWatchMetricAlarmEvent() throws JSONException {
32+
String expected = readResource("cloudwatch-composite-alarm.json");
33+
String actual = deserializeSerializeJsonToString(expected, CloudWatchAlarmEvent.class);
34+
35+
assertEquals(expected, actual, STRICT);
36+
}
37+
38+
private <T> String deserializeSerializeJsonToString(String expected, Class<T> modelClass) {
39+
PojoSerializer<T> serializer = jacksonFactory.getSerializer(modelClass);
40+
T event = serializer.fromJson(expected);
41+
42+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
43+
serializer.toJson(event, baos);
44+
45+
return new String(baos.toByteArray(), StandardCharsets.UTF_8);
46+
}
47+
48+
private String readResource(String name) {
49+
Path filePath = Paths.get("src", "test", "resources", name);
50+
byte[] bytes = new byte[0];
51+
try {
52+
bytes = Files.readAllBytes(filePath);
53+
} catch (IOException e) {
54+
e.printStackTrace();
55+
}
56+
return new String(bytes, StandardCharsets.UTF_8);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"source": "aws.cloudwatch",
3+
"alarmArn": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:SuppressionDemo.Main",
4+
"accountId": "111122223333",
5+
"time": "2023-08-04T12:56:46.138+0000",
6+
"region": "us-east-1",
7+
"alarmData": {
8+
"alarmName": "CompositeDemo.Main",
9+
"state": {
10+
"value": "ALARM",
11+
"reason": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:CompositeDemo.FirstChild transitioned to ALARM at Friday 04 August, 2023 12:54:46 UTC",
12+
"reasonData": "{\"triggeringAlarms\":[{\"arn\":\"arn:aws:cloudwatch:us-east-1:111122223333:alarm:CompositeDemo.FirstChild\",\"state\":{\"value\":\"ALARM\",\"timestamp\":\"2023-08-04T12:54:46.138+0000\"}}]}",
13+
"timestamp": "2023-08-04T12:56:46.138+0000"
14+
},
15+
"previousState": {
16+
"value": "ALARM",
17+
"reason": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:CompositeDemo.FirstChild transitioned to ALARM at Friday 04 August, 2023 12:54:46 UTC",
18+
"reasonData": "{\"triggeringAlarms\":[{\"arn\":\"arn:aws:cloudwatch:us-east-1:111122223333:alarm:CompositeDemo.FirstChild\",\"state\":{\"value\":\"ALARM\",\"timestamp\":\"2023-08-04T12:54:46.138+0000\"}}]}",
19+
"timestamp": "2023-08-04T12:54:46.138+0000",
20+
"actionsSuppressedBy": "WaitPeriod",
21+
"actionsSuppressedReason": "Actions suppressed by WaitPeriod"
22+
},
23+
"configuration": {
24+
"alarmRule": "ALARM(CompositeDemo.FirstChild) OR ALARM(CompositeDemo.SecondChild)",
25+
"actionsSuppressor": "CompositeDemo.ActionsSuppressor",
26+
"actionsSuppressorWaitPeriod": 120,
27+
"actionsSuppressorExtensionPeriod": 180
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"source": "aws.cloudwatch",
3+
"alarmArn": "arn:aws:cloudwatch:us-east-1:444455556666:alarm:lambda-demo-metric-alarm",
4+
"accountId": "444455556666",
5+
"time": "2023-08-04T12:36:15.490+0000",
6+
"region": "us-east-1",
7+
"alarmData": {
8+
"alarmName": "lambda-demo-metric-alarm",
9+
"state": {
10+
"value": "ALARM",
11+
"reason": "test",
12+
"timestamp": "2023-08-04T12:36:15.490+0000"
13+
},
14+
"previousState": {
15+
"value": "INSUFFICIENT_DATA",
16+
"reason": "Insufficient Data: 5 datapoints were unknown.",
17+
"reasonData": "{\"version\":\"1.0\",\"queryDate\":\"2023-08-04T12:31:29.591+0000\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[],\"threshold\":5.0,\"evaluatedDatapoints\":[{\"timestamp\":\"2023-08-04T12:30:00.000+0000\"},{\"timestamp\":\"2023-08-04T12:29:00.000+0000\"},{\"timestamp\":\"2023-08-04T12:28:00.000+0000\"},{\"timestamp\":\"2023-08-04T12:27:00.000+0000\"},{\"timestamp\":\"2023-08-04T12:26:00.000+0000\"}]}",
18+
"timestamp": "2023-08-04T12:31:29.595+0000"
19+
},
20+
"configuration": {
21+
"description": "Metric Alarm to test Lambda actions",
22+
"metrics": [
23+
{
24+
"id": "1234e046-06f0-a3da-9534-EXAMPLEe4c",
25+
"metricStat": {
26+
"metric": {
27+
"namespace": "AWS/Logs",
28+
"name": "CallCount",
29+
"dimensions": {
30+
"InstanceId": "i-12345678"
31+
}
32+
},
33+
"period": 60,
34+
"stat": "Average",
35+
"unit": "Percent"
36+
},
37+
"returnData": True
38+
}
39+
]
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)