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
+ }
0 commit comments