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 .IamPolicyResponse .ALLOW ;
16+ import static com .amazonaws .services .lambda .runtime .events .IamPolicyResponse .EXECUTE_API_INVOKE ;
17+ import static com .amazonaws .services .lambda .runtime .events .IamPolicyResponse .VERSION_2012_10_17 ;
18+ import static com .amazonaws .services .lambda .runtime .events .IamPolicyResponse .allowStatement ;
19+ import static com .amazonaws .services .lambda .runtime .events .IamPolicyResponse .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 IamPolicyResponseTest {
25+
26+ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
27+
28+ @ Test
29+ public void testAllowStatement () throws JsonProcessingException {
30+ IamPolicyResponse iamPolicyResponse = IamPolicyResponse .builder ()
31+ .withPrincipalId ("me" )
32+ .withPolicyDocument (IamPolicyResponse .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+ .build ();
37+
38+ String json = OBJECT_MAPPER .writeValueAsString (iamPolicyResponse );
39+
40+ assertThatJson (json ).isEqualTo (readResource ("iamPolicyResponses/allow.json" ));
41+ }
42+
43+ @ Test
44+ public void testDenyStatement () throws JsonProcessingException {
45+ IamPolicyResponse iamPolicyResponse = IamPolicyResponse .builder ()
46+ .withPrincipalId ("me" )
47+ .withPolicyDocument (IamPolicyResponse .PolicyDocument .builder ()
48+ .withVersion (VERSION_2012_10_17 )
49+ .withStatement (singletonList (denyStatement ("arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*" )))
50+ .build ())
51+ .build ();
52+
53+ String json = OBJECT_MAPPER .writeValueAsString (iamPolicyResponse );
54+
55+ assertThatJson (json ).isEqualTo (readResource ("iamPolicyResponses/deny.json" ));
56+ }
57+
58+ @ Test
59+ public void testStatementWithCondition () throws JsonProcessingException {
60+ Map <String , Map <String , Object >> conditions = new HashMap <>();
61+ conditions .put ("DateGreaterThan" , singletonMap ("aws:TokenIssueTime" , "2020-01-01T00:00:01Z" ));
62+
63+ IamPolicyResponse iamPolicyResponse = IamPolicyResponse .builder ()
64+ .withPrincipalId ("me" )
65+ .withPolicyDocument (IamPolicyResponse .PolicyDocument .builder ()
66+ .withVersion (VERSION_2012_10_17 )
67+ .withStatement (singletonList (IamPolicyResponse .Statement .builder ()
68+ .withAction (EXECUTE_API_INVOKE )
69+ .withEffect (ALLOW )
70+ .withResource (singletonList ("arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*" ))
71+ .withCondition (conditions )
72+ .build ()))
73+ .build ())
74+ .build ();
75+
76+ String json = OBJECT_MAPPER .writeValueAsString (iamPolicyResponse );
77+
78+ assertThatJson (json ).isEqualTo (readResource ("iamPolicyResponses/allow-with-condition.json" ));
79+ }
80+
81+ private String readResource (String name ) {
82+ Path filePath = Paths .get ("src" , "test" , "resources" , name );
83+ byte [] bytes = new byte [0 ];
84+ try {
85+ bytes = Files .readAllBytes (filePath );
86+ } catch (IOException e ) {
87+ e .printStackTrace ();
88+ }
89+ return new String (bytes , StandardCharsets .UTF_8 );
90+ }
91+ }
0 commit comments