|
6 | 6 | import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer; |
7 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; |
8 | 8 | import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.Arguments; |
| 11 | +import org.junit.jupiter.params.provider.MethodSource; |
9 | 12 |
|
10 | 13 | import java.io.ByteArrayOutputStream; |
11 | 14 | import java.io.IOException; |
12 | 15 | import java.nio.charset.StandardCharsets; |
13 | 16 | import java.nio.file.Files; |
14 | 17 | import java.nio.file.Path; |
15 | 18 | import java.nio.file.Paths; |
| 19 | +import java.util.stream.Stream; |
16 | 20 |
|
17 | 21 | import static org.junit.jupiter.api.Assertions.assertEquals; |
18 | 22 |
|
19 | | - |
20 | 23 | public class LambdaEventSerializersTest { |
21 | 24 |
|
22 | 25 | private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
23 | 26 | public static final ClassLoader SYSTEM_CLASS_LOADER = ClassLoader.getSystemClassLoader(); |
24 | 27 |
|
25 | | - @Test |
26 | | - public void testAPIGatewayProxyRequestEvent() throws IOException { |
27 | | - String expected = readEvent("api_gateway_proxy_request_event.json"); |
28 | | - String actual = deserializeSerializeJsonToString(expected, APIGatewayProxyRequestEvent.class); |
29 | | - |
30 | | - assertJsonEqual(expected, actual); |
31 | | - } |
32 | | - |
33 | | - @Test |
34 | | - public void testAPIGatewayProxyResponseEvent() throws IOException { |
35 | | - String expected = readEvent("api_gateway_proxy_response_event.json"); |
36 | | - String actual = deserializeSerializeJsonToString(expected, APIGatewayProxyResponseEvent.class); |
37 | | - |
38 | | - assertJsonEqual(expected, actual); |
39 | | - } |
40 | | - |
41 | | - @Test |
42 | | - public void testCloudFrontEvent() throws IOException { |
43 | | - String expected = readEvent("cloud_front_event.json"); |
44 | | - String actual = deserializeSerializeJsonToString(expected, CloudFrontEvent.class); |
45 | | - |
46 | | - assertJsonEqual(expected, actual); |
47 | | - } |
48 | | - |
49 | | - @Test |
50 | | - public void testCloudWatchLogsEvent() throws IOException { |
51 | | - String expected = readEvent("cloud_watch_logs_event.json"); |
52 | | - String actual = deserializeSerializeJsonToString(expected, CloudWatchLogsEvent.class); |
53 | | - |
54 | | - assertJsonEqual(expected, actual); |
55 | | - } |
56 | | - |
57 | | - @Test |
58 | | - public void testCodeCommitEvent() throws IOException { |
59 | | - String expected = readEvent("code_commit_event.json"); |
60 | | - String actual = deserializeSerializeJsonToString(expected, CodeCommitEvent.class); |
61 | | - |
62 | | - assertJsonEqual(expected, actual); |
63 | | - } |
64 | | - |
65 | | - @Test |
66 | | - public void testCognitoEvent() throws IOException { |
67 | | - String expected = readEvent("cognito_event.json"); |
68 | | - String actual = deserializeSerializeJsonToString(expected, CognitoEvent.class); |
69 | | - |
70 | | - assertJsonEqual(expected, actual); |
71 | | - } |
72 | | - |
73 | | - @Test |
74 | | - public void testConfigEvent() throws IOException { |
75 | | - String expected = readEvent("config_event.json"); |
76 | | - String actual = deserializeSerializeJsonToString(expected, ConfigEvent.class); |
77 | | - |
78 | | - assertJsonEqual(expected, actual); |
79 | | - } |
80 | | - |
81 | | - @Test |
82 | | - public void testDynamodbEvent() throws IOException { |
83 | | - String expected = readEvent("dynamodb_event.json"); |
84 | | - String actual = deserializeSerializeJsonToString(expected, DynamodbEvent.class); |
85 | | - |
86 | | - assertJsonEqual(expected, actual); |
87 | | - } |
88 | | - |
89 | | - @Test |
90 | | - public void testIoTButtonEvent() throws IOException { |
91 | | - String expected = readEvent("iot_button_event.json"); |
92 | | - String actual = deserializeSerializeJsonToString(expected, IoTButtonEvent.class); |
93 | | - |
94 | | - assertJsonEqual(expected, actual); |
95 | | - } |
96 | | - |
97 | | - @Test |
98 | | - public void testKinesisAnalyticsFirehoseInputPreprocessingEvent() throws IOException { |
99 | | - String expected = readEvent("kinesis_analytics_firehose_input_preprocessing_event.json"); |
100 | | - String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsFirehoseInputPreprocessingEvent.class); |
101 | | - |
102 | | - assertJsonEqual(expected, actual); |
103 | | - } |
104 | | - |
105 | | - @Test |
106 | | - public void testKinesisAnalyticsInputPreprocessingResponse() throws IOException { |
107 | | - String expected = readEvent("kinesis_analytics_input_preprocessing_response_event.json"); |
108 | | - String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsInputPreprocessingResponse.class); |
109 | | - |
110 | | - assertJsonEqual(expected, actual); |
111 | | - } |
112 | | - |
113 | | - @Test |
114 | | - public void testKinesisAnalyticsOutputDeliveryEvent() throws IOException { |
115 | | - String expected = readEvent("kinesis_analytics_output_delivery_event.json"); |
116 | | - String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsOutputDeliveryEvent.class); |
117 | | - |
118 | | - assertJsonEqual(expected, actual); |
119 | | - } |
120 | | - |
121 | | - @Test |
122 | | - public void testKinesisAnalyticsOutputDeliveryResponse() throws IOException { |
123 | | - String expected = readEvent("kinesis_analytics_output_delivery_response_event.json"); |
124 | | - String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsOutputDeliveryResponse.class); |
125 | | - |
126 | | - assertJsonEqual(expected, actual); |
127 | | - } |
128 | | - |
129 | | - @Test |
130 | | - public void testKinesisAnalyticsStreamsInputPreprocessingEvent() throws IOException { |
131 | | - String expected = readEvent("kinesis_analytics_streams_input_preprocessing_event.json"); |
132 | | - String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsStreamsInputPreprocessingEvent.class); |
133 | | - |
134 | | - assertJsonEqual(expected, actual); |
135 | | - } |
136 | | - |
137 | | - @Test |
138 | | - public void testKinesisEvent() throws IOException { |
139 | | - String expected = readEvent("kinesis_event.json"); |
140 | | - String actual = deserializeSerializeJsonToString(expected, KinesisEvent.class); |
141 | | - |
142 | | - assertJsonEqual(expected, actual); |
143 | | - } |
144 | | - |
145 | | - @Test |
146 | | - public void testKinesisFirehoseEvent() throws IOException { |
147 | | - String expected = readEvent("kinesis_firehose_event.json"); |
148 | | - String actual = deserializeSerializeJsonToString(expected, KinesisFirehoseEvent.class); |
149 | | - |
150 | | - assertJsonEqual(expected, actual); |
151 | | - } |
152 | | - |
153 | | - @Test |
154 | | - public void testLexEvent() throws IOException { |
155 | | - String expected = readEvent("lex_event.json"); |
156 | | - String actual = deserializeSerializeJsonToString(expected, LexEvent.class); |
157 | | - |
158 | | - assertJsonEqual(expected, actual); |
159 | | - } |
160 | | - |
161 | | - @Test |
162 | | - public void testS3Event() throws IOException { |
163 | | - String expected = readEvent("s3_event.json"); |
164 | | - String actual = deserializeSerializeJsonToString(expected, S3Event.class); |
165 | | - |
166 | | - assertJsonEqual(expected, actual); |
167 | | - } |
168 | | - |
169 | | - @Test |
170 | | - public void testScheduledEvent() throws IOException { |
171 | | - String expected = readEvent("scheduled_event.json"); |
172 | | - String actual = deserializeSerializeJsonToString(expected, ScheduledEvent.class); |
173 | | - |
174 | | - assertJsonEqual(expected, actual); |
175 | | - } |
176 | | - |
177 | | - @Test |
178 | | - public void testSNSEvent() throws IOException { |
179 | | - String expected = readEvent("sns_event.json"); |
180 | | - String actual = deserializeSerializeJsonToString(expected, SNSEvent.class); |
181 | | - |
182 | | - assertJsonEqual(expected, actual); |
183 | | - } |
184 | | - |
185 | | - @Test |
186 | | - public void testSQSEvent() throws IOException { |
187 | | - String expected = readEvent("sqs_event.json"); |
188 | | - String actual = deserializeSerializeJsonToString(expected, SQSEvent.class); |
| 28 | + private static Stream<Arguments> serdeArguments() { |
| 29 | + return Stream.of( |
| 30 | + Arguments.of("api_gateway_proxy_request_event.json", APIGatewayProxyRequestEvent.class), |
| 31 | + Arguments.of("api_gateway_proxy_response_event.json", APIGatewayProxyResponseEvent.class), |
| 32 | + Arguments.of("cloud_front_event.json", CloudFrontEvent.class), |
| 33 | + Arguments.of("cloud_watch_logs_event.json", CloudWatchLogsEvent.class), |
| 34 | + Arguments.of("code_commit_event.json", CodeCommitEvent.class), |
| 35 | + Arguments.of("api_gateway_proxy_response_event.json", APIGatewayProxyResponseEvent.class), |
| 36 | + Arguments.of("cognito_event.json", CognitoEvent.class), |
| 37 | + Arguments.of("config_event.json", ConfigEvent.class), |
| 38 | + Arguments.of("dynamodb_event.json", DynamodbEvent.class), |
| 39 | + Arguments.of("iot_button_event.json", IoTButtonEvent.class), |
| 40 | + Arguments.of("kinesis_analytics_firehose_input_preprocessing_event.json", KinesisAnalyticsFirehoseInputPreprocessingEvent.class), |
| 41 | + Arguments.of("kinesis_analytics_input_preprocessing_response_event.json", KinesisAnalyticsInputPreprocessingResponse.class), |
| 42 | + Arguments.of("kinesis_analytics_output_delivery_event.json", KinesisAnalyticsOutputDeliveryEvent.class), |
| 43 | + Arguments.of("kinesis_analytics_output_delivery_response_event.json", KinesisAnalyticsOutputDeliveryResponse.class), |
| 44 | + Arguments.of("kinesis_analytics_streams_input_preprocessing_event.json", KinesisAnalyticsStreamsInputPreprocessingEvent.class), |
| 45 | + Arguments.of("kinesis_event.json", KinesisEvent.class), |
| 46 | + Arguments.of("kinesis_firehose_event.json", KinesisFirehoseEvent.class), |
| 47 | + Arguments.of("lex_event.json", LexEvent.class), |
| 48 | + Arguments.of("s3_event.json", S3Event.class), |
| 49 | + Arguments.of("scheduled_event.json", ScheduledEvent.class), |
| 50 | + Arguments.of("sns_event.json", SNSEvent.class), |
| 51 | + Arguments.of("sqs_event.json", SQSEvent.class) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @ParameterizedTest(name = "Serde {0} Event") |
| 56 | + @MethodSource("serdeArguments") |
| 57 | + public void testAPIGatewayProxyRequestEvent(final String json, |
| 58 | + final Class<?> eventClass) throws IOException { |
| 59 | + String expected = readEvent(json); |
| 60 | + String actual = deserializeSerializeJsonToString(expected, eventClass); |
189 | 61 |
|
190 | 62 | assertJsonEqual(expected, actual); |
191 | 63 | } |
@@ -213,5 +85,4 @@ private String bytesToString(byte[] bytes) { |
213 | 85 | private void assertJsonEqual(String expected, String actual) throws IOException { |
214 | 86 | assertEquals(OBJECT_MAPPER.readTree(expected), OBJECT_MAPPER.readTree(actual)); |
215 | 87 | } |
216 | | - |
217 | 88 | } |
0 commit comments