22
33import static com .coffeebeans .springnativeawslambda .infra .TagUtils .TAG_VALUE_COST_CENTRE ;
44import static org .apache .commons .lang3 .StringUtils .EMPTY ;
5- import static org .assertj .core .api .Assertions .assertThat ;
65import static software .amazon .awscdk .CfnDeletionPolicy .RETAIN ;
76import static software .amazon .awscdk .assertions .Match .exact ;
87import static software .amazon .awscdk .assertions .Match .stringLikeRegexp ;
98
10- import com .coffeebeans .springnativeawslambda .infra .resource .CdkResourceType ;
9+ import com .coffeebeans .springnativeawslambda .infra .assertion .ApiRestAssert ;
10+ import com .coffeebeans .springnativeawslambda .infra .assertion .IamAssert ;
1111import com .coffeebeans .springnativeawslambda .infra .resource .IntrinsicFunctionBasedArn ;
1212import com .coffeebeans .springnativeawslambda .infra .resource .PolicyDocument ;
1313import com .coffeebeans .springnativeawslambda .infra .resource .PolicyPrincipal ;
3333import com .coffeebeans .springnativeawslambda .infra .resource .RoleProperties ;
3434import com .coffeebeans .springnativeawslambda .infra .resource .Tag ;
3535import java .util .List ;
36- import java .util .Map ;
3736import org .junit .jupiter .api .Test ;
3837
3938class ApiRestTest extends TemplateSupport {
@@ -46,27 +45,34 @@ void should_have_rest_api() {
4645 .tag (Tag .builder ().key ("COST_CENTRE" ).value (exact (TAG_VALUE_COST_CENTRE )).build ()).tag (Tag .builder ().key ("ENV" ).value (exact (TEST )).build ())
4746 .build ();
4847
49- final RestApi restApi = RestApi .builder (). properties ( restApiProperties ). build ();
50-
51- final Map < String , Map < String , Object >> actual = template . findResources ( CdkResourceType . APIGATEWAY_RESTAPI . getValue (), restApi );
48+ final RestApi expected = RestApi .builder ()
49+ . properties ( restApiProperties )
50+ . build ( );
5251
53- assertThat (actual ).isNotNull ().isNotEmpty ().hasSize (1 );
52+ ApiRestAssert .assertThat (template )
53+ .hasRestApi (expected );
5454 }
5555
5656 @ Test
5757 void should_have_rest_api_account () {
5858 final IntrinsicFunctionBasedArn cloudWatchRoleArn = IntrinsicFunctionBasedArn .builder ()
59- .attributesArn (stringLikeRegexp ("springnativeawslambdafunction(.*)" )). attributesArn ( "Arn" ). build ();
60-
61- final RestApiAccountProperties restApiAccountProperties = RestApiAccountProperties . builder (). cloudWatchRoleArn ( cloudWatchRoleArn ) .build ();
59+ .attributesArn (stringLikeRegexp ("springnativeawslambdafunction(.*)" ))
60+ . attributesArn ( "Arn" )
61+ .build ();
6262
63- final RestApiAccount restApiAccount = RestApiAccount .builder (). properties ( restApiAccountProperties )
64- .dependency ( stringLikeRegexp ( "springnativeawslambdafunctionrestapi(.*)" )). updateReplacePolicy ( stringLikeRegexp ( "Retain" ) )
65- .deletionPolicy ( stringLikeRegexp ( "Retain" )). build ();
63+ final RestApiAccountProperties restApiAccountProperties = RestApiAccountProperties .builder ()
64+ .cloudWatchRoleArn ( cloudWatchRoleArn )
65+ .build ();
6666
67- final Map <String , Map <String , Object >> actual = template .findResources (CdkResourceType .APIGATEWAY_RESTAPI_ACCOUNT .getValue (), restApiAccount );
67+ final RestApiAccount expected = RestApiAccount .builder ()
68+ .properties (restApiAccountProperties )
69+ .dependency (stringLikeRegexp ("springnativeawslambdafunctionrestapi(.*)" ))
70+ .updateReplacePolicy (stringLikeRegexp ("Retain" ))
71+ .deletionPolicy (stringLikeRegexp ("Retain" ))
72+ .build ();
6873
69- assertThat (actual ).isNotNull ().isNotEmpty ().hasSize (1 );
74+ ApiRestAssert .assertThat (template )
75+ .hasRestApiAccount (expected );
7076 }
7177
7278 @ Test
@@ -76,17 +82,16 @@ void should_have_rest_api_deployment() {
7682 .restApiId (ResourceReference .builder ().reference (stringLikeRegexp ("springnativeawslambdafunctionrestapi(.*)" )).build ())
7783 .description (exact ("Automatically created by the RestApi construct" )).build ();
7884
79- final RestApiDeployment restApiDeployment = RestApiDeployment .builder ().properties (restApiDeploymentProperties )
85+ final RestApiDeployment expected = RestApiDeployment .builder ().properties (restApiDeploymentProperties )
8086 .dependency (stringLikeRegexp ("springnativeawslambdafunctionrestapiproxyANY(.*)" ))
8187 .dependency (stringLikeRegexp ("springnativeawslambdafunctionrestapiproxy(.*)" ))
8288 .dependency (stringLikeRegexp ("springnativeawslambdafunctionrestapiANY(.*)" ))
8389 .dependency (stringLikeRegexp ("springnativeawslambdafunctionrestapinamePOST(.*)" ))
84- .dependency (stringLikeRegexp ("springnativeawslambdafunctionrestapiname(.*)" )).build ();
85-
86- final Map <String , Map <String , Object >> actual = template .findResources (CdkResourceType .APIGATEWAY_RESTAPI_DEPLOYMENT .getValue (),
87- restApiDeployment );
90+ .dependency (stringLikeRegexp ("springnativeawslambdafunctionrestapiname(.*)" ))
91+ .build ();
8892
89- assertThat (actual ).isNotNull ().isNotEmpty ().hasSize (1 );
93+ ApiRestAssert .assertThat (template )
94+ .hasRestApiDeployment (expected );
9095 }
9196
9297 @ Test
@@ -101,12 +106,12 @@ void should_have_rest_api_stage() {
101106 .stageName (exact ("test" )).tag (Tag .builder ().key ("COST_CENTRE" ).value (exact (TAG_VALUE_COST_CENTRE )).build ())
102107 .tag (Tag .builder ().key ("ENV" ).value (exact (TEST )).build ()).build ();
103108
104- final RestApiStage restApiDeployment = RestApiStage .builder ().properties (restApiDeploymentProperties )
105- .dependency (stringLikeRegexp ("springnativeawslambdafunctionrestapiAccount(.*)" )).build ();
106-
107- final Map <String , Map <String , Object >> actual = template .findResources (CdkResourceType .APIGATEWAY_RESTAPI_STAGE .getValue (), restApiDeployment );
109+ final RestApiStage expected = RestApiStage .builder ().properties (restApiDeploymentProperties )
110+ .dependency (stringLikeRegexp ("springnativeawslambdafunctionrestapiAccount(.*)" ))
111+ .build ();
108112
109- assertThat (actual ).isNotNull ().isNotEmpty ().hasSize (1 );
113+ ApiRestAssert .assertThat (template )
114+ .hasRestApiStage (expected );
110115 }
111116
112117 @ Test
@@ -120,11 +125,12 @@ void should_have_proxy_resource() {
120125 final RestApiResourceProperties restApiResourceProperties = RestApiResourceProperties .builder ().parentId (parentId ).pathPart (exact ("{proxy+}" ))
121126 .restApiId (restApiId ).build ();
122127
123- final RestApiResource restApiResource = RestApiResource .builder (). properties ( restApiResourceProperties ). build ();
124-
125- final Map < String , Map < String , Object >> actual = template . findResources ( CdkResourceType . APIGATEWAY_RESTAPI_RESOURCE . getValue (), restApiResource );
128+ final RestApiResource expected = RestApiResource .builder ()
129+ . properties ( restApiResourceProperties )
130+ . build ( );
126131
127- assertThat (actual ).isNotNull ().isNotEmpty ().hasSize (1 );
132+ ApiRestAssert .assertThat (template )
133+ .hasRestApiResource (expected );
128134 }
129135
130136 @ Test
@@ -133,38 +139,50 @@ void should_have_account_resource() {
133139 final IntrinsicFunctionBasedArn parentId = IntrinsicFunctionBasedArn .builder ()
134140 .attributesArn (stringLikeRegexp ("springnativeawslambdafunctionrestapi(.*)" )).attributesArn (exact ("RootResourceId" )).build ();
135141
136- final ResourceReference restApiId = ResourceReference .builder ().reference (stringLikeRegexp ("springnativeawslambdafunctionrestapi(.*)" )).build ();
142+ final ResourceReference restApiId = ResourceReference .builder ()
143+ .reference (stringLikeRegexp ("springnativeawslambdafunctionrestapi(.*)" ))
144+ .build ();
137145
138- final RestApiResourceProperties restApiResourceProperties = RestApiResourceProperties .builder ().parentId (parentId ).pathPart (exact ("name" ))
139- .restApiId (restApiId ).build ();
146+ final RestApiResourceProperties restApiResourceProperties = RestApiResourceProperties .builder ()
147+ .parentId (parentId ).pathPart (exact ("name" ))
148+ .restApiId (restApiId )
149+ .build ();
140150
141- final RestApiResource restApiResource = RestApiResource .builder ().properties (restApiResourceProperties ).build ();
151+ final RestApiResource expected = RestApiResource .builder ()
152+ .properties (restApiResourceProperties )
153+ .build ();
142154
143- final Map < String , Map < String , Object >> actual = template . findResources ( CdkResourceType . APIGATEWAY_RESTAPI_RESOURCE . getValue (), restApiResource );
144- assertThat ( actual ). isNotNull (). isNotEmpty (). hasSize ( 1 );
155+ ApiRestAssert . assertThat ( template )
156+ . hasRestApiResource ( expected );
145157 }
146158
147159 @ Test
148160 void should_have_post_method () {
149161
150162 final IntrinsicFunctionBasedArn uri = IntrinsicFunctionBasedArn .builder ().joinArn (EMPTY ).joinArn (
151- List .of ("arn:" , ResourceReference .builder ().reference (exact ("AWS::Partition" )).build (), ":apigateway:" ,
152- ResourceReference .builder ().reference (exact ("AWS::Region" )).build (), ":lambda:path/2015-03-31/functions/" ,
153- IntrinsicFunctionBasedArn .builder ().attributesArn (stringLikeRegexp ("springnativeawslambdafunction(.*)" )).attributesArn ("Arn" ).build (),
154- "/invocations" )).build ();
163+ List .of ("arn:" , ResourceReference .builder ().reference (exact ("AWS::Partition" )).build (), ":apigateway:" ,
164+ ResourceReference .builder ().reference (exact ("AWS::Region" )).build (), ":lambda:path/2015-03-31/functions/" ,
165+ IntrinsicFunctionBasedArn .builder ().attributesArn (stringLikeRegexp ("springnativeawslambdafunction(.*)" )).attributesArn ("Arn" ).build (),
166+ "/invocations" ))
167+ .build ();
155168
156169 final RestApiMethodIntegration restApiMethodIntegration = RestApiMethodIntegration .builder ().type (exact ("AWS_PROXY" ))
157- .integrationHttpMethod (exact ("POST" )).uri (uri ).build ();
170+ .integrationHttpMethod (exact ("POST" ))
171+ .uri (uri )
172+ .build ();
158173
159174 final RestApiMethodProperties restApiMethodProperties = RestApiNonRootMethodProperties .builder ().httpMethod (exact ("POST" ))
160175 .resourceId (ResourceReference .builder ().reference (stringLikeRegexp ("springnativeawslambdafunctionrestapiname(.*)" )).build ())
161176 .restApiId (ResourceReference .builder ().reference (stringLikeRegexp ("springnativeawslambdafunctionrestapi(.*)" )).build ())
162- .authorizationType (exact ("NONE" )).integration (restApiMethodIntegration ).build ();
177+ .authorizationType (exact ("NONE" )).integration (restApiMethodIntegration )
178+ .build ();
163179
164- final RestApiMethod restApiMethod = RestApiMethod .builder ().properties (restApiMethodProperties ).build ();
180+ final RestApiMethod expected = RestApiMethod .builder ()
181+ .properties (restApiMethodProperties )
182+ .build ();
165183
166- final Map < String , Map < String , Object >> actual = template . findResources ( CdkResourceType . APIGATEWAY_RESTAPI_METHOD . getValue (), restApiMethod );
167- assertThat ( actual ). isNotNull (). isNotEmpty (). hasSize ( 1 );
184+ ApiRestAssert . assertThat ( template )
185+ . hasRestApiMethod ( expected );
168186 }
169187
170188 @ Test
@@ -184,34 +202,42 @@ void should_have_proxy_method() {
184202 .restApiId (ResourceReference .builder ().reference (stringLikeRegexp ("springnativeawslambdafunctionrestapi(.*)" )).build ())
185203 .authorizationType (exact ("NONE" )).integration (restApiMethodIntegration ).build ();
186204
187- final RestApiMethod restApiMethod = RestApiMethod .builder ().properties (restApiMethodProperties ).build ();
205+ final RestApiMethod expected = RestApiMethod .builder ()
206+ .properties (restApiMethodProperties )
207+ .build ();
188208
189- final Map < String , Map < String , Object >> actual = template . findResources ( CdkResourceType . APIGATEWAY_RESTAPI_METHOD . getValue (), restApiMethod );
190- assertThat ( actual ). isNotNull (). isNotEmpty (). hasSize ( 1 );
209+ ApiRestAssert . assertThat ( template )
210+ . hasRestApiMethod ( expected );
191211 }
192212
193213 @ Test
194214 void should_have_root_method () {
195215
196216 final IntrinsicFunctionBasedArn uri = IntrinsicFunctionBasedArn .builder ().joinArn (EMPTY ).joinArn (
197- List .of ("arn:" , ResourceReference .builder ().reference (exact ("AWS::Partition" )).build (), ":apigateway:" ,
198- ResourceReference .builder ().reference (exact ("AWS::Region" )).build (), ":lambda:path/2015-03-31/functions/" ,
199- IntrinsicFunctionBasedArn .builder ().attributesArn (stringLikeRegexp ("springnativeawslambdafunction(.*)" )).attributesArn ("Arn" ).build (),
200- "/invocations" )).build ();
217+ List .of ("arn:" , ResourceReference .builder ().reference (exact ("AWS::Partition" )).build (), ":apigateway:" ,
218+ ResourceReference .builder ().reference (exact ("AWS::Region" )).build (), ":lambda:path/2015-03-31/functions/" ,
219+ IntrinsicFunctionBasedArn .builder ().attributesArn (stringLikeRegexp ("springnativeawslambdafunction(.*)" )).attributesArn ("Arn" ).build (),
220+ "/invocations" ))
221+ .build ();
201222
202223 final RestApiMethodIntegration restApiMethodIntegration = RestApiMethodIntegration .builder ().type (exact ("AWS_PROXY" ))
203- .integrationHttpMethod (exact ("POST" )).uri (uri ).build ();
224+ .integrationHttpMethod (exact ("POST" ))
225+ .uri (uri )
226+ .build ();
204227
205228 final RestApiMethodProperties restApiMethodProperties = RestApiRootMethodProperties .builder ().resourceId (
206229 IntrinsicFunctionBasedArn .builder ().attributesArn (stringLikeRegexp ("springnativeawslambdafunctionrestapi(.*)" ))
207230 .attributesArn (exact ("RootResourceId" )).build ()).httpMethod (exact ("ANY" ))
208231 .restApiId (ResourceReference .builder ().reference (stringLikeRegexp ("springnativeawslambdafunctionrestapi(.*)" )).build ())
209- .authorizationType (exact ("NONE" )).integration (restApiMethodIntegration ).build ();
232+ .authorizationType (exact ("NONE" )).integration (restApiMethodIntegration )
233+ .build ();
210234
211- final RestApiMethod restApiMethod = RestApiMethod .builder ().properties (restApiMethodProperties ).build ();
235+ final RestApiMethod expected = RestApiMethod .builder ()
236+ .properties (restApiMethodProperties )
237+ .build ();
212238
213- final Map < String , Map < String , Object >> actual = template . findResources ( CdkResourceType . APIGATEWAY_RESTAPI_METHOD . getValue (), restApiMethod );
214- assertThat ( actual ). isNotNull (). isNotEmpty (). hasSize ( 1 );
239+ ApiRestAssert . assertThat ( template )
240+ . hasRestApiMethod ( expected );
215241 }
216242
217243 @ Test
@@ -230,10 +256,9 @@ void should_have_role_with_AmazonAPIGatewayPushToCloudWatchLogs_policy_for_rest_
230256 final RoleProperties roleProperties = RoleProperties .builder ().managedPolicyArn (managedPolicyArn )
231257 .assumeRolePolicyDocument (assumeRolePolicyDocument ).build ();
232258
233- final Role role = Role .builder ().properties (roleProperties ).deletionPolicy (RETAIN ).updateReplacePolicy (RETAIN ).build ();
234-
235- final Map <String , Map <String , Object >> actual = template .findResources (CdkResourceType .ROLE .getValue (), role );
259+ final Role expected = Role .builder ().properties (roleProperties ).deletionPolicy (RETAIN ).updateReplacePolicy (RETAIN ).build ();
236260
237- assertThat (actual ).isNotNull ().isNotEmpty ().hasSize (1 );
261+ IamAssert .assertThat (template )
262+ .hasRole (expected );
238263 }
239264}
0 commit comments