Skip to content

Commit 7b33b93

Browse files
authored
Cdk assertions (#36)
* added assertj style for cdk `CDKAssert` Signed-off-by: Matto <muhamadto@gmail.com> * added assertj style for cdk `CDKAssert` Signed-off-by: Matto <muhamadto@gmail.com> --------- Signed-off-by: Matto <muhamadto@gmail.com>
1 parent e6f6dbf commit 7b33b93

File tree

6 files changed

+249
-130
lines changed

6 files changed

+249
-130
lines changed

spring-native-aws-lambda-infra/src/test/java/com/coffeebeans/springnativeawslambda/infra/ApiRestTest.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@
1919
package com.coffeebeans.springnativeawslambda.infra;
2020

2121
import static com.coffeebeans.springnativeawslambda.infra.TagUtils.TAG_VALUE_COST_CENTRE;
22+
import static com.coffeebeans.springnativeawslambda.infra.assertion.CDKAssert.assertThat;
2223
import static com.coffeebeans.springnativeawslambda.infra.resource.Policy.PolicyStatement.PolicyStatementEffect.ALLOW;
2324
import static org.apache.commons.lang3.StringUtils.EMPTY;
2425
import static software.amazon.awscdk.assertions.Match.exact;
2526
import static software.amazon.awscdk.assertions.Match.stringLikeRegexp;
2627

27-
import com.coffeebeans.springnativeawslambda.infra.assertion.ApiRestAssert;
2828
import com.coffeebeans.springnativeawslambda.infra.assertion.IamAssert;
2929
import com.coffeebeans.springnativeawslambda.infra.resource.IntrinsicFunctionArn;
3030
import com.coffeebeans.springnativeawslambda.infra.resource.Policy.PolicyDocument;
3131
import com.coffeebeans.springnativeawslambda.infra.resource.Policy.PolicyPrincipal;
3232
import com.coffeebeans.springnativeawslambda.infra.resource.Policy.PolicyStatement;
3333
import com.coffeebeans.springnativeawslambda.infra.resource.ResourceReference;
34-
import com.coffeebeans.springnativeawslambda.infra.resource.RestApi;
35-
import com.coffeebeans.springnativeawslambda.infra.resource.RestApi.RestApiProperties;
3634
import com.coffeebeans.springnativeawslambda.infra.resource.RestApiAccount;
3735
import com.coffeebeans.springnativeawslambda.infra.resource.RestApiAccount.RestApiAccountProperties;
3836
import com.coffeebeans.springnativeawslambda.infra.resource.RestApiDeployment;
@@ -56,18 +54,11 @@ class ApiRestTest extends TemplateSupport {
5654

5755
@Test
5856
void should_have_rest_api() {
59-
final RestApiProperties restApiProperties = RestApiProperties.builder()
60-
.name("spring-native-aws-lambda-function-rest-api")
61-
.tag(Tag.builder().key("COST_CENTRE").value(TAG_VALUE_COST_CENTRE).build())
62-
.tag(Tag.builder().key("ENV").value(TEST).build())
63-
.build();
64-
65-
final RestApi expected = RestApi.builder()
66-
.properties(restApiProperties)
67-
.build();
6857

69-
ApiRestAssert.assertThat(template)
70-
.hasRestApi(expected);
58+
assertThat(template)
59+
.hasRestApiWithName("spring-native-aws-lambda-function-rest-api")
60+
.hasTag("COST_CENTRE", TAG_VALUE_COST_CENTRE)
61+
.hasTag("ENV", TEST);
7162
}
7263

7364
@Test
@@ -88,7 +79,7 @@ void should_have_rest_api_account() {
8879
.deletionPolicy("Retain")
8980
.build();
9081

91-
ApiRestAssert.assertThat(template)
82+
assertThat(template)
9283
.hasRestApiAccount(expected);
9384
}
9485

@@ -107,7 +98,7 @@ void should_have_rest_api_deployment() {
10798
.dependency(stringLikeRegexp("springnativeawslambdafunctionrestapiname(.*)"))
10899
.build();
109100

110-
ApiRestAssert.assertThat(template)
101+
assertThat(template)
111102
.hasRestApiDeployment(expected);
112103
}
113104

@@ -134,7 +125,7 @@ void should_have_rest_api_stage() {
134125
.dependency(stringLikeRegexp("springnativeawslambdafunctionrestapiAccount(.*)"))
135126
.build();
136127

137-
ApiRestAssert.assertThat(template)
128+
assertThat(template)
138129
.hasRestApiStage(expected);
139130
}
140131

@@ -160,7 +151,7 @@ void should_have_proxy_resource() {
160151
.properties(restApiResourceProperties)
161152
.build();
162153

163-
ApiRestAssert.assertThat(template)
154+
assertThat(template)
164155
.hasRestApiResource(expected);
165156
}
166157

@@ -184,7 +175,7 @@ void should_have_account_resource() {
184175
.properties(restApiResourceProperties)
185176
.build();
186177

187-
ApiRestAssert.assertThat(template)
178+
assertThat(template)
188179
.hasRestApiResource(expected);
189180
}
190181

@@ -221,7 +212,7 @@ void should_have_post_method() {
221212
.properties(restApiMethodProperties)
222213
.build();
223214

224-
ApiRestAssert.assertThat(template)
215+
assertThat(template)
225216
.hasRestApiMethod(expected);
226217
}
227218

@@ -258,7 +249,7 @@ void should_have_proxy_method() {
258249
.properties(restApiMethodProperties)
259250
.build();
260251

261-
ApiRestAssert.assertThat(template)
252+
assertThat(template)
262253
.hasRestApiMethod(expected);
263254
}
264255

@@ -295,7 +286,7 @@ void should_have_root_method() {
295286
.properties(restApiMethodProperties)
296287
.build();
297288

298-
ApiRestAssert.assertThat(template)
289+
assertThat(template)
299290
.hasRestApiMethod(expected);
300291
}
301292

spring-native-aws-lambda-infra/src/test/java/com/coffeebeans/springnativeawslambda/infra/assertion/ApiRestAssert.java

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,65 +18,68 @@
1818

1919
package com.coffeebeans.springnativeawslambda.infra.assertion;
2020

21-
import static com.coffeebeans.springnativeawslambda.infra.resource.CdkResourceType.APIGATEWAY_RESTAPI;
22-
import static com.coffeebeans.springnativeawslambda.infra.resource.CdkResourceType.APIGATEWAY_RESTAPI_ACCOUNT;
23-
import static com.coffeebeans.springnativeawslambda.infra.resource.CdkResourceType.APIGATEWAY_RESTAPI_DEPLOYMENT;
24-
import static com.coffeebeans.springnativeawslambda.infra.resource.CdkResourceType.APIGATEWAY_RESTAPI_METHOD;
25-
import static com.coffeebeans.springnativeawslambda.infra.resource.CdkResourceType.APIGATEWAY_RESTAPI_RESOURCE;
26-
import static com.coffeebeans.springnativeawslambda.infra.resource.CdkResourceType.APIGATEWAY_RESTAPI_STAGE;
27-
2821
import com.coffeebeans.springnativeawslambda.infra.resource.RestApi;
29-
import com.coffeebeans.springnativeawslambda.infra.resource.RestApiAccount;
30-
import com.coffeebeans.springnativeawslambda.infra.resource.RestApiDeployment;
31-
import com.coffeebeans.springnativeawslambda.infra.resource.RestApiMethod;
32-
import com.coffeebeans.springnativeawslambda.infra.resource.RestApiResource;
33-
import com.coffeebeans.springnativeawslambda.infra.resource.RestApiStage;
22+
import com.coffeebeans.springnativeawslambda.infra.resource.Tag;
3423
import org.assertj.core.api.AbstractAssert;
35-
import software.amazon.awscdk.assertions.Template;
24+
import org.assertj.core.api.Assertions;
3625

37-
public class ApiRestAssert extends AbstractAssert<ApiRestAssert, Template> {
26+
public class ApiRestAssert extends AbstractAssert<ApiRestAssert, RestApi> {
3827

39-
private ApiRestAssert(final Template actual) {
28+
private ApiRestAssert(final RestApi actual) {
4029
super(actual, ApiRestAssert.class);
4130
}
4231

43-
public static ApiRestAssert assertThat(final Template actual) {
32+
public static ApiRestAssert assertThat(final RestApi actual) {
4433
return new ApiRestAssert(actual);
4534
}
4635

47-
public ApiRestAssert hasRestApi(final RestApi expected) {
48-
actual.hasResource(APIGATEWAY_RESTAPI.getValue(), expected);
49-
50-
return this;
51-
}
52-
53-
public ApiRestAssert hasRestApiAccount(final RestApiAccount expected) {
54-
actual.hasResource(APIGATEWAY_RESTAPI_ACCOUNT.getValue(), expected);
55-
56-
return this;
57-
}
58-
59-
public ApiRestAssert hasRestApiDeployment(final RestApiDeployment expected) {
60-
actual.hasResource(APIGATEWAY_RESTAPI_DEPLOYMENT.getValue(), expected);
61-
62-
return this;
63-
}
64-
65-
public ApiRestAssert hasRestApiStage(final RestApiStage expected) {
66-
actual.hasResource(APIGATEWAY_RESTAPI_STAGE.getValue(), expected);
36+
public ApiRestAssert hasTag(final Tag expected) {
37+
Assertions.assertThat(actual.properties().tags())
38+
.contains(expected);
6739

6840
return this;
6941
}
7042

71-
public ApiRestAssert hasRestApiResource(final RestApiResource expected) {
72-
actual.hasResource(APIGATEWAY_RESTAPI_RESOURCE.getValue(), expected);
43+
public ApiRestAssert hasTag(final String key, final String value) {
44+
Assertions.assertThat(actual.properties().tags())
45+
.contains(Tag.builder().key(key).value(value).build());
7346

7447
return this;
7548
}
7649

77-
public ApiRestAssert hasRestApiMethod(final RestApiMethod expected) {
78-
actual.hasResource(APIGATEWAY_RESTAPI_METHOD.getValue(), expected);
79-
80-
return this;
81-
}
50+
// public ApiRestAssert hasRestApi(final RestApi expected) {
51+
// actual.hasResource(APIGATEWAY_RESTAPI.getValue(), expected);
52+
//
53+
// return this;
54+
// }
55+
//
56+
// public ApiRestAssert hasRestApiAccount(final RestApiAccount expected) {
57+
// actual.hasResource(APIGATEWAY_RESTAPI_ACCOUNT.getValue(), expected);
58+
//
59+
// return this;
60+
// }
61+
//
62+
// public ApiRestAssert hasRestApiDeployment(final RestApiDeployment expected) {
63+
// actual.hasResource(APIGATEWAY_RESTAPI_DEPLOYMENT.getValue(), expected);
64+
//
65+
// return this;
66+
// }
67+
//
68+
// public ApiRestAssert hasRestApiStage(final RestApiStage expected) {
69+
// actual.hasResource(APIGATEWAY_RESTAPI_STAGE.getValue(), expected);
70+
//
71+
// return this;
72+
// }
73+
//
74+
// public ApiRestAssert hasRestApiResource(final RestApiResource expected) {
75+
// actual.hasResource(APIGATEWAY_RESTAPI_RESOURCE.getValue(), expected);
76+
//
77+
// return this;
78+
// }
79+
//
80+
// public ApiRestAssert hasRestApiMethod(final RestApiMethod expected) {
81+
// actual.hasResource(APIGATEWAY_RESTAPI_METHOD.getValue(), expected);
82+
//
83+
// return this;
84+
// }
8285
}

spring-native-aws-lambda-infra/src/test/java/com/coffeebeans/springnativeawslambda/infra/assertion/BucketAssert.Java

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,9 @@
1919
package com.coffeebeans.springnativeawslambda.infra.assertion;
2020

2121
import static com.coffeebeans.springnativeawslambda.infra.resource.CdkResourceType.BUCKET;
22-
import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
2322

2423
import com.coffeebeans.springnativeawslambda.infra.resource.Bucket;
25-
import com.coffeebeans.springnativeawslambda.infra.resource.Bucket.BucketProperties;
26-
import com.fasterxml.jackson.databind.ObjectMapper;
27-
import java.util.Map;
28-
import java.util.Optional;
29-
import org.apache.commons.collections4.MapUtils;
3024
import org.assertj.core.api.AbstractAssert;
31-
import org.assertj.core.api.Assertions;
3225
import software.amazon.awscdk.assertions.Template;
3326

3427

@@ -47,57 +40,4 @@ public class BucketAssert extends AbstractAssert<BucketAssert, Template> {
4740

4841
return this;
4942
}
50-
51-
public BucketAssert hasBucketWithName(final String bucketName) {
52-
isNotNull();
53-
final Map<String, Map<String, Object>> buckets = actual.findResources(BUCKET.getValue());
54-
55-
if (MapUtils.isEmpty(buckets)) {
56-
failWithMessage("No bucket found");
57-
}
58-
59-
final Optional<Bucket> bucket = buckets.keySet().stream()
60-
.map((value) -> new ObjectMapper().convertValue(value, Bucket.class))
61-
.filter((b) -> equalsIgnoreCase(bucketName, b.bucketProperties().bucketName()))
62-
.findFirst();
63-
64-
if (bucket.isPresent()) {
65-
failWithMessage("No bucket found");
66-
}
67-
68-
final BucketProperties bucketProperties = bucket.get().bucketProperties();
69-
70-
Assertions
71-
.assertThat(bucketProperties.bucketName())
72-
.isEqualTo(bucketName);
73-
return this;
74-
}
75-
76-
// public BucketAssert hasLifecycleConfiguration(final LifecycleConfiguration lifecycleConfig) {
77-
// isNotNull();
78-
// final Bucket actualBucket = (Bucket) actual.getResource(BUCKET.getValue());
79-
// final Properties properties = actualBucket.properties();
80-
// assertThat(properties.lifecycleConfiguration()).isEqualTo(lifecycleConfig);
81-
// return this;
82-
// }
83-
//
84-
// public BucketAssert hasRuleWithPrefix(final String prefix) {
85-
// isNotNull();
86-
// final Bucket actualBucket = (Bucket) actual.getResource(BUCKET.getValue());
87-
// final LifecycleConfiguration lifecycleConfig = actualBucket.properties().lifecycleConfiguration();
88-
// assertThat(lifecycleConfig.rules())
89-
// .filteredOn(rule -> rule.prefix().equals(prefix))
90-
// .hasSize(1);
91-
// return this;
92-
// }
93-
//
94-
// public BucketAssert hasNoRuleWithPrefix(final String prefix) {
95-
// isNotNull();
96-
// final Bucket actualBucket = (Bucket) actual.findResources(BUCKET.getValue());
97-
// final LifecycleConfiguration lifecycleConfig = actualBucket.properties().lifecycleConfiguration();
98-
// assertThat(lifecycleConfig.rules())
99-
// .filteredOn(rule -> rule.prefix().equals(prefix))
100-
// .isEmpty();
101-
// return this;
102-
// }
10343
}

0 commit comments

Comments
 (0)