20
20
import static org .junit .jupiter .api .Assertions .fail ;
21
21
import static org .mockito .Mockito .when ;
22
22
23
- import java .io .IOException ;
24
- import java .util .ArrayList ;
25
- import java .util .HashMap ;
26
- import java .util .List ;
27
- import java .util .Map ;
28
- import java .util .stream .Stream ;
29
-
30
- import org .aspectj .lang .ProceedingJoinPoint ;
31
- import org .aspectj .lang .Signature ;
32
- import org .junit .jupiter .api .BeforeEach ;
33
- import org .junit .jupiter .api .Test ;
34
- import org .junit .jupiter .params .ParameterizedTest ;
35
- import org .junit .jupiter .params .provider .Arguments ;
36
- import org .junit .jupiter .params .provider .ArgumentsSource ;
37
- import org .junit .jupiter .params .provider .MethodSource ;
38
- import org .mockito .Mock ;
39
- import org .mockito .MockitoAnnotations ;
40
-
41
23
import com .amazonaws .services .lambda .runtime .Context ;
42
24
import com .amazonaws .services .lambda .runtime .RequestHandler ;
43
25
import com .amazonaws .services .lambda .runtime .events .APIGatewayProxyRequestEvent ;
55
37
import com .amazonaws .services .lambda .runtime .events .KinesisFirehoseEvent ;
56
38
import com .amazonaws .services .lambda .runtime .events .RabbitMQEvent ;
57
39
import com .amazonaws .services .lambda .runtime .events .SNSEvent ;
40
+ import com .amazonaws .services .lambda .runtime .events .SQSBatchResponse ;
58
41
import com .amazonaws .services .lambda .runtime .events .SQSEvent ;
59
42
import com .amazonaws .services .lambda .runtime .events .ScheduledEvent ;
60
- import com .amazonaws .services .lambda .runtime .serialization . PojoSerializer ;
61
- import com .amazonaws .services .lambda .runtime .serialization . events . LambdaEventSerializers ;
43
+ import com .amazonaws .services .lambda .runtime .events . StreamsEventResponse ;
44
+ import com .amazonaws .services .lambda .runtime .tests . annotations . Event ;
62
45
import com .networknt .schema .SpecVersion ;
63
-
46
+ import java .io .IOException ;
47
+ import java .util .ArrayList ;
48
+ import java .util .HashMap ;
49
+ import java .util .List ;
50
+ import java .util .Map ;
51
+ import java .util .stream .Collectors ;
52
+ import java .util .stream .Stream ;
53
+ import org .aspectj .lang .ProceedingJoinPoint ;
54
+ import org .aspectj .lang .Signature ;
55
+ import org .junit .jupiter .api .BeforeEach ;
56
+ import org .junit .jupiter .api .Test ;
57
+ import org .junit .jupiter .params .ParameterizedTest ;
58
+ import org .junit .jupiter .params .provider .Arguments ;
59
+ import org .junit .jupiter .params .provider .ArgumentsSource ;
60
+ import org .junit .jupiter .params .provider .MethodSource ;
61
+ import org .mockito .Mock ;
62
+ import org .mockito .MockitoAnnotations ;
64
63
import software .amazon .lambda .powertools .validation .Validation ;
65
64
import software .amazon .lambda .powertools .validation .ValidationConfig ;
66
65
import software .amazon .lambda .powertools .validation .ValidationException ;
67
66
import software .amazon .lambda .powertools .validation .handlers .GenericSchemaV7APIGatewayProxyRequestEventHandler ;
68
67
import software .amazon .lambda .powertools .validation .handlers .GenericSchemaV7StringHandler ;
68
+ import software .amazon .lambda .powertools .validation .handlers .KinesisHandlerWithError ;
69
+ import software .amazon .lambda .powertools .validation .handlers .SQSHandlerWithError ;
69
70
import software .amazon .lambda .powertools .validation .handlers .SQSWithCustomEnvelopeHandler ;
70
71
import software .amazon .lambda .powertools .validation .handlers .SQSWithWrongEnvelopeHandler ;
72
+ import software .amazon .lambda .powertools .validation .handlers .StandardKinesisHandler ;
73
+ import software .amazon .lambda .powertools .validation .handlers .StandardSQSHandler ;
71
74
import software .amazon .lambda .powertools .validation .handlers .ValidationInboundAPIGatewayV2HTTPEventHandler ;
72
75
import software .amazon .lambda .powertools .validation .model .MyCustomEvent ;
73
76
74
77
75
- public class ValidationAspectTest {
78
+ class ValidationAspectTest {
76
79
77
80
@ Mock
78
81
Validation validation ;
@@ -167,7 +170,7 @@ void testValidateOutboundJsonSchemaWithHandledExceptions(Object object) throws T
167
170
}
168
171
169
172
@ Test
170
- public void testValidateOutboundJsonSchema_APIGWV2 () throws Throwable {
173
+ void testValidateOutboundJsonSchema_APIGWV2 () throws Throwable {
171
174
when (validation .schemaVersion ()).thenReturn (SpecVersion .VersionFlag .V7 );
172
175
when (pjp .getSignature ()).thenReturn (signature );
173
176
when (pjp .getSignature ().getDeclaringType ()).thenReturn (RequestHandler .class );
@@ -187,7 +190,7 @@ public void testValidateOutboundJsonSchema_APIGWV2() throws Throwable {
187
190
}
188
191
189
192
@ Test
190
- public void validate_inputOK_schemaInClasspath_shouldValidate () {
193
+ void validate_inputOK_schemaInClasspath_shouldValidate () {
191
194
GenericSchemaV7APIGatewayProxyRequestEventHandler handler = new GenericSchemaV7APIGatewayProxyRequestEventHandler ();
192
195
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent ();
193
196
event .setBody ("{" +
@@ -204,7 +207,7 @@ public void validate_inputOK_schemaInClasspath_shouldValidate() {
204
207
}
205
208
206
209
@ Test
207
- public void validate_inputKO_schemaInClasspath_shouldThrowValidationException () {
210
+ void validate_inputKO_schemaInClasspath_shouldThrowValidationException () {
208
211
GenericSchemaV7APIGatewayProxyRequestEventHandler handler = new GenericSchemaV7APIGatewayProxyRequestEventHandler ();
209
212
210
213
Map <String , String > headers = new HashMap <>();
@@ -232,7 +235,7 @@ public void validate_inputKO_schemaInClasspath_shouldThrowValidationException()
232
235
}
233
236
234
237
@ Test
235
- public void validate_inputOK_schemaInString_shouldValidate () {
238
+ void validate_inputOK_schemaInString_shouldValidate () {
236
239
ValidationInboundAPIGatewayV2HTTPEventHandler handler = new ValidationInboundAPIGatewayV2HTTPEventHandler ();
237
240
APIGatewayV2HTTPEvent event = new APIGatewayV2HTTPEvent ();
238
241
event .setBody ("{" +
@@ -248,7 +251,7 @@ public void validate_inputOK_schemaInString_shouldValidate() {
248
251
249
252
250
253
@ Test
251
- public void validate_inputKO_schemaInString_shouldThrowValidationException () {
254
+ void validate_inputKO_schemaInString_shouldThrowValidationException () {
252
255
ValidationInboundAPIGatewayV2HTTPEventHandler handler = new ValidationInboundAPIGatewayV2HTTPEventHandler ();
253
256
254
257
Map <String , String > headers = new HashMap <>();
@@ -268,49 +271,77 @@ public void validate_inputKO_schemaInString_shouldThrowValidationException() {
268
271
assertThat (response .getMultiValueHeaders ()).isEmpty ();
269
272
}
270
273
271
- @ Test
272
- public void validate_SQS () {
273
- PojoSerializer <SQSEvent > pojoSerializer =
274
- LambdaEventSerializers .serializerFor (SQSEvent .class , ClassLoader .getSystemClassLoader ());
275
- SQSEvent event = pojoSerializer .fromJson (this .getClass ().getResourceAsStream ("/sqs.json" ));
276
-
274
+ @ ParameterizedTest
275
+ @ Event (value = "sqs.json" , type = SQSEvent .class )
276
+ void validate_SQS (SQSEvent event ) {
277
277
GenericSchemaV7StringHandler <Object > handler = new GenericSchemaV7StringHandler <>();
278
278
assertThat (handler .handleRequest (event , context )).isEqualTo ("OK" );
279
279
}
280
280
281
- @ Test
282
- public void validate_SQS_CustomEnvelopeTakePrecedence () {
283
- PojoSerializer <SQSEvent > pojoSerializer =
284
- LambdaEventSerializers .serializerFor (SQSEvent .class , ClassLoader .getSystemClassLoader ());
285
- SQSEvent event = pojoSerializer .fromJson (this .getClass ().getResourceAsStream ("/sqs_message.json" ));
281
+ @ ParameterizedTest
282
+ @ Event (value = "sqs_invalid_messages.json" , type = SQSEvent .class )
283
+ void validate_SQS_with_validation_partial_failure (SQSEvent event ) {
284
+ StandardSQSHandler handler = new StandardSQSHandler ();
285
+ SQSBatchResponse response = handler .handleRequest (event , context );
286
+ assertThat (response .getBatchItemFailures ()).hasSize (2 );
287
+ assertThat (response .getBatchItemFailures ().stream ().map (SQSBatchResponse .BatchItemFailure ::getItemIdentifier ).collect (
288
+ Collectors .toList ())).contains ("d9144555-9a4f-4ec3-99a0-fc4e625a8db3" , "d9144555-9a4f-4ec3-99a0-fc4e625a8db5" );
289
+ }
290
+
291
+ @ ParameterizedTest
292
+ @ Event (value = "sqs_invalid_messages.json" , type = SQSEvent .class )
293
+ void validate_SQS_with_partial_failure (SQSEvent event ) {
294
+ SQSHandlerWithError handler = new SQSHandlerWithError ();
295
+ SQSBatchResponse response = handler .handleRequest (event , context );
296
+ assertThat (response .getBatchItemFailures ()).hasSize (3 );
297
+ assertThat (response .getBatchItemFailures ().stream ().map (SQSBatchResponse .BatchItemFailure ::getItemIdentifier ).collect (
298
+ Collectors .toList ())).contains ("d9144555-9a4f-4ec3-99a0-fc4e625a8db3" , "d9144555-9a4f-4ec3-99a0-fc4e625a8db5" , "1234" );
299
+ }
286
300
301
+ @ ParameterizedTest
302
+ @ Event (value = "sqs_message.json" , type = SQSEvent .class )
303
+ void validate_SQS_CustomEnvelopeTakePrecedence (SQSEvent event ) {
287
304
SQSWithCustomEnvelopeHandler handler = new SQSWithCustomEnvelopeHandler ();
288
305
assertThat (handler .handleRequest (event , context )).isEqualTo ("OK" );
289
306
}
290
307
291
- @ Test
292
- public void validate_SQS_WrongEnvelope_shouldThrowValidationException () {
293
- PojoSerializer <SQSEvent > pojoSerializer =
294
- LambdaEventSerializers .serializerFor (SQSEvent .class , ClassLoader .getSystemClassLoader ());
295
- SQSEvent event = pojoSerializer .fromJson (this .getClass ().getResourceAsStream ("/sqs_message.json" ));
296
-
308
+ @ ParameterizedTest
309
+ @ Event (value = "sqs_message.json" , type = SQSEvent .class )
310
+ void validate_SQS_WrongEnvelope_shouldThrowValidationException (SQSEvent event ) {
297
311
SQSWithWrongEnvelopeHandler handler = new SQSWithWrongEnvelopeHandler ();
298
312
assertThatExceptionOfType (ValidationException .class ).isThrownBy (() -> handler .handleRequest (event , context ));
299
313
}
300
314
301
- @ Test
302
- public void validate_Kinesis () {
303
- PojoSerializer <KinesisEvent > pojoSerializer =
304
- LambdaEventSerializers .serializerFor (KinesisEvent .class , ClassLoader .getSystemClassLoader ());
305
- KinesisEvent event = pojoSerializer .fromJson (this .getClass ().getResourceAsStream ("/kinesis.json" ));
306
-
315
+ @ ParameterizedTest
316
+ @ Event (value = "kinesis.json" , type = KinesisEvent .class )
317
+ void validate_Kinesis (KinesisEvent event ) {
307
318
GenericSchemaV7StringHandler <Object > handler = new GenericSchemaV7StringHandler <>();
308
319
assertThat (handler .handleRequest (event , context )).isEqualTo ("OK" );
309
320
}
310
321
322
+ @ ParameterizedTest
323
+ @ Event (value = "kinesis_invalid_messages.json" , type = KinesisEvent .class )
324
+ void validate_Kinesis_with_validation_partial_failure (KinesisEvent event ) {
325
+ StandardKinesisHandler handler = new StandardKinesisHandler ();
326
+ StreamsEventResponse response = handler .handleRequest (event , context );
327
+ assertThat (response .getBatchItemFailures ()).hasSize (2 );
328
+ assertThat (response .getBatchItemFailures ().stream ().map (StreamsEventResponse .BatchItemFailure ::getItemIdentifier ).collect (
329
+ Collectors .toList ())).contains ("49545115243490985018280067714973144582180062593244200962" , "49545115243490985018280067714973144582180062593244200964" );
330
+ }
331
+
332
+ @ ParameterizedTest
333
+ @ Event (value = "kinesis_invalid_messages.json" , type = KinesisEvent .class )
334
+ void validate_Kinesis_with_partial_failure (KinesisEvent event ) {
335
+ KinesisHandlerWithError handler = new KinesisHandlerWithError ();
336
+ StreamsEventResponse response = handler .handleRequest (event , context );
337
+ assertThat (response .getBatchItemFailures ()).hasSize (3 );
338
+ assertThat (response .getBatchItemFailures ().stream ().map (StreamsEventResponse .BatchItemFailure ::getItemIdentifier ).collect (
339
+ Collectors .toList ())).contains ("49545115243490985018280067714973144582180062593244200962" , "49545115243490985018280067714973144582180062593244200964" , "1234" );
340
+ }
341
+
311
342
@ ParameterizedTest
312
343
@ MethodSource ("provideEventAndEventType" )
313
- public void validateEEvent (String jsonResource , Class eventClass ) throws IOException {
344
+ void validateEEvent (String jsonResource , Class eventClass ) throws IOException {
314
345
Object event = ValidationConfig .get ().getObjectMapper ()
315
346
.readValue (this .getClass ().getResourceAsStream (jsonResource ), eventClass );
316
347
0 commit comments