1
+ package com .amazonaws .services .lambda .runtime .events ;
2
+
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .databind .ObjectMapper ;
5
+ import org .junit .jupiter .api .Test ;
6
+
7
+ import java .io .IOException ;
8
+ import java .nio .charset .StandardCharsets ;
9
+ import java .nio .file .Files ;
10
+ import java .nio .file .Path ;
11
+ import java .nio .file .Paths ;
12
+ import java .util .HashMap ;
13
+ import java .util .Map ;
14
+
15
+ import static com .amazonaws .services .lambda .runtime .events .IamPolicyResponseV1 .ALLOW ;
16
+ import static com .amazonaws .services .lambda .runtime .events .IamPolicyResponseV1 .EXECUTE_API_INVOKE ;
17
+ import static com .amazonaws .services .lambda .runtime .events .IamPolicyResponseV1 .VERSION_2012_10_17 ;
18
+ import static com .amazonaws .services .lambda .runtime .events .IamPolicyResponseV1 .allowStatement ;
19
+ import static com .amazonaws .services .lambda .runtime .events .IamPolicyResponseV1 .denyStatement ;
20
+ import static java .util .Collections .singletonList ;
21
+ import static java .util .Collections .singletonMap ;
22
+ import static net .javacrumbs .jsonunit .assertj .JsonAssertions .assertThatJson ;
23
+
24
+ public class IamPolicyResponseV1Test {
25
+
26
+ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
27
+
28
+ @ Test
29
+ public void testAllowStatement () throws JsonProcessingException {
30
+ IamPolicyResponseV1 iamPolicyResponse = IamPolicyResponseV1 .builder ()
31
+ .withPrincipalId ("me" )
32
+ .withPolicyDocument (IamPolicyResponseV1 .PolicyDocument .builder ()
33
+ .withVersion (VERSION_2012_10_17 )
34
+ .withStatement (singletonList (allowStatement ("arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*" )))
35
+ .build ())
36
+ .withUsageIdentifierKey ("123ABC" )
37
+ .build ();
38
+
39
+ String json = OBJECT_MAPPER .writeValueAsString (iamPolicyResponse );
40
+
41
+ assertThatJson (json ).isEqualTo (readResource ("iamPolicyV1Responses/allow.json" ));
42
+ }
43
+
44
+ @ Test
45
+ public void testDenyStatement () throws JsonProcessingException {
46
+ IamPolicyResponseV1 iamPolicyResponse = IamPolicyResponseV1 .builder ()
47
+ .withPrincipalId ("me" )
48
+ .withPolicyDocument (IamPolicyResponseV1 .PolicyDocument .builder ()
49
+ .withVersion (VERSION_2012_10_17 )
50
+ .withStatement (singletonList (denyStatement ("arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*" )))
51
+ .build ())
52
+ .withUsageIdentifierKey ("123ABC" )
53
+ .build ();
54
+
55
+ String json = OBJECT_MAPPER .writeValueAsString (iamPolicyResponse );
56
+
57
+ assertThatJson (json ).isEqualTo (readResource ("iamPolicyV1Responses/deny.json" ));
58
+ }
59
+
60
+ @ Test
61
+ public void testStatementWithCondition () throws JsonProcessingException {
62
+ Map <String , Map <String , Object >> conditions = new HashMap <>();
63
+ conditions .put ("DateGreaterThan" , singletonMap ("aws:TokenIssueTime" , "2020-01-01T00:00:01Z" ));
64
+
65
+ IamPolicyResponseV1 iamPolicyResponse = IamPolicyResponseV1 .builder ()
66
+ .withPrincipalId ("me" )
67
+ .withPolicyDocument (IamPolicyResponseV1 .PolicyDocument .builder ()
68
+ .withVersion (VERSION_2012_10_17 )
69
+ .withStatement (singletonList (IamPolicyResponseV1 .Statement .builder ()
70
+ .withAction (EXECUTE_API_INVOKE )
71
+ .withEffect (ALLOW )
72
+ .withResource (singletonList ("arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*" ))
73
+ .withCondition (conditions )
74
+ .build ()))
75
+ .build ())
76
+ .withUsageIdentifierKey ("123ABC" )
77
+ .build ();
78
+
79
+ String json = OBJECT_MAPPER .writeValueAsString (iamPolicyResponse );
80
+
81
+ assertThatJson (json ).isEqualTo (readResource ("iamPolicyV1Responses/allow-with-condition.json" ));
82
+ }
83
+
84
+ private String readResource (String name ) {
85
+ Path filePath = Paths .get ("src" , "test" , "resources" , name );
86
+ byte [] bytes = new byte [0 ];
87
+ try {
88
+ bytes = Files .readAllBytes (filePath );
89
+ } catch (IOException e ) {
90
+ e .printStackTrace ();
91
+ }
92
+ return new String (bytes , StandardCharsets .UTF_8 );
93
+ }
94
+ }
0 commit comments