Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 8165ccc

Browse files
committed
Initial project setup
Signed-off-by: muhamadto <[email protected]>
1 parent 9c6afd2 commit 8165ccc

28 files changed

+2721
-181
lines changed

.github/workflows/build.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ on:
1919
push:
2020
branches: [ "*" ]
2121
pull_request:
22-
branches: [ "java17", "java11" ]
22+
branches: [ "main" ]
2323
types: [ opened, synchronize, reopened ]
2424

2525
jobs:
2626
build:
2727
runs-on: ubuntu-latest
28-
strategy:
29-
fail-fast: true
3028
steps:
3129
- name: Checkout repository
3230
uses: actions/checkout@v3
@@ -52,4 +50,4 @@ jobs:
5250
env:
5351
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5452
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
55-
run: mvn --no-transfer-progress clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=muhamadto_spring-native-aws-lambda --settings settings-spring.xml
53+
run: mvn --no-transfer-progress clean verify

.github/workflows/codeql-analysis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ on:
1919
push:
2020
branches: [ "*" ]
2121
pull_request:
22-
branches: [ "java17", "java11" ]
22+
branches: [ "main" ]
2323
types: [ opened, synchronize, reopened ]
2424

2525
jobs:

.github/workflows/release.yml

+32-60
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,42 @@
1-
# Licensed to Muhammad Hamadto
1+
## Licensed to Muhammad Hamadto
2+
#
3+
## Licensed under the Apache License, Version 2.0 (the "License");
4+
## you may not use this file except in compliance with the License.
5+
## You may obtain a copy of the License at
6+
## http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
## See the NOTICE file distributed with this work for additional information regarding copyright ownership.
9+
#
10+
## Unless required by applicable law or agreed to in writing, software
11+
## distributed under the License is distributed on an "AS IS" BASIS,
12+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
## See the License for the specific language governing permissions and
14+
## limitations under the License.
215

3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
# http://www.apache.org/licenses/LICENSE-2.0
7-
8-
# See the NOTICE file distributed with this work for additional information regarding copyright ownership.
9-
10-
# Unless required by applicable law or agreed to in writing, software
11-
# distributed under the License is distributed on an "AS IS" BASIS,
12-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
# See the License for the specific language governing permissions and
14-
# limitations under the License.
15-
16-
name: "Deploy to AWS"
17-
#run-name: Deploy to ${{ inputs.deploy_target }} by @${{ github.actor }}
1816

17+
name: Publish package to GitHub Packages
1918
on:
2019
release:
21-
types: [ published ]
20+
types: [published]
2221

2322
jobs:
24-
release:
23+
publish:
2524
runs-on: ubuntu-latest
26-
container:
27-
image: ghcr.io/muhamadto/spring-native-amazonlinux2-base:17-amazonlinux2
28-
options: --user=worker:ci
29-
permissions:
30-
id-token: write
31-
contents: read
32-
env:
33-
ENV: dev
34-
COST_CENTRE: coffeebeans-core
35-
AWS_DEFAULT_REGION: 'ap-southeast-2'
36-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3725
steps:
38-
- name: Configure AWS credentials
39-
uses: aws-actions/configure-aws-credentials@master
26+
- uses: actions/checkout@v3
27+
- name: Set up Maven Central Repository
28+
uses: actions/setup-java@v3
4029
with:
41-
role-to-assume: ${{ secrets.ROLE_ARN }}
42-
duration_seconds: 3600
43-
role-session-name: github-actions-example-lambda
44-
aws-region: ap-southeast-2
45-
- name: Checkout repository
46-
uses: actions/checkout@v3
47-
- name: Cache Maven packages
48-
uses: actions/cache@v3
49-
with:
50-
path: ~/.m2
51-
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
52-
restore-keys: ${{ runner.os }}-m2
53-
- name: Build with Maven
30+
java-version: '17'
31+
distribution: 'temurin'
32+
server-id: ossrh
33+
server-username: MAVEN_USERNAME
34+
server-password: MAVEN_PASSWORD
35+
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
36+
gpg-passphrase: MAVEN_GPG_PASSPHRASE
37+
- name: Publish package to Maven Central
38+
run: mvn --batch-mode deploy -Ppublisher
5439
env:
55-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
57-
ENV: ${{ env.ENV }}
58-
COST_CENTRE: ${{ env.COST_CENTRE }}
59-
run: ./mvnw -ntp -Pnative clean package -DskipTests
60-
- name: cdk diff
61-
uses: noverant/aws-cdk-github-actions@v1
62-
with:
63-
cdk_subcommand: 'diff'
64-
actions_comment: false
65-
- name: cdk deploy
66-
uses: noverant/aws-cdk-github-actions@v1
67-
with:
68-
cdk_subcommand: 'deploy'
69-
cdk_args: '--require-approval never'
70-
actions_comment: false
40+
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
41+
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
42+
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
target/
2-
!spring-native-aws-lambda-function/.mvn/wrapper/maven-wrapper.jar
2+
!example-lambda-function/.mvn/wrapper/maven-wrapper.jar
33
!**/src/main/**/target/
44
!**/src/test/**/target/
55

README.md

+123-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,124 @@
1-
# cdk-assertions
1+
# cdk-fluent-assertions
22

3-
AssertJ-based fluent assertions for AWS CDK testing
3+
# cdk-fluent-assertions
4+
5+
[![CodeQL](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/codeql-analysis.yml)
6+
[![Build](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/build.yml/badge.svg)](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/build.yml)
7+
[![Publish package to GitHub Packages](https://github.com/muhamadto/cdk-fluent-assertions/actions/workflows/release.yml/badge.svg?event=release)](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+
```

cdk-fluent-assertions-examples/pom.xml

-49
This file was deleted.

cdk-fluent-assertions/pom.xml

-48
This file was deleted.

lombok.config

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lombok.addLombokGeneratedAnnotation=true

0 commit comments

Comments
 (0)