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

Commit

Permalink
Initial project setup
Browse files Browse the repository at this point in the history
Signed-off-by: muhamadto <[email protected]>
  • Loading branch information
muhamadto committed Jun 4, 2023
1 parent 9c6afd2 commit 074c6d9
Show file tree
Hide file tree
Showing 28 changed files with 2,721 additions and 181 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ on:
push:
branches: [ "*" ]
pull_request:
branches: [ "java17", "java11" ]
branches: [ "main" ]
types: [ opened, synchronize, reopened ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: true
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -52,4 +50,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
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
run: mvn --no-transfer-progress clean verify
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ on:
push:
branches: [ "*" ]
pull_request:
branches: [ "java17", "java11" ]
branches: [ "main" ]
types: [ opened, synchronize, reopened ]

jobs:
Expand Down
92 changes: 32 additions & 60 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,70 +1,42 @@
# Licensed to Muhammad Hamadto
## Licensed to Muhammad Hamadto
#
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
## http://www.apache.org/licenses/LICENSE-2.0
#
## See the NOTICE file distributed with this work for additional information regarding copyright ownership.
#
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0

# See the NOTICE file distributed with this work for additional information regarding copyright ownership.

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: "Deploy to AWS"
#run-name: Deploy to ${{ inputs.deploy_target }} by @${{ github.actor }}

name: Publish package to GitHub Packages
on:
release:
types: [ published ]
types: [published]

jobs:
release:
publish:
runs-on: ubuntu-latest
container:
image: ghcr.io/muhamadto/spring-native-amazonlinux2-base:17-amazonlinux2
options: --user=worker:ci
permissions:
id-token: write
contents: read
env:
ENV: dev
COST_CENTRE: coffeebeans-core
AWS_DEFAULT_REGION: 'ap-southeast-2'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@master
- uses: actions/checkout@v3
- name: Set up Maven Central Repository
uses: actions/setup-java@v3
with:
role-to-assume: ${{ secrets.ROLE_ARN }}
duration_seconds: 3600
role-session-name: github-actions-example-lambda
aws-region: ap-southeast-2
- name: Checkout repository
uses: actions/checkout@v3
- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build with Maven
java-version: '17'
distribution: 'temurin'
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Publish package to Maven Central
run: mvn --batch-mode deploy -Ppublisher
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
ENV: ${{ env.ENV }}
COST_CENTRE: ${{ env.COST_CENTRE }}
run: ./mvnw -ntp -Pnative clean package -DskipTests
- name: cdk diff
uses: noverant/aws-cdk-github-actions@v1
with:
cdk_subcommand: 'diff'
actions_comment: false
- name: cdk deploy
uses: noverant/aws-cdk-github-actions@v1
with:
cdk_subcommand: 'deploy'
cdk_args: '--require-approval never'
actions_comment: false
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
target/
!spring-native-aws-lambda-function/.mvn/wrapper/maven-wrapper.jar
!example-lambda-function/.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

Expand Down
125 changes: 123 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,124 @@
# cdk-assertions
# cdk-fluent-assertions

AssertJ-based fluent assertions for AWS CDK testing
# cdk-fluent-assertions

[![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)
[![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)
[![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)

AssertJ-like fluent assertions for AWS CDK testing

### AWS CDK testing framework

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).

Here is an example to very the stack contains a specific role:


```java

template.hasResourceProperties("AWS::IAM::Role", Match.objectEquals(
Collections.singletonMap("AssumeRolePolicyDocument", Map.of(
"Version", "2012-10-17",
"Statement", Collections.singletonList(Map.of(
"Action", "sts:AssumeRole",
"Effect", "Allow",
"Principal", Collections.singletonMap(
"Service", Collections.singletonMap(
"Fn::Join", Arrays.asList(
"",
Arrays.asList("states.", Match.anyValue(), ".amazonaws.com")
)
)
)
))
))
));
```

### AWS CDK fluent testing library
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.

To illustrate, an equivalent representation to the aforementioned example can be found below: as follows"
```java
CDKStackAssert.assertThat(template)
.containsRoleWithManagedPolicyArn(managedPolicyArn)
.hasAssumeRolePolicyDocument("states.amazonaws.com", null, "Allow", "2012-10-17", "sts:AssumeRole");
```

#### How to use it

* The library is available on Maven Central. To use it, add the following dependency to your project:

```xml
<dependency>
<groupId>cloud.pianola</groupId>
<artifactId>cdk-fluent-assertions</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
```

* Create a class `TemplateSupport` in your testing packages. This class will be used to load the template and create the `CDKStackAssert` object.

```java
public abstract class TemplateSupport {

public static final String ENV = "test";
public static final String TEST_CDK_BUCKET = "test-cdk-bucket";
public static final String QUALIFIER = "test";
static Template template;
private static final String STACK_NAME = "example-lambda-function-test-stack";

@TempDir
private static Path TEMP_DIR;

@BeforeAll
static void initAll() throws IOException {
final Path lambdaCodePath = TestLambdaUtils.getTestLambdaCodePath(TEMP_DIR);

final Map<String, String> tags = createTags(ENV, TAG_VALUE_COST_CENTRE);
final App app = new App();
final ExampleLambdaStack stack = StackUtils.createStack(app, STACK_NAME, lambdaCodePath.toString(), QUALIFIER, TEST_CDK_BUCKET, ENV);

tags.entrySet().stream()
.filter(tag -> Objects.nonNull(tag.getValue()))
.forEach(tag -> Tags.of(app).add(tag.getKey(), tag.getValue()));

template = Template.fromStack(stack);
}

@AfterAll
static void cleanup() {
template = null;
}
}
```
* Extend the `TemplateSupport` class in your test classes and use the `CDKStackAssert` object to verify your stack.

```java
class LambdaTest extends TemplateSupport {

public static final String TEST = "test";

@Test
void should_have_lambda_function() {

CDKStackAssert.assertThat(template)
.containsFunction("example-lambda-function")
.hasHandler("org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest")
.hasCode("test-cdk-bucket", "(.*).zip")
.hasRole("examplelambdafunctionrole(.*)")
.hasDependency("examplelambdafunctionrole(.*)")
.hasDependency("examplelambdafunctionroleDefaultPolicy(.*)")
.hasTag("COST_CENTRE", TAG_VALUE_COST_CENTRE)
.hasTag("ENV", TEST)
.hasEnvironmentVariable("ENV", TEST)
.hasEnvironmentVariable("SPRING_PROFILES_ACTIVE", TEST)
.hasDescription("Lambda example")
.hasMemorySize(512)
.hasRuntime("provided.al2")
.hasTimeout(3);
}
}
```
49 changes: 0 additions & 49 deletions cdk-fluent-assertions-examples/pom.xml

This file was deleted.

48 changes: 0 additions & 48 deletions cdk-fluent-assertions/pom.xml

This file was deleted.

1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.addLombokGeneratedAnnotation=true
Loading

0 comments on commit 074c6d9

Please sign in to comment.