|
1 |
| -# cdk-assertions |
| 1 | +# cdk-fluent-assertions |
2 | 2 |
|
3 |
| -AssertJ-based fluent assertions for AWS CDK testing |
| 3 | +# cdk-fluent-assertions |
| 4 | + |
| 5 | +[](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/codeql-analysis.yml) |
| 6 | +[](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/build.yml) |
| 7 | +[](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/release.yml) |
| 8 | + |
| 9 | +AssertJ-like fluent assertions for AWS CDK testing |
| 10 | + |
| 11 | +### AWS CDK testing framework |
| 12 | + |
| 13 | +AWS offers a framework to verify that your stack is well-formed and that it matches your expectations. This framework can be found [here](https://docs.aws.amazon.com/cdk/latest/guide/testing.html). |
| 14 | + |
| 15 | +Here is an example to very the stack contains a specific role: |
| 16 | + |
| 17 | + |
| 18 | +```java |
| 19 | + |
| 20 | +template.hasResourceProperties("AWS::IAM::Role", Match.objectEquals( |
| 21 | + Collections.singletonMap("AssumeRolePolicyDocument", Map.of( |
| 22 | + "Version", "2012-10-17", |
| 23 | + "Statement", Collections.singletonList(Map.of( |
| 24 | + "Action", "sts:AssumeRole", |
| 25 | + "Effect", "Allow", |
| 26 | + "Principal", Collections.singletonMap( |
| 27 | + "Service", Collections.singletonMap( |
| 28 | + "Fn::Join", Arrays.asList( |
| 29 | + "", |
| 30 | + Arrays.asList("states.", Match.anyValue(), ".amazonaws.com") |
| 31 | + ) |
| 32 | + ) |
| 33 | + ) |
| 34 | + )) |
| 35 | + )) |
| 36 | +)); |
| 37 | +``` |
| 38 | + |
| 39 | +### AWS CDK fluent testing library |
| 40 | +While working on a small [open-source project](https://github.com/muhamadto/cdk-fluent-assertions), I was varying a CDK stack containing some resources. I noticed that the tests written using the AWS framework were verbose and presented challenges in terms of readability and maintainability. In response, I decided to create a small library to make it easier to read, write and maintain tests. |
| 41 | + |
| 42 | +To illustrate, an equivalent representation to the aforementioned example can be found below: as follows" |
| 43 | +```java |
| 44 | +CDKStackAssert.assertThat(template) |
| 45 | + .containsRoleWithManagedPolicyArn(managedPolicyArn) |
| 46 | + .hasAssumeRolePolicyDocument("states.amazonaws.com", null, "Allow", "2012-10-17", "sts:AssumeRole"); |
| 47 | +``` |
| 48 | + |
| 49 | +#### How to use it |
| 50 | + |
| 51 | +* The library is available as Github package. To use it, add the following dependency to your project: |
| 52 | + |
| 53 | +```xml |
| 54 | +<dependency> |
| 55 | + <groupId>cloud.pianola</groupId> |
| 56 | + <artifactId>cdk-fluent-assertions</artifactId> |
| 57 | + <version>1.0.0</version> |
| 58 | + <scope>test</scope> |
| 59 | +</dependency> |
| 60 | +``` |
| 61 | + |
| 62 | +* Create a class `TemplateSupport` in your testing packages. This class will be used to load the template and create the `CDKStackAssert` object. |
| 63 | + |
| 64 | +```java |
| 65 | +public abstract class TemplateSupport { |
| 66 | + |
| 67 | + public static final String ENV = "test"; |
| 68 | + public static final String TEST_CDK_BUCKET = "test-cdk-bucket"; |
| 69 | + public static final String QUALIFIER = "test"; |
| 70 | + static Template template; |
| 71 | + private static final String STACK_NAME = "example-lambda-function-test-stack"; |
| 72 | + |
| 73 | + @TempDir |
| 74 | + private static Path TEMP_DIR; |
| 75 | + |
| 76 | + @BeforeAll |
| 77 | + static void initAll() throws IOException { |
| 78 | + final Path lambdaCodePath = TestLambdaUtils.getTestLambdaCodePath(TEMP_DIR); |
| 79 | + |
| 80 | + final Map<String, String> tags = createTags(ENV, TAG_VALUE_COST_CENTRE); |
| 81 | + final App app = new App(); |
| 82 | + final ExampleLambdaStack stack = StackUtils.createStack(app, STACK_NAME, lambdaCodePath.toString(), QUALIFIER, TEST_CDK_BUCKET, ENV); |
| 83 | + |
| 84 | + tags.entrySet().stream() |
| 85 | + .filter(tag -> Objects.nonNull(tag.getValue())) |
| 86 | + .forEach(tag -> Tags.of(app).add(tag.getKey(), tag.getValue())); |
| 87 | + |
| 88 | + template = Template.fromStack(stack); |
| 89 | + } |
| 90 | + |
| 91 | + @AfterAll |
| 92 | + static void cleanup() { |
| 93 | + template = null; |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | +* Extend the `TemplateSupport` class in your test classes and use the `CDKStackAssert` object to verify your stack. |
| 98 | + |
| 99 | +```java |
| 100 | +class LambdaTest extends TemplateSupport { |
| 101 | + |
| 102 | + public static final String TEST = "test"; |
| 103 | + |
| 104 | + @Test |
| 105 | + void should_have_lambda_function() { |
| 106 | + |
| 107 | + CDKStackAssert.assertThat(template) |
| 108 | + .containsFunction("example-lambda-function") |
| 109 | + .hasHandler("org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest") |
| 110 | + .hasCode("test-cdk-bucket", "(.*).zip") |
| 111 | + .hasRole("examplelambdafunctionrole(.*)") |
| 112 | + .hasDependency("examplelambdafunctionrole(.*)") |
| 113 | + .hasDependency("examplelambdafunctionroleDefaultPolicy(.*)") |
| 114 | + .hasTag("COST_CENTRE", TAG_VALUE_COST_CENTRE) |
| 115 | + .hasTag("ENV", TEST) |
| 116 | + .hasEnvironmentVariable("ENV", TEST) |
| 117 | + .hasEnvironmentVariable("SPRING_PROFILES_ACTIVE", TEST) |
| 118 | + .hasDescription("Lambda example") |
| 119 | + .hasMemorySize(512) |
| 120 | + .hasRuntime("provided.al2") |
| 121 | + .hasTimeout(3); |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
0 commit comments